-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient.go
More file actions
189 lines (158 loc) · 4.25 KB
/
client.go
File metadata and controls
189 lines (158 loc) · 4.25 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
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
package patch
import (
"context"
"net/http"
"net/url"
)
// Client is an HTTP client that uses the BaseClient to send requests
type Client struct {
BaseURL string
DefaultEncoder Encoder
StatusValidator func(int) bool
BaseClient Doer
}
// Doer executes HTTP requests. It is implemented by http.Client{}.
// You can wrap an http.Client{} in a custom Doer implementation
// to create middleware.
type Doer interface {
Do(*http.Request) (*http.Response, error)
}
// New returns a new Client with sensible defaults.
// The defaults can be overridden by supplying Options.
func New(opts ...Option) *Client {
return NewFromBaseClient(&http.Client{
Timeout: DefaultTimeout,
}, opts...)
}
// NewFromBaseClient returns a new Client that wraps BaseClient
func NewFromBaseClient(baseClient Doer, opts ...Option) *Client {
c := &Client{
DefaultEncoder: &EncoderJSON{},
StatusValidator: DefaultStatusValidator,
BaseClient: baseClient,
}
for _, opt := range opts {
opt(c)
}
return c
}
// Get performs a GET request
func (c *Client) Get(ctx context.Context, url string, v interface{}) (*Response, error) {
r := &Request{Ctx: ctx, Method: http.MethodGet, URL: url}
rsp, err := c.Send(r).Response()
if err != nil {
return rsp, err
}
if v != nil {
return rsp, rsp.Decode(v)
}
return rsp, nil
}
// Post performs a POST request
func (c *Client) Post(ctx context.Context, url string, body interface{}, v interface{}) (*Response, error) {
r := &Request{Ctx: ctx, Method: http.MethodPost, URL: url, Body: body}
rsp, err := c.Send(r).Response()
if err != nil {
return rsp, err
}
if v != nil {
return rsp, rsp.Decode(v)
}
return rsp, nil
}
// Put performs a PUT request
func (c *Client) Put(ctx context.Context, url string, body interface{}, v interface{}) (*Response, error) {
r := &Request{Ctx: ctx, Method: http.MethodPut, URL: url, Body: body}
rsp, err := c.Send(r).Response()
if err != nil {
return rsp, err
}
if v != nil {
return rsp, rsp.Decode(v)
}
return rsp, nil
}
// Patch performs a PATCH request
func (c *Client) Patch(ctx context.Context, url string, body interface{}, v interface{}) (*Response, error) {
r := &Request{Ctx: ctx, Method: http.MethodPatch, URL: url, Body: body}
rsp, err := c.Send(r).Response()
if err != nil {
return rsp, err
}
if v != nil {
return rsp, rsp.Decode(v)
}
return rsp, nil
}
// Delete performs a DELETE request
func (c *Client) Delete(ctx context.Context, url string, body interface{}, v interface{}) (*Response, error) {
r := &Request{Ctx: ctx, Method: http.MethodDelete, URL: url, Body: body}
rsp, err := c.Send(r).Response()
if err != nil {
return rsp, err
}
if v != nil {
return rsp, rsp.Decode(v)
}
return rsp, nil
}
// Send performs the HTTP request and returns a Future
func (c *Client) Send(request *Request) *Future {
done := make(chan struct{})
ftr := &Future{done: done}
go func() {
defer close(done)
ftr.response, ftr.err = c.send(request)
}()
return ftr
}
func (c *Client) Do(req *http.Request) (*http.Response, error) {
rsp, err := c.BaseClient.Do(req)
if err != nil {
return nil, err
}
// Execute the status validator if set
if c.StatusValidator != nil && !c.StatusValidator(rsp.StatusCode) {
return rsp, BadStatusError(rsp.StatusCode)
}
return rsp, nil
}
func (c *Client) send(request *Request) (*Response, error) {
if err := request.validate(); err != nil {
return nil, err
}
/* Build the HTTP request */
path := request.URL
if c.BaseURL != "" {
base, err := url.Parse(c.BaseURL)
if err != nil {
return nil, err
}
ref, err := url.Parse(request.URL)
if err != nil {
return nil, err
}
path = base.ResolveReference(ref).String()
}
body, contentType, err := request.prepareBody(c.DefaultEncoder)
if err != nil {
return nil, err
}
req, err := http.NewRequest(request.Method, path, body)
if err != nil {
return nil, err
}
if request.Ctx != nil {
req = req.WithContext(request.Ctx)
}
if request.Headers != nil {
req.Header = request.Headers
}
// Set the Content-Type header (unless an override was provided in request)
if contentType != "" && req.Header.Get("Content-Type") == "" {
req.Header.Set("Content-Type", contentType)
}
/* Make the HTTP request */
rsp, err := c.Do(req)
return &Response{Response: rsp}, err
}