From 93a8b94621e5754bf5def841775fb3745be1dacb Mon Sep 17 00:00:00 2001 From: Neale Pickett Date: Tue, 11 Jul 2017 22:53:15 -0600 Subject: [PATCH 1/2] Optional custom http client --- client.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/client.go b/client.go index c298c62..4963f78 100644 --- a/client.go +++ b/client.go @@ -17,6 +17,7 @@ type Auth struct { Password string `json:"password"` URL string StreamingURL string + HTTP *http.Client } // The token and related elements returned after a successful auth @@ -50,9 +51,13 @@ func NewClient(auth *Auth) (*Client, error) { auth.StreamingURL = StreamingURL } + hclient := auth.HTTP + if hclient == nil { + hclient = &http.Client{} + } client := &Client{ Auth: auth, - HTTP: &http.Client{}, + HTTP: hclient, } token, err := client.authorize(auth) if err != nil { From cc2495329ec1ffe608d80493ab22b0a13c596b10 Mon Sep 17 00:00:00 2001 From: Neale Pickett Date: Sat, 4 Aug 2018 12:15:48 -0600 Subject: [PATCH 2/2] Fixed so JSON will encode --- client.go | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/client.go b/client.go index 4963f78..6ece97a 100644 --- a/client.go +++ b/client.go @@ -17,7 +17,6 @@ type Auth struct { Password string `json:"password"` URL string StreamingURL string - HTTP *http.Client } // The token and related elements returned after a successful auth @@ -42,8 +41,8 @@ var ( ActiveClient *Client ) -// Generates a new client for the Tesla API -func NewClient(auth *Auth) (*Client, error) { +// Generates a new client for the Tesla API, using a provided HTTP client +func NewClientWithHttpClient(auth *Auth, hclient *http.Client) (*Client, error) { if auth.URL == "" { auth.URL = BaseURL } @@ -51,10 +50,6 @@ func NewClient(auth *Auth) (*Client, error) { auth.StreamingURL = StreamingURL } - hclient := auth.HTTP - if hclient == nil { - hclient = &http.Client{} - } client := &Client{ Auth: auth, HTTP: hclient, @@ -68,10 +63,19 @@ func NewClient(auth *Auth) (*Client, error) { return client, nil } +// Generates a new client for the Tesla API +func NewClient(auth *Auth) (*Client, error) { + hclient := &http.Client{} + return NewClientWithHttpClient(auth, hclient) +} + // Authorizes against the Tesla API with the appropriate credentials func (c Client) authorize(auth *Auth) (*Token, error) { auth.GrantType = "password" - data, _ := json.Marshal(auth) + data, err := json.Marshal(auth) + if err != nil { + return nil, err + } body, err := c.post(AuthURL, data) if err != nil { return nil, err