-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.go
215 lines (165 loc) · 5.39 KB
/
http.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
package gomts
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"log/slog"
"net/http"
"net/http/httputil"
"github.com/google/go-querystring/query"
"github.com/google/uuid"
)
var (
ErrMissingToken = errors.New("missing MyTimeStation API auth token")
)
// mtsTransport implements http.Transport for MyTimeStation API requests.
type mtsTransport struct {
conf *Config
// logr is used for logging dumped requests/responses if debug is enabled.
logr *slog.Logger
}
// getWrappedTransport gets the underlying http.RoundTripper that will be used
// to perform the request (after MTS headers are added) and before the errors
// are coupled.
//
// If not set, http.DefaultTransport is used.
func (t *mtsTransport) getWrappedTransport() http.RoundTripper {
if t.conf.Transport != nil {
return t.conf.Transport
}
return http.DefaultTransport
}
// RoundTrip implements http.Transport.
func (t *mtsTransport) RoundTrip(req *http.Request) (*http.Response, error) {
if t.conf.GetAuthToken() == "" {
return nil, ErrMissingToken
}
correlationID := uuid.New().String()
// set user agent
req.Header.Add("User-Agent", t.conf.GetUserAgent())
// accept JSON only
req.Header.Add("Accept", "application/json")
// dump request if debug is enabled
if t.conf.Debug {
t.logRequest(req, correlationID)
}
// set basic auth
req.SetBasicAuth(t.conf.GetAuthToken(), "")
// perform request
resp, err := t.getWrappedTransport().RoundTrip(req)
if err != nil {
return nil, fmt.Errorf("request failed: %w", err)
}
// dump response if debug is enabled
if t.conf.Debug {
t.logResponse(resp, correlationID)
}
if resp.StatusCode < 200 || resp.StatusCode > 299 {
// non 2XX status codes should be mapped to response errors
return nil, mapResponseToError(resp)
}
return resp, nil
}
// mapResponseToError maps a non-2XX http.Response to an *Error.
func mapResponseToError(resp *http.Response) *Error {
var errResp ErrorResponse
defer resp.Body.Close()
json.NewDecoder(resp.Body).Decode(&errResp)
err := errResp.Error
if err.ErrorCode == 0 {
err.ErrorCode = resp.StatusCode
}
if err.ErrorText == "" {
err.ErrorText = http.StatusText(err.ErrorCode)
}
return &err
}
func (t *mtsTransport) logRequest(req *http.Request, correlationID string) {
logr := t.logr.With(slog.String("correlationID", correlationID))
reqBytes, err := httputil.DumpRequestOut(req, true)
if err != nil {
// should never happen
logr.ErrorContext(req.Context(), "failed to dump request", slog.Any("error", err))
}
t.logr.DebugContext(req.Context(), "outbound request", slog.String("request", string(reqBytes)))
}
func (t *mtsTransport) logResponse(resp *http.Response, correlationID string) {
logr := t.logr.With(slog.String("correlationID", correlationID))
respBytes, err := httputil.DumpResponse(resp, true)
if err != nil {
// should never happen
logr.ErrorContext(resp.Request.Context(), "failed to dump response", slog.Any("error", err))
}
t.logr.DebugContext(resp.Request.Context(), "received response", slog.String("r", string(respBytes)))
}
// httpGet makes an HTTP GET request with the given client.
func httpGet[T any](ctx context.Context, c *client, path string) (*T, error) {
return httpDo[T](ctx, c, http.MethodGet, path, nil)
}
// httpPut makes an HTTP PUT request with the given client.
func httpPut[T any](ctx context.Context, c *client, path string, body any) (*T, error) {
return httpDo[T](ctx, c, http.MethodPut, path, body)
}
// httpPost makes an HTTP POST request with the given client.
func httpPost[T any](ctx context.Context, c *client, path string, body any) (*T, error) {
return httpDo[T](ctx, c, http.MethodPost, path, body)
}
// httpDelete makes an HTTP DELETE request with the given client.
func httpDelete[T any](ctx context.Context, c *client, path string) (*T, error) {
return httpDo[T](ctx, c, http.MethodDelete, path, nil)
}
func httpDo[T any](ctx context.Context, c *client, method, path string, body any) (*T, error) {
url := c.conf.GetBaseURL() + path
req, err := newHTTPRequest(ctx, method, url, body)
if err != nil {
return nil, err
}
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
return mapResponseBody[T](c, resp)
}
func newHTTPRequest(ctx context.Context, method, reqURL string, body any) (*http.Request, error) {
var (
bodyReader io.Reader
contentType string
)
if body != nil {
buf := new(bytes.Buffer)
if _, ok := body.(formRequest); ok {
contentType = "application/x-www-form-urlencoded"
values, err := query.Values(body)
if err != nil {
return nil, fmt.Errorf("could not marshal url-form-encoded: %w", err)
}
buf.WriteString(values.Encode())
} else {
contentType = "application/json"
if err := json.NewEncoder(buf).Encode(body); err != nil {
return nil, fmt.Errorf("could not marshal json: %w", err)
}
}
bodyReader = buf
}
req, err := http.NewRequest(method, reqURL, bodyReader)
if err != nil {
return nil, fmt.Errorf("could not build request: %w", err)
}
req.Header.Add("Content-Type", contentType)
return req.WithContext(ctx), nil
}
// mapResponseBody maps resp.Body to type *T.
func mapResponseBody[T any](c *client, resp *http.Response) (*T, error) {
var out T
dec := json.NewDecoder(resp.Body)
defer func() {
if err := resp.Body.Close(); err != nil {
c.logr.ErrorContext(resp.Request.Context(), "failed to close response body", slog.Any("error", err))
}
}()
return &out, dec.Decode(&out)
}