-
Notifications
You must be signed in to change notification settings - Fork 20
/
client.go
226 lines (172 loc) · 5.26 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
// Copyright © 2020 Mike Berezin
//
// Use of this source code is governed by an MIT license.
// Details in the LICENSE file.
package airtable
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"time"
)
const (
airtableBaseURL = "https://api.airtable.com/v0"
rateLimit = 4
)
// Client client for airtable api.
type Client struct {
client *http.Client
rateLimiter <-chan time.Time
baseURL string
apiKey string
}
// NewClient airtable client constructor
// your API KEY you can get on your account page
// https://airtable.com/account
func NewClient(apiKey string) *Client {
return &Client{
client: http.DefaultClient,
rateLimiter: time.Tick(time.Second / time.Duration(rateLimit)),
apiKey: apiKey,
baseURL: airtableBaseURL,
}
}
// Set custom http client for custom usage
func (at *Client) SetCustomClient(client *http.Client) {
at.client = client
}
// SetRateLimit rate limit setter for custom usage
// Airtable limit is 5 requests per second (we use 4)
// https://airtable.com/{yourDatabaseID}/api/docs#curl/ratelimits
func (at *Client) SetRateLimit(customRateLimit int) {
at.rateLimiter = time.Tick(time.Second / time.Duration(customRateLimit))
}
func (at *Client) SetBaseURL(baseURL string) error {
url, err := url.Parse(baseURL)
if err != nil {
return fmt.Errorf("failed to parse baseURL: %s", err)
}
if url.Scheme == "" {
return fmt.Errorf("scheme of http or https must be specified")
}
if url.Scheme != "https" && url.Scheme != "http" {
return fmt.Errorf("http or https baseURL must be used")
}
at.baseURL = url.String()
return nil
}
func (at *Client) rateLimit() {
<-at.rateLimiter
}
func (at *Client) get(ctx context.Context, db, table, recordID string, params url.Values, target any) error {
at.rateLimit()
url := fmt.Sprintf("%s/%s/%s", at.baseURL, db, table)
if recordID != "" {
url += fmt.Sprintf("/%s", recordID)
}
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
if err != nil {
return fmt.Errorf("cannot create request: %w", err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", at.apiKey))
req.URL.RawQuery = params.Encode()
err = at.do(req, target)
if err != nil {
return err
}
return nil
}
func (at *Client) post(ctx context.Context, db, table string, data, response any) error {
at.rateLimit()
url := fmt.Sprintf("%s/%s/%s", at.baseURL, db, table)
body, err := json.Marshal(data)
if err != nil {
return fmt.Errorf("cannot marshal body: %w", err)
}
req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewReader(body))
if err != nil {
return fmt.Errorf("cannot create request: %w", err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", at.apiKey))
return at.do(req, response)
}
func (at *Client) delete(ctx context.Context, db, table string, recordIDs []string, target any) error {
at.rateLimit()
rawURL := fmt.Sprintf("%s/%s/%s", at.baseURL, db, table)
params := url.Values{}
for _, recordID := range recordIDs {
params.Add("records[]", recordID)
}
req, err := http.NewRequestWithContext(ctx, "DELETE", rawURL, nil)
if err != nil {
return fmt.Errorf("cannot create request: %w", err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", at.apiKey))
req.URL.RawQuery = params.Encode()
err = at.do(req, target)
if err != nil {
return err
}
return nil
}
func (at *Client) patch(ctx context.Context, db, table, data, response any) error {
at.rateLimit()
url := fmt.Sprintf("%s/%s/%s", at.baseURL, db, table)
body, err := json.Marshal(data)
if err != nil {
return fmt.Errorf("cannot marshal body: %w", err)
}
req, err := http.NewRequestWithContext(ctx, "PATCH", url, bytes.NewReader(body))
if err != nil {
return fmt.Errorf("cannot create request: %w", err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", at.apiKey))
return at.do(req, response)
}
func (at *Client) put(ctx context.Context, db, table, data, response any) error {
at.rateLimit()
url := fmt.Sprintf("%s/%s/%s", at.baseURL, db, table)
body, err := json.Marshal(data)
if err != nil {
return fmt.Errorf("cannot marshal body: %w", err)
}
req, err := http.NewRequestWithContext(ctx, "PUT", url, bytes.NewReader(body))
if err != nil {
return fmt.Errorf("cannot create request: %w", err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", at.apiKey))
return at.do(req, response)
}
func (at *Client) do(req *http.Request, response any) error {
if req == nil {
return errors.New("nil request")
}
url := req.URL.RequestURI()
resp, err := at.client.Do(req)
if err != nil {
return fmt.Errorf("HTTP request failure on %s: %w", url, err)
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode > 299 {
return makeHTTPClientError(url, resp)
}
b, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("HTTP Read error on response for %s: %w", url, err)
}
err = json.Unmarshal(b, response)
if err != nil {
return fmt.Errorf("JSON decode failed on %s:\n%s\nerror: %w", url, string(b), err)
}
return nil
}