Working field fetch

This commit is contained in:
Ian Gulliver
2024-06-22 21:46:58 -07:00
parent b615906097
commit 6abf2d051e
9 changed files with 205 additions and 645 deletions

164
table.go
View File

@@ -1,149 +1,59 @@
// Copyright © 2020 Mike Berezin
//
// Use of this source code is governed by an MIT license.
// Details in the LICENSE file.
package airtable
import (
"context"
"net/url"
"fmt"
)
// Records base type of airtable records.
type Records struct {
Records []*Record `json:"records"`
Offset string `json:"offset,omitempty"`
// The Airtable API will perform best-effort automatic data conversion
// from string values if the typecast parameter is passed in.
// Automatic conversion is disabled by default to ensure data integrity,
// but it may be helpful for integrating with 3rd party data sources.
Typecast bool `json:"typecast,omitempty"`
}
// Table represents table object.
type Table struct {
client *Client
dbName string
tableName string
ID string `json:"id"`
PrimaryFieldID string `json:"primaryFieldId"`
Name string `json:"name"`
Description string `json:"description"`
Fields []*Field `json:"fields"`
Views []*View `json:"views"`
c *Client
b *Base
}
// GetTable return table object.
func (c *Client) GetTable(dbName, tableName string) *Table {
return &Table{
client: c,
dbName: dbName,
tableName: tableName,
}
type Field struct {
ID string `json:"id"`
Type string `json:"type"`
Name string `json:"name"`
Description string `json:"description"`
Options map[string]any `json:"options"`
}
func (t *Table) GetRecordsWithParams(params url.Values) ([]*Record, error) {
return t.GetRecordsWithParamsContext(context.Background(), params)
type View struct {
ID string `json:"id"`
Type string `json:"type"`
Name string `json:"name"`
}
func (t *Table) GetRecordsWithParamsContext(ctx context.Context, params url.Values) ([]*Record, error) {
records, err := listAll[Record](ctx, t.client, t.dbName, t.tableName, params, "records")
func (b *Base) ListTables(ctx context.Context) ([]*Table, error) {
return listAll[Table](ctx, b.c, fmt.Sprintf("meta/bases/%s/tables", b.ID), nil, "tables", func(t *Table) error {
t.c = b.c
t.b = b
return nil
})
}
func (b *Base) GetTableByName(ctx context.Context, name string) (*Table, error) {
tables, err := b.ListTables(ctx)
if err != nil {
return nil, err
}
for _, record := range records {
record.client = t.client
record.table = t
for _, table := range tables {
if table.Name == name {
return table, nil
}
}
return records, nil
return nil, fmt.Errorf("table '%s' not found", name)
}
// AddRecords method to add lines to table (up to 10 in one request)
// https://airtable.com/{yourDatabaseID}/api/docs#curl/table:{yourTableName}:create
func (t *Table) AddRecords(records *Records) (*Records, error) {
return t.AddRecordsContext(context.Background(), records)
}
// AddRecordsContext method to add lines to table (up to 10 in one request)
// with custom context
func (t *Table) AddRecordsContext(ctx context.Context, records *Records) (*Records, error) {
result := new(Records)
err := t.client.post(ctx, t.dbName, t.tableName, records, result)
if err != nil {
return nil, err
}
for _, record := range result.Records {
record.client = t.client
record.table = t
}
return result, err
}
// UpdateRecords full update records.
func (t *Table) UpdateRecords(records *Records) (*Records, error) {
return t.UpdateRecordsContext(context.Background(), records)
}
// UpdateRecordsContext full update records with custom context.
func (t *Table) UpdateRecordsContext(ctx context.Context, records *Records) (*Records, error) {
response := new(Records)
err := t.client.put(ctx, t.dbName, t.tableName, records, response)
if err != nil {
return nil, err
}
for _, record := range response.Records {
record.client = t.client
record.table = t
}
return response, nil
}
// UpdateRecordsPartial partial update records.
func (t *Table) UpdateRecordsPartial(records *Records) (*Records, error) {
return t.UpdateRecordsPartialContext(context.Background(), records)
}
// UpdateRecordsPartialContext partial update records with custom context.
func (t *Table) UpdateRecordsPartialContext(ctx context.Context, records *Records) (*Records, error) {
response := new(Records)
err := t.client.patch(ctx, t.dbName, t.tableName, records, response)
if err != nil {
return nil, err
}
for _, record := range response.Records {
record.client = t.client
record.table = t
}
return response, nil
}
// DeleteRecords delete records by recordID
// up to 10 ids in one request.
func (t *Table) DeleteRecords(recordIDs []string) (*Records, error) {
return t.DeleteRecordsContext(context.Background(), recordIDs)
}
// DeleteRecordsContext delete records by recordID
// with custom context
func (t *Table) DeleteRecordsContext(ctx context.Context, recordIDs []string) (*Records, error) {
response := new(Records)
err := t.client.delete(ctx, t.dbName, t.tableName, recordIDs, response)
if err != nil {
return nil, err
}
for _, record := range response.Records {
record.client = t.client
record.table = t
}
return response, nil
func (t Table) String() string {
return fmt.Sprintf("%s.%s", t.b, t.Name)
}