Skip to content

Commit 11d1ad7

Browse files
Use new helpers
1 parent 386eb2a commit 11d1ad7

File tree

1 file changed

+17
-48
lines changed

1 file changed

+17
-48
lines changed

account_child.go

Lines changed: 17 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@ package linodego
22

33
import (
44
"context"
5-
"fmt"
6-
"net/url"
7-
8-
"github.com/go-resty/resty/v2"
95
)
106

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

20-
// ChildAccountsPagedResponse represents a Linode API response
21-
// for listing child accounts under the current account.
22-
type ChildAccountsPagedResponse struct {
23-
*PageOptions
24-
Data []ChildAccount `json:"data"`
25-
}
26-
27-
// endpoint returns the URL of this paginated endpoint
28-
func (ChildAccountsPagedResponse) endpoint(_ ...any) string {
29-
return "account/child-accounts"
30-
}
31-
32-
func (resp *ChildAccountsPagedResponse) castResult(r *resty.Request, e string) (int, int, error) {
33-
res, err := coupleAPIErrors(r.SetResult(ChildAccountsPagedResponse{}).Get(e))
34-
if err != nil {
35-
return 0, 0, err
36-
}
37-
38-
castedRes := res.Result().(*ChildAccountsPagedResponse)
39-
resp.Data = append(resp.Data, castedRes.Data...)
40-
return castedRes.Pages, castedRes.Results, nil
41-
}
42-
4316
// ListChildAccounts lists child accounts under the current account.
4417
func (c *Client) ListChildAccounts(ctx context.Context, opts *ListOptions) ([]ChildAccount, error) {
45-
response := ChildAccountsPagedResponse{}
46-
47-
err := c.listHelper(ctx, &response, opts)
48-
49-
return response.Data, err
18+
return getPaginatedResults[ChildAccount](
19+
ctx,
20+
c,
21+
"account/child-accounts",
22+
opts,
23+
)
5024
}
5125

5226
// GetChildAccount gets a single child accounts under the current account.
5327
func (c *Client) GetChildAccount(ctx context.Context, euuid string) (*ChildAccount, error) {
54-
e := fmt.Sprintf("account/child-accounts/%s", url.PathEscape(euuid))
55-
req := c.R(ctx).SetResult(ChildAccount{})
56-
r, err := coupleAPIErrors(req.Get(e))
57-
if err != nil {
58-
return nil, err
59-
}
60-
return r.Result().(*ChildAccount), nil
28+
return doGETRequest[ChildAccount](
29+
ctx,
30+
c,
31+
formatAPIPath("account/child-accounts/%s", euuid),
32+
)
6133
}
6234

6335
// CreateChildAccountToken creates a short-lived token that can be used to
6436
// access the Linode API under a child account.
6537
// The attributes of this token are not currently configurable.
6638
func (c *Client) CreateChildAccountToken(ctx context.Context, euuid string) (*ChildAccountToken, error) {
67-
e := fmt.Sprintf("account/child-accounts/%s/token", url.PathEscape(euuid))
68-
69-
req := c.R(ctx).SetResult(&ChildAccountToken{})
70-
r, err := coupleAPIErrors(req.Post(e))
71-
if err != nil {
72-
return nil, err
73-
}
74-
75-
return r.Result().(*ChildAccountToken), nil
39+
return doPOSTRequest[ChildAccountToken, any](
40+
ctx,
41+
c,
42+
formatAPIPath("account/child-accounts/%s/token", euuid),
43+
nil,
44+
)
7645
}

0 commit comments

Comments
 (0)