diff --git a/go.mod b/go.mod index 474498d..ad9c38d 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.16 require ( github.com/bitrise-io/go-utils v1.0.1 github.com/gofrs/uuid v4.2.0+incompatible + github.com/hashicorp/go-retryablehttp v0.7.1 github.com/mitchellh/mapstructure v1.4.3 github.com/stretchr/testify v1.7.0 golang.org/x/sys v0.0.0-20220712014510-0a85c31ab51e diff --git a/go.sum b/go.sum index 90c5d24..ce85819 100644 --- a/go.sum +++ b/go.sum @@ -10,8 +10,9 @@ github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9n github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48= github.com/hashicorp/go-hclog v0.9.2 h1:CG6TE5H9/JXsFWJCfoIVpKFIkFe6ysEuHirp4DxCsHI= github.com/hashicorp/go-hclog v0.9.2/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrjA0H7acj2lQ= -github.com/hashicorp/go-retryablehttp v0.7.0 h1:eu1EI/mbirUgP5C8hVsTNaGZreBDlYiwC1FZWkvQPQ4= github.com/hashicorp/go-retryablehttp v0.7.0/go.mod h1:vAew36LZh98gCBJNLH42IQ1ER/9wtLZZ8meHqQvEYWY= +github.com/hashicorp/go-retryablehttp v0.7.1 h1:sUiuQAnLlbvmExtFQs72iFW/HXeUn8Z1aJLQ4LJJbTQ= +github.com/hashicorp/go-retryablehttp v0.7.1/go.mod h1:vAew36LZh98gCBJNLH42IQ1ER/9wtLZZ8meHqQvEYWY= github.com/mitchellh/mapstructure v1.4.3 h1:OVowDSCllw/YjdLkam3/sm7wEtOy59d8ndGgCcyj8cs= github.com/mitchellh/mapstructure v1.4.3/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= diff --git a/retryhttp/retryhttp.go b/retryhttp/retryhttp.go new file mode 100644 index 0000000..5169ac7 --- /dev/null +++ b/retryhttp/retryhttp.go @@ -0,0 +1,25 @@ +package retryhttp + +import ( + "github.com/bitrise-io/go-utils/v2/log" + "github.com/hashicorp/go-retryablehttp" +) + +// NewClient returns a retryable HTTP client with common defaults +func NewClient(logger log.Logger) *retryablehttp.Client { + client := retryablehttp.NewClient() + client.Logger = &httpLogAdaptor{logger: logger} + client.ErrorHandler = retryablehttp.PassthroughErrorHandler + + return client +} + +// httpLogAdaptor adapts the retryablehttp.Logger interface to the go-utils logger. +type httpLogAdaptor struct { + logger log.Logger +} + +// Printf implements the retryablehttp.Logger interface +func (a *httpLogAdaptor) Printf(fmtStr string, vars ...interface{}) { + a.logger.Debugf(fmtStr, vars...) +}