Feature/put post (#9)

This commit is contained in:
Yaroslav Rudenko
2021-10-02 14:27:00 +03:00
committed by GitHub
parent 8deb157f4d
commit 3bd1ace8d8
6 changed files with 107 additions and 28 deletions

View File

@@ -9,7 +9,7 @@ import (
"net/url"
)
// Records base type of airtable records
// Records base type of airtable records.
type Records struct {
Records []*Record `json:"records"`
Offset string `json:"offset,omitempty"`
@@ -21,14 +21,14 @@ type Records struct {
Typecast bool `json:"typecast,omitempty"`
}
// Table represents table object
// Table represents table object.
type Table struct {
client *Client
dbName string
tableName string
}
// GetTable return table object
// GetTable return table object.
func (c *Client) GetTable(dbName, tableName string) *Table {
return &Table{
client: c,
@@ -41,14 +41,17 @@ func (c *Client) GetTable(dbName, tableName string) *Table {
// 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
}
@@ -56,55 +59,68 @@ func (t *Table) GetRecordsWithParams(params url.Values) (*Records, error) {
// 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
// UpdateRecords full update records.
func (t *Table) UpdateRecords(records *Records) (*Records, error) {
response := new(Records)
err := t.client.post(t.dbName, t.tableName, records, response)
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
// 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
}