This commit is contained in:
Aliaksandr Pliutau 2015-11-20 09:51:18 +07:00
parent 3e1a1f8ddd
commit 5043557577
2 changed files with 23 additions and 0 deletions

View File

@ -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")
}
}
}

View File

@ -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
}