-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
293 lines (272 loc) · 8.69 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
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
package gocosmosdb
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
"reflect"
"time"
"github.com/davecgh/go-spew/spew"
"github.com/hashicorp/go-cleanhttp"
"github.com/hashicorp/go-retryablehttp"
"github.com/intwinelabs/logger"
"github.com/moul/http2curl"
)
// Client - struct to hold the underlying SQL REST API client
type apiClient struct {
uri string
config Config
httpClient *retryablehttp.Client
logger *logger.Logger
}
func newAPIClient(conf *Config) *apiClient {
client := &apiClient{}
httpClient := retryablehttp.NewClient()
httpClient.Logger = nil
client.httpClient = httpClient
var zeroDuration time.Duration
if conf.RetryWaitMin == zeroDuration {
client.httpClient.RetryWaitMin = 10 * time.Millisecond
} else {
client.httpClient.RetryWaitMin = conf.RetryWaitMin
}
if conf.RetryWaitMax == zeroDuration {
client.httpClient.RetryWaitMax = 50 * time.Millisecond
} else {
client.httpClient.RetryWaitMax = conf.RetryWaitMax
}
client.httpClient.RetryMax = conf.RetryMax
if conf.Pooled {
client.httpClient.HTTPClient.Transport = cleanhttp.DefaultPooledTransport()
}
return client
}
// apply - iterates over all opts and runs the functions to apply additional request headers
func (c *apiClient) apply(r *Request, opts []CallOption) (err error) {
if err = r.DefaultHeaders(c.config.MasterKey); err != nil {
return err
}
for i := 0; i < len(opts); i++ {
// check to make sure someone did not pass nil ass a call option
if opts[i] != nil {
if err = opts[i](r); err != nil {
return err
}
}
}
return nil
}
// GetURI - returns a clients URI
func (c *apiClient) getURI() string {
return c.uri
}
// GetConfig - return a clients URI
func (c *apiClient) getConfig() Config {
return c.config
}
// EnableDebug - enables the CosmosDB debug mode
func (c *apiClient) enableDebug() {
c.config.Debug = true
}
// DisableDebug - disables the CosmosDB debug mode
func (c *apiClient) disableDebug() {
c.config.Debug = false
}
// Read - reads a resource by self link
func (c *apiClient) read(link string, ret interface{}, opts ...CallOption) (*Response, error) {
return c.method("GET", link, http.StatusOK, ret, &bytes.Buffer{}, opts...)
}
// Delete - deletes a resource by self link
func (c *apiClient) delete(link string, opts ...CallOption) (*Response, error) {
return c.method("DELETE", link, http.StatusNoContent, nil, &bytes.Buffer{}, opts...)
}
// Query - queries a resource
func (c *apiClient) query(link, query string, ret interface{}, opts ...CallOption) (*Response, error) {
query = escapeSQL(query)
buf := bytes.NewBufferString(querify(query))
req, err := http.NewRequest("POST", path(c.uri, link), buf)
if err != nil {
return nil, err
}
r := ResourceRequest(link, req)
if c.config.PartitionKeyStructField != "" {
opts = append(opts, CrossPartition())
}
if err = c.apply(r, opts); err != nil {
return nil, err
}
r.QueryHeaders(buf.Len())
// revert version if collection is not partitioned
if c.config.PartitionKeyStructField == "" {
r.Header.Set(HeaderVersion, SupportedAPIVersionNoPartition)
}
// try the request and return if successful
return c.do(r, http.StatusOK, ret)
}
// QueryWithParameters - queries a resource
func (c *apiClient) queryWithParameters(link string, query *QueryWithParameters, ret interface{}, opts ...CallOption) (*Response, error) {
query.Query = escapeSQL(query.Query)
q, err := stringify(query)
if err != nil {
return nil, err
}
buf := bytes.NewBuffer(q)
req, err := http.NewRequest("POST", path(c.uri, link), buf)
if err != nil {
return nil, err
}
r := ResourceRequest(link, req)
if c.config.PartitionKeyStructField != "" {
opts = append(opts, CrossPartition())
}
if err = c.apply(r, opts); err != nil {
return nil, err
}
r.QueryHeaders(buf.Len())
// revert version if collection is not partitioned
if c.config.PartitionKeyStructField == "" {
r.Header.Set(HeaderVersion, SupportedAPIVersionNoPartition)
}
return c.do(r, http.StatusOK, ret)
}
// Create - creates a resource
func (c *apiClient) create(link string, body, ret interface{}, opts ...CallOption) (*Response, error) {
data, err := stringify(body)
if err != nil {
return nil, err
}
buf := bytes.NewBuffer(data)
return c.method("POST", link, http.StatusCreated, ret, buf, opts...)
}
// Replace - replaces a resource
func (c *apiClient) replace(link string, body, ret interface{}, opts ...CallOption) (*Response, error) {
data, err := stringify(body)
if err != nil {
return nil, err
}
buf := bytes.NewBuffer(data)
if c.config.PartitionKeyStructField != "" {
partKey := reflect.ValueOf(body).Elem().FieldByName(c.config.PartitionKeyStructField)
partKeyI := partKey.Interface()
opts = append(opts, PartitionKey(partKeyI))
}
return c.method("PUT", link, http.StatusOK, ret, buf, opts...)
}
// Upsert - upserts a resource
func (c *apiClient) upsert(link string, body, ret interface{}, opts ...CallOption) (*Response, error) {
opts = append(opts, Upsert())
data, err := stringify(body)
if err != nil {
return nil, err
}
buf := bytes.NewBuffer(data)
if c.config.PartitionKeyStructField != "" {
partKey := reflect.ValueOf(body).Elem().FieldByName(c.config.PartitionKeyStructField)
partKeyI := partKey.Interface()
opts = append(opts, PartitionKey(partKeyI))
}
return c.method(http.MethodPost, link, http.StatusOK, ret, buf, opts...)
}
// ReplaceAsync - replaces a resource
func (c *apiClient) replaceAsync(link string, body, ret interface{}, opts ...CallOption) (*Response, error) {
data, err := stringify(body)
if err != nil {
return nil, err
}
buf := bytes.NewBuffer(data)
var Etag string
var resource map[string]interface{}
err = json.Unmarshal(data, &resource)
if err != nil {
return nil, err
}
if valInterface, ok := resource["_etag"]; ok {
if val, ok := valInterface.(string); ok {
Etag = val
}
} else {
return nil, errors.New("_etag does not exist for async replace")
}
if c.config.PartitionKeyStructField != "" {
partKey := reflect.ValueOf(body).Elem().FieldByName(c.config.PartitionKeyStructField)
partKeyI := partKey.Interface()
opts = append(opts, PartitionKey(partKeyI))
}
opts = append(opts, IfMatch(Etag))
return c.method("PUT", link, http.StatusOK, ret, buf, opts...)
}
// Execute - executes a resource
func (c *apiClient) execute(link string, body, ret interface{}, opts ...CallOption) (*Response, error) {
data, err := stringify(body)
if err != nil {
return nil, err
}
buf := bytes.NewBuffer(data)
return c.method("POST", link, http.StatusOK, ret, buf, opts...)
}
// method - generic method for a resource
func (c *apiClient) method(method, link string, status int, ret interface{}, body *bytes.Buffer, opts ...CallOption) (*Response, error) {
req, err := http.NewRequest(method, path(c.uri, link), body)
if err != nil {
return nil, err
}
r := ResourceRequest(link, req)
if err = c.apply(r, opts); err != nil {
return nil, err
}
// revert version if collection is not partitioned
if c.config.PartitionKeyStructField == "" {
r.Header.Set(HeaderVersion, SupportedAPIVersionNoPartition)
}
return c.do(r, status, ret)
}
// do - private do function
func (c *apiClient) do(r *Request, status int, data interface{}) (*Response, error) {
if c.config.Debug && c.logger != nil {
r.QueryMetricsHeaders()
c.logger.Infof("CosmosDB Request: ID: %+v, Type: %+v, HTTP Request: %+v", r.rId, r.rType, r.Request)
curl, _ := http2curl.GetCurlCommand(r.Request)
c.logger.Infof("CURL: %s", curl)
}
var rr *retryablehttp.Request
var err error
if r.rContext != nil {
req := r.WithContext(r.rContext)
rr, err = retryablehttp.FromRequest(req)
} else {
rr, err = retryablehttp.FromRequest(r.Request)
}
if err != nil {
return nil, fmt.Errorf("error creating retryable request: %s", err)
}
resp, err := c.httpClient.Do(rr)
if err != nil {
return nil, err
}
if c.config.Debug && c.config.Verbose && c.logger != nil {
c.logger.Infof("CosmosDB Request: %s", spew.Sdump(resp.Request))
c.logger.Infof("CosmosDB Response Headers: %s", spew.Sdump(resp.Header))
c.logger.Infof("CosmosDB Response Content-Length: %s", spew.Sdump(resp.ContentLength))
}
defer resp.Body.Close()
if resp.StatusCode != status {
err := &RequestError{}
readJson(resp.Body, &err)
err.StatusCode = resp.StatusCode
err.RId = r.rId
err.RType = r.rType
err.Request = r.Request
return nil, err
}
if data == nil {
return nil, nil
}
if c.config.Debug && c.config.Verbose && c.logger != nil {
c.logger.Infof("CosmosDB Request: %s", spew.Sdump(resp.Request))
c.logger.Infof("CosmosDB Response Headers: %s", spew.Sdump(resp.Header))
c.logger.Infof("CosmosDB Response Content-Length: %s", spew.Sdump(resp.ContentLength))
c.logger.Infof("CosmosDB Response Content: %s", spew.Sdump(data))
}
return &Response{resp.Header}, readJson(resp.Body, data)
}