Add Client.Patch method for PATCH HTTP requests

Follows the same pattern as Put/Post, accepting context, URL, and body.
Closes an obvious gap in the REST client API.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-03-22 21:47:11 +03:00
parent 4d47918a66
commit f6384ecbea
2 changed files with 66 additions and 3 deletions

View File

@@ -20,6 +20,7 @@ type Client struct {
baseURL string
errorMapper ErrorMapper
balancerCloser *balancer.Closer
maxResponseBody int64
}
// New creates a new Client with the given options.
@@ -77,9 +78,10 @@ func New(opts ...Option) *Client {
Transport: rt,
Timeout: o.timeout,
},
baseURL: o.baseURL,
errorMapper: o.errorMapper,
balancerCloser: balancerCloser,
baseURL: o.baseURL,
errorMapper: o.errorMapper,
balancerCloser: balancerCloser,
maxResponseBody: o.maxResponseBody,
}
}
@@ -99,6 +101,13 @@ func (c *Client) Do(ctx context.Context, req *http.Request) (*Response, error) {
}
}
if c.maxResponseBody > 0 {
resp.Body = &limitedReadCloser{
R: io.LimitedReader{R: resp.Body, N: c.maxResponseBody},
C: resp.Body,
}
}
r := newResponse(resp)
if c.errorMapper != nil {
@@ -142,6 +151,15 @@ func (c *Client) Put(ctx context.Context, url string, body io.Reader) (*Response
return c.Do(ctx, req)
}
// Patch performs a PATCH request to the given URL with the given body.
func (c *Client) Patch(ctx context.Context, url string, body io.Reader) (*Response, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodPatch, url, body)
if err != nil {
return nil, err
}
return c.Do(ctx, req)
}
// Delete performs a DELETE request to the given URL.
func (c *Client) Delete(ctx context.Context, url string) (*Response, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, url, nil)