impr: correct offset usage and add setters, add tests
This commit is contained in:
@@ -8,6 +8,7 @@ package airtable
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
// GetRecordsConfig helper type to use in
|
// GetRecordsConfig helper type to use in
|
||||||
@@ -35,7 +36,7 @@ func (grc *GetRecordsConfig) ReturnFields(fieldNames ...string) *GetRecordsConfi
|
|||||||
|
|
||||||
// WithFilterFormula add filter to request
|
// WithFilterFormula add filter to request
|
||||||
func (grc *GetRecordsConfig) WithFilterFormula(filterFormula string) *GetRecordsConfig {
|
func (grc *GetRecordsConfig) WithFilterFormula(filterFormula string) *GetRecordsConfig {
|
||||||
grc.params.Add("filterByFormula", filterFormula)
|
grc.params.Set("filterByFormula", filterFormula)
|
||||||
return grc
|
return grc
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -45,15 +46,46 @@ func (grc *GetRecordsConfig) WithSort(sortQueries ...struct {
|
|||||||
direction string
|
direction string
|
||||||
}) *GetRecordsConfig {
|
}) *GetRecordsConfig {
|
||||||
for queryNum, sortQuery := range sortQueries {
|
for queryNum, sortQuery := range sortQueries {
|
||||||
grc.params.Add(fmt.Sprintf("sort[%v][field]", queryNum), sortQuery.fieldName)
|
grc.params.Set(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][direction]", queryNum), sortQuery.direction)
|
||||||
}
|
}
|
||||||
return grc
|
return grc
|
||||||
}
|
}
|
||||||
|
|
||||||
// FromView add view parameter to get records
|
// FromView add view parameter to get records
|
||||||
func (grc *GetRecordsConfig) FromView(viewNameOrID string) *GetRecordsConfig {
|
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
|
return grc
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -63,9 +95,9 @@ func (grc *GetRecordsConfig) FromView(viewNameOrID string) *GetRecordsConfig {
|
|||||||
// and user locale data
|
// and user locale data
|
||||||
// https://support.airtable.com/hc/en-us/articles/220340268-Supported-locale-modifiers-for-SET-LOCALE
|
// https://support.airtable.com/hc/en-us/articles/220340268-Supported-locale-modifiers-for-SET-LOCALE
|
||||||
func (grc *GetRecordsConfig) InStringFormat(timeZone, userLocale string) *GetRecordsConfig {
|
func (grc *GetRecordsConfig) InStringFormat(timeZone, userLocale string) *GetRecordsConfig {
|
||||||
grc.params.Add("cellFormat", "string")
|
grc.params.Set("cellFormat", "string")
|
||||||
grc.params.Add("timeZone", timeZone)
|
grc.params.Set("timeZone", timeZone)
|
||||||
grc.params.Add("userLocale", userLocale)
|
grc.params.Set("userLocale", userLocale)
|
||||||
return grc
|
return grc
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -27,6 +27,9 @@ func TestGetRecordsConfig_Do(t *testing.T) {
|
|||||||
WithSort(sortQuery1, sortQuery2).
|
WithSort(sortQuery1, sortQuery2).
|
||||||
ReturnFields("Field1", "Field2").
|
ReturnFields("Field1", "Field2").
|
||||||
InStringFormat("Europe/Moscow", "ru").
|
InStringFormat("Europe/Moscow", "ru").
|
||||||
|
MaxRecords(100).
|
||||||
|
PageSize(10).
|
||||||
|
WithOffset("hhh").
|
||||||
Do()
|
Do()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("there should not be an err, but was: %v", err)
|
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 {
|
if len(records.Records) != 3 {
|
||||||
t.Errorf("there should be 3 records, but was %v", len(records.Records))
|
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")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
2
go.mod
2
go.mod
@@ -1,3 +1,3 @@
|
|||||||
module github.com/mehanizm/airtable
|
module github.com/mehanizm/airtable
|
||||||
|
|
||||||
go 1.13
|
go 1.15
|
||||||
|
|||||||
2
table.go
2
table.go
@@ -12,7 +12,7 @@ import (
|
|||||||
// Records base type of airtable records
|
// Records base type of airtable records
|
||||||
type Records struct {
|
type Records struct {
|
||||||
Records []*Record `json:"records"`
|
Records []*Record `json:"records"`
|
||||||
Offset int `json:"offset,omitempty"`
|
Offset string `json:"offset,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Table represents table object
|
// Table represents table object
|
||||||
|
|||||||
3
testdata/get_records_with_filter.json
vendored
3
testdata/get_records_with_filter.json
vendored
@@ -27,5 +27,6 @@
|
|||||||
},
|
},
|
||||||
"createdTime": "2020-04-10T11:30:49.000Z"
|
"createdTime": "2020-04-10T11:30:49.000Z"
|
||||||
}
|
}
|
||||||
]
|
],
|
||||||
|
"offset": "10"
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user