/
roundtripper.go
37 lines (30 loc) · 1003 Bytes
/
roundtripper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package retryhttp
import (
"net/http"
"strings"
"github.com/ActiveState/cli/internal/locale"
"github.com/ActiveState/cli/pkg/platform"
"github.com/hashicorp/go-retryablehttp"
)
// RoundTripper implements the http.RoundTripper interface, using a retrying
// HTTP client to execute requests.
type RoundTripper struct {
client *Client
}
func (rt *RoundTripper) init() {
if rt.client == nil {
rt.client = DefaultClient
}
}
// RoundTrip satisfies the http.RoundTripper interface.
func (rt *RoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
retryableReq, err := retryablehttp.FromRequest(req)
if err != nil {
return nil, locale.WrapError(err, "err_retry_convert_req", "Could not convert request to retryable format")
}
resp, err := rt.client.Do(retryableReq)
if err != nil && resp != nil && resp.StatusCode == http.StatusForbidden && strings.EqualFold(resp.Header.Get("server"), "cloudfront") {
return nil, platform.NewCountryBlockedError()
}
return resp, err
}