Skip to content

Commit

Permalink
Migrated account to account_logins to request helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikZilber committed Jun 20, 2024
1 parent 5ef1b79 commit 407dd8c
Show file tree
Hide file tree
Showing 14 changed files with 685 additions and 770 deletions.
5 changes: 2 additions & 3 deletions account.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,10 @@ type CreditCard struct {
// GetAccount gets the contact and billing information related to the Account.
func (c *Client) GetAccount(ctx context.Context) (*Account, error) {
e := "account"
req := c.R(ctx).SetResult(&Account{})
r, err := coupleAPIErrors(req.Get(e))
response, err := doGETRequest[Account](ctx, c, e)
if err != nil {
return nil, err
}

return r.Result().(*Account), nil
return response, nil
}
39 changes: 6 additions & 33 deletions account_availability.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ package linodego

import (
"context"
"fmt"
"net/url"

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

// AccountAvailability returns the resources availability in a region to an account.
Expand All @@ -20,46 +16,23 @@ type AccountAvailability struct {
Available []string `json:"available"`
}

// AccountAvailabilityPagedResponse represents a paginated Account Availability API response
type AccountAvailabilityPagedResponse struct {
*PageOptions
Data []AccountAvailability `json:"data"`
}

// endpoint gets the endpoint URL for AccountAvailability
func (AccountAvailabilityPagedResponse) endpoint(_ ...any) string {
return "/account/availability"
}

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

// ListAccountAvailabilities lists all regions and the resource availabilities to the account.
func (c *Client) ListAccountAvailabilities(ctx context.Context, opts *ListOptions) ([]AccountAvailability, error) {
response := AccountAvailabilityPagedResponse{}
err := c.listHelper(ctx, &response, opts)
response, err := getPaginatedResults[AccountAvailability](ctx, c, "account/availability", opts)
if err != nil {
return nil, err
}
return response.Data, nil

return response, nil
}

// GetAccountAvailability gets the resources availability in a region to the customer.
func (c *Client) GetAccountAvailability(ctx context.Context, regionID string) (*AccountAvailability, error) {
req := c.R(ctx).SetResult(&AccountAvailability{})
regionID = url.PathEscape(regionID)
b := fmt.Sprintf("account/availability/%s", regionID)
r, err := coupleAPIErrors(req.Get(b))
b := formatAPIPath("account/availability/%s", regionID)
response, err := doGETRequest[AccountAvailability](ctx, c, b)
if err != nil {
return nil, err
}

return r.Result().(*AccountAvailability), nil
return response, nil
}
49 changes: 9 additions & 40 deletions account_betas.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@ package linodego
import (
"context"
"encoding/json"
"fmt"
"net/url"
"time"

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

Expand All @@ -28,17 +25,6 @@ type AccountBetaProgramCreateOpts struct {
ID string `json:"id"`
}

// AccountBetasPagedResponse represents a paginated Account Beta Programs API response
type AccountBetasPagedResponse struct {
*PageOptions
Data []AccountBetaProgram `json:"data"`
}

// endpoint gets the endpoint URL for AccountBetaProgram
func (AccountBetasPagedResponse) endpoint(_ ...any) string {
return "/account/betas"
}

// UnmarshalJSON implements the json.Unmarshaler interface
func (cBeta *AccountBetaProgram) UnmarshalJSON(b []byte) error {
type Mask AccountBetaProgram
Expand All @@ -63,52 +49,35 @@ func (cBeta *AccountBetaProgram) UnmarshalJSON(b []byte) error {
return nil
}

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

// ListAccountBetaPrograms lists all beta programs an account is enrolled in.
func (c *Client) ListAccountBetaPrograms(ctx context.Context, opts *ListOptions) ([]AccountBetaProgram, error) {
response := AccountBetasPagedResponse{}
err := c.listHelper(ctx, &response, opts)
response, err := getPaginatedResults[AccountBetaProgram](ctx, c, "/account/betas", opts)
if err != nil {
return nil, err
}
return response.Data, nil

return response, nil
}

// GetAccountBetaProgram gets the details of a beta program an account is enrolled in.
func (c *Client) GetAccountBetaProgram(ctx context.Context, betaID string) (*AccountBetaProgram, error) {
req := c.R(ctx).SetResult(&AccountBetaProgram{})
betaID = url.PathEscape(betaID)
b := fmt.Sprintf("/account/betas/%s", betaID)
r, err := coupleAPIErrors(req.Get(b))
b := formatAPIPath("/account/betas/%s", betaID)

response, err := doGETRequest[AccountBetaProgram](ctx, c, b)
if err != nil {
return nil, err
}

return r.Result().(*AccountBetaProgram), nil
return response, nil
}

// JoinBetaProgram enrolls an account into a beta program.
func (c *Client) JoinBetaProgram(ctx context.Context, opts AccountBetaProgramCreateOpts) (*AccountBetaProgram, error) {
body, err := json.Marshal(opts)
if err != nil {
return nil, err
}

e := "account/betas"
req := c.R(ctx).SetResult(&AccountBetaProgram{}).SetBody(string(body))
r, err := coupleAPIErrors(req.Post(e))
response, err := doPOSTRequest[AccountBetaProgram](ctx, c, e, opts)
if err != nil {
return nil, err
}

return r.Result().(*AccountBetaProgram), nil
return response, nil
}
43 changes: 9 additions & 34 deletions account_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ package linodego
import (
"context"
"encoding/json"
"fmt"
"time"

"github.com/go-resty/resty/v2"
"github.com/linode/linodego/internal/duration"
"github.com/linode/linodego/internal/parseabletime"
)
Expand Down Expand Up @@ -270,17 +268,6 @@ type EventEntity struct {
URL string `json:"url"`
}

// EventsPagedResponse represents a paginated Events API response
type EventsPagedResponse struct {
*PageOptions
Data []Event `json:"data"`
}

// endpoint gets the endpoint URL for Event
func (EventsPagedResponse) endpoint(_ ...any) string {
return "account/events"
}

// UnmarshalJSON implements the json.Unmarshaler interface
func (i *Event) UnmarshalJSON(b []byte) error {
type Mask Event
Expand All @@ -303,51 +290,39 @@ func (i *Event) UnmarshalJSON(b []byte) error {
return nil
}

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
// on the Account. The Events returned depend on the token grants and the grants
// of the associated user.
func (c *Client) ListEvents(ctx context.Context, opts *ListOptions) ([]Event, error) {
response := EventsPagedResponse{}
err := c.listHelper(ctx, &response, opts)
response, err := getPaginatedResults[Event](ctx, c, "account/events", opts)
if err != nil {
return nil, err
}

return response.Data, nil
return response, nil
}

// GetEvent gets the Event with the Event ID
func (c *Client) GetEvent(ctx context.Context, eventID int) (*Event, error) {
req := c.R(ctx).SetResult(&Event{})
e := fmt.Sprintf("account/events/%d", eventID)
r, err := coupleAPIErrors(req.Get(e))
e := formatAPIPath("account/events/%d", eventID)
response, err := doGETRequest[Event](ctx, c, e)
if err != nil {
return nil, err
}

return r.Result().(*Event), nil
return response, nil
}

// MarkEventRead marks a single Event as read.
func (c *Client) MarkEventRead(ctx context.Context, event *Event) error {
e := fmt.Sprintf("account/events/%d/read", event.ID)
_, err := coupleAPIErrors(c.R(ctx).Post(e))
e := formatAPIPath("account/events/%d/read", event.ID)
_, err := doPOSTRequest[Event](ctx, c, e, []any{})
return err
}

// MarkEventsSeen marks all Events up to and including this Event by ID as seen.
func (c *Client) MarkEventsSeen(ctx context.Context, event *Event) error {
e := fmt.Sprintf("account/events/%d/seen", event.ID)
_, err := coupleAPIErrors(c.R(ctx).Post(e))
e := formatAPIPath("account/events/%d/seen", event.ID)
_, err := doPOSTRequest[Event](ctx, c, e, []any{})
return err
}
62 changes: 7 additions & 55 deletions account_invoices.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ package linodego
import (
"context"
"encoding/json"
"fmt"
"time"

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

Expand All @@ -31,36 +29,14 @@ type InvoiceItem struct {
To *time.Time `json:"-"`
}

// InvoicesPagedResponse represents a paginated Invoice API response
type InvoicesPagedResponse struct {
*PageOptions
Data []Invoice `json:"data"`
}

// endpoint gets the endpoint URL for Invoice
func (InvoicesPagedResponse) endpoint(_ ...any) string {
return "account/invoices"
}

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
func (c *Client) ListInvoices(ctx context.Context, opts *ListOptions) ([]Invoice, error) {
response := InvoicesPagedResponse{}
err := c.listHelper(ctx, &response, opts)
response, err := getPaginatedResults[Invoice](ctx, c, "account/invoices", opts)
if err != nil {
return nil, err
}

return response.Data, nil
return response, nil
}

// UnmarshalJSON implements the json.Unmarshaler interface
Expand Down Expand Up @@ -107,45 +83,21 @@ func (i *InvoiceItem) UnmarshalJSON(b []byte) error {

// GetInvoice gets a single Invoice matching the provided ID
func (c *Client) GetInvoice(ctx context.Context, invoiceID int) (*Invoice, error) {
req := c.R(ctx).SetResult(&Invoice{})
e := fmt.Sprintf("account/invoices/%d", invoiceID)
r, err := coupleAPIErrors(req.Get(e))
e := formatAPIPath("account/invoices/%d", invoiceID)
response, err := doGETRequest[Invoice](ctx, c, e)
if err != nil {
return nil, err
}

return r.Result().(*Invoice), nil
}

// InvoiceItemsPagedResponse represents a paginated Invoice Item API response
type InvoiceItemsPagedResponse struct {
*PageOptions
Data []InvoiceItem `json:"data"`
}

// endpoint gets the endpoint URL for InvoiceItems associated with a specific Invoice
func (InvoiceItemsPagedResponse) endpoint(ids ...any) string {
id := ids[0].(int)
return fmt.Sprintf("account/invoices/%d/items", id)
}

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
return response, nil
}

// ListInvoiceItems gets the invoice items associated with a specific Invoice
func (c *Client) ListInvoiceItems(ctx context.Context, invoiceID int, opts *ListOptions) ([]InvoiceItem, error) {
response := InvoiceItemsPagedResponse{}
err := c.listHelper(ctx, &response, opts, invoiceID)
response, err := getPaginatedResults[InvoiceItem](ctx, c, formatAPIPath("account/invoices/%d/items", invoiceID), opts)
if err != nil {
return nil, err
}

return response.Data, nil
return response, nil
}
Loading

0 comments on commit 407dd8c

Please sign in to comment.