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

Migrated account_notifications to account_user_grants to request helpers #528

Merged
Show file tree
Hide file tree
Changes from all commits
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
27 changes: 2 additions & 25 deletions account_notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"time"

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

Expand Down Expand Up @@ -57,40 +56,18 @@ const (
NotificationMaintenance NotificationType = "maintenance"
)

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

// endpoint gets the endpoint URL for Notification
func (NotificationsPagedResponse) endpoint(_ ...any) string {
return "account/notifications"
}

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,
// often time-sensitive items related to the Account. An account cannot interact directly with
// Notifications, and a Notification will disappear when the circumstances causing it
// have been resolved. For example, if the account has an important Ticket open, a response
// to the Ticket will dismiss the Notification.
func (c *Client) ListNotifications(ctx context.Context, opts *ListOptions) ([]Notification, error) {
response := NotificationsPagedResponse{}
err := c.listHelper(ctx, &response, opts)
response, err := getPaginatedResults[Notification](ctx, c, "account/notifications", opts)
if err != nil {
return nil, err
}

return response.Data, nil
return response, nil
}

// UnmarshalJSON implements the json.Unmarshaler interface
Expand Down
69 changes: 12 additions & 57 deletions account_oauth_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@ package linodego

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

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

// OAuthClientStatus constants start with OAuthClient and include Linode API Instance Status values
Expand Down Expand Up @@ -85,92 +80,52 @@ func (i OAuthClient) GetUpdateOptions() (o OAuthClientUpdateOptions) {
return
}

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

// endpoint gets the endpoint URL for OAuthClient
func (OAuthClientsPagedResponse) endpoint(_ ...any) string {
return "account/oauth-clients"
}

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

return response.Data, nil
return response, nil
}

// GetOAuthClient gets the OAuthClient with the provided ID
func (c *Client) GetOAuthClient(ctx context.Context, clientID string) (*OAuthClient, error) {
req := c.R(ctx).SetResult(&OAuthClient{})
clientID = url.PathEscape(clientID)
e := fmt.Sprintf("account/oauth-clients/%s", clientID)
r, err := coupleAPIErrors(req.Get(e))
e := formatAPIPath("account/oauth-clients/%s", clientID)
response, err := doGETRequest[OAuthClient](ctx, c, e)
if err != nil {
return nil, err
}

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

// CreateOAuthClient creates an OAuthClient
func (c *Client) CreateOAuthClient(ctx context.Context, opts OAuthClientCreateOptions) (*OAuthClient, error) {
body, err := json.Marshal(opts)
if err != nil {
return nil, err
}

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

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

// UpdateOAuthClient updates the OAuthClient with the specified id
func (c *Client) UpdateOAuthClient(ctx context.Context, clientID string, opts OAuthClientUpdateOptions) (*OAuthClient, error) {
body, err := json.Marshal(opts)
if err != nil {
return nil, err
}

req := c.R(ctx).SetResult(&OAuthClient{}).SetBody(string(body))

clientID = url.PathEscape(clientID)

e := fmt.Sprintf("account/oauth-clients/%s", clientID)
r, err := coupleAPIErrors(req.Put(e))
e := formatAPIPath("account/oauth-clients/%s", clientID)
response, err := doPUTRequest[OAuthClient](ctx, c, e, opts)
if err != nil {
return nil, err
}

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

// DeleteOAuthClient deletes the OAuthClient with the specified id
func (c *Client) DeleteOAuthClient(ctx context.Context, clientID string) error {
clientID = url.PathEscape(clientID)
e := fmt.Sprintf("account/oauth-clients/%s", clientID)
_, err := coupleAPIErrors(c.R(ctx).Delete(e))
e := formatAPIPath("account/oauth-clients/%s", clientID)
err := doDELETERequest(ctx, c, e)
return err
}
45 changes: 7 additions & 38 deletions account_payments.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 Down Expand Up @@ -57,63 +55,34 @@ func (i Payment) GetCreateOptions() (o PaymentCreateOptions) {
return
}

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

// endpoint gets the endpoint URL for Payment
func (PaymentsPagedResponse) endpoint(_ ...any) string {
return "account/payments"
}

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

return response.Data, nil
return response, nil
}

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

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

// CreatePayment creates a Payment
func (c *Client) CreatePayment(ctx context.Context, opts PaymentCreateOptions) (*Payment, error) {
body, err := json.Marshal(opts)
if err != nil {
return nil, err
}

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

return r.Result().(*Payment), nil
return response, nil
}
18 changes: 6 additions & 12 deletions account_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package linodego

import (
"context"
"encoding/json"
)

// AccountSettings are the account wide flags or plans that effect new resources
Expand Down Expand Up @@ -38,29 +37,24 @@ type AccountSettingsUpdateOptions struct {

// GetAccountSettings gets the account wide flags or plans that effect new resources
func (c *Client) GetAccountSettings(ctx context.Context) (*AccountSettings, error) {
req := c.R(ctx).SetResult(&AccountSettings{})
e := "account/settings"
r, err := coupleAPIErrors(req.Get(e))

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

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

// UpdateAccountSettings updates the settings associated with the account
func (c *Client) UpdateAccountSettings(ctx context.Context, opts AccountSettingsUpdateOptions) (*AccountSettings, error) {
body, err := json.Marshal(opts)
if err != nil {
return nil, err
}

req := c.R(ctx).SetResult(&AccountSettings{}).SetBody(string(body))
e := "account/settings"
r, err := coupleAPIErrors(req.Put(e))

response, err := doPUTRequest[AccountSettings](ctx, c, e, opts)
if err != nil {
return nil, err
}

return r.Result().(*AccountSettings), nil
return response, nil
}
6 changes: 3 additions & 3 deletions account_transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ type AccountTransferRegion struct {

// GetAccountTransfer gets current Account's network utilization for the current month.
func (c *Client) GetAccountTransfer(ctx context.Context) (*AccountTransfer, error) {
req := c.R(ctx).SetResult(&AccountTransfer{})
e := "account/transfer"
r, err := coupleAPIErrors(req.Get(e))

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

return r.Result().(*AccountTransfer), nil
return response, nil
}
24 changes: 6 additions & 18 deletions account_user_grants.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ package linodego

import (
"context"
"encoding/json"
"fmt"
"net/url"
)

type GrantPermissionLevel string
Expand Down Expand Up @@ -69,30 +66,21 @@ type UserGrantsUpdateOptions struct {
}

func (c *Client) GetUserGrants(ctx context.Context, username string) (*UserGrants, error) {
username = url.PathEscape(username)
e := fmt.Sprintf("account/users/%s/grants", username)
req := c.R(ctx).SetResult(&UserGrants{})
r, err := coupleAPIErrors(req.Get(e))
e := formatAPIPath("account/users/%s/grants", username)
response, err := doGETRequest[UserGrants](ctx, c, e)
if err != nil {
return nil, err
}

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

func (c *Client) UpdateUserGrants(ctx context.Context, username string, opts UserGrantsUpdateOptions) (*UserGrants, error) {
body, err := json.Marshal(opts)
e := formatAPIPath("account/users/%s/grants", username)
response, err := doPUTRequest[UserGrants](ctx, c, e, opts)
if err != nil {
return nil, err
}

username = url.PathEscape(username)
e := fmt.Sprintf("account/users/%s/grants", username)
req := c.R(ctx).SetResult(&UserGrants{}).SetBody(string(body))
r, err := coupleAPIErrors(req.Put(e))
if err != nil {
return nil, err
}

return r.Result().(*UserGrants), nil
return response, nil
}
Loading