-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhttp.go
332 lines (279 loc) · 7.5 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
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
package mixpanel
import (
"bytes"
"compress/gzip"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/http/httputil"
"net/url"
"strconv"
)
type MpCompression int
var (
None MpCompression = 0
Gzip MpCompression = 1
)
var (
ErrUnexpectedStatus = errors.New("unexpected status code")
apiErrorStatus = 0
)
type HttpError struct {
Status int
Body string
}
func newHttpError(statusCode int, data io.Reader) error {
body, err := io.ReadAll(data)
if err != nil {
return err
}
return HttpError{
Status: statusCode,
Body: string(body),
}
}
func (h HttpError) Error() string {
return fmt.Sprintf("unexpected status code: %d, body: %s", h.Status, h.Body)
}
func (h HttpError) Unwrap() error {
return ErrUnexpectedStatus
}
type httpOptions func(req *http.Request)
func gzipHeader() httpOptions {
return func(req *http.Request) {
req.Header.Set(contentEncodingHeader, "gzip")
}
}
func applicationJsonHeader() httpOptions {
return func(req *http.Request) {
req.Header.Set(contentTypeHeader, contentTypeApplicationJson)
}
}
func applicationFormData() httpOptions {
return func(req *http.Request) {
req.Header.Set(contentTypeHeader, contentTypeApplicationForm)
}
}
func (m *ApiClient) importAuthOptions() httpOptions {
return func(req *http.Request) {
if m.serviceAccount != nil {
req.SetBasicAuth(m.serviceAccount.Username, m.serviceAccount.Secret)
} else if m.apiSecret != "" {
req.SetBasicAuth(m.apiSecret, "")
} else {
req.SetBasicAuth(m.token, "")
}
}
}
func (m *ApiClient) useApiSecret() httpOptions {
return func(req *http.Request) {
req.SetBasicAuth(m.apiSecret, "")
}
}
// exportServiceAccount uses the service account if available and adds the query params
// or falls back to apiSecret
func (m *ApiClient) exportServiceAccount() httpOptions {
return func(req *http.Request) {
if m.serviceAccount != nil {
req.SetBasicAuth(m.serviceAccount.Username, m.serviceAccount.Secret)
values := url.Values{}
values.Add("project_id", strconv.Itoa(m.projectID))
addQueryParams(values)(req)
} else {
req.SetBasicAuth(m.apiSecret, "")
}
}
}
func acceptJson() httpOptions {
return func(req *http.Request) {
req.Header.Set(acceptHeader, acceptJsonHeader)
}
}
func addQueryParams(query url.Values) httpOptions {
return func(req *http.Request) {
rQuery := req.URL.Query()
for key, values := range query {
rQuery[key] = values
}
req.URL.RawQuery = rQuery.Encode()
}
}
func acceptPlainText() httpOptions {
return func(req *http.Request) {
req.Header.Set(acceptHeader, acceptPlainTextHeader)
}
}
type debugHttpCalls struct {
writer io.Writer
}
func (d *debugHttpCalls) writeDebug(r *http.Request) error {
if d.writer == nil {
return nil
}
requestDump, err := httputil.DumpRequest(r, true)
if err != nil {
return fmt.Errorf("failed to dump request %w", err)
}
_, err = d.writer.Write([]byte("-----Start Request-----\n"))
if err != nil {
return fmt.Errorf("failed to write start header %w", err)
}
_, err = d.writer.Write(requestDump)
if err != nil {
return fmt.Errorf("failed to write debug_http payload %w", err)
}
_, err = d.writer.Write([]byte("\n-----End Request-----\n\n"))
if err != nil {
return fmt.Errorf("failed to write end header %w", err)
}
return nil
}
func gzipBody(data []byte) ([]byte, error) {
var buf bytes.Buffer
gzip := gzip.NewWriter(&buf)
if _, err := gzip.Write(data); err != nil {
return nil, fmt.Errorf("failed to compress body using gzip: %w", err)
}
if err := gzip.Close(); err != nil {
return nil, fmt.Errorf("failed to close gzip writer: %w", err)
}
return buf.Bytes(), nil
}
type requestPostPayloadType int
const (
jsonPayload requestPostPayloadType = iota
formPayload
)
func makeRequestBody(body any, bodyType requestPostPayloadType, compress MpCompression) (*bytes.Reader, error) {
if body == nil {
return nil, fmt.Errorf("body is nil")
}
var err error
jsonData, err := json.Marshal(body)
if err != nil {
return nil, fmt.Errorf("failed to create http body: %w", err)
}
switch bodyType {
case jsonPayload:
return requestBodyJsonCompress(jsonData, compress)
case formPayload:
return requestForm(jsonData)
default:
return nil, fmt.Errorf("unknown body type: %d", bodyType)
}
}
func requestBodyJsonCompress(jsonPayload []byte, compress MpCompression) (*bytes.Reader, error) {
switch compress {
case None:
return bytes.NewReader(jsonPayload), nil
case Gzip:
jsonData, err := gzipBody(jsonPayload)
if err != nil {
return nil, fmt.Errorf("failed to gzip body: %w", err)
}
return bytes.NewReader(jsonData), nil
default:
return nil, fmt.Errorf("unknown compression type: %d", compress)
}
}
func requestForm(jsonPayload []byte) (*bytes.Reader, error) {
form := url.Values{}
form.Add("data", string(jsonPayload))
return bytes.NewReader([]byte(form.Encode())), nil
}
func (m *ApiClient) doRequestBody(
ctx context.Context,
method string,
requestUrl string,
body io.Reader,
options ...httpOptions,
) (*http.Response, error) {
request, err := http.NewRequestWithContext(ctx, method, requestUrl, body)
if err != nil {
return nil, fmt.Errorf("failed to make request: %w", err)
}
for _, o := range options {
o(request)
}
if err := m.debugHttpCall.writeDebug(request); err != nil {
return nil, fmt.Errorf("failed to write debug_http call: %w", err)
}
return m.client.Do(request)
}
func (m *ApiClient) doPeopleRequest(ctx context.Context, body any, u string) error {
requestBody, err := makeRequestBody(body, jsonPayload, None)
if err != nil {
return fmt.Errorf("failed to create request body: %w", err)
}
response, err := m.doRequestBody(
ctx,
http.MethodPost,
m.apiEndpoint+u,
requestBody,
acceptPlainText(),
applicationJsonHeader(),
)
if err != nil {
return fmt.Errorf("failed to post request: %w", err)
}
defer response.Body.Close()
return processPeopleRequestResponse(response)
}
func (m *ApiClient) doIdentifyRequest(ctx context.Context, body any, u string, option ...httpOptions) error {
requestBody, err := makeRequestBody(body, formPayload, None)
if err != nil {
return fmt.Errorf("failed to create request body: %w", err)
}
requestOptions := append([]httpOptions{acceptPlainText(), applicationFormData()}, option...)
response, err := m.doRequestBody(
ctx,
http.MethodPost,
m.apiEndpoint+u,
requestBody,
requestOptions...,
)
if err != nil {
return fmt.Errorf("failed to post request: %w", err)
}
defer response.Body.Close()
return processPeopleRequestResponse(response)
}
func processPeopleRequestResponse(response *http.Response) error {
switch response.StatusCode {
case http.StatusOK:
var code int
if err := json.NewDecoder(response.Body).Decode(&code); err != nil {
return fmt.Errorf("failed to decode response: %w", err)
}
if code == apiErrorStatus {
return errors.New("api return code 0")
}
return nil
case http.StatusUnauthorized:
return fmt.Errorf("unauthorized: %w", newHttpError(response.StatusCode, response.Body))
case http.StatusForbidden:
return fmt.Errorf("forbidden: %w", newHttpError(response.StatusCode, response.Body))
default:
return newHttpError(response.StatusCode, response.Body)
}
}
type VerboseError struct {
ApiError string `json:"error"`
Status int `json:"status"`
}
func (a VerboseError) Error() string {
return a.ApiError
}
func parseVerboseApiError(jsonReader io.Reader) error {
var r VerboseError
if err := json.NewDecoder(jsonReader).Decode(&r); err != nil {
return fmt.Errorf("failed to json decode response body: %w", err)
}
if r.Status == apiErrorStatus {
return r
}
return nil
}