From 376ceb94d8cd5e2f0f871f028aac92fc3c3db10a Mon Sep 17 00:00:00 2001 From: Daniel Gonzalez Date: Wed, 3 Nov 2021 00:01:24 +0100 Subject: [PATCH] feat: make http client in gotado configurable This makes the tado client more flexible by allowing to use arbitrary http clients. This also makes the WithTimeout() method redundant, since one can use an http client with any desired timeout. In addition, this commit adds the HTTPClient interface to the gotado client as abstraction to the use http client. Any http client that adheres to the HTTPClient interface can be used by gotado. BREAKING CHANGE: Client method `WithTimeout` removed. --- client.go | 13 ++++++++----- examples/away/main.go | 4 +++- examples/earlystart/main.go | 4 +++- examples/me/main.go | 4 +++- examples/mischomeinfo/main.go | 4 +++- examples/overlay/main.go | 4 +++- examples/presence/main.go | 4 +++- examples/schedule/main.go | 4 +++- 8 files changed, 29 insertions(+), 12 deletions(-) diff --git a/client.go b/client.go index 1556a5f..d1c7e98 100644 --- a/client.go +++ b/client.go @@ -5,7 +5,6 @@ import ( "fmt" "io" "net/http" - "time" oauth2int "github.com/gonzolino/gotado/internal/oauth2" "golang.org/x/oauth2" @@ -16,6 +15,10 @@ const ( tokenURL = "https://auth.tado.com/oauth/token" ) +type HTTPClient interface { + Do(req *http.Request) (*http.Response, error) +} + // Client to access the tado° API type Client struct { // ClientID specifies the client ID to use for authentication @@ -23,7 +26,7 @@ type Client struct { // ClientSecret specifies the client secret to use for authentication ClientSecret string - http *http.Client + http HTTPClient } // NewClient creates a new tado° client @@ -35,9 +38,9 @@ func NewClient(clientID, clientSecret string) *Client { } } -// WithTimeout configures the tado° object with the given timeout for HTTP requests. -func (c *Client) WithTimeout(timeout time.Duration) *Client { - c.http.Timeout = timeout +// WithHTTPClient configures the http client to use for tado° API interactions +func (c *Client) WithHTTPClient(httpClient *http.Client) *Client { + c.http = httpClient return c } diff --git a/examples/away/main.go b/examples/away/main.go index 147158a..e0ae7c8 100644 --- a/examples/away/main.go +++ b/examples/away/main.go @@ -3,6 +3,7 @@ package main import ( "context" "fmt" + "net/http" "os" "time" @@ -36,7 +37,8 @@ func main() { ctx := context.Background() // Create authenticated tado° client - client := gotado.NewClient(clientID, clientSecret).WithTimeout(5 * time.Second) + httpClient := &http.Client{Timeout: 5 * time.Second} + client := gotado.NewClient(clientID, clientSecret).WithHTTPClient(httpClient) client, err := client.WithCredentials(ctx, username, password) if err != nil { fmt.Fprintf(os.Stderr, "Authentication failed: %v\n", err) diff --git a/examples/earlystart/main.go b/examples/earlystart/main.go index da290ea..7d94432 100644 --- a/examples/earlystart/main.go +++ b/examples/earlystart/main.go @@ -3,6 +3,7 @@ package main import ( "context" "fmt" + "net/http" "os" "time" @@ -36,7 +37,8 @@ func main() { ctx := context.Background() // Create authenticated tado° client - client := gotado.NewClient(clientID, clientSecret).WithTimeout(5 * time.Second) + httpClient := &http.Client{Timeout: 5 * time.Second} + client := gotado.NewClient(clientID, clientSecret).WithHTTPClient(httpClient) client, err := client.WithCredentials(ctx, username, password) if err != nil { fmt.Fprintf(os.Stderr, "Authentication failed: %v\n", err) diff --git a/examples/me/main.go b/examples/me/main.go index ce8ac70..a6e2279 100644 --- a/examples/me/main.go +++ b/examples/me/main.go @@ -3,6 +3,7 @@ package main import ( "context" "fmt" + "net/http" "os" "time" @@ -30,7 +31,8 @@ func main() { ctx := context.Background() // Create authenticated tado° client - client := gotado.NewClient(clientID, clientSecret).WithTimeout(5 * time.Second) + httpClient := &http.Client{Timeout: 5 * time.Second} + client := gotado.NewClient(clientID, clientSecret).WithHTTPClient(httpClient) client, err := client.WithCredentials(ctx, username, password) if err != nil { fmt.Fprintf(os.Stderr, "Authentication failed: %v\n", err) diff --git a/examples/mischomeinfo/main.go b/examples/mischomeinfo/main.go index 779f9af..6873acc 100644 --- a/examples/mischomeinfo/main.go +++ b/examples/mischomeinfo/main.go @@ -3,6 +3,7 @@ package main import ( "context" "fmt" + "net/http" "os" "time" @@ -36,7 +37,8 @@ func main() { ctx := context.Background() // Create authenticated tado° client - client := gotado.NewClient(clientID, clientSecret).WithTimeout(5 * time.Second) + httpClient := &http.Client{Timeout: 5 * time.Second} + client := gotado.NewClient(clientID, clientSecret).WithHTTPClient(httpClient) client, err := client.WithCredentials(ctx, username, password) if err != nil { fmt.Fprintf(os.Stderr, "Authentication failed: %v\n", err) diff --git a/examples/overlay/main.go b/examples/overlay/main.go index e5a09cb..7662ff8 100644 --- a/examples/overlay/main.go +++ b/examples/overlay/main.go @@ -3,6 +3,7 @@ package main import ( "context" "fmt" + "net/http" "os" "time" @@ -36,7 +37,8 @@ func main() { ctx := context.Background() // Create authenticated tado° client - client := gotado.NewClient(clientID, clientSecret).WithTimeout(5 * time.Second) + httpClient := &http.Client{Timeout: 5 * time.Second} + client := gotado.NewClient(clientID, clientSecret).WithHTTPClient(httpClient) client, err := client.WithCredentials(ctx, username, password) if err != nil { fmt.Fprintf(os.Stderr, "Authentication failed: %v\n", err) diff --git a/examples/presence/main.go b/examples/presence/main.go index 02fd09e..97de26d 100644 --- a/examples/presence/main.go +++ b/examples/presence/main.go @@ -3,6 +3,7 @@ package main import ( "context" "fmt" + "net/http" "os" "time" @@ -36,7 +37,8 @@ func main() { ctx := context.Background() // Create authenticated tado° client - client := gotado.NewClient(clientID, clientSecret).WithTimeout(5 * time.Second) + httpClient := &http.Client{Timeout: 5 * time.Second} + client := gotado.NewClient(clientID, clientSecret).WithHTTPClient(httpClient) client, err := client.WithCredentials(ctx, username, password) if err != nil { fmt.Fprintf(os.Stderr, "Authentication failed: %v\n", err) diff --git a/examples/schedule/main.go b/examples/schedule/main.go index 358e74d..f144c8d 100644 --- a/examples/schedule/main.go +++ b/examples/schedule/main.go @@ -3,6 +3,7 @@ package main import ( "context" "fmt" + "net/http" "os" "time" @@ -36,7 +37,8 @@ func main() { ctx := context.Background() // Create authenticated tado° client - client := gotado.NewClient(clientID, clientSecret).WithTimeout(5 * time.Second) + httpClient := &http.Client{Timeout: 5 * time.Second} + client := gotado.NewClient(clientID, clientSecret).WithHTTPClient(httpClient) client, err := client.WithCredentials(ctx, username, password) if err != nil { fmt.Fprintf(os.Stderr, "Authentication failed: %v\n", err)