/
core.go
269 lines (241 loc) · 6.71 KB
/
core.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package core
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"mime/multipart"
"net/http"
)
const (
// contentType specifies the JSON Content-Type header value.
contentType = "application/json"
contentTypeHeader = "Content-Type"
)
// HTTPClient is an interface for a subset of the *http.Client.
type HTTPClient interface {
Do(*http.Request) (*http.Response, error)
}
// MergeHeaders merges the given headers together, where the right
// takes precedence over the left.
func MergeHeaders(left, right http.Header) http.Header {
for key, values := range right {
if len(values) > 1 {
left[key] = values
continue
}
if value := right.Get(key); value != "" {
left.Set(key, value)
}
}
return left
}
// WriteMultipartJSON writes the given value as a JSON part.
// This is used to serialize non-primitive multipart properties
// (i.e. lists, objects, etc).
func WriteMultipartJSON(writer *multipart.Writer, field string, value interface{}) error {
bytes, err := json.Marshal(value)
if err != nil {
return err
}
return writer.WriteField(field, string(bytes))
}
// APIError is a lightweight wrapper around the standard error
// interface that preserves the status code from the RPC, if any.
type APIError struct {
err error
StatusCode int `json:"-"`
}
// NewAPIError constructs a new API error.
func NewAPIError(statusCode int, err error) *APIError {
return &APIError{
err: err,
StatusCode: statusCode,
}
}
// Unwrap returns the underlying error. This also makes the error compatible
// with errors.As and errors.Is.
func (a *APIError) Unwrap() error {
if a == nil {
return nil
}
return a.err
}
// Error returns the API error's message.
func (a *APIError) Error() string {
if a == nil || (a.err == nil && a.StatusCode == 0) {
return ""
}
if a.err == nil {
return fmt.Sprintf("%d", a.StatusCode)
}
if a.StatusCode == 0 {
return a.err.Error()
}
return fmt.Sprintf("%d: %s", a.StatusCode, a.err.Error())
}
// ErrorDecoder decodes *http.Response errors and returns a
// typed API error (e.g. *APIError).
type ErrorDecoder func(statusCode int, body io.Reader) error
// Caller calls APIs and deserializes their response, if any.
type Caller struct {
client HTTPClient
retrier *Retrier
}
// CallerParams represents the parameters used to constrcut a new *Caller.
type CallerParams struct {
Client HTTPClient
MaxAttempts uint
}
// NewCaller returns a new *Caller backed by the given parameters.
func NewCaller(params *CallerParams) *Caller {
var httpClient HTTPClient = http.DefaultClient
if params.Client != nil {
httpClient = params.Client
}
var retryOptions []RetryOption
if params.MaxAttempts > 0 {
retryOptions = append(retryOptions, WithMaxAttempts(params.MaxAttempts))
}
return &Caller{
client: httpClient,
retrier: NewRetrier(retryOptions...),
}
}
// CallParams represents the parameters used to issue an API call.
type CallParams struct {
URL string
Method string
MaxAttempts uint
Headers http.Header
Client HTTPClient
Request interface{}
Response interface{}
ResponseIsOptional bool
ErrorDecoder ErrorDecoder
}
// Call issues an API call according to the given call parameters.
func (c *Caller) Call(ctx context.Context, params *CallParams) error {
req, err := newRequest(ctx, params.URL, params.Method, params.Headers, params.Request)
if err != nil {
return err
}
// If the call has been cancelled, don't issue the request.
if err := ctx.Err(); err != nil {
return err
}
client := c.client
if params.Client != nil {
// Use the HTTP client scoped to the request.
client = params.Client
}
var retryOptions []RetryOption
if params.MaxAttempts > 0 {
retryOptions = append(retryOptions, WithMaxAttempts(params.MaxAttempts))
}
resp, err := c.retrier.Run(
client.Do,
req,
params.ErrorDecoder,
retryOptions...,
)
if err != nil {
return err
}
// Close the response body after we're done.
defer resp.Body.Close()
// Check if the call was cancelled before we return the error
// associated with the call and/or unmarshal the response data.
if err := ctx.Err(); err != nil {
return err
}
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return decodeError(resp, params.ErrorDecoder)
}
// Mutate the response parameter in-place.
if params.Response != nil {
if writer, ok := params.Response.(io.Writer); ok {
_, err = io.Copy(writer, resp.Body)
} else {
err = json.NewDecoder(resp.Body).Decode(params.Response)
}
if err != nil {
if err == io.EOF {
if params.ResponseIsOptional {
// The response is optional, so we should ignore the
// io.EOF error
return nil
}
return fmt.Errorf("expected a %T response, but the server responded with nothing", params.Response)
}
return err
}
}
return nil
}
// newRequest returns a new *http.Request with all of the fields
// required to issue the call.
func newRequest(
ctx context.Context,
url string,
method string,
endpointHeaders http.Header,
request interface{},
) (*http.Request, error) {
requestBody, err := newRequestBody(request)
if err != nil {
return nil, err
}
req, err := http.NewRequestWithContext(ctx, method, url, requestBody)
if err != nil {
return nil, err
}
req = req.WithContext(ctx)
req.Header.Set(contentTypeHeader, contentType)
for name, values := range endpointHeaders {
req.Header[name] = values
}
return req, nil
}
// newRequestBody returns a new io.Reader that represents the HTTP request body.
func newRequestBody(request interface{}) (io.Reader, error) {
var requestBody io.Reader
if request != nil {
if body, ok := request.(io.Reader); ok {
requestBody = body
} else {
requestBytes, err := json.Marshal(request)
if err != nil {
return nil, err
}
requestBody = bytes.NewReader(requestBytes)
}
}
return requestBody, nil
}
// decodeError decodes the error from the given HTTP response. Note that
// it's the caller's responsibility to close the response body.
func decodeError(response *http.Response, errorDecoder ErrorDecoder) error {
if errorDecoder != nil {
// This endpoint has custom errors, so we'll
// attempt to unmarshal the error into a structured
// type based on the status code.
return errorDecoder(response.StatusCode, response.Body)
}
// This endpoint doesn't have any custom error
// types, so we just read the body as-is, and
// put it into a normal error.
bytes, err := io.ReadAll(response.Body)
if err != nil && err != io.EOF {
return err
}
if err == io.EOF {
// The error didn't have a response body,
// so all we can do is return an error
// with the status code.
return NewAPIError(response.StatusCode, nil)
}
return NewAPIError(response.StatusCode, errors.New(string(bytes)))
}