This commit is contained in:
Ian Gulliver
2024-06-22 12:44:40 -07:00
parent 9d13c602a1
commit 14facea054
12 changed files with 2 additions and 450 deletions

View File

@@ -1,37 +0,0 @@
name: Go
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
build:
name: Build
runs-on: ubuntu-latest
steps:
- name: Set up Go 1.18
uses: actions/setup-go@v1
with:
go-version: 1.18
id: go
- name: Check out code into the Go module directory
uses: actions/checkout@v2
- name: Get dependencies
run: |
go get -v -t -d ./...
- name: Generate coverage report
run: go test -race -coverprofile=coverage.txt -covermode=atomic
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v1
with:
file: ./coverage.txt
name: codecov-umbrella
fail_ci_if_error: true

21
LICENSE
View File

@@ -1,21 +0,0 @@
MIT License
Copyright (c) 2020 Mike Berezin
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

177
README.md
View File

@@ -1,177 +0,0 @@
Golang Airtable API
================
[![GoDoc](https://godoc.org/github.com/mehanizm/airtable?status.svg)](https://pkg.go.dev/github.com/mehanizm/airtable)
![Go](https://github.com/mehanizm/airtable/workflows/Go/badge.svg)
[![codecov](https://codecov.io/gh/mehanizm/airtable/branch/master/graph/badge.svg)](https://codecov.io/gh/mehanizm/airtable)
[![Go Report](https://goreportcard.com/badge/github.com/mehanizm/airtable)](https://goreportcard.com/badge/github.com/mehanizm/airtable)
[![Mentioned in Awesome Go](https://awesome.re/mentioned-badge-flat.svg)](https://github.com/avelino/awesome-go)
A simple #golang package to access the [Airtable API](https://airtable.com/api).
Table of contents
===
- [Golang Airtable API](#golang-airtable-api)
- [Table of contents](#table-of-contents)
- [Installation](#installation)
- [Basic usage](#basic-usage)
- [Initialize client](#initialize-client)
- [Get table](#get-table)
- [List records](#list-records)
- [Add records](#add-records)
- [Get record by ID](#get-record-by-id)
- [Update records](#update-records)
- [Delete record](#delete-record)
- [Bulk delete records](#bulk-delete-records)
- [Special thanks](#special-thanks)
## Installation
The Golang Airtable API has been tested compatible with Go 1.13 on up.
```
go get github.com/mehanizm/airtable
```
## Basic usage
### Initialize client
You should get `your_api_token` in the airtable [account page](https://airtable.com/account)
```Go
client := airtable.NewClient("your_api_token")
```
You can use custom http client here
```Go
client.SetCustomClient(http.DefaultClient)
```
### Custom context
Each method below can be used with custom context. Simply use `MethodNameContext` call and provide context as first argument.
### List bases
```Go
bases, err := client.GetBases().WithOffset("").Do()
```
### Get base schema
```Go
schema, err := client.GetBaseSchema("your_database_ID").Do()
```
### Get table
To get the `your_database_ID` you should go to [main API page](https://airtable.com/api) and select the database.
```Go
table := client.GetTable("your_database_ID", "your_table_name")
```
### List records
To get records from the table you can use something like this
```Go
records, err := table.GetRecords().
FromView("view_1").
WithFilterFormula("AND({Field1}='value_1',NOT({Field2}='value_2'))").
WithSort(sortQuery1, sortQuery2).
ReturnFields("Field1", "Field2").
InStringFormat("Europe/Moscow", "ru").
Do()
if err != nil {
// Handle error
}
```
### Add records
```Go
recordsToSend := &airtable.Records{
Records: []*airtable.Record{
{
Fields: map[string]any{
"Field1": "value1",
"Field2": true,
},
},
},
}
receivedRecords, err := table.AddRecords(recordsToSend)
if err != nil {
// Handle error
}
```
### Get record by ID
```Go
record, err := table.GetRecord("recordID")
if err != nil {
// Handle error
}
```
### Update records
To partial update one record
```Go
res, err := record.UpdateRecordPartial(map[string]any{"Field_2": false})
if err != nil {
// Handle error
}
```
To full update records
```Go
toUpdateRecords := &airtable.Records{
Records: []*airtable.Record{
{
Fields: map[string]any{
"Field1": "value1",
"Field2": true,
},
},
{
Fields: map[string]any{
"Field1": "value1",
"Field2": true,
},
},
},
}
updatedRecords, err := table.UpdateRecords(toUpdateRecords)
if err != nil {
// Handle error
}
```
### Delete record
```Go
res, err := record.DeleteRecord()
if err != nil {
// Handle error
}
```
### Bulk delete records
To delete up to 10 records
```Go
records, err := table.DeleteRecords([]string{"recordID1", "recordsID2"})
if err != nil {
// Handle error
}
```
## Special thanks
Inspired by [Go Trello API](github.com/adlio/trello)

View File

@@ -1,43 +0,0 @@
package main
import (
"fmt"
"github.com/mehanizm/airtable"
)
const (
airtableAPIKey = "xxx"
airtableDBName = "xxx"
airtableTableName = "xxx"
)
func main() {
airtableClient := airtable.NewClient(airtableAPIKey)
airtableTable := airtableClient.GetTable(airtableDBName, airtableTableName)
offset := ""
for {
records, err := airtableTable.GetRecords().
WithFilterFormula("NOT({SomeBoolColumn})").
ReturnFields("Column1", "Column2", "Column3", "Column4").
MaxRecords(100).
PageSize(10).
WithOffset(offset).
Do()
if err != nil {
panic(err)
}
for recordNum, record := range records.Records {
fmt.Println("====iteration====")
fmt.Println(recordNum, record)
}
offset = records.Offset
if offset == "" {
break
}
}
}

4
go.mod
View File

@@ -1,3 +1,3 @@
module github.com/mehanizm/airtable
module github.com/flamingcow66/airtable
go 1.20
go 1.22

View File

@@ -1,72 +0,0 @@
{
"tables": [
{
"description": "Apartments to track.",
"fields": [
{
"description": "Name of the apartment",
"id": "fld1VnoyuotSTyxW1",
"name": "Name",
"type": "singleLineText"
},
{
"id": "fldoaIqdn5szURHpw",
"name": "Pictures",
"type": "multipleAttachments"
},
{
"id": "fldumZe00w09RYTW6",
"name": "District",
"options": {
"inverseLinkFieldId": "fldWnCJlo2z6ttT8Y",
"isReversed": false,
"linkedTableId": "tblK6MZHez0ZvBChZ",
"prefersSingleRecordLink": true
},
"type": "multipleRecordLinks"
}
],
"id": "tbltp8DGLhqbUmjK1",
"name": "Apartments",
"primaryFieldId": "fld1VnoyuotSTyxW1",
"views": [
{
"id": "viwQpsuEDqHFqegkp",
"name": "Grid view",
"type": "grid"
}
]
},
{
"fields": [
{
"id": "fldEVzvQOoULO38yl",
"name": "Name",
"type": "singleLineText"
},
{
"description": "Apartments that belong to this district",
"id": "fldWnCJlo2z6ttT8Y",
"name": "Apartments",
"options": {
"inverseLinkFieldId": "fldumZe00w09RYTW6",
"isReversed": false,
"linkedTableId": "tbltp8DGLhqbUmjK1",
"prefersSingleRecordLink": false
},
"type": "multipleRecordLinks"
}
],
"id": "tblK6MZHez0ZvBChZ",
"name": "Districts",
"primaryFieldId": "fldEVzvQOoULO38yl",
"views": [
{
"id": "viwi3KXvrKug2mIBS",
"name": "Grid view",
"type": "grid"
}
]
}
]
}

View File

@@ -1,22 +0,0 @@
{
"records": [
{
"id": "recnTq6CsvFM6vX2m",
"fields": {
"Field1": "Field1",
"Field2": true,
"Field3": "2020-04-06T06:00:00.000Z"
},
"createdTime": "2020-04-10T11:30:57.000Z"
},
{
"id": "recr3qAQbM7juKa4o",
"fields": {
"Field1": "Field1",
"Field2": true,
"Field3": "2020-04-06T06:00:00.000Z"
},
"createdTime": "2020-04-10T11:30:49.000Z"
}
]
}

View File

@@ -1,8 +0,0 @@
{
"records": [
{
"id": "recnTq6CsvFM6vX2m",
"deleted": true
}
]
}

View File

@@ -1,12 +0,0 @@
{
"records": [
{
"id": "recnTq6CsvFM6vX2m",
"deleted": true
},
{
"id": "recr3qAQbM7juKa4o",
"deleted": true
}
]
}

View File

@@ -1,15 +0,0 @@
{
"bases": [
{
"id": "appLkNDICXNqxSDhG",
"name": "Apartment Hunting",
"permissionLevel": "create"
},
{
"id": "appSW9R5uCNmRmfl6",
"name": "Project Tracker",
"permissionLevel": "edit"
}
],
"offset": "itr23sEjsdfEr3282/appSW9R5uCNmRmfl6"
}

View File

@@ -1,9 +0,0 @@
{
"id": "recnTq6CsvFM6vX2m",
"fields": {
"Field1": "Field1",
"Field2": true,
"Field3": "2020-04-06T06:00:00.000Z"
},
"createdTime": "2020-04-10T11:30:57.000Z"
}

View File

@@ -1,32 +0,0 @@
{
"records": [
{
"id": "recnTq6CsvFM6vX2m",
"fields": {
"Field1": "Field1",
"Field2": true,
"Field3": "2020-04-06T06:00:00.000Z"
},
"createdTime": "2020-04-10T11:30:57.000Z"
},
{
"id": "recr3qAQbM7juKa4o",
"fields": {
"Field1": "Field1",
"Field2": true,
"Field3": "2020-04-06T06:00:00.000Z"
},
"createdTime": "2020-04-10T11:30:49.000Z"
},
{
"id": "recr3qAQbM7juKa4a",
"fields": {
"Field1": "Field1",
"Field2": false,
"Field3": "2020-04-06T06:00:00.000Z"
},
"createdTime": "2020-04-10T11:30:49.000Z"
}
],
"offset": "10"
}