Skip to content

Commit

Permalink
feat(lib): Set up debug info
Browse files Browse the repository at this point in the history
  • Loading branch information
SkYNewZ committed Apr 26, 2020
1 parent 655826a commit d7a421c
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 11 deletions.
16 changes: 13 additions & 3 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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,
}
}
Expand Down
23 changes: 22 additions & 1 deletion client_logger.go
Original file line number Diff line number Diff line change
@@ -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,
}
}

Expand All @@ -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
}
13 changes: 9 additions & 4 deletions client_logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
Expand Down
2 changes: 1 addition & 1 deletion client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 6 additions & 2 deletions cmd/radarr/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"fmt"
"os"
"sort"
"time"
Expand Down Expand Up @@ -53,7 +54,9 @@ var app *cli.App = &cli.App{
Authors: []*cli.Author{{Email: "[email protected]", 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
}
Expand All @@ -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)
}
}
15 changes: 15 additions & 0 deletions example_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down

0 comments on commit d7a421c

Please sign in to comment.