-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
183 lines (165 loc) · 4.7 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
package client
import (
"bytes"
"context"
"encoding/json"
"io"
"io/ioutil"
"net/http"
"net/url"
"strings"
)
// Client is used to send requests to the API
type Client struct {
auth Auth
url string
ctx context.Context
}
type requestOptions struct {
body io.Reader
query url.Values
headers http.Header
ctx context.Context
}
// RequestOption is a function used to set options of a request to the API
type RequestOption func(*requestOptions) error
// NewClient returns a new API Client
func NewClient(ctx context.Context, url string, auth Auth) *Client {
if ctx == nil {
ctx = context.Background()
}
return &Client{
url: strings.TrimRight(url, "/"),
auth: auth,
ctx: ctx,
}
}
// WithHeader is a RequestOption that sets a header of the HTTP request
func WithHeader(name, value string) RequestOption {
return func(opts *requestOptions) error {
opts.headers.Set(name, value)
return nil
}
}
// WithContentType is a RequestOption that sets the Content-Type header of the HTTP request
func WithContentType(contentType string) RequestOption {
return WithHeader("Content-Type", contentType)
}
// WithBody is a RequestOption that sets the body of the HTTP request
func WithBody(body io.Reader) RequestOption {
return func(opts *requestOptions) error {
if prev := opts.body; prev != nil && prev.(io.Closer) != nil && prev != body {
prev.(io.Closer).Close()
}
opts.body = body
return nil
}
}
// WithQuery is a RequestOption that sets the query part of the HTTP request URL
func WithQuery(query url.Values) RequestOption {
return func(opts *requestOptions) error {
opts.query = query
return nil
}
}
// WithQueryValue is a RequestOption that sets one parameter of the the query
// part of the HTTP request URL
func WithQueryValue(key, value string, append bool) RequestOption {
return func(opts *requestOptions) error {
if opts.query == nil {
opts.query = make(url.Values)
}
if append {
opts.query.Add(key, value)
} else {
opts.query.Set(key, value)
}
return nil
}
}
// WithJSONBody is a RequestOption that sets the request's body to a JSON string
func WithJSONBody(content interface{}) RequestOption {
return func(opts *requestOptions) error {
b, err := json.Marshal(content)
if err != nil {
return err
}
if prev := opts.body; prev != nil && prev.(io.Closer) != nil {
prev.(io.Closer).Close()
}
opts.body = bytes.NewBuffer(b)
return nil
}
}
// WitchContext is a RequestOption that sets the request's context
func WitchContext(ctx context.Context) RequestOption {
return func(opts *requestOptions) error {
opts.ctx = ctx
return nil
}
}
// Request sends an HTTP request with arbitrary HTTP method
func (c *Client) Request(method string, endpoint string, options ...RequestOption) (*Response, error) {
headers := make(http.Header)
headers.Set("Content-Type", "application/json")
opts := requestOptions{
headers: headers,
ctx: c.ctx,
}
for _, opt := range options {
if err := opt(&opts); err != nil {
return nil, err
}
}
u, err := url.Parse(c.url + "/" + endpoint)
if err != nil {
return nil, err
}
if opts.query != nil {
u.RawQuery = opts.query.Encode()
}
req, err := http.NewRequest(method, u.String(), opts.body)
if err != nil {
return nil, err
}
req.Header = opts.headers
if c.auth != nil {
err = c.auth.ApplyRequestOptions(req)
if err != nil {
return nil, err
}
}
res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
if res.StatusCode < 200 || res.StatusCode >= 300 {
defer res.Body.Close()
b, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
return nil, &resError{code: res.StatusCode, err: string(b)}
}
return &Response{HTTPResponse: res}, nil
}
// Get is convenience wrapper around Request() to send a GET request
func (c *Client) Get(endpoint string, options ...RequestOption) (*Response, error) {
return c.Request("GET", endpoint, options...)
}
// Post is convenience wrapper around Request() to send a POST request
func (c *Client) Post(endpoint string, options ...RequestOption) (*Response, error) {
return c.Request("POST", endpoint, options...)
}
// Patch is convenience wrapper around Request() to send a Patch request
func (c *Client) Patch(endpoint string, options ...RequestOption) (*Response, error) {
return c.Request("PATCH", endpoint, options...)
}
// Put is convenience wrapper around Request() to send a PUT request
func (c *Client) Put(endpoint string, options ...RequestOption) (*Response, error) {
return c.Request("PUT", endpoint, options...)
}
// Delete is convenience wrapper around Request() to send a DELETE request
func (c *Client) Delete(endpoint string, options ...RequestOption) (*Response, error) {
return c.Request("DELETE", endpoint, options...)
}