Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: generic paginator #491

Merged
merged 21 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions fastly/acl_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ type ACLEntry struct {

const aclEntriesPath = "/service/%s/acl/%s/entries"

// ListACLEntriesInput is the input parameter to ListACLEntries function.
type ListACLEntriesInput struct {
// GetACLEntriesInput is the input parameter to GetACLEntries function.
type GetACLEntriesInput struct {
// ACLID is an alphanumeric string identifying a ACL (required).
ACLID string
// Direction is the direction in which to sort results.
Expand All @@ -38,7 +38,7 @@ type ListACLEntriesInput struct {
}

// GetACLEntries returns a ListPaginator for paginating through the resources.
func (c *Client) GetACLEntries(i *ListACLEntriesInput) *ListPaginator[ACLEntry] {
func (c *Client) GetACLEntries(i *GetACLEntriesInput) *ListPaginator[ACLEntry] {
return newPaginator[ACLEntry](c, &listInput{
Direction: i.Direction,
Sort: i.Sort,
Expand All @@ -47,6 +47,22 @@ func (c *Client) GetACLEntries(i *ListACLEntriesInput) *ListPaginator[ACLEntry]
}, fmt.Sprintf(aclEntriesPath, i.ServiceID, i.ACLID))
}

// ListACLEntriesInput is the input parameter to ListACLEntries function.
type ListACLEntriesInput struct {
// ACLID is an alphanumeric string identifying a ACL (required).
ACLID string
// Direction is the direction in which to sort results.
Direction string
// Page is the current page.
Page int
// PerPage is the number of records per page.
PerPage int
Integralist marked this conversation as resolved.
Show resolved Hide resolved
// ServiceID is an alphanumeric string identifying the service (required).
ServiceID string
// Sort is the field on which to sort.
Sort string
}

// ListACLEntries retrieves all resources.
func (c *Client) ListACLEntries(i *ListACLEntriesInput) ([]*ACLEntry, error) {
if i.ACLID == "" {
Expand All @@ -55,7 +71,14 @@ func (c *Client) ListACLEntries(i *ListACLEntriesInput) ([]*ACLEntry, error) {
if i.ServiceID == "" {
return nil, ErrMissingServiceID
}
p := c.GetACLEntries(i)
p := c.GetACLEntries(&GetACLEntriesInput{
ACLID: i.ACLID,
Direction: i.Direction,
Page: i.Page,
PerPage: i.PerPage,
ServiceID: i.ServiceID,
Sort: i.Sort,
})
var results []*ACLEntry
for p.HasNext() {
data, err := p.GetNext()
Expand Down
5 changes: 2 additions & 3 deletions fastly/acl_entry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,13 @@ func TestClient_ACLEntries(t *testing.T) {
var es2 []*ACLEntry
var paginator *ListPaginator[ACLEntry]
record(t, fixtureBase+"list2", func(c *Client) {
listACLEntriesInput := &ListACLEntriesInput{
paginator = c.GetACLEntries(&GetACLEntriesInput{
ACLID: testACL.ID,
Direction: "ascend",
PerPage: 50,
ServiceID: testService.ID,
Sort: "ip",
}
paginator = c.GetACLEntries(listACLEntriesInput)
})

for paginator.HasNext() {
data, err := paginator.GetNext()
Expand Down
31 changes: 27 additions & 4 deletions fastly/dictionary_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ type DictionaryItem struct {
UpdatedAt *time.Time `mapstructure:"updated_at"`
}

// ListDictionaryItemsInput is used as input to the ListDictionaryItems function.
type ListDictionaryItemsInput struct {
// GetDictionaryItemsInput is used as input to the GetDictionaryItems function.
type GetDictionaryItemsInput struct {
// DictionaryID is the ID of the dictionary to retrieve items for (required).
DictionaryID string
// Direction is the direction in which to sort results.
Expand All @@ -36,7 +36,7 @@ type ListDictionaryItemsInput struct {
}

// GetDictionaryItems returns a ListPaginator for paginating through the resources.
func (c *Client) GetDictionaryItems(i *ListDictionaryItemsInput) *ListPaginator[DictionaryItem] {
func (c *Client) GetDictionaryItems(i *GetDictionaryItemsInput) *ListPaginator[DictionaryItem] {
return newPaginator[DictionaryItem](c, &listInput{
Direction: i.Direction,
Sort: i.Sort,
Expand All @@ -45,6 +45,22 @@ func (c *Client) GetDictionaryItems(i *ListDictionaryItemsInput) *ListPaginator[
}, fmt.Sprintf(dictionaryItemsPath, i.ServiceID, i.DictionaryID))
}

// ListDictionaryItemsInput is used as input to the ListDictionaryItems function.
type ListDictionaryItemsInput struct {
// DictionaryID is the ID of the dictionary to retrieve items for (required).
DictionaryID string
// Direction is the direction in which to sort results.
Direction string
// Page is the current page.
Page int
// PerPage is the number of records per page.
PerPage int
// ServiceID is the ID of the service (required).
ServiceID string
// Sort is the field on which to sort.
Sort string
}

// ListDictionaryItems retrieves all resources.
func (c *Client) ListDictionaryItems(i *ListDictionaryItemsInput) ([]*DictionaryItem, error) {
if i.DictionaryID == "" {
Expand All @@ -53,7 +69,14 @@ func (c *Client) ListDictionaryItems(i *ListDictionaryItemsInput) ([]*Dictionary
if i.ServiceID == "" {
return nil, ErrMissingServiceID
}
p := c.GetDictionaryItems(i)
p := c.GetDictionaryItems(&GetDictionaryItemsInput{
DictionaryID: i.DictionaryID,
Direction: i.Direction,
Page: i.Page,
PerPage: i.PerPage,
ServiceID: i.ServiceID,
Sort: i.Sort,
})
var results []*DictionaryItem
for p.HasNext() {
data, err := p.GetNext()
Expand Down
5 changes: 2 additions & 3 deletions fastly/dictionary_item_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,13 @@ func TestClient_DictionaryItems(t *testing.T) {
var dictionaryItems2 []*DictionaryItem
var paginator *ListPaginator[DictionaryItem]
record(t, fixtureBase+"list2", func(c *Client) {
listDictionaryItemsInput := &ListDictionaryItemsInput{
paginator = c.GetDictionaryItems(&GetDictionaryItemsInput{
DictionaryID: testDictionary.ID,
Direction: "ascend",
PerPage: 50,
ServiceID: testService.ID,
Sort: "item_key",
}
paginator = c.GetDictionaryItems(listDictionaryItemsInput)
})

for paginator.HasNext() {
data, err := paginator.GetNext()
Expand Down
25 changes: 21 additions & 4 deletions fastly/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ type ServiceDomain struct {
// ServiceDomainsList represents a list of service domains.
type ServiceDomainsList []*ServiceDomain

// ListServicesInput is used as input to the ListServices function.
type ListServicesInput struct {
// GetServicesInput is used as input to the GetServices function.
type GetServicesInput struct {
// Direction is the direction in which to sort results.
Direction string
// Page is the current page.
Expand All @@ -62,7 +62,7 @@ type ListServicesInput struct {
}

// GetServices returns a ListPaginator for paginating through the resources.
func (c *Client) GetServices(i *ListServicesInput) *ListPaginator[Service] {
func (c *Client) GetServices(i *GetServicesInput) *ListPaginator[Service] {
return newPaginator[Service](c, &listInput{
Direction: i.Direction,
Sort: i.Sort,
Expand All @@ -71,9 +71,26 @@ func (c *Client) GetServices(i *ListServicesInput) *ListPaginator[Service] {
}, "/service")
}

// ListServicesInput is used as input to the ListServices function.
type ListServicesInput struct {
// Direction is the direction in which to sort results.
Direction string
// Page is the current page.
Page int
// PerPage is the number of records per page.
PerPage int
// Sort is the field on which to sort.
Sort string
}

// ListServices retrieves all resources.
func (c *Client) ListServices(i *ListServicesInput) ([]*Service, error) {
p := c.GetServices(i)
p := c.GetServices(&GetServicesInput{
Direction: i.Direction,
Sort: i.Sort,
Page: i.Page,
PerPage: i.PerPage,
})
var results []*Service
for p.HasNext() {
data, err := p.GetNext()
Expand Down
5 changes: 2 additions & 3 deletions fastly/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,11 @@ func TestClient_Services(t *testing.T) {
var ss2 []*Service
var paginator *ListPaginator[Service]
record(t, "services/list_paginator", func(c *Client) {
listServicesInput := &ListServicesInput{
paginator = c.GetServices(&GetServicesInput{
Direction: "descend",
PerPage: 200,
Sort: "created",
}
paginator = c.GetServices(listServicesInput)
})

for paginator.HasNext() {
data, err := paginator.GetNext()
Expand Down
Loading