diff --git a/api/client.go b/api/client.go index 3dffce6..acb30d8 100644 --- a/api/client.go +++ b/api/client.go @@ -37,6 +37,8 @@ type Client struct { http *http.Client } +// checkError checks the response from the server and returns an error if the +// status code is not in the 200-299 range. func checkError(resp *http.Response, body []byte) error { if resp.StatusCode < http.StatusBadRequest { return nil @@ -69,6 +71,7 @@ func ClientFromEnvironment() (*Client, error) { }, nil } +// NewClient creates a new [Client] with the given base URL and HTTP client. func NewClient(base *url.URL, http *http.Client) *Client { return &Client{ base: base, @@ -76,6 +79,8 @@ func NewClient(base *url.URL, http *http.Client) *Client { } } +// do performs an HTTP request and unmarshals the response into the given +// response data. func (c *Client) do(ctx context.Context, method, path string, reqData, respData any) error { var reqBody io.Reader var data []byte @@ -129,8 +134,12 @@ func (c *Client) do(ctx context.Context, method, path string, reqData, respData return nil } +// maxBufferSize is the maximum size of the buffer used to read from the +// server. const maxBufferSize = 512 * format.KiloByte +// stream performs an HTTP request and calls the given function for each +// response. func (c *Client) stream(ctx context.Context, method, path string, data any, fn func([]byte) error) error { var buf io.Reader if data != nil {