From d7a421ce0c825aa8c6955eb0cf3ccb4278c7184f Mon Sep 17 00:00:00 2001 From: Quentin Lemaire Date: Sat, 18 Apr 2020 15:15:59 +0200 Subject: [PATCH] feat(lib): Set up debug info --- client.go | 16 +++++++++++++--- client_logger.go | 23 ++++++++++++++++++++++- client_logger_test.go | 13 +++++++++---- client_test.go | 2 +- cmd/radarr/main.go | 8 ++++++-- example_client_test.go | 15 +++++++++++++++ 6 files changed, 66 insertions(+), 11 deletions(-) diff --git a/client.go b/client.go index 68fe63f..1fce94a 100644 --- a/client.go +++ b/client.go @@ -13,9 +13,14 @@ type HTTPClientInterface interface { Do(req *http.Request) (*http.Response, error) } +// ClientOptions describe optional value to create a Radarr client +type ClientOptions struct { + Verbose bool +} + // New Create a Radarr client // Optionnally specify an http.Client -func New(radarrURL, apiKey string, client HTTPClientInterface) (*Service, error) { +func New(radarrURL, apiKey string, client HTTPClientInterface, opts ...*ClientOptions) (*Service, error) { valid, err := url.ParseRequestURI(radarrURL) if err != nil { return nil, errors.New("Please specify a valid URL") @@ -25,10 +30,15 @@ func New(radarrURL, apiKey string, client HTTPClientInterface) (*Service, error) return nil, errors.New("Please specify your Radarr API key") } - // if client not specified, defaulting to default http client + var verbose bool = false + if len(opts) > 0 { + verbose = opts[0].Verbose + } + + // if client not specified, defaulting to default http client with our tansport if client == nil { client = &http.Client{ - Transport: newTransport(apiKey), + Transport: newTransport(apiKey, verbose), Timeout: time.Second * 10, } } diff --git a/client_logger.go b/client_logger.go index 765675c..f335f36 100644 --- a/client_logger.go +++ b/client_logger.go @@ -1,19 +1,23 @@ package radarr import ( + "fmt" "net/http" + "net/http/httputil" ) // Custom http transport to log request type transport struct { transport http.RoundTripper apiKey string + verbose bool } -func newTransport(apiKey string) *transport { +func newTransport(apiKey string, verbose bool) *transport { return &transport{ apiKey: apiKey, transport: http.DefaultTransport, + verbose: verbose, } } @@ -23,7 +27,24 @@ func (t *transport) RoundTrip(req *http.Request) (*http.Response, error) { req.Header.Add("Content-Type", "application/json; charset=utf-8") req.Header.Add("X-Api-Key", t.apiKey) + if t.verbose { + data, err := httputil.DumpRequest(req, true) + if err != nil { + return nil, err + } + fmt.Printf("[DEBUG] Request: %s\n", string(data)) + } + // Send request to the orignal transport resp, err := t.transport.RoundTrip(req) + + if t.verbose { + data, err := httputil.DumpResponse(resp, false) + if err != nil { + return nil, err + } + fmt.Printf("[DEBUG] Response: %s\n", string(data)) + } + return resp, err } diff --git a/client_logger_test.go b/client_logger_test.go index 85183c8..21a2e93 100644 --- a/client_logger_test.go +++ b/client_logger_test.go @@ -16,20 +16,25 @@ func (r *dummyHTTPTransport) RoundTrip(req *http.Request) (*http.Response, error } func Test_newTransport(t *testing.T) { + type args struct { + verbose bool + key string + } + tests := []struct { name string want *transport - key string + args args }{ { name: "Constructor", - want: &transport{transport: http.DefaultTransport, apiKey: "foo"}, - key: "foo", + want: &transport{transport: http.DefaultTransport, apiKey: "foo", verbose: false}, + args: args{verbose: false, key: "foo"}, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := newTransport(tt.key); !reflect.DeepEqual(got, tt.want) { + if got := newTransport(tt.args.key, tt.args.verbose); !reflect.DeepEqual(got, tt.want) { t.Errorf("newTransport() = %v, want %v", got, tt.want) } }) diff --git a/client_test.go b/client_test.go index ed17d1a..24cbb69 100644 --- a/client_test.go +++ b/client_test.go @@ -23,7 +23,7 @@ func TestNew(t *testing.T) { client := http.Client{} client.Timeout = time.Second * 10 - client.Transport = newTransport(dummyAPIKey) + client.Transport = newTransport(dummyAPIKey, false) var serviceWithDefaultHTTPClient *Service = &Service{url: dummyURL, client: &client} serviceWithDefaultHTTPClient.Movies = newMovieService(serviceWithDefaultHTTPClient) serviceWithDefaultHTTPClient.Diskspace = newDiskspaceService(serviceWithDefaultHTTPClient) diff --git a/cmd/radarr/main.go b/cmd/radarr/main.go index 349bc6d..1153189 100644 --- a/cmd/radarr/main.go +++ b/cmd/radarr/main.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "os" "sort" "time" @@ -53,7 +54,9 @@ var app *cli.App = &cli.App{ Authors: []*cli.Author{{Email: "quentin@lemairepro.fr", Name: "SkYNewZ"}}, Before: func(c *cli.Context) error { // Instantiate Radarr client - s, err := radarr.New(c.String("url"), c.String("apiKey"), nil) + s, err := radarr.New(c.String("url"), c.String("apiKey"), nil, &radarr.ClientOptions{ + Verbose: verbose, + }) if err != nil { return err } @@ -77,6 +80,7 @@ func init() { func main() { if err := app.Run(os.Args); err != nil { - log.Fatal(err) + fmt.Println(err) + os.Exit(1) } } diff --git a/example_client_test.go b/example_client_test.go index f8f1419..e353173 100644 --- a/example_client_test.go +++ b/example_client_test.go @@ -23,6 +23,21 @@ func ExampleNew_basic() { fmt.Printf("%s", movie.Title) } +func ExampleNew_verbose() { + client, err := radarr.New("https://my.radarr-instance.fr", "radarr-api-key", nil, &radarr.ClientOptions{ + Verbose: true, + }) + if err != nil { + log.Fatalln(err) + } + + movie, err := client.Movies.Get(217) + if err != nil { + log.Fatalln(err) + } + fmt.Printf("%s", movie.Title) +} + // Instantiate a client with a custom HTTP client func ExampleNew_advanced() { client, err := radarr.New("https://my.radarr-instance.fr", "radarr-api-key", &http.Client{