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

Bring changes from v2 to main #865

Merged
merged 4 commits into from
Sep 22, 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
10 changes: 3 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,12 @@
<p align="center"><a href="#features">Features</a> section describes in detail about Resty capabilities</p>
</p>
<p align="center">
<p align="center"><a href="https://github.com/go-resty/resty/actions/workflows/ci.yml?query=branch%3Amaster"><img src="https://github.com/go-resty/resty/actions/workflows/ci.yml/badge.svg" alt="Build Status"></a> <a href="https://codecov.io/gh/go-resty/resty/branch/master"><img src="https://codecov.io/gh/go-resty/resty/branch/master/graph/badge.svg" alt="Code Coverage"></a> <a href="https://goreportcard.com/report/go-resty/resty"><img src="https://goreportcard.com/badge/go-resty/resty" alt="Go Report Card"></a> <a href="https://github.com/go-resty/resty/releases/latest"><img src="https://img.shields.io/badge/version-2.15.0-blue.svg" alt="Release Version"></a> <a href="https://pkg.go.dev/github.com/go-resty/resty/v2"><img src="https://pkg.go.dev/badge/github.com/go-resty/resty" alt="GoDoc"></a> <a href="LICENSE"><img src="https://img.shields.io/github/license/go-resty/resty.svg" alt="License"></a> <a href="https://github.com/avelino/awesome-go"><img src="https://awesome.re/mentioned-badge.svg" alt="Mentioned in Awesome Go"></a></p>
</p>
<p align="center">
<h4 align="center">Resty Communication Channels</h4>
<p align="center"><a href="https://gitter.im/go_resty/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge"><img src="https://badges.gitter.im/go_resty/community.svg" alt="Chat on Gitter - Resty Community"></a> <a href="https://twitter.com/go_resty"><img src="https://img.shields.io/badge/twitter-@go__resty-55acee.svg" alt="Twitter @go_resty"></a></p>
<p align="center"><a href="https://github.com/go-resty/resty/actions/workflows/ci.yml?query=branch%3Av2"><img src="https://github.com/go-resty/resty/actions/workflows/ci.yml/badge.svg?branch=v2" alt="Build Status"></a> <a href="https://app.codecov.io/gh/go-resty/resty/tree/v2"><img src="https://codecov.io/gh/go-resty/resty/branch/v2/graph/badge.svg" alt="Code Coverage"></a> <a href="https://goreportcard.com/report/go-resty/resty"><img src="https://goreportcard.com/badge/go-resty/resty" alt="Go Report Card"></a> <a href="https://github.com/go-resty/resty/releases/latest"><img src="https://img.shields.io/badge/version-2.15.2-blue.svg" alt="Release Version"></a> <a href="https://pkg.go.dev/github.com/go-resty/resty/v2"><img src="https://pkg.go.dev/badge/github.com/go-resty/resty" alt="GoDoc"></a> <a href="LICENSE"><img src="https://img.shields.io/github/license/go-resty/resty.svg" alt="License"></a> <a href="https://github.com/avelino/awesome-go"><img src="https://awesome.re/mentioned-badge.svg" alt="Mentioned in Awesome Go"></a></p>
</p>

## News

* v2.15.0 [released](https://github.com/go-resty/resty/releases/tag/v2.15.0) and tagged on Sep 14, 2024.
* v2.15.2 [released](https://github.com/go-resty/resty/releases/tag/v2.15.2) and tagged on Sep 21, 2024.
* v2.0.0 [released](https://github.com/go-resty/resty/releases/tag/v2.0.0) and tagged on Jul 16, 2019.
* v1.12.0 [released](https://github.com/go-resty/resty/releases/tag/v1.12.0) and tagged on Feb 27, 2019.
* v1.0 released and tagged on Sep 25, 2017. - Resty's first version was released on Sep 15, 2015 then it grew gradually as a very handy and helpful library. Its been a two years since first release. I'm very thankful to Resty users and its [contributors](https://github.com/go-resty/resty/graphs/contributors).
Expand Down Expand Up @@ -109,7 +105,7 @@ Resty author also published following projects for Go Community.

```bash
# Go Modules
require github.com/go-resty/resty/v2 v2.15.0
require github.com/go-resty/resty/v2 v2.15.2
```

## Usage
Expand Down
12 changes: 7 additions & 5 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1243,12 +1243,14 @@ func (c *Client) execute(req *Request) (*Response, error) {
}

if err != nil || req.notParseResponse || c.notParseResponse {
logErr := responseLogger(c, response)
response.setReceivedAt()
if logErr := responseLogger(c, response); logErr != nil {
return response, wrapErrors(logErr, err)
}
if err != nil {
return response, errors.Join(err, logErr)
return response, err
}
return response, wrapNoRetryErr(logErr)
return response, nil
}

if !req.isSaveResponse {
Expand All @@ -1260,7 +1262,7 @@ func (c *Client) execute(req *Request) (*Response, error) {
if _, ok := body.(*gzip.Reader); !ok {
body, err = gzip.NewReader(body)
if err != nil {
err = errors.Join(err, responseLogger(c, response))
err = wrapErrors(responseLogger(c, response), err)
response.setReceivedAt()
return response, err
}
Expand All @@ -1269,7 +1271,7 @@ func (c *Client) execute(req *Request) (*Response, error) {
}

if response.body, err = readAllWithLimit(body, req.responseBodyLimit); err != nil {
err = errors.Join(err, responseLogger(c, response))
err = wrapErrors(responseLogger(c, response), err)
response.setReceivedAt()
return response, err
}
Expand Down
37 changes: 37 additions & 0 deletions request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1710,6 +1710,43 @@ func TestRequestDoNotParseResponse(t *testing.T) {
assertNil(t, resp.RawBody())
}

func TestRequestDoNotParseResponseDebugLog(t *testing.T) {
ts := createGetServer(t)
defer ts.Close()

t.Run("do not parse response debug log client level", func(t *testing.T) {
c := dc().
SetDoNotParseResponse(true).
SetDebug(true)

var lgr bytes.Buffer
c.outputLogTo(&lgr)

_, err := c.R().
SetQueryParam("request_no", strconv.FormatInt(time.Now().Unix(), 10)).
Get(ts.URL + "/")

assertError(t, err)
assertEqual(t, true, strings.Contains(lgr.String(), "***** DO NOT PARSE RESPONSE - Enabled *****"))
})

t.Run("do not parse response debug log request level", func(t *testing.T) {
c := dc()

var lgr bytes.Buffer
c.outputLogTo(&lgr)

_, err := c.R().
SetDebug(true).
SetDoNotParseResponse(true).
SetQueryParam("request_no", strconv.FormatInt(time.Now().Unix(), 10)).
Get(ts.URL + "/")

assertError(t, err)
assertEqual(t, true, strings.Contains(lgr.String(), "***** DO NOT PARSE RESPONSE - Enabled *****"))
})
}

type noCtTest struct {
Response string `json:"response"`
}
Expand Down
3 changes: 3 additions & 0 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@ func (r *Response) setReceivedAt() {
}

func (r *Response) fmtBodyString(sl int64) string {
if r.Request.client.notParseResponse || r.Request.notParseResponse {
return "***** DO NOT PARSE RESPONSE - Enabled *****"
}
if len(r.body) > 0 {
if int64(len(r.body)) > sl {
return fmt.Sprintf("***** RESPONSE TOO LARGE (size - %d) *****", len(r.body))
Expand Down
2 changes: 1 addition & 1 deletion resty.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

// Version # of resty
const Version = "2.15.0"
const Version = "2.15.2"

// New method creates a new Resty client.
func New() *Client {
Expand Down
26 changes: 26 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,32 @@ func copyHeaders(hdrs http.Header) http.Header {
return nh
}

func wrapErrors(n error, inner error) error {
if inner == nil {
return n
}
if n == nil {
return inner
}
return &restyError{
err: n,
inner: inner,
}
}

type restyError struct {
err error
inner error
}

func (e *restyError) Error() string {
return e.err.Error()
}

func (e *restyError) Unwrap() error {
return e.inner
}

type noRetryErr struct {
err error
}
Expand Down
16 changes: 16 additions & 0 deletions util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package resty

import (
"bytes"
"errors"
"mime/multipart"
"testing"
)
Expand Down Expand Up @@ -89,3 +90,18 @@ func TestWriteMultipartFormFileReaderError(t *testing.T) {
assertNotNil(t, err)
assertEqual(t, "read error", err.Error())
}

func TestRestyErrorFuncs(t *testing.T) {
ne1 := errors.New("new error 1")
nie1 := errors.New("inner error 1")

e := wrapErrors(ne1, nie1)
assertEqual(t, "new error 1", e.Error())
assertEqual(t, "inner error 1", errors.Unwrap(e).Error())

e = wrapErrors(ne1, nil)
assertEqual(t, "new error 1", e.Error())

e = wrapErrors(nil, nie1)
assertEqual(t, "inner error 1", e.Error())
}