Skip to content

Commit

Permalink
Use new helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
lgarber-akamai committed Feb 26, 2024
1 parent 386eb2a commit 11d1ad7
Showing 1 changed file with 17 additions and 48 deletions.
65 changes: 17 additions & 48 deletions account_child.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"
)

// ChildAccount represents an account under the current account.
Expand All @@ -17,60 +13,33 @@ type ChildAccount = Account
// NOTE: This is an alias to prevent any future breaking changes.
type ChildAccountToken = Token

// ChildAccountsPagedResponse represents a Linode API response
// for listing child accounts under the current account.
type ChildAccountsPagedResponse struct {
*PageOptions
Data []ChildAccount `json:"data"`
}

// endpoint returns the URL of this paginated endpoint
func (ChildAccountsPagedResponse) endpoint(_ ...any) string {
return "account/child-accounts"
}

func (resp *ChildAccountsPagedResponse) castResult(r *resty.Request, e string) (int, int, error) {
res, err := coupleAPIErrors(r.SetResult(ChildAccountsPagedResponse{}).Get(e))
if err != nil {
return 0, 0, err
}

castedRes := res.Result().(*ChildAccountsPagedResponse)
resp.Data = append(resp.Data, castedRes.Data...)
return castedRes.Pages, castedRes.Results, nil
}

// ListChildAccounts lists child accounts under the current account.
func (c *Client) ListChildAccounts(ctx context.Context, opts *ListOptions) ([]ChildAccount, error) {
response := ChildAccountsPagedResponse{}

err := c.listHelper(ctx, &response, opts)

return response.Data, err
return getPaginatedResults[ChildAccount](
ctx,
c,
"account/child-accounts",
opts,
)
}

// GetChildAccount gets a single child accounts under the current account.
func (c *Client) GetChildAccount(ctx context.Context, euuid string) (*ChildAccount, error) {
e := fmt.Sprintf("account/child-accounts/%s", url.PathEscape(euuid))
req := c.R(ctx).SetResult(ChildAccount{})
r, err := coupleAPIErrors(req.Get(e))
if err != nil {
return nil, err
}
return r.Result().(*ChildAccount), nil
return doGETRequest[ChildAccount](
ctx,
c,
formatAPIPath("account/child-accounts/%s", euuid),
)
}

// CreateChildAccountToken creates a short-lived token that can be used to
// access the Linode API under a child account.
// The attributes of this token are not currently configurable.
func (c *Client) CreateChildAccountToken(ctx context.Context, euuid string) (*ChildAccountToken, error) {
e := fmt.Sprintf("account/child-accounts/%s/token", url.PathEscape(euuid))

req := c.R(ctx).SetResult(&ChildAccountToken{})
r, err := coupleAPIErrors(req.Post(e))
if err != nil {
return nil, err
}

return r.Result().(*ChildAccountToken), nil
return doPOSTRequest[ChildAccountToken, any](
ctx,
c,
formatAPIPath("account/child-accounts/%s/token", euuid),
nil,
)
}

0 comments on commit 11d1ad7

Please sign in to comment.