Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(enhancement): inherit client values in the request during a creation and use it afterwards #796 #840

Merged
merged 2 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
name: Build
strategy:
matrix:
go: [ 'stable', '1.19.x' ]
go: [ 'stable', '1.20.x' ]
os: [ ubuntu-latest ]

runs-on: ${{ matrix.os }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/label-actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
build:
strategy:
matrix:
go: [ 'stable', '1.19.x' ]
go: [ 'stable', '1.20.x' ]
os: [ ubuntu-latest ]

name: Run Build
Expand Down
26 changes: 15 additions & 11 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ type Client struct {
closeConnection bool
notParseResponse bool
trace bool
debugBodySizeLimit int64
debugBodySizeLimit int
outputDirectory string
scheme string
log Logger
Expand Down Expand Up @@ -565,16 +565,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 @@ -726,7 +730,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.lock.Lock()
defer c.lock.Unlock()
c.debugBodySizeLimit = sl
Expand Down Expand Up @@ -1601,7 +1605,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 @@ -479,7 +479,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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/go-resty/resty/v3

go 1.19
go 1.20

require (
golang.org/x/net v0.27.0
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
Loading