Skip to content

Commit

Permalink
feat: make http client in gotado configurable
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
gonzolino committed Nov 11, 2021
1 parent 8b2f320 commit 376ceb9
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 12 deletions.
13 changes: 8 additions & 5 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"io"
"net/http"
"time"

oauth2int "github.com/gonzolino/gotado/internal/oauth2"
"golang.org/x/oauth2"
Expand All @@ -16,14 +15,18 @@ 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
ClientID string
// ClientSecret specifies the client secret to use for authentication
ClientSecret string

http *http.Client
http HTTPClient
}

// NewClient creates a new tado° client
Expand All @@ -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
}

Expand Down
4 changes: 3 additions & 1 deletion examples/away/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"fmt"
"net/http"
"os"
"time"

Expand Down Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion examples/earlystart/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"fmt"
"net/http"
"os"
"time"

Expand Down Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion examples/me/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"fmt"
"net/http"
"os"
"time"

Expand Down Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion examples/mischomeinfo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"fmt"
"net/http"
"os"
"time"

Expand Down Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion examples/overlay/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"fmt"
"net/http"
"os"
"time"

Expand Down Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion examples/presence/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"fmt"
"net/http"
"os"
"time"

Expand Down Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion examples/schedule/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"fmt"
"net/http"
"os"
"time"

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 376ceb9

Please sign in to comment.