forked from simplesurance/bunny-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
398 lines (325 loc) · 10.3 KB
/
client.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
// Package bunny provides functionality to interact with the Bunny CDN HTTP API.
package bunny
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"mime"
"net/http"
"net/http/httputil"
"net/url"
"github.com/google/go-querystring/query"
"github.com/google/uuid"
)
const (
// BaseURL is the base URL of the Bunny CDN HTTP API.
BaseURL = "https://api.bunny.net"
// AccessKeyHeaderKey is the name of the HTTP header that contains the Bunny API key.
AccessKeyHeaderKey = "AccessKey"
// DefaultUserAgent is the default value of the sent HTTP User-Agent header.
DefaultUserAgent = "bunny-go"
)
const (
hdrContentTypeName = "content-type"
contentTypeJSON = "application/json"
)
// Logf is a log function signature.
type Logf func(format string, v ...interface{})
// Client is a Bunny CDN HTTP API Client.
type Client struct {
baseURL *url.URL
apiKey string
httpClient http.Client
httpRequestLogf Logf
httpResponseLogf Logf
logf Logf
userAgent string
PullZone *PullZoneService
StorageZone *StorageZoneService
DNSZone *DNSZoneService
}
var discardLogF = func(string, ...interface{}) {}
// NewClient returns a new bunny.net API client.
// The APIKey can be found in on the Account Settings page.
//
// Bunny.net API docs: https://support.bunny.net/hc/en-us/articles/360012168840-Where-do-I-find-my-API-key-
func NewClient(APIKey string, opts ...Option) *Client {
clt := Client{
baseURL: mustParseURL(BaseURL),
apiKey: APIKey,
httpClient: *http.DefaultClient,
userAgent: DefaultUserAgent,
httpRequestLogf: discardLogF,
httpResponseLogf: discardLogF,
logf: discardLogF,
}
clt.PullZone = &PullZoneService{client: &clt}
clt.StorageZone = &StorageZoneService{client: &clt}
clt.DNSZone = &DNSZoneService{client: &clt}
for _, opt := range opts {
opt(&clt)
}
return &clt
}
func mustParseURL(urlStr string) *url.URL {
res, err := url.Parse(urlStr)
if err != nil {
panic(fmt.Sprintf("Parsing url: %s failed: %s", urlStr, err))
}
return res
}
// newRequest creates an bunny.net API request.
// urlStr maybe absolute or relative, if it is relative it is joined with
// client.baseURL.
func (c *Client) newRequest(method, urlStr string, body io.Reader) (*http.Request, error) {
url, err := c.baseURL.Parse(urlStr)
if err != nil {
return nil, err
}
req, err := http.NewRequest(method, url.String(), body)
if err != nil {
return nil, err
}
req.Header.Set(AccessKeyHeaderKey, c.apiKey)
req.Header.Add("Accept", contentTypeJSON)
req.Header.Set("User-Agent", c.userAgent)
if body != nil {
req.Header.Set(hdrContentTypeName, contentTypeJSON)
}
return req, nil
}
// newGetRequest creates an bunny.NET API GET request.
// params must be a struct or nil, it is encoded into a query parameter.
// The struct must contain `url` tags of the go-querystring package.
func (c *Client) newGetRequest(urlStr string, params interface{}) (*http.Request, error) {
if params != nil {
queryvals, err := query.Values(params)
if err != nil {
return nil, err
}
urlStr = urlStr + "?" + queryvals.Encode()
}
return c.newRequest(http.MethodGet, urlStr, nil)
}
func toJSON(data interface{}) (io.Reader, error) {
var buf io.ReadWriter
if data == nil {
return http.NoBody, nil
}
buf = &bytes.Buffer{}
enc := json.NewEncoder(buf)
enc.SetEscapeHTML(false)
if err := enc.Encode(data); err != nil {
return nil, err
}
return buf, nil
}
// newPostRequest creates a bunny.NET API POST request.
// If body is not nil, it is encoded as JSON and send as HTTP-Body.
func (c *Client) newPostRequest(urlStr string, body interface{}) (*http.Request, error) {
buf, err := toJSON(body)
if err != nil {
return nil, err
}
req, err := c.newRequest(http.MethodPost, urlStr, buf)
if err != nil {
return nil, err
}
return req, nil
}
// newDeleteRequest creates a bunny.NET API DELETE request.
// If body is not nil, it is encoded as JSON and send as HTTP-Body.
func (c *Client) newDeleteRequest(urlStr string, body interface{}) (*http.Request, error) {
buf, err := toJSON(body)
if err != nil {
return nil, err
}
return c.newRequest(http.MethodDelete, urlStr, buf)
}
// newPutRequest creates a bunny.NET API PUT request.
// If body is not nil, it is encoded as JSON and sent as a HTTP-Body.
func (c *Client) newPutRequest(urlStr string, body interface{}) (*http.Request, error) {
buf, err := toJSON(body)
if err != nil {
return nil, err
}
return c.newRequest(http.MethodPut, urlStr, buf)
}
// sendRequest sends a http Request to the bunny API.
// If the server returns a 2xx status code with an response body, the body is
// unmarshaled as JSON into result.
// If the ctx times out ctx.Error() is returned.
// If sending the response fails (http.Client.Do), the error will be returned.
// If the server returns an 401 error, an AuthenticationError error is returned.
// If the server returned an error and contains an APIError as JSON in the body,
// an APIError is returned.
// If the server returned a status code that is not 2xx an HTTPError is returned.
// If the HTTP request was successful, the response body is read and
// unmarshaled into result.
func (c *Client) sendRequest(ctx context.Context, req *http.Request, result interface{}) error {
if ctx != nil {
req = req.WithContext(ctx)
}
logReqID := c.logRequest(req)
resp, err := c.httpClient.Do(req)
if err != nil {
if urlErr, ok := err.(*url.Error); ok {
if urlErr.Timeout() && ctx.Err() != nil {
return ctx.Err()
}
}
return err
}
c.logResponse(resp, logReqID)
defer resp.Body.Close() //nolint: errcheck
if err := c.checkResp(req, resp); err != nil {
return err
}
return c.unmarshalHTTPJSONBody(resp, req.URL.String(), result)
}
func ensureJSONContentType(hdr http.Header) error {
val := hdr.Get(hdrContentTypeName)
if val == "" {
return fmt.Errorf("%s header is missing or empty", hdrContentTypeName)
}
contentType, _, err := mime.ParseMediaType(val)
if err != nil {
return fmt.Errorf("could not parse %s header value: %w", hdrContentTypeName, err)
}
if contentType != contentTypeJSON {
return fmt.Errorf("expected %s to be %q, got: %q", hdrContentTypeName, contentTypeJSON, contentType)
}
return nil
}
// checkResp checks if the resp indicates that the request was successful.
// If it wasn't an error is returned.
func (c *Client) checkResp(req *http.Request, resp *http.Response) error {
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
return nil
}
switch resp.StatusCode {
case http.StatusUnauthorized:
msg, err := io.ReadAll(resp.Body)
if err != nil {
// ignore connection errors causing that the body can
// not be received
msg = []byte(http.StatusText(http.StatusUnauthorized))
}
return &AuthenticationError{
Message: string(msg),
}
default:
httpErr := HTTPError{
RequestURL: req.URL.String(),
StatusCode: resp.StatusCode,
}
return c.parseHTTPRespErrBody(resp, &httpErr)
}
}
// parseHTTPRespErrBody processes the body of an http.Response with an non 2xx
// status code.
// If the response body is empty, baseErr is returned.
// If the body could no be parsed because of an error, the occurred errors are
// added to baseErr and baseErr is returned.
// If the body contains json data it is parsed and an APIError is returned.
func (c *Client) parseHTTPRespErrBody(resp *http.Response, baseErr *HTTPError) error {
var err error
baseErr.RespBody, err = io.ReadAll(resp.Body)
if err != nil {
baseErr.Errors = append(baseErr.Errors, fmt.Errorf("reading response body failed: %w", err))
return baseErr
}
if len(baseErr.RespBody) == 0 {
return baseErr
}
err = ensureJSONContentType(resp.Header)
if err != nil {
baseErr.Errors = append(baseErr.Errors, fmt.Errorf("processing response failed: %w", err))
return baseErr
}
var apiErr APIError
if err := json.Unmarshal(baseErr.RespBody, &apiErr); err != nil {
baseErr.Errors = append(baseErr.Errors, fmt.Errorf("could not parse body as APIError: %w", err))
return baseErr
}
apiErr.HTTPError = *baseErr
return &apiErr
}
func (c *Client) unmarshalHTTPJSONBody(resp *http.Response, reqURL string, result interface{}) error {
body, err := io.ReadAll(resp.Body)
if err != nil {
return &HTTPError{
RequestURL: reqURL,
StatusCode: resp.StatusCode,
Errors: []error{fmt.Errorf("reading response body failed: %w", err)},
}
}
if len(body) == 0 {
if result != nil {
return &HTTPError{
RequestURL: reqURL,
StatusCode: resp.StatusCode,
Errors: []error{fmt.Errorf("response has no body, expected a json %T response body", result)},
}
}
return nil
}
if result == nil {
c.logf("http-response contains body but none was expected")
return nil
}
err = ensureJSONContentType(resp.Header)
if err != nil {
return &HTTPError{
RequestURL: reqURL,
RespBody: body,
StatusCode: resp.StatusCode,
Errors: []error{fmt.Errorf("processing response failed: %w", err)},
}
}
if err := json.Unmarshal(body, result); err != nil {
return &HTTPError{
RequestURL: reqURL,
RespBody: body,
StatusCode: resp.StatusCode,
Errors: []error{fmt.Errorf("could not parse body as %T: %w", result, err)},
}
}
return nil
}
// logRequest dumps the http request to the http request logger and returns a
// unique request identifier. The identifier can be used when logging the
// response for the request, to make it easier to associate request and
// response log messages.
func (c *Client) logRequest(req *http.Request) string {
if c.httpRequestLogf == nil {
return ""
}
logReqID := uuid.New().String()
// hide the access key in the dumped request
accessKey := req.Header.Get(AccessKeyHeaderKey)
if accessKey != "" {
req.Header.Set(AccessKeyHeaderKey, "***hidden***")
defer func() { req.Header.Set(AccessKeyHeaderKey, accessKey) }()
}
debugReq, err := httputil.DumpRequestOut(req, true)
if err != nil {
c.httpRequestLogf("dumping http request (reqID: %s) failed: %s", logReqID, err)
return logReqID
}
c.httpRequestLogf("sending http-request (reqID: %s): %s", logReqID, string(debugReq))
return logReqID
}
func (c *Client) logResponse(resp *http.Response, logReqID string) {
if c.httpResponseLogf == nil {
return
}
debugResp, err := httputil.DumpResponse(resp, true)
if err != nil {
c.httpRequestLogf("dumping http response (reqID: %s) failed: %s", logReqID, err)
return
}
c.httpRequestLogf("received http-response (reqID: %s): %s", logReqID, string(debugResp))
}