Add context.Context parameter to all API methods

Enable request timeout and cancellation control by adding context.Context
as the first parameter to all SDK API methods. This allows users to:
- Set per-request timeouts
- Cancel in-flight requests
- Pass request-scoped values
This commit is contained in:
2025-12-29 11:00:13 -05:00
parent 5576cced09
commit eb6100b736
8 changed files with 140 additions and 64 deletions

View File

@@ -2,6 +2,7 @@ package client
import (
"bytes"
"context"
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
@@ -62,7 +63,7 @@ func (c *Client) createHmac(msgStr string) []byte {
return h.Sum(nil)
}
func (c *Client) doRequest(method, path, contentType string, body any, response any) error {
func (c *Client) doRequest(ctx context.Context, method, path, contentType string, body any, response any) error {
var bodyBytes []byte
var err error
@@ -83,7 +84,7 @@ func (c *Client) doRequest(method, path, contentType string, body any, response
}
fullURL := c.baseURL + path
req, err := http.NewRequest(method, fullURL, bytes.NewBuffer(bodyBytes))
req, err := http.NewRequestWithContext(ctx, method, fullURL, bytes.NewBuffer(bodyBytes))
if err != nil {
return fmt.Errorf("failed to create request: %w", err)
}
@@ -127,18 +128,18 @@ func checkByteSlice(body interface{}) bool {
return ok
}
func (c *Client) Get(path string, response any) error {
return c.doRequest(http.MethodGet, path, "", nil, response)
func (c *Client) Get(ctx context.Context, path string, response any) error {
return c.doRequest(ctx, http.MethodGet, path, "", nil, response)
}
func (c *Client) Post(path, contentType string, body any, response any) error {
return c.doRequest(http.MethodPost, path, contentType, body, response)
func (c *Client) Post(ctx context.Context, path, contentType string, body any, response any) error {
return c.doRequest(ctx, http.MethodPost, path, contentType, body, response)
}
func (c *Client) Put(path, contentType string, body any, response any) error {
return c.doRequest(http.MethodPut, path, contentType, body, response)
func (c *Client) Put(ctx context.Context, path, contentType string, body any, response any) error {
return c.doRequest(ctx, http.MethodPut, path, contentType, body, response)
}
func (c *Client) Delete(path string, response any) error {
return c.doRequest(http.MethodDelete, path, "", nil, response)
func (c *Client) Delete(ctx context.Context, path string, response any) error {
return c.doRequest(ctx, http.MethodDelete, path, "", nil, response)
}