Skip to content

Commit

Permalink
Improve and document response handling (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
francescomari authored Apr 16, 2024
1 parent bcbf7de commit ec301c6
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions ims/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit ec301c6

Please sign in to comment.