From 21be415ee13872f4be7c44541ceb42fa7470fc01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olive=CC=81r=20Falvai?= Date: Tue, 2 Aug 2022 11:11:03 +0200 Subject: [PATCH 1/5] Add retryable HTTP client from v1 --- http/retryhttp.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 http/retryhttp.go diff --git a/http/retryhttp.go b/http/retryhttp.go new file mode 100644 index 0000000..00993c3 --- /dev/null +++ b/http/retryhttp.go @@ -0,0 +1,25 @@ +package http + +import ( + "github.com/bitrise-io/go-utils/v2/log" + "github.com/hashicorp/go-retryablehttp" +) + +// 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...) +} + +// NewHTTPClient returns a retryable HTTP client with common defaults +func NewHTTPClient(logger log.Logger) *retryablehttp.Client { + client := retryablehttp.NewClient() + client.Logger = &HTTPLogAdaptor{} + client.ErrorHandler = retryablehttp.PassthroughErrorHandler + + return client +} From 97a97396313edd3847924144fb5a46fa1792ec69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olive=CC=81r=20Falvai?= Date: Tue, 2 Aug 2022 11:13:57 +0200 Subject: [PATCH 2/5] Make log adaptor private --- http/retryhttp.go | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/http/retryhttp.go b/http/retryhttp.go index 00993c3..9007767 100644 --- a/http/retryhttp.go +++ b/http/retryhttp.go @@ -5,21 +5,21 @@ import ( "github.com/hashicorp/go-retryablehttp" ) -// 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...) -} - // NewHTTPClient returns a retryable HTTP client with common defaults func NewHTTPClient(logger log.Logger) *retryablehttp.Client { client := retryablehttp.NewClient() - client.Logger = &HTTPLogAdaptor{} + 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...) +} From d14f1b233455a73c5d4ba740898722593c60e918 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olive=CC=81r=20Falvai?= Date: Tue, 2 Aug 2022 11:15:18 +0200 Subject: [PATCH 3/5] Fix go.mod --- go.mod | 1 + go.sum | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) 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= From 89f0bf605d5310e45ad9984813b262bdacbbb135 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olive=CC=81r=20Falvai?= Date: Tue, 2 Aug 2022 13:46:58 +0200 Subject: [PATCH 4/5] Rename package --- {http => retryhttp}/retryhttp.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename {http => retryhttp}/retryhttp.go (97%) diff --git a/http/retryhttp.go b/retryhttp/retryhttp.go similarity index 97% rename from http/retryhttp.go rename to retryhttp/retryhttp.go index 9007767..57a4be3 100644 --- a/http/retryhttp.go +++ b/retryhttp/retryhttp.go @@ -1,4 +1,4 @@ -package http +package retryhttp import ( "github.com/bitrise-io/go-utils/v2/log" From 8eea93cd080d79f91334cfe9f8a26c79d421d52a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olive=CC=81r=20Falvai?= Date: Tue, 2 Aug 2022 14:13:51 +0200 Subject: [PATCH 5/5] Rename constructor --- retryhttp/retryhttp.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/retryhttp/retryhttp.go b/retryhttp/retryhttp.go index 57a4be3..5169ac7 100644 --- a/retryhttp/retryhttp.go +++ b/retryhttp/retryhttp.go @@ -5,8 +5,8 @@ import ( "github.com/hashicorp/go-retryablehttp" ) -// NewHTTPClient returns a retryable HTTP client with common defaults -func NewHTTPClient(logger log.Logger) *retryablehttp.Client { +// 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