This repository was archived by the owner on Aug 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathrequest.go
More file actions
127 lines (107 loc) · 3.11 KB
/
request.go
File metadata and controls
127 lines (107 loc) · 3.11 KB
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
package coder
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"os"
"strings"
"golang.org/x/xerrors"
)
type requestOptions struct {
BaseURLOverride *url.URL
Query url.Values
Headers http.Header
Reader io.Reader
}
type requestOption func(*requestOptions)
// withQueryParams sets the provided query parameters on the request.
func withQueryParams(q url.Values) func(o *requestOptions) {
return func(o *requestOptions) {
o.Query = q
}
}
func withHeaders(h http.Header) func(o *requestOptions) {
return func(o *requestOptions) {
o.Headers = h
}
}
func withBaseURL(base *url.URL) func(o *requestOptions) {
return func(o *requestOptions) {
o.BaseURLOverride = base
}
}
func withBody(w io.Reader) func(o *requestOptions) {
return func(o *requestOptions) {
o.Reader = w
}
}
// request is a helper to set the cookie, marshal the payload and execute the request.
func (c *DefaultClient) request(ctx context.Context, method, path string, in interface{}, options ...requestOption) (*http.Response, error) {
url := *c.baseURL
var config requestOptions
for _, o := range options {
o(&config)
}
if config.BaseURLOverride != nil {
url = *config.BaseURLOverride
}
if config.Query != nil {
url.RawQuery = config.Query.Encode()
}
url.Path = fmt.Sprint(strings.TrimSuffix(url.Path, "/"), path)
// If we have incoming data, encode it as json.
var payload io.Reader
if in != nil {
body, err := json.Marshal(in)
if err != nil {
return nil, xerrors.Errorf("marshal request: %w", err)
}
payload = bytes.NewReader(body)
}
if config.Reader != nil {
payload = config.Reader
}
// Create the http request.
req, err := http.NewRequestWithContext(ctx, method, url.String(), payload)
if err != nil {
return nil, xerrors.Errorf("create request: %w", err)
}
if config.Headers == nil {
req.Header = http.Header{}
} else {
req.Header = config.Headers
}
// Provide the session token in a header
req.Header.Set("Session-Token", c.token)
customAuthHeader, ok := os.LookupEnv("ENDPOINT_AUTH_HEADER")
if ok {
req.Header.Set("Authorization", customAuthHeader)
}
// Execute the request.
return c.httpClient.Do(req)
}
// requestBody is a helper extending the Client.request helper, checking the response code
// and decoding the response payload.
func (c *DefaultClient) requestBody(ctx context.Context, method, path string, in, out interface{}, opts ...requestOption) error {
resp, err := c.request(ctx, method, path, in, opts...)
if err != nil {
return xerrors.Errorf("Execute request: %q", err)
}
defer func() { _ = resp.Body.Close() }() // Best effort, likely connection dropped.
// Responses in the 100 are handled by the http lib, in the 200 range, we have a success.
// Consider anything at or above 300 to be an error.
if resp.StatusCode > 299 {
return fmt.Errorf("unexpected status code %d: %w", resp.StatusCode, NewHTTPError(resp))
}
// If we expect a payload, process it as json.
if out != nil {
if err := json.NewDecoder(resp.Body).Decode(&out); err != nil {
return xerrors.Errorf("decode response body: %w", err)
}
}
return nil
}