Skip to content

Commit

Permalink
Pagination Rewrite (#268)
Browse files Browse the repository at this point in the history
  • Loading branch information
jriddle-linode authored Aug 18, 2022
1 parent 4783c42 commit 38b1365
Show file tree
Hide file tree
Showing 186 changed files with 3,168 additions and 3,114 deletions.
24 changes: 15 additions & 9 deletions account_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"time"

"github.com/go-resty/resty/v2"
"github.com/linode/linodego/internal/duration"
"github.com/linode/linodego/internal/parseabletime"
)
Expand Down Expand Up @@ -180,11 +181,11 @@ const (
// can be used to access it.
type EventEntity struct {
// ID may be a string or int, it depends on the EntityType
ID interface{} `json:"id"`
Label string `json:"label"`
Type EntityType `json:"type"`
Status string `json:"status"`
URL string `json:"url"`
ID any `json:"id"`
Label string `json:"label"`
Type EntityType `json:"type"`
Status string `json:"status"`
URL string `json:"url"`
}

// EventsPagedResponse represents a paginated Events API response
Expand All @@ -194,7 +195,7 @@ type EventsPagedResponse struct {
}

// endpoint gets the endpoint URL for Event
func (EventsPagedResponse) endpoint(c *Client) string {
func (EventsPagedResponse) endpoint(c *Client, _ ...any) string {
endpoint, err := c.Events.Endpoint()
if err != nil {
panic(err)
Expand Down Expand Up @@ -237,9 +238,14 @@ func (i Event) endpointWithID(c *Client) string {
return endpoint
}

// appendData appends Events when processing paginated Event responses
func (resp *EventsPagedResponse) appendData(r *EventsPagedResponse) {
resp.Data = append(resp.Data, r.Data...)
func (resp *EventsPagedResponse) castResult(r *resty.Request, e string) (int, int, error) {
res, err := coupleAPIErrors(r.SetResult(EventsPagedResponse{}).Get(e))
if err != nil {
return 0, 0, err
}
castedRes := res.Result().(*EventsPagedResponse)
resp.Data = append(resp.Data, castedRes.Data...)
return castedRes.Pages, castedRes.Results, nil
}

// ListEvents gets a collection of Event objects representing actions taken
Expand Down
30 changes: 21 additions & 9 deletions account_invoices.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"time"

"github.com/go-resty/resty/v2"
"github.com/linode/linodego/internal/parseabletime"
)

Expand Down Expand Up @@ -35,7 +36,7 @@ type InvoicesPagedResponse struct {
}

// endpoint gets the endpoint URL for Invoice
func (InvoicesPagedResponse) endpoint(c *Client) string {
func (InvoicesPagedResponse) endpoint(c *Client, _ ...any) string {
endpoint, err := c.Invoices.Endpoint()
if err != nil {
panic(err)
Expand All @@ -44,9 +45,14 @@ func (InvoicesPagedResponse) endpoint(c *Client) string {
return endpoint
}

// appendData appends Invoices when processing paginated Invoice responses
func (resp *InvoicesPagedResponse) appendData(r *InvoicesPagedResponse) {
resp.Data = append(resp.Data, r.Data...)
func (resp *InvoicesPagedResponse) castResult(r *resty.Request, e string) (int, int, error) {
res, err := coupleAPIErrors(r.SetResult(InvoicesPagedResponse{}).Get(e))
if err != nil {
return 0, 0, err
}
castedRes := res.Result().(*InvoicesPagedResponse)
resp.Data = append(resp.Data, castedRes.Data...)
return castedRes.Pages, castedRes.Results, nil
}

// ListInvoices gets a paginated list of Invoices against the Account
Expand Down Expand Up @@ -125,7 +131,8 @@ type InvoiceItemsPagedResponse struct {
}

// endpointWithID gets the endpoint URL for InvoiceItems associated with a specific Invoice
func (InvoiceItemsPagedResponse) endpointWithID(c *Client, id int) string {
func (InvoiceItemsPagedResponse) endpoint(c *Client, ids ...any) string {
id := ids[0].(int)
endpoint, err := c.InvoiceItems.endpointWithParams(id)
if err != nil {
panic(err)
Expand All @@ -134,15 +141,20 @@ func (InvoiceItemsPagedResponse) endpointWithID(c *Client, id int) string {
return endpoint
}

// appendData appends InvoiceItems when processing paginated Invoice Item responses
func (resp *InvoiceItemsPagedResponse) appendData(r *InvoiceItemsPagedResponse) {
resp.Data = append(resp.Data, r.Data...)
func (resp *InvoiceItemsPagedResponse) castResult(r *resty.Request, e string) (int, int, error) {
res, err := coupleAPIErrors(r.SetResult(InvoiceItemsPagedResponse{}).Get(e))
if err != nil {
return 0, 0, err
}
castedRes := res.Result().(*InvoiceItemsPagedResponse)
resp.Data = append(resp.Data, castedRes.Data...)
return castedRes.Pages, castedRes.Results, nil
}

// ListInvoiceItems gets the invoice items associated with a specific Invoice
func (c *Client) ListInvoiceItems(ctx context.Context, id int, opts *ListOptions) ([]InvoiceItem, error) {
response := InvoiceItemsPagedResponse{}
err := c.listHelperWithID(ctx, &response, id, opts)
err := c.listHelper(ctx, &response, opts, id)
if err != nil {
return nil, err
}
Expand Down
14 changes: 10 additions & 4 deletions account_notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"time"

"github.com/go-resty/resty/v2"
"github.com/linode/linodego/internal/parseabletime"
)

Expand Down Expand Up @@ -63,7 +64,7 @@ type NotificationsPagedResponse struct {
}

// endpoint gets the endpoint URL for Notification
func (NotificationsPagedResponse) endpoint(c *Client) string {
func (NotificationsPagedResponse) endpoint(c *Client, _ ...any) string {
endpoint, err := c.Notifications.Endpoint()
if err != nil {
panic(err)
Expand All @@ -72,9 +73,14 @@ func (NotificationsPagedResponse) endpoint(c *Client) string {
return endpoint
}

// appendData appends Notifications when processing paginated Notification responses
func (resp *NotificationsPagedResponse) appendData(r *NotificationsPagedResponse) {
resp.Data = append(resp.Data, r.Data...)
func (resp *NotificationsPagedResponse) castResult(r *resty.Request, e string) (int, int, error) {
res, err := coupleAPIErrors(r.SetResult(NotificationsPagedResponse{}).Get(e))
if err != nil {
return 0, 0, err
}
castedRes := res.Result().(*NotificationsPagedResponse)
resp.Data = append(resp.Data, castedRes.Data...)
return castedRes.Pages, castedRes.Results, nil
}

// ListNotifications gets a collection of Notification objects representing important,
Expand Down
15 changes: 11 additions & 4 deletions account_oauth_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"encoding/json"
"fmt"

"github.com/go-resty/resty/v2"
)

// OAuthClientStatus constants start with OAuthClient and include Linode API Instance Status values
Expand Down Expand Up @@ -89,7 +91,7 @@ type OAuthClientsPagedResponse struct {
}

// endpoint gets the endpoint URL for OAuthClient
func (OAuthClientsPagedResponse) endpoint(c *Client) string {
func (OAuthClientsPagedResponse) endpoint(c *Client, _ ...any) string {
endpoint, err := c.OAuthClients.Endpoint()
if err != nil {
panic(err)
Expand All @@ -98,9 +100,14 @@ func (OAuthClientsPagedResponse) endpoint(c *Client) string {
return endpoint
}

// appendData appends OAuthClients when processing paginated OAuthClient responses
func (resp *OAuthClientsPagedResponse) appendData(r *OAuthClientsPagedResponse) {
resp.Data = append(resp.Data, r.Data...)
func (resp *OAuthClientsPagedResponse) castResult(r *resty.Request, e string) (int, int, error) {
res, err := coupleAPIErrors(r.SetResult(OAuthClientsPagedResponse{}).Get(e))
if err != nil {
return 0, 0, err
}
castedRes := res.Result().(*OAuthClientsPagedResponse)
resp.Data = append(resp.Data, castedRes.Data...)
return castedRes.Pages, castedRes.Results, nil
}

// ListOAuthClients lists OAuthClients
Expand Down
14 changes: 10 additions & 4 deletions account_payments.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"time"

"github.com/go-resty/resty/v2"
"github.com/linode/linodego/internal/parseabletime"
)

Expand Down Expand Up @@ -63,7 +64,7 @@ type PaymentsPagedResponse struct {
}

// endpoint gets the endpoint URL for Payment
func (PaymentsPagedResponse) endpoint(c *Client) string {
func (PaymentsPagedResponse) endpoint(c *Client, _ ...any) string {
endpoint, err := c.Payments.Endpoint()
if err != nil {
panic(err)
Expand All @@ -72,9 +73,14 @@ func (PaymentsPagedResponse) endpoint(c *Client) string {
return endpoint
}

// appendData appends Payments when processing paginated Payment responses
func (resp *PaymentsPagedResponse) appendData(r *PaymentsPagedResponse) {
resp.Data = append(resp.Data, r.Data...)
func (resp *PaymentsPagedResponse) castResult(r *resty.Request, e string) (int, int, error) {
res, err := coupleAPIErrors(r.SetResult(PaymentsPagedResponse{}).Get(e))
if err != nil {
return 0, 0, err
}
castedRes := res.Result().(*PaymentsPagedResponse)
resp.Data = append(resp.Data, castedRes.Data...)
return castedRes.Pages, castedRes.Results, nil
}

// ListPayments lists Payments
Expand Down
15 changes: 11 additions & 4 deletions account_users.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"encoding/json"
"fmt"

"github.com/go-resty/resty/v2"
)

// User represents a User object
Expand Down Expand Up @@ -52,7 +54,7 @@ type UsersPagedResponse struct {
}

// endpoint gets the endpoint URL for User
func (UsersPagedResponse) endpoint(c *Client) string {
func (UsersPagedResponse) endpoint(c *Client, _ ...any) string {
endpoint, err := c.Users.Endpoint()
if err != nil {
panic(err)
Expand All @@ -61,9 +63,14 @@ func (UsersPagedResponse) endpoint(c *Client) string {
return endpoint
}

// appendData appends Users when processing paginated User responses
func (resp *UsersPagedResponse) appendData(r *UsersPagedResponse) {
resp.Data = append(resp.Data, r.Data...)
func (resp *UsersPagedResponse) castResult(r *resty.Request, e string) (int, int, error) {
res, err := coupleAPIErrors(r.SetResult(UsersPagedResponse{}).Get(e))
if err != nil {
return 0, 0, err
}
castedRes := res.Result().(*UsersPagedResponse)
resp.Data = append(resp.Data, castedRes.Data...)
return castedRes.Pages, castedRes.Results, nil
}

// ListUsers lists Users on the account
Expand Down
37 changes: 28 additions & 9 deletions databases.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"time"

"github.com/go-resty/resty/v2"
"github.com/linode/linodego/internal/parseabletime"
)

Expand Down Expand Up @@ -57,50 +58,68 @@ type DatabasesPagedResponse struct {
Data []Database `json:"data"`
}

func (DatabasesPagedResponse) endpoint(c *Client) string {
func (DatabasesPagedResponse) endpoint(c *Client, _ ...any) string {
endpoint, err := c.Databases.Endpoint()
if err != nil {
panic(err)
}
return fmt.Sprintf("%s/instances", endpoint)
}

func (resp *DatabasesPagedResponse) appendData(r *DatabasesPagedResponse) {
resp.Data = append(resp.Data, r.Data...)
func (resp *DatabasesPagedResponse) castResult(r *resty.Request, e string) (int, int, error) {
res, err := coupleAPIErrors(r.SetResult(DatabasesPagedResponse{}).Get(e))
if err != nil {
return 0, 0, err
}
castedRes := res.Result().(*DatabasesPagedResponse)
resp.Data = append(resp.Data, castedRes.Data...)
return castedRes.Pages, castedRes.Results, nil
}

type DatabaseEnginesPagedResponse struct {
*PageOptions
Data []DatabaseEngine `json:"data"`
}

func (DatabaseEnginesPagedResponse) endpoint(c *Client) string {
func (DatabaseEnginesPagedResponse) endpoint(c *Client, _ ...any) string {
endpoint, err := c.Databases.Endpoint()
if err != nil {
panic(err)
}
return fmt.Sprintf("%s/engines", endpoint)
}

func (resp *DatabaseEnginesPagedResponse) appendData(r *DatabaseEnginesPagedResponse) {
resp.Data = append(resp.Data, r.Data...)
func (resp *DatabaseEnginesPagedResponse) castResult(r *resty.Request, e string) (int, int, error) {
res, err := coupleAPIErrors(r.SetResult(DatabaseEnginesPagedResponse{}).Get(e))
if err != nil {
return 0, 0, err
}
castedRes := res.Result().(*DatabaseEnginesPagedResponse)
resp.Data = append(resp.Data, castedRes.Data...)
return castedRes.Pages, castedRes.Results, nil
}

type DatabaseTypesPagedResponse struct {
*PageOptions
Data []DatabaseType `json:"data"`
}

func (DatabaseTypesPagedResponse) endpoint(c *Client) string {
func (DatabaseTypesPagedResponse) endpoint(c *Client, _ ...any) string {
endpoint, err := c.Databases.Endpoint()
if err != nil {
panic(err)
}
return fmt.Sprintf("%s/types", endpoint)
}

func (resp *DatabaseTypesPagedResponse) appendData(r *DatabaseTypesPagedResponse) {
resp.Data = append(resp.Data, r.Data...)
func (resp *DatabaseTypesPagedResponse) castResult(r *resty.Request, e string) (int, int, error) {
res, err := coupleAPIErrors(r.SetResult(DatabaseTypesPagedResponse{}).Get(e))
if err != nil {
return 0, 0, err
}
castedRes := res.Result().(*DatabaseTypesPagedResponse)
resp.Data = append(resp.Data, castedRes.Data...)
return castedRes.Pages, castedRes.Results, nil
}

// A Database is a instance of Linode Managed Databases
Expand Down
18 changes: 13 additions & 5 deletions domain_records.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"encoding/json"
"fmt"

"github.com/go-resty/resty/v2"
)

// DomainRecord represents a DomainRecord object
Expand Down Expand Up @@ -88,7 +90,8 @@ type DomainRecordsPagedResponse struct {
}

// endpoint gets the endpoint URL for InstanceConfig
func (DomainRecordsPagedResponse) endpointWithID(c *Client, id int) string {
func (DomainRecordsPagedResponse) endpoint(c *Client, ids ...any) string {
id, _ := ids[0].(int)
endpoint, err := c.DomainRecords.endpointWithParams(id)
if err != nil {
panic(err)
Expand All @@ -97,15 +100,20 @@ func (DomainRecordsPagedResponse) endpointWithID(c *Client, id int) string {
return endpoint
}

// appendData appends DomainRecords when processing paginated DomainRecord responses
func (resp *DomainRecordsPagedResponse) appendData(r *DomainRecordsPagedResponse) {
resp.Data = append(resp.Data, r.Data...)
func (resp *DomainRecordsPagedResponse) castResult(r *resty.Request, e string) (int, int, error) {
res, err := coupleAPIErrors(r.SetResult(DomainRecordsPagedResponse{}).Get(e))
if err != nil {
return 0, 0, err
}
castedRes := res.Result().(*DomainRecordsPagedResponse)
resp.Data = append(resp.Data, castedRes.Data...)
return castedRes.Pages, castedRes.Results, nil
}

// ListDomainRecords lists DomainRecords
func (c *Client) ListDomainRecords(ctx context.Context, domainID int, opts *ListOptions) ([]DomainRecord, error) {
response := DomainRecordsPagedResponse{}
err := c.listHelperWithID(ctx, &response, domainID, opts)
err := c.listHelper(ctx, &response, opts, domainID)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit 38b1365

Please sign in to comment.