Skip to content

Commit

Permalink
Optimize SetResponseBodyTransformer
Browse files Browse the repository at this point in the history
  • Loading branch information
imroc committed Jan 11, 2023
1 parent 97163fc commit 3b87ac1
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 8 deletions.
4 changes: 2 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ type Client struct {
udBeforeRequest []RequestMiddleware
afterResponse []ResponseMiddleware
wrappedRoundTrip RoundTripper
responseBodyTransformer func([]byte) ([]byte, error)
responseBodyTransformer func(rawBody []byte, req *Request, resp *Response) (transformedBody []byte, err error)
}

// R create a new request.
Expand Down Expand Up @@ -154,7 +154,7 @@ func (c *Client) GetTransport() *Transport {

// SetResponseBodyTransformer set the response body transformer, which can modify the
// response body before unmarshalled if auto-read response body is not disabled.
func (c *Client) SetResponseBodyTransformer(fn func(body []byte) ([]byte, error)) *Client {
func (c *Client) SetResponseBodyTransformer(fn func(rawBody []byte, req *Request, resp *Response) (transformedBody []byte, err error)) *Client {
c.responseBodyTransformer = fn
return c
}
Expand Down
9 changes: 6 additions & 3 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -594,9 +594,12 @@ func TestEnableDumpAllAsync(t *testing.T) {
}

func TestSetResponseBodyTransformer(t *testing.T) {
c := tc().SetResponseBodyTransformer(func(body []byte) ([]byte, error) {
result, err := url.QueryUnescape(string(body))
return []byte(result), err
c := tc().SetResponseBodyTransformer(func(rawBody []byte, req *Request, resp *Response) (transformedBody []byte, err error) {
if resp.IsSuccess() {
result, err := url.QueryUnescape(string(rawBody))
return []byte(result), err
}
return rawBody, nil
})
user := &UserInfo{}
resp, err := c.R().SetResult(user).Get("/urlencode")
Expand Down
2 changes: 1 addition & 1 deletion client_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ func AddCommonRetryCondition(condition RetryConditionFunc) *Client {

// SetResponseBodyTransformer is a global wrapper methods which delegated
// to the default client, create a request and SetResponseBodyTransformer for request.
func SetResponseBodyTransformer(fn func(body []byte) ([]byte, error)) *Client {
func SetResponseBodyTransformer(fn func(rawBody []byte, req *Request, resp *Response) (transformedBody []byte, err error)) *Client {
return defaultClient.SetResponseBodyTransformer(fn)
}

Expand Down
5 changes: 3 additions & 2 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,11 @@ func (r *Response) ToBytes() ([]byte, error) {
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
r.setReceivedAt()
r.body = body
if err == nil && r.Request.client.responseBodyTransformer != nil {
body, err = r.Request.client.responseBodyTransformer(body)
body, err = r.Request.client.responseBodyTransformer(body, r.Request, r)
r.body = body
}
r.body = body
return body, err
}

Expand Down

0 comments on commit 3b87ac1

Please sign in to comment.