-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathtable.go
More file actions
163 lines (131 loc) · 4.53 KB
/
table.go
File metadata and controls
163 lines (131 loc) · 4.53 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// 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"
)
type PerformUpsert struct {
FieldsToMergeOn []string `json:"fieldsToMergeOn"`
}
// 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"`
// fieldsToMergeOn will be used as an external ID to match records for updates.
// For records where no match is found, a new Airtable record will be created.
// https://airtable.com/developers/web/api/update-multiple-records#request-performupsert
PerformUpsert *PerformUpsert `json:"performUpsert,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) {
return t.GetRecordsWithParamsContext(context.Background(), params)
}
// GetRecordsWithParamsContext get records with url values params
// with custom context
func (t *Table) GetRecordsWithParamsContext(ctx context.Context, params url.Values) (*Records, error) {
records := new(Records)
err := t.client.get(ctx, 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) {
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
}