Skip to content

Commit b3416e3

Browse files
authored
Add retryable HTTP client from v1 (#162)
* Add retryable HTTP client from v1 * Make log adaptor private * Fix go.mod * Rename package * Rename constructor
1 parent 45944c2 commit b3416e3

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.16
55
require (
66
github.com/bitrise-io/go-utils v1.0.1
77
github.com/gofrs/uuid v4.2.0+incompatible
8+
github.com/hashicorp/go-retryablehttp v0.7.1
89
github.com/mitchellh/mapstructure v1.4.3
910
github.com/stretchr/testify v1.7.0
1011
golang.org/x/sys v0.0.0-20220712014510-0a85c31ab51e

go.sum

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9n
1010
github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48=
1111
github.com/hashicorp/go-hclog v0.9.2 h1:CG6TE5H9/JXsFWJCfoIVpKFIkFe6ysEuHirp4DxCsHI=
1212
github.com/hashicorp/go-hclog v0.9.2/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrjA0H7acj2lQ=
13-
github.com/hashicorp/go-retryablehttp v0.7.0 h1:eu1EI/mbirUgP5C8hVsTNaGZreBDlYiwC1FZWkvQPQ4=
1413
github.com/hashicorp/go-retryablehttp v0.7.0/go.mod h1:vAew36LZh98gCBJNLH42IQ1ER/9wtLZZ8meHqQvEYWY=
14+
github.com/hashicorp/go-retryablehttp v0.7.1 h1:sUiuQAnLlbvmExtFQs72iFW/HXeUn8Z1aJLQ4LJJbTQ=
15+
github.com/hashicorp/go-retryablehttp v0.7.1/go.mod h1:vAew36LZh98gCBJNLH42IQ1ER/9wtLZZ8meHqQvEYWY=
1516
github.com/mitchellh/mapstructure v1.4.3 h1:OVowDSCllw/YjdLkam3/sm7wEtOy59d8ndGgCcyj8cs=
1617
github.com/mitchellh/mapstructure v1.4.3/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
1718
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=

retryhttp/retryhttp.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package retryhttp
2+
3+
import (
4+
"github.com/bitrise-io/go-utils/v2/log"
5+
"github.com/hashicorp/go-retryablehttp"
6+
)
7+
8+
// NewClient returns a retryable HTTP client with common defaults
9+
func NewClient(logger log.Logger) *retryablehttp.Client {
10+
client := retryablehttp.NewClient()
11+
client.Logger = &httpLogAdaptor{logger: logger}
12+
client.ErrorHandler = retryablehttp.PassthroughErrorHandler
13+
14+
return client
15+
}
16+
17+
// httpLogAdaptor adapts the retryablehttp.Logger interface to the go-utils logger.
18+
type httpLogAdaptor struct {
19+
logger log.Logger
20+
}
21+
22+
// Printf implements the retryablehttp.Logger interface
23+
func (a *httpLogAdaptor) Printf(fmtStr string, vars ...interface{}) {
24+
a.logger.Debugf(fmtStr, vars...)
25+
}

0 commit comments

Comments
 (0)