-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathapi.go
166 lines (141 loc) · 4.54 KB
/
api.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package api
import (
"bytes"
"log"
"net/http"
"os"
"reflect"
"strings"
"github.com/ActiveState/cli/internal/multilog"
"github.com/ActiveState/cli/internal/runbits/rationalize"
"github.com/ActiveState/cli/pkg/platform/api/errors"
"github.com/alecthomas/template"
"github.com/ActiveState/cli/pkg/sysinfo"
"github.com/ActiveState/cli/internal/condition"
"github.com/ActiveState/cli/internal/constants"
"github.com/ActiveState/cli/internal/logging"
"github.com/ActiveState/cli/internal/retryhttp"
"github.com/ActiveState/cli/internal/singleton/uniqid"
)
// NewHTTPClient creates a new HTTP client that will retry requests and
// add additional request information to the request headers
func NewHTTPClient() *http.Client {
if condition.InUnitTest() {
return http.DefaultClient
}
return &http.Client{
Transport: NewRoundTripper(retryhttp.DefaultClient.StandardClient().Transport),
}
}
// RoundTripper is an implementation of http.RoundTripper that adds additional request information
type RoundTripper struct {
transport http.RoundTripper
}
// RoundTrip executes a single HTTP transaction, returning a Response for the provided Request.
func (r *RoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
req.Header.Add("User-Agent", r.UserAgent())
req.Header.Add("X-Requestor", uniqid.Text())
resp, err := r.transport.RoundTrip(req)
if err != nil && resp != nil && resp.StatusCode == http.StatusForbidden && strings.EqualFold(resp.Header.Get("server"), "cloudfront") {
return nil, api_errors.NewCountryBlockedError()
}
// This code block is for integration testing purposes only.
if os.Getenv(constants.DebugServiceRequestsEnvVarName) != "" &&
(condition.OnCI() || condition.BuiltOnDevMachine()) {
logging.Debug("URL: %s\n", req.URL)
logging.Debug("User-Agent: %s\n", resp.Request.Header.Get("User-Agent"))
logging.Debug("X-Requestor: %s\n", resp.Request.Header.Get("X-Requestor"))
}
return resp, err
}
// UserAgent returns the user agent used by the State Tool
func (r *RoundTripper) UserAgent() string {
var osVersionStr string
osVersion, err := sysinfo.OSVersion()
if err != nil {
logging.Error("Could not detect OS version: %v", err)
} else {
osVersionStr = osVersion.Version
}
agentTemplate, err := template.New("").Parse(constants.UserAgentTemplate)
if err != nil {
log.Panicf("Parsing user agent template failed: %v", err)
}
var userAgent bytes.Buffer
err = agentTemplate.Execute(&userAgent, struct {
UserAgent string
OS string
OSVersion string
Architecture string
}{
UserAgent: constants.UserAgent,
OS: sysinfo.OS().String(),
OSVersion: osVersionStr,
Architecture: sysinfo.Architecture().String(),
})
if err != nil {
multilog.Error("Could not execute user agent template: %v", err)
}
return userAgent.String()
}
// NewRoundTripper creates a new instance of RoundTripper
func NewRoundTripper(transport http.RoundTripper) http.RoundTripper {
return &RoundTripper{transport}
}
// ErrorCode tries to retrieve the code associated with an API error
func ErrorCode(err interface{}) int {
codeVal := reflect.Indirect(reflect.ValueOf(err)).FieldByName("Code")
if codeVal.IsValid() {
return int(codeVal.Int())
}
return ErrorCodeFromPayload(err)
}
// ErrorCodeFromPayload tries to retrieve the code associated with an API error from a
// Message object referenced as a Payload.
func ErrorCodeFromPayload(err interface{}) int {
errVal := reflect.ValueOf(err)
payloadVal := reflect.Indirect(errVal).FieldByName("Payload")
if !payloadVal.IsValid() {
return -1
}
codePtr := reflect.Indirect(payloadVal).FieldByName("Code")
if !codePtr.IsValid() {
return -1
}
codeVal := reflect.Indirect(codePtr)
if !codeVal.IsValid() {
return -1
}
return int(codeVal.Int())
}
// ErrorMessageFromPayload tries to retrieve the code associated with an API error from a
// Message object referenced as a Payload.
func ErrorMessageFromPayload(err error) string {
errVal := reflect.ValueOf(err)
payloadVal := reflect.Indirect(errVal).FieldByName("Payload")
if !payloadVal.IsValid() {
return err.Error()
}
codePtr := reflect.Indirect(payloadVal).FieldByName("Message")
if !codePtr.IsValid() {
return err.Error()
}
codeVal := reflect.Indirect(codePtr)
if !codeVal.IsValid() {
return err.Error()
}
return codeVal.String()
}
func ErrorFromPayload(err error) error {
return ErrorFromPayloadTyped(err)
}
func ErrorFromPayloadTyped(err error) *rationalize.ErrAPI {
if err == nil {
return nil
}
return &rationalize.ErrAPI{
err,
ErrorCodeFromPayload(err),
ErrorMessageFromPayload(err),
}
}