diff --git a/get-records.go b/get-records.go index 77a4e96..e7d159a 100644 --- a/get-records.go +++ b/get-records.go @@ -8,6 +8,7 @@ package airtable import ( "fmt" "net/url" + "strconv" ) // GetRecordsConfig helper type to use in @@ -35,7 +36,7 @@ func (grc *GetRecordsConfig) ReturnFields(fieldNames ...string) *GetRecordsConfi // WithFilterFormula add filter to request func (grc *GetRecordsConfig) WithFilterFormula(filterFormula string) *GetRecordsConfig { - grc.params.Add("filterByFormula", filterFormula) + grc.params.Set("filterByFormula", filterFormula) return grc } @@ -45,15 +46,46 @@ func (grc *GetRecordsConfig) WithSort(sortQueries ...struct { direction string }) *GetRecordsConfig { for queryNum, sortQuery := range sortQueries { - grc.params.Add(fmt.Sprintf("sort[%v][field]", queryNum), sortQuery.fieldName) - grc.params.Add(fmt.Sprintf("sort[%v][direction]", queryNum), sortQuery.direction) + grc.params.Set(fmt.Sprintf("sort[%v][field]", queryNum), sortQuery.fieldName) + grc.params.Set(fmt.Sprintf("sort[%v][direction]", queryNum), sortQuery.direction) } return grc } // FromView add view parameter to get records func (grc *GetRecordsConfig) FromView(viewNameOrID string) *GetRecordsConfig { - grc.params.Add("view", viewNameOrID) + grc.params.Set("view", viewNameOrID) + return grc +} + +// The maximum total number of records that will be returned in your requests. +// If this value is larger than pageSize (which is 100 by default), +// you may have to load multiple pages to reach this total. +// See the Pagination section below for more. +func (grc *GetRecordsConfig) MaxRecords(maxRecords int) *GetRecordsConfig { + grc.params.Set("maxRecords", strconv.Itoa(maxRecords)) + return grc +} + +// The number of records returned in each request. +// Must be less than or equal to 100. Default is 100. +// See the Pagination section below for more. +func (grc *GetRecordsConfig) PageSize(pageSize int) *GetRecordsConfig { + grc.params.Set("pageSize", strconv.Itoa(pageSize)) + return grc +} + +// Pagination +// The server returns one page of records at a time. +// Each page will contain pageSize records, which is 100 by default. + +// If there are more records, the response will contain an offset. +// To fetch the next page of records, include offset in the next request's parameters. + +// Pagination will stop when you've reached the end of your table. +// If the maxRecords parameter is passed, pagination will stop once you've reached this maximum. +func (grc *GetRecordsConfig) WithOffset(offset string) *GetRecordsConfig { + grc.params.Set("offset", offset) return grc } @@ -63,9 +95,9 @@ func (grc *GetRecordsConfig) FromView(viewNameOrID string) *GetRecordsConfig { // and user locale data // https://support.airtable.com/hc/en-us/articles/220340268-Supported-locale-modifiers-for-SET-LOCALE func (grc *GetRecordsConfig) InStringFormat(timeZone, userLocale string) *GetRecordsConfig { - grc.params.Add("cellFormat", "string") - grc.params.Add("timeZone", timeZone) - grc.params.Add("userLocale", userLocale) + grc.params.Set("cellFormat", "string") + grc.params.Set("timeZone", timeZone) + grc.params.Set("userLocale", userLocale) return grc } diff --git a/get-records_test.go b/get-records_test.go index 05a84da..d001e1e 100644 --- a/get-records_test.go +++ b/get-records_test.go @@ -27,6 +27,9 @@ func TestGetRecordsConfig_Do(t *testing.T) { WithSort(sortQuery1, sortQuery2). ReturnFields("Field1", "Field2"). InStringFormat("Europe/Moscow", "ru"). + MaxRecords(100). + PageSize(10). + WithOffset("hhh"). Do() if err != nil { t.Errorf("there should not be an err, but was: %v", err) @@ -34,4 +37,10 @@ func TestGetRecordsConfig_Do(t *testing.T) { if len(records.Records) != 3 { t.Errorf("there should be 3 records, but was %v", len(records.Records)) } + + table.client.baseURL = mockErrorResponse(400).URL + records, err = table.GetRecords().Do() + if err == nil { + t.Errorf("there should be an err, but was nil") + } } diff --git a/go.mod b/go.mod index 647ee02..7937559 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ module github.com/mehanizm/airtable -go 1.13 +go 1.15 diff --git a/table.go b/table.go index adb18b0..a9bf2a9 100644 --- a/table.go +++ b/table.go @@ -12,7 +12,7 @@ import ( // Records base type of airtable records type Records struct { Records []*Record `json:"records"` - Offset int `json:"offset,omitempty"` + Offset string `json:"offset,omitempty"` } // Table represents table object diff --git a/testdata/get_records_with_filter.json b/testdata/get_records_with_filter.json index 6901191..47d8816 100644 --- a/testdata/get_records_with_filter.json +++ b/testdata/get_records_with_filter.json @@ -27,5 +27,6 @@ }, "createdTime": "2020-04-10T11:30:49.000Z" } - ] + ], + "offset": "10" } \ No newline at end of file