-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
262 lines (218 loc) · 7.12 KB
/
api.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
package airtable
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"github.com/alexhokl/helper/httphelper"
"github.com/alexhokl/helper/jsonhelper"
)
const defaultMaxRecords = 100
const contentTypeJSON = "application/json"
const apiBasePath = "https://api.airtable.com/v0"
const defaultOffset = "first_call"
// UpdateRecordsRequest returns a request to update records of Airtable
func UpdateRecordsRequest(baseID string, tableName string, patchBody *bytes.Buffer, ctx context.Context) (*http.Request, error) {
path := fmt.Sprintf("%s/%s/%s", apiBasePath, baseID, tableName)
headers := map[string]string{
"Content-Type": contentTypeJSON,
}
request, err := httphelper.NewRequest(http.MethodPatch, path, nil, headers, patchBody)
if err != nil {
return nil, err
}
if ctx != nil {
request = request.WithContext(ctx)
}
return request, nil
}
// ListRecords returns a list of records from Airtable
func ListRecords[T AirtableFields](httpClient *http.Client, baseID string, tableName string, viewName string, ctx context.Context, maxRecords int) ([]*AirtableRecord[T], error) {
var items []*AirtableRecord[T]
offset := defaultOffset
for offset != "" {
request, err := listRecordsRequest(baseID, tableName, viewName, ctx, maxRecords, offset)
if err != nil {
return nil, fmt.Errorf("unable to create request: %v", err)
}
response, err := httpClient.Do(request)
if err != nil {
return nil, err
}
body, err := ioutil.ReadAll(response.Body)
response.Body.Close()
if err != nil {
return nil, err
}
if !httphelper.IsSuccessResponse(response) {
return nil, handleErrorResponse(response.StatusCode, body)
}
if !httphelper.HasContentType(response, contentTypeJSON) {
return nil, fmt.Errorf("Content-Type is not %s", contentTypeJSON)
}
var list AirtableRecords[T]
if err := jsonhelper.ParseJSONFromBytes(&list, body); err != nil {
return nil, err
}
for i := range list.Records {
items = append(items, &list.Records[i])
}
offset = list.Offset
}
return items, nil
}
// UpdateRecords updates records of Airtable and returns the records updated
func UpdateRecords[T AirtableFields](httpClient *http.Client, request *http.Request) ([]*AirtableRecord[T], error) {
response, err := httpClient.Do(request)
if err != nil {
return nil, err
}
body, err := ioutil.ReadAll(response.Body)
response.Body.Close()
if err != nil {
return nil, err
}
if !httphelper.IsSuccessResponse(response) {
return nil, handleErrorResponse(response.StatusCode, body)
}
if !httphelper.HasContentType(response, contentTypeJSON) {
return nil, fmt.Errorf("Content-Type is not %s", contentTypeJSON)
}
var list AirtableRecords[T]
if err := jsonhelper.ParseJSONFromBytes(&list, body); err != nil {
return nil, err
}
var items []*AirtableRecord[T]
for i := range list.Records {
items = append(items, &list.Records[i])
}
return items, nil
}
func CreateRecord[Tin AirtableFields, T AirtableFields](httpClient *http.Client, record *Tin, baseID string, tableName string, ctx context.Context) ([]*AirtableRecord[T], error) {
path := fmt.Sprintf("%s/%s/%s", apiBasePath, baseID, tableName)
headers := map[string]string{
"Content-Type": contentTypeJSON,
}
viewModel := CreateRecordsRequest[Tin]{}
viewModel.Records = append(
viewModel.Records,
CreateRecordRequest[Tin]{
Fields: *record,
},
)
viewModel.Typecast = true
body, err := EncodePostAsJSON(viewModel)
if err != nil {
return nil, err
}
request, err := httphelper.NewRequest(http.MethodPost, path, nil, headers, body)
if err != nil {
return nil, err
}
if ctx != nil {
request = request.WithContext(ctx)
}
response, err := httpClient.Do(request)
if err != nil {
return nil, err
}
responseBody, err := ioutil.ReadAll(response.Body)
response.Body.Close()
if err != nil {
return nil, err
}
if !httphelper.IsSuccessResponse(response) {
return nil, handleErrorResponse(response.StatusCode, responseBody)
}
if !httphelper.HasContentType(response, contentTypeJSON) {
return nil, fmt.Errorf("Content-Type is not %s", contentTypeJSON)
}
var list AirtableRecords[T]
if err := jsonhelper.ParseJSONFromBytes(&list, responseBody); err != nil {
return nil, err
}
var items []*AirtableRecord[T]
for i := range list.Records {
items = append(items, &list.Records[i])
}
return items, nil
}
// EncodePostAsJSON returns bytes of JSON encoded patch request
func EncodePostAsJSON[T AirtableFields](req CreateRecordsRequest[T]) (bodyBuf *bytes.Buffer, err error) {
bodyBuf = &bytes.Buffer{}
err = json.NewEncoder(bodyBuf).Encode(req)
if err != nil {
return nil, err
}
return bodyBuf, nil
}
// EncodePatchAsJSON returns bytes of JSON encoded patch request
func EncodePatchAsJSON[T any](patchRequest PatchItemsRequest[T]) (bodyBuf *bytes.Buffer, err error) {
bodyBuf = &bytes.Buffer{}
err = json.NewEncoder(bodyBuf).Encode(patchRequest)
if err != nil {
return nil, err
}
return bodyBuf, nil
}
func handleErrorResponse(statusCode int, responseBody []byte) error {
switch statusCode {
case http.StatusTooManyRequests:
return fmt.Errorf("rate limited by airtable API")
case http.StatusUnauthorized:
return fmt.Errorf("unauthorized to access airtable API; possible malformed access token")
case http.StatusForbidden:
return getError(responseBody, "forbidden to access airtable API")
case http.StatusNotFound:
return getError(responseBody, "not found")
case http.StatusRequestEntityTooLarge:
return fmt.Errorf("request body too large")
case http.StatusUnprocessableEntity:
return getError(responseBody, "unprocessable entity")
default:
return fmt.Errorf("API error: %d [%s]", statusCode, string(responseBody))
}
}
func getError(responseBody []byte, message string) error {
var errorResponse ErrorResponse
if err := jsonhelper.ParseJSONFromBytes(&errorResponse, responseBody); err != nil {
var simpleErrorResponse SimpleErrorResponse
if err := jsonhelper.ParseJSONFromBytes(&simpleErrorResponse, responseBody); err != nil {
return err
}
return fmt.Errorf("%s: %s", message, simpleErrorResponse.Error)
}
return getErrorFromErrorResponse(errorResponse, message)
}
func getErrorFromErrorResponse(errorResponse ErrorResponse, message string) error {
if errorResponse.Error.Message != "" {
return fmt.Errorf("%s: error type [%s], message [%s]", message, errorResponse.Error.Type, errorResponse.Error.Message)
}
return fmt.Errorf("%s: error type [%s]", message, errorResponse.Error.Type)
}
func listRecordsRequest(baseID string, tableName string, viewName string, ctx context.Context, maxRecords int, offset string) (*http.Request, error) {
path := fmt.Sprintf("%s/%s/%s", apiBasePath, baseID, tableName)
headers := map[string]string{
"Content-Type": contentTypeJSON,
}
if maxRecords == 0 {
maxRecords = defaultMaxRecords
}
queryParams := map[string]string{
"maxRecords": fmt.Sprintf("%d", maxRecords),
"view": viewName,
}
if offset != defaultOffset && offset != "" {
queryParams["offset"] = offset
}
request, err := httphelper.NewRequest(http.MethodGet, path, queryParams, headers, nil)
if err != nil {
return nil, err
}
if ctx != nil {
request = request.WithContext(ctx)
}
return request, nil
}