Skip to content

Commit

Permalink
feat(enhancement): inherit client values in the request during a crea…
Browse files Browse the repository at this point in the history
…tion and use it afterwards #796
  • Loading branch information
jeevatkm committed Sep 1, 2024
1 parent 0953f5a commit 1c1fdb8
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 134 deletions.
26 changes: 15 additions & 11 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ type Client struct {
closeConnection bool
notParseResponse bool
trace bool
debugBodySizeLimit int64
debugBodySizeLimit int
outputDirectory string
scheme string
log Logger
Expand Down Expand Up @@ -473,16 +473,20 @@ func (c *Client) R() *Request {
FormData: url.Values{},
Header: http.Header{},
Cookies: make([]*http.Cookie, 0),
PathParams: map[string]string{},
RawPathParams: map[string]string{},
PathParams: make(map[string]string),
RawPathParams: make(map[string]string),
Debug: c.Debug,

client: c,
multipartFiles: []*File{},
multipartFields: []*MultipartField{},
jsonEscapeHTML: c.jsonEscapeHTML,
log: c.log,
responseBodyLimit: c.ResponseBodyLimit,
client: c,
multipartFiles: []*File{},
multipartFields: []*MultipartField{},
jsonEscapeHTML: c.jsonEscapeHTML,
log: c.log,
setContentLength: c.setContentLength,
trace: c.trace,
notParseResponse: c.notParseResponse,
debugBodySizeLimit: c.debugBodySizeLimit,
responseBodyLimit: c.ResponseBodyLimit,
}
return r
}
Expand Down Expand Up @@ -615,7 +619,7 @@ func (c *Client) SetDebug(d bool) *Client {
// SetDebugBodyLimit sets the maximum size for which the response and request body will be logged in debug mode.
//
// client.SetDebugBodyLimit(1000000)
func (c *Client) SetDebugBodyLimit(sl int64) *Client {
func (c *Client) SetDebugBodyLimit(sl int) *Client {
c.debugBodySizeLimit = sl
return c
}
Expand Down Expand Up @@ -1280,7 +1284,7 @@ func (c *Client) execute(req *Request) (*Response, error) {
RawResponse: resp,
}

if err != nil || req.notParseResponse || c.notParseResponse {
if err != nil || req.notParseResponse {
response.setReceivedAt()
return response, err
}
Expand Down
2 changes: 1 addition & 1 deletion client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ func TestClientOptions(t *testing.T) {
client.SetDebug(true)
assertEqual(t, client.Debug, true)

var sl int64 = 1000000
var sl int = 1000000
client.SetDebugBodyLimit(sl)
assertEqual(t, client.debugBodySizeLimit, sl)

Expand Down
14 changes: 7 additions & 7 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func parseRequestBody(c *Client, r *Request) error {
}

// by default resty won't set content length, you can if you want to :)
if c.setContentLength || r.setContentLength {
if r.setContentLength {
if r.bodyBuf == nil {
r.Header.Set(hdrContentLengthKey, "0")
} else {
Expand All @@ -207,7 +207,7 @@ func createHTTPRequest(c *Client, r *Request) (err error) {
if r.bodyBuf == nil {
if reader, ok := r.Body.(io.Reader); ok && isPayloadSupported(r.Method, c.AllowGetMethodPayload) {
r.RawRequest, err = http.NewRequest(r.Method, r.URL, reader)
} else if c.setContentLength || r.setContentLength {
} else if r.setContentLength {
r.RawRequest, err = http.NewRequest(r.Method, r.URL, http.NoBody)
} else {
r.RawRequest, err = http.NewRequest(r.Method, r.URL, nil)
Expand Down Expand Up @@ -239,7 +239,7 @@ func createHTTPRequest(c *Client, r *Request) (err error) {
}

// Enable trace
if c.trace || r.trace {
if r.trace {
r.clientTrace = &clientTrace{}
r.ctx = r.clientTrace.createContext(r.Context())
}
Expand Down Expand Up @@ -326,7 +326,7 @@ func requestLogger(c *Client, r *Request) error {
}
}
}
rl := &RequestLog{Header: rh, Body: r.fmtBodyString(c.debugBodySizeLimit)}
rl := &RequestLog{Header: rh, Body: r.fmtBodyString(r.debugBodySizeLimit)}
if c.requestLog != nil {
if err := c.requestLog(rl); err != nil {
return err
Expand Down Expand Up @@ -356,7 +356,7 @@ func requestLogger(c *Client, r *Request) error {

func responseLogger(c *Client, res *Response) error {
if res.Request.Debug {
rl := &ResponseLog{Header: copyHeaders(res.Header()), Body: res.fmtBodyString(c.debugBodySizeLimit)}
rl := &ResponseLog{Header: copyHeaders(res.Header()), Body: res.fmtBodyString(res.Request.debugBodySizeLimit)}
if c.responseLog != nil {
if err := c.responseLog(rl); err != nil {
return err
Expand Down Expand Up @@ -411,7 +411,7 @@ func parseResponseBody(c *Client, res *Response) (err error) {
if res.Request.Error != nil {
unmarshalErr := Unmarshalc(c, ct, res.body, res.Request.Error)
if unmarshalErr != nil {
c.log.Warnf("Cannot unmarshal response body: %s", unmarshalErr)
res.Request.log.Warnf("Cannot unmarshal response body: %s", unmarshalErr)
}
}
}
Expand Down Expand Up @@ -499,7 +499,7 @@ func handleRequestBody(c *Client, r *Request) error {

switch body := r.Body.(type) {
case io.Reader:
if c.setContentLength || r.setContentLength { // keep backward compatibility
if r.setContentLength { // keep backward compatibility
r.bodyBuf = acquireBuffer()
if _, err := r.bodyBuf.ReadFrom(body); err != nil {
return err
Expand Down
Loading

0 comments on commit 1c1fdb8

Please sign in to comment.