From ec301c679024aebf354129e58d7a740e3cf1572c Mon Sep 17 00:00:00 2001 From: Francesco Mari Date: Tue, 16 Apr 2024 13:56:06 +0200 Subject: [PATCH] Improve and document response handling (#28) --- ims/client.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/ims/client.go b/ims/client.go index 46d2226..4181b8d 100644 --- a/ims/client.go +++ b/ims/client.go @@ -80,12 +80,22 @@ type Response struct { RetryAfter string } -func (c *Client) do(req *http.Request) (*Response, error) { +func (c *Client) do(req *http.Request) (_ *Response, e error) { res, err := c.client.Do(req) if err != nil { return nil, err } - defer res.Body.Close() + + defer func() { + if err := res.Body.Close(); err != nil && e == nil { + e = fmt.Errorf("close body: %v", err) + } + }() + + // If the call to io.ReadAll is removed, make sure to io.Copy the response + // body into io.Discard to allow reusing the underlying connection for + // Keep-Alive support in HTTP 1.x. See the documentation of the Body field + // in http.Response for further details. data, err := io.ReadAll(res.Body) if err != nil {