forked from mehanizm/airtable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
table.go
126 lines (100 loc) · 2.9 KB
/
table.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright © 2020 Mike Berezin
//
// Use of this source code is governed by an MIT license.
// Details in the LICENSE file.
package airtable
import (
"net/url"
)
// 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
}
// GetTable return table object.
func (c *Client) GetTable(dbName, tableName string) *Table {
return &Table{
client: c,
dbName: dbName,
tableName: tableName,
}
}
// GetRecordsWithParams get records with url values params
// https://airtable.com/{yourDatabaseID}/api/docs#curl/table:{yourTableName}:list
func (t *Table) GetRecordsWithParams(params url.Values) (*Records, error) {
records := new(Records)
err := t.client.get(t.dbName, t.tableName, "", params, records)
if err != nil {
return nil, err
}
for _, record := range records.Records {
record.client = t.client
record.table = t
}
return records, nil
}
// 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) {
result := new(Records)
err := t.client.post(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) {
response := new(Records)
err := t.client.put(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) {
response := new(Records)
err := t.client.patch(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) {
response := new(Records)
err := t.client.delete(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
}