Feature/put post (#9)
This commit is contained in:
62
client.go
62
client.go
@@ -7,6 +7,7 @@ package airtable
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
@@ -21,7 +22,7 @@ const (
|
||||
rateLimit = 4
|
||||
)
|
||||
|
||||
// Client client for airtable api
|
||||
// Client client for airtable api.
|
||||
type Client struct {
|
||||
client *http.Client
|
||||
rateLimiter <-chan time.Time
|
||||
@@ -73,74 +74,118 @@ func (at *Client) rateLimit() {
|
||||
|
||||
func (at *Client) get(db, table, recordID string, params url.Values, target interface{}) error {
|
||||
at.rateLimit()
|
||||
|
||||
url := fmt.Sprintf("%s/%s/%s", at.baseURL, db, table)
|
||||
if recordID != "" {
|
||||
url += fmt.Sprintf("/%s", recordID)
|
||||
}
|
||||
req, err := http.NewRequest("GET", url, nil)
|
||||
|
||||
req, err := http.NewRequestWithContext(context.Background(), "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(db, table string, data, response interface{}) 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.NewRequest("POST", url, bytes.NewReader(body))
|
||||
|
||||
req, err := http.NewRequestWithContext(context.Background(), "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(db, table string, recordIDs []string, target interface{}) 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.NewRequest("DELETE", rawURL, nil)
|
||||
|
||||
req, err := http.NewRequestWithContext(context.Background(), "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(db, table, data, response interface{}) 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.NewRequest("PATCH", url, bytes.NewReader(body))
|
||||
|
||||
req, err := http.NewRequestWithContext(context.Background(), "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(db, table, data, response interface{}) 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(context.Background(), "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)
|
||||
}
|
||||
|
||||
@@ -148,22 +193,29 @@ func (at *Client) do(req *http.Request, response interface{}) 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 := ioutil.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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user