master
20918 3 weeks ago
parent 4244d058c6
commit 0555e21684

@ -37,6 +37,8 @@ type Client struct {
http *http.Client 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 { func checkError(resp *http.Response, body []byte) error {
if resp.StatusCode < http.StatusBadRequest { if resp.StatusCode < http.StatusBadRequest {
return nil return nil
@ -69,6 +71,7 @@ func ClientFromEnvironment() (*Client, error) {
}, nil }, nil
} }
// NewClient creates a new [Client] with the given base URL and HTTP client.
func NewClient(base *url.URL, http *http.Client) *Client { func NewClient(base *url.URL, http *http.Client) *Client {
return &Client{ return &Client{
base: base, 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 { func (c *Client) do(ctx context.Context, method, path string, reqData, respData any) error {
var reqBody io.Reader var reqBody io.Reader
var data []byte var data []byte
@ -129,8 +134,12 @@ func (c *Client) do(ctx context.Context, method, path string, reqData, respData
return nil return nil
} }
// maxBufferSize is the maximum size of the buffer used to read from the
// server.
const maxBufferSize = 512 * format.KiloByte 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 { func (c *Client) stream(ctx context.Context, method, path string, data any, fn func([]byte) error) error {
var buf io.Reader var buf io.Reader
if data != nil { if data != nil {

Loading…
Cancel
Save