Files
airtable/client.go

133 lines
3.1 KiB
Go
Raw Normal View History

// Copyright © 2020 Mike Berezin
//
// Use of this source code is governed by an MIT license.
// Details in the LICENSE file.
package airtable
import (
"bytes"
2021-10-02 14:27:00 +03:00
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"time"
)
const (
2024-06-22 15:13:30 -07:00
defaultBaseURL = "https://api.airtable.com/v0"
defaultRateLimit = 4
)
2021-10-02 14:27:00 +03:00
// Client client for airtable api.
type Client struct {
2024-06-22 15:13:30 -07:00
Client *http.Client
BaseURL string
APIKey string
rateLimiter <-chan time.Time
}
2024-06-22 15:13:30 -07:00
// New airtable client constructor
// your API KEY you can get on your account page
// https://airtable.com/account
2024-06-22 15:13:30 -07:00
func New(apiKey string) *Client {
c := &Client{
Client: http.DefaultClient,
APIKey: apiKey,
BaseURL: defaultBaseURL,
}
2024-06-22 15:13:30 -07:00
c.SetRateLimit(defaultRateLimit)
return c
}
// 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
2024-06-22 15:13:30 -07:00
func (at *Client) SetRateLimit(rateLimit int) {
at.rateLimiter = time.Tick(time.Second / time.Duration(rateLimit))
}
2024-06-22 15:13:30 -07:00
func (at *Client) waitForRateLimit() {
<-at.rateLimiter
}
2023-07-09 22:33:45 +05:00
func (at *Client) get(ctx context.Context, db, table, recordID string, params url.Values, target any) error {
2024-06-22 15:13:30 -07:00
return at.do(ctx, "GET", db, table, recordID, params, nil, target)
}
2024-06-22 15:13:30 -07:00
func (at *Client) post(ctx context.Context, db, table string, data, target any) error {
return at.do(ctx, "POST", db, table, "", nil, data, target)
}
2023-07-09 22:33:45 +05:00
func (at *Client) delete(ctx context.Context, db, table string, recordIDs []string, target any) error {
params := url.Values{}
2021-10-02 14:27:00 +03:00
for _, recordID := range recordIDs {
params.Add("records[]", recordID)
}
2021-10-02 14:27:00 +03:00
2024-06-22 15:13:30 -07:00
return at.do(ctx, "DELETE", db, table, "", params, nil, target)
}
2021-10-02 14:27:00 +03:00
2024-06-22 15:13:30 -07:00
func (at *Client) patch(ctx context.Context, db, table string, data, target any) error {
return at.do(ctx, "PATCH", db, table, "", nil, data, target)
}
2021-10-02 14:27:00 +03:00
2024-06-22 15:13:30 -07:00
func (at *Client) put(ctx context.Context, db, table string, data, target any) error {
return at.do(ctx, "PUT", db, table, "", nil, data, target)
}
2024-06-22 15:13:30 -07:00
func (at *Client) do(ctx context.Context, method, db, table, recordID string, params url.Values, data, target any) error {
var err error
2021-10-02 14:27:00 +03:00
2024-06-22 15:13:30 -07:00
at.waitForRateLimit()
2021-10-02 14:27:00 +03:00
2024-06-22 15:13:30 -07:00
url := fmt.Sprintf("%s/%s/%s", at.BaseURL, db, table)
2021-10-02 14:27:00 +03:00
2024-06-22 15:13:30 -07:00
if recordID != "" {
url += fmt.Sprintf("/%s", recordID)
}
2021-10-02 14:27:00 +03:00
2024-06-22 15:13:30 -07:00
body := []byte{}
2021-10-02 14:27:00 +03:00
2024-06-22 15:13:30 -07:00
if data != nil {
body, err = json.Marshal(data)
if err != nil {
return fmt.Errorf("marshalling message body: %w", err)
}
2021-10-02 14:27:00 +03:00
}
2024-06-22 15:13:30 -07:00
req, err := http.NewRequestWithContext(ctx, method, url, bytes.NewReader(body))
2021-10-02 14:27:00 +03:00
if err != nil {
return fmt.Errorf("cannot create request: %w", err)
}
2024-06-22 15:13:30 -07:00
req.URL.RawQuery = params.Encode()
2021-10-02 14:27:00 +03:00
2024-06-22 15:13:30 -07:00
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", at.APIKey))
2021-10-02 14:27:00 +03:00
2024-06-22 15:13:30 -07:00
resp, err := at.Client.Do(req)
if err != nil {
2024-06-22 15:13:30 -07:00
return fmt.Errorf("http request failed: %w", err)
}
2021-10-02 14:27:00 +03:00
defer resp.Body.Close()
2021-10-02 14:27:00 +03:00
if resp.StatusCode < 200 || resp.StatusCode > 299 {
return makeHTTPClientError(url, resp)
}
2021-10-02 14:27:00 +03:00
2024-06-22 15:13:30 -07:00
dec := json.NewDecoder(resp.Body)
2021-10-02 14:27:00 +03:00
2024-06-22 15:13:30 -07:00
err = dec.Decode(target)
if err != nil {
2024-06-22 15:13:30 -07:00
return fmt.Errorf("json decode failed: %w", err)
}
2021-10-02 14:27:00 +03:00
return nil
}