From 0555e21684ba7e884ac892028f2a1138f4548c70 Mon Sep 17 00:00:00 2001 From: 20918 <2091823062@qq.com> Date: Tue, 20 May 2025 16:49:32 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/client.go | 9 +++++++++ 1 file changed, 9 insertions(+) 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 {