Skip to content

Commit

Permalink
Merge pull request #22 from andrespd99/master
Browse files Browse the repository at this point in the history
Added support for Upload Attachment API
  • Loading branch information
mehanizm authored Dec 23, 2024
2 parents 56620ac + a65588f commit 64873bd
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 8 deletions.
49 changes: 49 additions & 0 deletions attachment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package airtable

import "context"

type Attachment struct {
ContentType string `json:"contentType"`
File string `json:"file"`
FileName string `json:"filename"`
}

type FieldAttachments struct {
client *Client
table *Table
// Parent record's ID of the attached file.
Id string `json:"id"`
CreatedTime string `json:"createdTime"`
// Mapped array of attachments attached to this field.
//
// Key is the ID of the record's attachment field and the value is the list of attached files.
Attachments map[string][]FieldAttachmentDetails `json:"fields"`
}

type FieldAttachmentDetails struct {
// Airtable's attachment ID
Id string `json:"id"`
URL string `json:"url"`
FileName string `json:"filename"`
// In bytes
Size int `json:"size"`
// Content-Type value
Type string `json:"type"`
}

func (t *Table) UploadAttachment(recordID string, attachmentFieldIdOrName string, data Attachment) (*FieldAttachments, error) {
return t.UploadAttachmentContext(context.Background(), recordID, attachmentFieldIdOrName, data)
}
func (t *Table) UploadAttachmentContext(ctx context.Context, recordID string, attachmentFieldIdOrName string, data Attachment) (*FieldAttachments, error) {
result := new(FieldAttachments)

err := t.client.postAttachment(ctx, t.dbName, recordID, attachmentFieldIdOrName, data, result)
if err != nil {
return nil, err
}

result.client = t.client
result.table = t

return result, nil
}
40 changes: 32 additions & 8 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,30 @@ import (
)

const (
airtableBaseURL = "https://api.airtable.com/v0"
rateLimit = 4
airtableBaseURL = "https://api.airtable.com/v0"
airtableUploadAttachmentBaseURL = "https://content.airtable.com/v0"
rateLimit = 4
)

// Client client for airtable api.
type Client struct {
client *http.Client
client *http.Client
rateLimiter *rate.Limiter
baseURL string
apiKey string
baseURL string
uploadAttachmentBaseURL 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,
client: http.DefaultClient,
rateLimiter: rate.NewLimiter(rate.Limit(rateLimit), 1),
apiKey: apiKey,
baseURL: airtableBaseURL,
apiKey: apiKey,
baseURL: airtableBaseURL,
uploadAttachmentBaseURL: airtableUploadAttachmentBaseURL,
}
}

Expand Down Expand Up @@ -131,6 +134,27 @@ func (at *Client) post(ctx context.Context, db, table string, data, response any
return at.do(req, response)
}

func (at *Client) postAttachment(ctx context.Context, db, recordID string, attachmentFieldIdOrName string, data Attachment, response any) error {
at.rateLimit()

Check failure on line 138 in client.go

View workflow job for this annotation

GitHub Actions / Build

not enough arguments in call to at.rateLimit

url := fmt.Sprintf("%s/%s/%s/%s/uploadAttachment", at.uploadAttachmentBaseURL, db, recordID, attachmentFieldIdOrName)

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 {
err := at.rateLimit(ctx)
if err != nil {
Expand Down

0 comments on commit 64873bd

Please sign in to comment.