-
Notifications
You must be signed in to change notification settings - Fork 0
/
stein.go
281 lines (221 loc) · 6.63 KB
/
stein.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
package gostein
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
)
type errorResponse struct {
Error string `json:"error"`
}
// GetParams is the parameters for the read operations: Get or search
type GetParams struct {
Offset int64
Limit int64
Condition map[string]string
}
// builds the query string from the given params with query string escaping
func (gp GetParams) queryString() string {
queryString := ""
if gp.Offset > 0 {
queryString = queryString + fmt.Sprintf("offset=%d&", gp.Offset)
}
if gp.Limit > 0 {
queryString = queryString + fmt.Sprintf("limit=%d&", gp.Limit)
}
if len(gp.Condition) > 0 {
jsonSearch, err := json.Marshal(gp.Condition)
if err != nil {
return ""
}
querySearchJSON := url.QueryEscape(string(jsonSearch))
queryString = queryString + fmt.Sprintf("search=%s", querySearchJSON)
}
return removeSuffix(queryString, "&")
}
// UpdateParams is the parameters for the update operation
type UpdateParams struct {
Condition map[string]string `json:"condition"`
Set map[string]string `json:"set"`
Limit int64 `json:"limit,omitempty"`
}
// DeleteParams is the parameters for the delete operation
type DeleteParams struct {
Condition map[string]string `json:"condition"`
Limit int64 `json:"limit,omitempty"`
}
type BasicAuth struct {
Username string
Password string
}
// Interface is the interface for the stein client
type Interface interface {
Get(sheet string, params GetParams) ([]map[string]interface{}, error)
Add(sheet string, rows ...map[string]interface{}) (usedSheetRange string, err error)
Update(sheet string, params UpdateParams) (countUpdatedRows int64, err error)
Delete(sheet string, params DeleteParams) (countDeletedRows int64, err error)
}
type stein struct {
url string
httpClient *http.Client
basicAuth *BasicAuth
}
// New creates a new stein client
// url is the base url of the stein api. i.e: https://api.steinhq.com/v1/storages/[your-api-id]
// httpClient is the http client to use. If nil, http.DefaultClient will be used
func New(url string, httpClient *http.Client, basicAuth *BasicAuth) Interface {
if httpClient == nil {
httpClient = http.DefaultClient
}
url = removeSuffix(url, "/")
return &stein{
url: url,
httpClient: httpClient,
basicAuth: basicAuth,
}
}
func (s *stein) decodeJSON(r io.Reader, v interface{}) error {
decoder := json.NewDecoder(r)
return decoder.Decode(v)
}
func (s *stein) doRequest(method string, resourceURL string, body io.Reader) (*http.Response, error) {
req, err := http.NewRequest(method, resourceURL, body)
if err != nil {
return nil, err
}
if s.basicAuth != nil {
req.SetBasicAuth(s.basicAuth.Username, s.basicAuth.Password)
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")
return s.httpClient.Do(req)
}
func (s *stein) validateResponse(resp *http.Response) error {
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
return nil
}
if resp.StatusCode == http.StatusUnauthorized {
return ErrNot2XX{StatusCode: resp.StatusCode, Message: "Unauthorized"}
}
var errorResponse errorResponse
err := s.decodeJSON(resp.Body, &errorResponse)
if err != nil {
return err
}
return ErrNot2XX{StatusCode: resp.StatusCode, Message: errorResponse.Error}
}
// Get returns the rows in the given sheet
func (s *stein) Get(sheet string, params GetParams) ([]map[string]interface{}, error) {
resource := fmt.Sprintf("%s/%s", s.url, removePrefix(sheet, "/"))
queryParams := params.queryString()
if queryParams != "" {
resource = resource + "?" + queryParams
}
resp, err := s.doRequest(http.MethodGet, resource, nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
err = s.validateResponse(resp)
if err != nil {
return nil, err
}
data := make([]map[string]interface{}, 0)
err = s.decodeJSON(resp.Body, &data)
if err != nil {
return nil, ErrDecodeJSON{Err: err}
}
return data, nil
}
func (s *stein) Add(sheet string, rows ...map[string]interface{}) (usedSheetRange string, err error) {
var (
result = struct {
UsedSheetRange string `json:"updatedRange"`
}{}
resource = fmt.Sprintf("%s/%s", s.url, removePrefix(sheet, "/"))
)
jsonRow, err := json.Marshal(rows)
if err != nil {
return result.UsedSheetRange, err
}
resp, err := s.doRequest(http.MethodPost, resource, bytes.NewReader(jsonRow))
if err != nil {
return result.UsedSheetRange, err
}
defer resp.Body.Close()
err = s.validateResponse(resp)
if err != nil {
return result.UsedSheetRange, err
}
err = s.decodeJSON(resp.Body, &result)
if err != nil {
return result.UsedSheetRange, ErrDecodeJSON{Err: err}
}
return result.UsedSheetRange, nil
}
// Update updates the rows in the given sheet
func (s *stein) Update(sheet string, params UpdateParams) (countUpdatedRows int64, err error) {
var (
result = struct {
TotalUpdatedRows int64 `json:"totalUpdatedRows"`
}{}
resource = fmt.Sprintf("%s/%s", s.url, removePrefix(sheet, "/"))
)
// prevent error "Expected property `condition` to be of type `object` but received type `null` in object"
if params.Condition == nil {
params.Condition = make(map[string]string)
}
if params.Set == nil {
return result.TotalUpdatedRows, ErrParamCantBeNil{ParamName: "set"}
}
payload, err := json.Marshal(params)
if err != nil {
return result.TotalUpdatedRows, err
}
resp, err := s.doRequest(http.MethodPut, resource, bytes.NewReader(payload))
if err != nil {
return result.TotalUpdatedRows, err
}
defer resp.Body.Close()
err = s.validateResponse(resp)
if err != nil {
return result.TotalUpdatedRows, err
}
err = s.decodeJSON(resp.Body, &result)
if err != nil {
return result.TotalUpdatedRows, ErrDecodeJSON{Err: err}
}
return result.TotalUpdatedRows, nil
}
func (s *stein) Delete(sheet string, params DeleteParams) (countDeletedRows int64, err error) {
var (
result = struct {
CountDeletedRows int64 `json:"clearedRowsCount"`
}{}
resource = fmt.Sprintf("%s/%s", s.url, removePrefix(sheet, "/"))
)
// prevent error "Expected property `condition` to be of type `object` but received type `null` in object"
if params.Condition == nil {
params.Condition = make(map[string]string)
}
payload, err := json.Marshal(params)
if err != nil {
return countDeletedRows, err
}
resp, err := s.doRequest(http.MethodDelete, resource, bytes.NewReader(payload))
if err != nil {
return countDeletedRows, err
}
defer resp.Body.Close()
err = s.validateResponse(resp)
if err != nil {
return countDeletedRows, err
}
err = s.decodeJSON(resp.Body, &result)
if err != nil {
return countDeletedRows, ErrDecodeJSON{Err: err}
}
return result.CountDeletedRows, nil
}