feat: add read-only support of the meta API (#13)

* Add bases API.

* Implement get tables schema

* Rename GetBase to GetBaseSchema

* Add tests

* Update doc
This commit is contained in:
François de Metz
2023-01-01 15:18:29 +01:00
committed by GitHub
parent 6275e61012
commit 1b1e6e9e92
7 changed files with 275 additions and 0 deletions

89
base.go Normal file
View File

@@ -0,0 +1,89 @@
package airtable
import (
"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"`
}
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) {
bases := new(Bases)
err := at.get("meta", "bases", "", params, bases)
if err != nil {
return nil, err
}
return bases, nil
}
// Table represents table object.
type BaseConfig struct {
client *Client
dbId string
params url.Values
}
// 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.GetTablesWithParams()
}
// GetTablesWithParams get tables from a base with url values params
// https://airtable.com/developers/web/api/get-base-schema
func (b *BaseConfig) GetTablesWithParams() (*Tables, error) {
tables := new(Tables)
err := b.client.get("meta/bases", b.dbId, "tables", nil, tables)
if err != nil {
return nil, err
}
return tables, nil
}