Files
airtable/client.go

227 lines
5.3 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"
2020-04-17 23:12:07 +03:00
"errors"
"fmt"
"io"
"net/http"
"net/url"
"time"
)
const (
airtableBaseURL = "https://api.airtable.com/v0"
rateLimit = 4
)
2021-10-02 14:27:00 +03:00
// 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 interface{}) error {
at.rateLimit()
2021-10-02 14:27:00 +03:00
url := fmt.Sprintf("%s/%s/%s", at.baseURL, db, table)
if recordID != "" {
url += fmt.Sprintf("/%s", recordID)
}
2021-10-02 14:27:00 +03:00
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
if err != nil {
return fmt.Errorf("cannot create request: %w", err)
}
2021-10-02 14:27:00 +03: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
req.URL.RawQuery = params.Encode()
2021-10-02 14:27:00 +03:00
2020-04-17 23:12:07 +03:00
err = at.do(req, target)
if err != nil {
return err
}
2021-10-02 14:27:00 +03:00
return nil
}
func (at *Client) post(ctx context.Context, db, table string, data, response interface{}) error {
at.rateLimit()
2021-10-02 14:27:00 +03:00
url := fmt.Sprintf("%s/%s/%s", at.baseURL, db, table)
2021-10-02 14:27:00 +03:00
body, err := json.Marshal(data)
if err != nil {
return fmt.Errorf("cannot marshal body: %w", err)
}
2021-10-02 14:27:00 +03:00
req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewReader(body))
if err != nil {
return fmt.Errorf("cannot create request: %w", err)
}
2021-10-02 14:27:00 +03: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
2020-04-17 23:12:07 +03:00
return at.do(req, response)
}
func (at *Client) delete(ctx context.Context, db, table string, recordIDs []string, target interface{}) error {
at.rateLimit()
2021-10-02 14:27:00 +03:00
rawURL := fmt.Sprintf("%s/%s/%s", at.baseURL, db, table)
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
req, err := http.NewRequestWithContext(ctx, "DELETE", rawURL, nil)
if err != nil {
return fmt.Errorf("cannot create request: %w", err)
}
2021-10-02 14:27:00 +03: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
req.URL.RawQuery = params.Encode()
2021-10-02 14:27:00 +03:00
2020-04-17 23:12:07 +03:00
err = at.do(req, target)
if err != nil {
return err
}
2021-10-02 14:27:00 +03:00
return nil
}
func (at *Client) patch(ctx context.Context, db, table, data, response interface{}) error {
at.rateLimit()
2021-10-02 14:27:00 +03:00
url := fmt.Sprintf("%s/%s/%s", at.baseURL, db, table)
2021-10-02 14:27:00 +03:00
body, err := json.Marshal(data)
if err != nil {
return fmt.Errorf("cannot marshal body: %w", err)
}
2021-10-02 14:27:00 +03:00
req, err := http.NewRequestWithContext(ctx, "PATCH", url, bytes.NewReader(body))
if err != nil {
return fmt.Errorf("cannot create request: %w", err)
}
2021-10-02 14:27:00 +03: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
return at.do(req, response)
}
func (at *Client) put(ctx context.Context, db, table, data, response interface{}) error {
2021-10-02 14:27:00 +03:00
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))
2021-10-02 14:27:00 +03:00
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))
2020-04-17 23:12:07 +03:00
return at.do(req, response)
}
2020-04-17 23:12:07 +03:00
func (at *Client) do(req *http.Request, response interface{}) error {
if req == nil {
return errors.New("nil request")
}
2021-10-02 14:27:00 +03:00
2020-04-17 23:12:07 +03:00
url := req.URL.RequestURI()
2021-10-02 14:27:00 +03:00
resp, err := at.client.Do(req)
if err != nil {
return fmt.Errorf("HTTP request failure on %s: %w", url, 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
b, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("HTTP Read error on response for %s: %w", url, err)
}
2021-10-02 14:27:00 +03:00
err = json.Unmarshal(b, response)
if err != nil {
return fmt.Errorf("JSON decode failed on %s:\n%s\nerror: %w", url, string(b), err)
}
2021-10-02 14:27:00 +03:00
return nil
}