diff --git a/client.go b/client.go index 7f37792..499d341 100644 --- a/client.go +++ b/client.go @@ -6,6 +6,7 @@ import ( "io" "io/ioutil" "net/http" + "os" ) // NewClient returns new Client struct @@ -19,10 +20,18 @@ func NewClient(clientID string, secret string, APIBase string) (*Client, error) clientID, secret, APIBase, + "", nil, }, nil } +// SetLogFile func +func (c *Client) SetLogFile(filepath string) error { + c.LogFile = filepath + + return nil +} + // SetAccessToken sets saved token to current client func (c *Client) SetAccessToken(token string) error { c.Token = &TokenResponse{ @@ -46,6 +55,8 @@ func (c *Client) Send(req *http.Request, v interface{}) error { } resp, err := c.client.Do(req) + c.log(req, resp) + if err != nil { return err } @@ -75,3 +86,14 @@ func (c *Client) Send(req *http.Request, v interface{}) error { return nil } + +func (c *Client) log(request *http.Request, response *http.Response) { + if c.LogFile != "" { + os.OpenFile(c.LogFile, os.O_CREATE, 0755) + + logFile, err := os.OpenFile(c.LogFile, os.O_APPEND|os.O_RDWR|os.O_CREATE, 0755) + if err == nil { + logFile.WriteString("URL: " + request.RequestURI + "\n\n") + } + } +} diff --git a/types.go b/types.go index bcf8d0a..a7ca480 100644 --- a/types.go +++ b/types.go @@ -20,6 +20,7 @@ type ( ClientID string Secret string APIBase string + LogFile string // If user set log file name all requests will be logged there Token *TokenResponse }