Skip to content

Commit

Permalink
fix: properly pass context to http client requests
Browse files Browse the repository at this point in the history
  • Loading branch information
gonzolino committed Mar 18, 2022
1 parent 9b906ad commit a222c80
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
24 changes: 12 additions & 12 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,17 @@ func (c *client) Do(req *http.Request) (*http.Response, error) {
}

// Request performs an HTTP request to the tado° API
func (c *client) Request(method, url string, body io.Reader) (*http.Response, error) {
req, err := http.NewRequest(method, url, body)
func (c *client) Request(ctx context.Context, method, url string, body io.Reader) (*http.Response, error) {
req, err := http.NewRequestWithContext(ctx, method, url, body)
if err != nil {
return nil, fmt.Errorf("unable to create http request: %w", err)
}
return c.Do(req)
}

// RequestWithHeaders performs an HTTP request to the tado° API with the given map of HTTP headers
func (c *client) RequestWithHeaders(method, url string, body io.Reader, headers map[string]string) (*http.Response, error) {
req, err := http.NewRequest(method, url, body)
func (c *client) RequestWithHeaders(ctx context.Context, method, url string, body io.Reader, headers map[string]string) (*http.Response, error) {
req, err := http.NewRequestWithContext(ctx, method, url, body)
if err != nil {
return nil, fmt.Errorf("unable to create http request: %w", err)
}
Expand All @@ -94,8 +94,8 @@ func (c *client) RequestWithHeaders(method, url string, body io.Reader, headers
}

// get retrieves an object from the tado° API.
func (c *client) get(url string, v interface{}) error {
resp, err := c.Request(http.MethodGet, url, nil)
func (c *client) get(ctx context.Context, url string, v interface{}) error {
resp, err := c.Request(ctx, http.MethodGet, url, nil)
if err != nil {
return err
}
Expand All @@ -113,8 +113,8 @@ func (c *client) get(url string, v interface{}) error {
}

// post sends a post request to the tado° API.
func (c *client) post(url string) error {
resp, err := c.Request(http.MethodPost, url, nil)
func (c *client) post(ctx context.Context, url string) error {
resp, err := c.Request(ctx, http.MethodPost, url, nil)
if err != nil {
return err
}
Expand All @@ -133,12 +133,12 @@ func (c *client) post(url string) error {
// If the update is successful and v is a pointer, put will decode the response
// body into the value pointed to by v. If v is not a pointer the response body
// will be ignored.
func (c *client) put(url string, v interface{}) error {
func (c *client) put(ctx context.Context, url string, v interface{}) error {
data, err := json.Marshal(v)
if err != nil {
return fmt.Errorf("unable to marshal object: %w", err)
}
resp, err := c.RequestWithHeaders(http.MethodPut, url, bytes.NewReader(data),
resp, err := c.RequestWithHeaders(ctx, http.MethodPut, url, bytes.NewReader(data),
map[string]string{"Content-Type": "application/json;charset=utf-8"})
if err != nil {
return err
Expand All @@ -161,8 +161,8 @@ func (c *client) put(url string, v interface{}) error {
}

// delete deletes an object from the tado° API.
func (c *client) delete(url string) error {
resp, err := c.Request(http.MethodDelete, url, nil)
func (c *client) delete(ctx context.Context, url string) error {
resp, err := c.Request(ctx, http.MethodDelete, url, nil)
if err != nil {
return err
}
Expand Down
8 changes: 4 additions & 4 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func TestGet(t *testing.T) {
client.http = mockHTTPClient{Response: tc.mockResp, Error: tc.mockErr}

result := &foobar{}
err := client.get(tc.url, result)
err := client.get(context.Background(), tc.url, result)

if tc.wantErr != nil {
assert.EqualError(t, err, tc.wantErr.Error())
Expand Down Expand Up @@ -219,7 +219,7 @@ func TestPost(t *testing.T) {
client := newClient("test", "test")
client.http = mockHTTPClient{Response: tc.mockResp, Error: tc.mockErr}

err := client.post(tc.url)
err := client.post(context.Background(), tc.url)

if tc.wantErr != nil {
assert.EqualError(t, err, tc.wantErr.Error())
Expand Down Expand Up @@ -349,7 +349,7 @@ func TestPut(t *testing.T) {
client.http = mockHTTPClient{Response: tc.mockResp, Error: tc.mockErr}
data := tc.data

err := client.put(tc.url, data)
err := client.put(context.Background(), tc.url, data)

if tc.wantErr != nil {
assert.EqualError(t, err, tc.wantErr.Error())
Expand Down Expand Up @@ -423,7 +423,7 @@ func TestDelete(t *testing.T) {
client := newClient("test", "test")
client.http = mockHTTPClient{Response: tc.mockResp, Error: tc.mockErr}

err := client.delete(tc.url)
err := client.delete(context.Background(), tc.url)

if tc.wantErr != nil {
assert.EqualError(t, err, tc.wantErr.Error())
Expand Down

0 comments on commit a222c80

Please sign in to comment.