-
Notifications
You must be signed in to change notification settings - Fork 20
/
base.go
108 lines (89 loc) · 2.59 KB
/
base.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
package airtable
import (
"context"
"net/url"
)
// Base type of airtable base.
type Base struct {
ID string `json:"id"`
Name string `json:"name"`
PermissionLevel string `json:"permissionLevel"`
}
// Base type of airtable bases.
type Bases struct {
Bases []*Base `json:"bases"`
Offset string `json:"offset,omitempty"`
}
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"`
}
type View struct {
ID string `json:"id"`
Type string `json:"type"`
Name string `json:"name"`
}
type TableSchema struct {
ID string `json:"id"`
PrimaryFieldID string `json:"primaryFieldId"`
Name string `json:"name"`
Description string `json:"description"`
Fields []*Field `json:"fields"`
Views []*View `json:"views"`
}
type Tables struct {
Tables []*TableSchema `json:"tables"`
}
// GetBasesWithParams get bases with url values params
// https://airtable.com/developers/web/api/list-bases
func (at *Client) GetBasesWithParams(params url.Values) (*Bases, error) {
return at.GetBasesWithParamsContext(context.Background(), params)
}
// getBasesWithParamsContext get bases with url values params
// with custom context
func (at *Client) GetBasesWithParamsContext(ctx context.Context, params url.Values) (*Bases, error) {
bases := new(Bases)
err := at.get(ctx, "meta", "bases", "", params, bases)
if err != nil {
return nil, err
}
return bases, nil
}
// Table represents table object.
type BaseConfig struct {
client *Client
dbId string
}
// GetBase return Base object.
func (c *Client) GetBaseSchema(dbId string) *BaseConfig {
return &BaseConfig{
client: c,
dbId: dbId,
}
}
// Do send the prepared
func (b *BaseConfig) Do() (*Tables, error) {
return b.GetTables()
}
// Do send the prepared with custom context
func (b *BaseConfig) DoContext(ctx context.Context) (*Tables, error) {
return b.GetTablesContext(ctx)
}
// GetTables get tables from a base with url values params
// https://airtable.com/developers/web/api/get-base-schema
func (b *BaseConfig) GetTables() (*Tables, error) {
return b.GetTablesContext(context.Background())
}
// getTablesContext get tables from a base with url values params
// with custom context
func (b *BaseConfig) GetTablesContext(ctx context.Context) (*Tables, error) {
tables := new(Tables)
err := b.client.get(ctx, "meta/bases", b.dbId, "tables", nil, tables)
if err != nil {
return nil, err
}
return tables, nil
}