Skip to content

Commit

Permalink
refactor: optimize allocation
Browse files Browse the repository at this point in the history
  • Loading branch information
jeevatkm committed Oct 26, 2024
1 parent a6a489b commit 94aeffb
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,17 +263,19 @@ func createHTTPRequest(c *Client, r *Request) (err error) {
r.RawRequest = r.RawRequest.WithContext(r.ctx)
}

bodyCopy, err := getBodyCopy(r)
if err != nil {
return err
}

// assign get body func for the underlying raw request instance
r.RawRequest.GetBody = func() (io.ReadCloser, error) {
if r.RawRequest.GetBody == nil {
bodyCopy, err := getBodyCopy(r)
if err != nil {
return err
}
if bodyCopy != nil {
return io.NopCloser(bytes.NewReader(bodyCopy.Bytes())), nil
buf := bodyCopy.Bytes()
r.RawRequest.GetBody = func() (io.ReadCloser, error) {
b := bytes.NewReader(buf)
return io.NopCloser(b), nil
}
}
return nil, nil
}

return
Expand Down

0 comments on commit 94aeffb

Please sign in to comment.