Use shared client and close body response

This commit is contained in:
Andre Medeiros 2021-07-07 16:24:23 -04:00
parent c2ea210003
commit c8da9bbff3

View file

@ -15,10 +15,21 @@ const (
type Client struct { type Client struct {
id string id string
secret string secret string
client *http.Client
} }
func NewClient(id, secret string) *Client { func NewClient(id, secret string) *Client {
return &Client{id, secret} tr := &http.Transport{
MaxIdleConnsPerHost: 8,
}
client := &http.Client{Transport: tr}
return &Client{
id,
secret,
client,
}
} }
type AuthenticatedClient struct { type AuthenticatedClient struct {
@ -34,18 +45,16 @@ func (rc *Client) NewAuthenticatedClient(refreshToken, accessToken string) *Auth
} }
func (rac *AuthenticatedClient) request(r *Request) ([]byte, error) { func (rac *AuthenticatedClient) request(r *Request) ([]byte, error) {
tr := &http.Transport{}
client := &http.Client{Transport: tr}
req, err := r.HTTPRequest() req, err := r.HTTPRequest()
if err != nil { if err != nil {
return nil, err return nil, err
} }
resp, err := client.Do(req) resp, err := rac.client.Do(req)
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer resp.Body.Close()
return ioutil.ReadAll(resp.Body) return ioutil.ReadAll(resp.Body)
} }