Skip to content

Commit

Permalink
Remove Resource Factory (linode#274)
Browse files Browse the repository at this point in the history
removed resource factory from client that was used to generate endpoint urls
moved URLs to strings and sprints
reformatted some code to simplify and make all the files more similar
  • Loading branch information
jriddle-linode authored Oct 5, 2022
1 parent 6dd24b7 commit d72adb3
Show file tree
Hide file tree
Showing 231 changed files with 19,726 additions and 12,126 deletions.
8 changes: 8 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ linters-settings:
linters:
enable-all: true
disable:
- bodyclose
- contextcheck
- nilerr
- noctx
- rowserrcheck
- sqlclosecheck
- structcheck
- tparallel
- vetshadow
- errname
- forcetypeassert
Expand Down
9 changes: 3 additions & 6 deletions account.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,9 @@ type CreditCard struct {

// GetAccount gets the contact and billing information related to the Account.
func (c *Client) GetAccount(ctx context.Context) (*Account, error) {
e, err := c.Account.Endpoint()
if err != nil {
return nil, err
}

r, err := coupleAPIErrors(c.R(ctx).SetResult(&Account{}).Get(e))
e := "account"
req := c.R(ctx).SetResult(&Account{})
r, err := coupleAPIErrors(req.Get(e))
if err != nil {
return nil, err
}
Expand Down
43 changes: 8 additions & 35 deletions account_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,13 +195,8 @@ type EventsPagedResponse struct {
}

// endpoint gets the endpoint URL for Event
func (EventsPagedResponse) endpoint(c *Client, _ ...any) string {
endpoint, err := c.Events.Endpoint()
if err != nil {
panic(err)
}

return endpoint
func (EventsPagedResponse) endpoint(_ ...any) string {
return "account/events"
}

// UnmarshalJSON implements the json.Unmarshaler interface
Expand All @@ -226,18 +221,6 @@ func (i *Event) UnmarshalJSON(b []byte) error {
return nil
}

// endpointWithID gets the endpoint URL for a specific Event
func (i Event) endpointWithID(c *Client) string {
endpoint, err := c.Events.Endpoint()
if err != nil {
panic(err)
}

endpoint = fmt.Sprintf("%s/%d", endpoint, i.ID)

return endpoint
}

func (resp *EventsPagedResponse) castResult(r *resty.Request, e string) (int, int, error) {
res, err := coupleAPIErrors(r.SetResult(EventsPagedResponse{}).Get(e))
if err != nil {
Expand All @@ -262,14 +245,10 @@ func (c *Client) ListEvents(ctx context.Context, opts *ListOptions) ([]Event, er
}

// GetEvent gets the Event with the Event ID
func (c *Client) GetEvent(ctx context.Context, id int) (*Event, error) {
e, err := c.Events.Endpoint()
if err != nil {
return nil, err
}

e = fmt.Sprintf("%s/%d", e, id)
r, err := c.R(ctx).SetResult(&Event{}).Get(e)
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))
if err != nil {
return nil, err
}
Expand All @@ -279,20 +258,14 @@ func (c *Client) GetEvent(ctx context.Context, id int) (*Event, error) {

// MarkEventRead marks a single Event as read.
func (c *Client) MarkEventRead(ctx context.Context, event *Event) error {
e := event.endpointWithID(c)
e = fmt.Sprintf("%s/read", e)

e := fmt.Sprintf("account/events/%d/read", event.ID)
_, err := coupleAPIErrors(c.R(ctx).Post(e))

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 := event.endpointWithID(c)
e = fmt.Sprintf("%s/seen", e)

e := fmt.Sprintf("account/events/%d/seen", event.ID)
_, err := coupleAPIErrors(c.R(ctx).Post(e))

return err
}
36 changes: 11 additions & 25 deletions account_invoices.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,8 @@ type InvoicesPagedResponse struct {
}

// endpoint gets the endpoint URL for Invoice
func (InvoicesPagedResponse) endpoint(c *Client, _ ...any) string {
endpoint, err := c.Invoices.Endpoint()
if err != nil {
panic(err)
}

return endpoint
func (InvoicesPagedResponse) endpoint(_ ...any) string {
return "account/invoices"
}

func (resp *InvoicesPagedResponse) castResult(r *resty.Request, e string) (int, int, error) {
Expand Down Expand Up @@ -109,14 +104,10 @@ func (i *InvoiceItem) UnmarshalJSON(b []byte) error {
}

// GetInvoice gets the a single Invoice matching the provided ID
func (c *Client) GetInvoice(ctx context.Context, id int) (*Invoice, error) {
e, err := c.Invoices.Endpoint()
if err != nil {
return nil, err
}

e = fmt.Sprintf("%s/%d", e, id)
r, err := coupleAPIErrors(c.R(ctx).SetResult(&Invoice{}).Get(e))
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))
if err != nil {
return nil, err
}
Expand All @@ -130,15 +121,10 @@ type InvoiceItemsPagedResponse struct {
Data []InvoiceItem `json:"data"`
}

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

return endpoint
return fmt.Sprintf("account/invoices/%d/items", id)
}

func (resp *InvoiceItemsPagedResponse) castResult(r *resty.Request, e string) (int, int, error) {
Expand All @@ -152,9 +138,9 @@ func (resp *InvoiceItemsPagedResponse) castResult(r *resty.Request, e string) (i
}

// ListInvoiceItems gets the invoice items associated with a specific Invoice
func (c *Client) ListInvoiceItems(ctx context.Context, id int, opts *ListOptions) ([]InvoiceItem, error) {
func (c *Client) ListInvoiceItems(ctx context.Context, invoiceID int, opts *ListOptions) ([]InvoiceItem, error) {
response := InvoiceItemsPagedResponse{}
err := c.listHelper(ctx, &response, opts, id)
err := c.listHelper(ctx, &response, opts, invoiceID)
if err != nil {
return nil, err
}
Expand Down
9 changes: 2 additions & 7 deletions account_notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,8 @@ type NotificationsPagedResponse struct {
}

// endpoint gets the endpoint URL for Notification
func (NotificationsPagedResponse) endpoint(c *Client, _ ...any) string {
endpoint, err := c.Notifications.Endpoint()
if err != nil {
panic(err)
}

return endpoint
func (NotificationsPagedResponse) endpoint(_ ...any) string {
return "account/notifications"
}

func (resp *NotificationsPagedResponse) castResult(r *resty.Request, e string) (int, int, error) {
Expand Down
76 changes: 19 additions & 57 deletions account_oauth_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,8 @@ type OAuthClientsPagedResponse struct {
}

// endpoint gets the endpoint URL for OAuthClient
func (OAuthClientsPagedResponse) endpoint(c *Client, _ ...any) string {
endpoint, err := c.OAuthClients.Endpoint()
if err != nil {
panic(err)
}

return endpoint
func (OAuthClientsPagedResponse) endpoint(_ ...any) string {
return "account/oauth-clients"
}

func (resp *OAuthClientsPagedResponse) castResult(r *resty.Request, e string) (int, int, error) {
Expand All @@ -122,14 +117,10 @@ func (c *Client) ListOAuthClients(ctx context.Context, opts *ListOptions) ([]OAu
}

// GetOAuthClient gets the OAuthClient with the provided ID
func (c *Client) GetOAuthClient(ctx context.Context, id string) (*OAuthClient, error) {
e, err := c.OAuthClients.Endpoint()
if err != nil {
return nil, err
}

e = fmt.Sprintf("%s/%s", e, id)
r, err := coupleAPIErrors(c.R(ctx).SetResult(&OAuthClient{}).Get(e))
func (c *Client) GetOAuthClient(ctx context.Context, clientID string) (*OAuthClient, error) {
req := c.R(ctx).SetResult(&OAuthClient{})
e := fmt.Sprintf("account/oauth-clients/%s", clientID)
r, err := coupleAPIErrors(req.Get(e))
if err != nil {
return nil, err
}
Expand All @@ -138,25 +129,15 @@ func (c *Client) GetOAuthClient(ctx context.Context, id string) (*OAuthClient, e
}

// CreateOAuthClient creates an OAuthClient
func (c *Client) CreateOAuthClient(ctx context.Context, createOpts OAuthClientCreateOptions) (*OAuthClient, error) {
var body string

e, err := c.OAuthClients.Endpoint()
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{})

if bodyData, err := json.Marshal(createOpts); err == nil {
body = string(bodyData)
} else {
return nil, NewError(err)
}

r, err := coupleAPIErrors(req.
SetBody(body).
Post(e))
req := c.R(ctx).SetResult(&OAuthClient{}).SetBody(string(body))
e := "account/oauth-clients"
r, err := coupleAPIErrors(req.Post(e))
if err != nil {
return nil, err
}
Expand All @@ -165,27 +146,15 @@ func (c *Client) CreateOAuthClient(ctx context.Context, createOpts OAuthClientCr
}

// UpdateOAuthClient updates the OAuthClient with the specified id
func (c *Client) UpdateOAuthClient(ctx context.Context, id string, updateOpts OAuthClientUpdateOptions) (*OAuthClient, error) {
var body string

e, err := c.OAuthClients.Endpoint()
func (c *Client) UpdateOAuthClient(ctx context.Context, clientID string, opts OAuthClientUpdateOptions) (*OAuthClient, error) {
body, err := json.Marshal(opts)
if err != nil {
return nil, err
}

e = fmt.Sprintf("%s/%s", e, id)

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

if bodyData, err := json.Marshal(updateOpts); err == nil {
body = string(bodyData)
} else {
return nil, NewError(err)
}

r, err := coupleAPIErrors(req.
SetBody(body).
Put(e))
req := c.R(ctx).SetResult(&OAuthClient{}).SetBody(string(body))
e := fmt.Sprintf("account/oauth-clients/%s", clientID)
r, err := coupleAPIErrors(req.Put(e))
if err != nil {
return nil, err
}
Expand All @@ -194,15 +163,8 @@ func (c *Client) UpdateOAuthClient(ctx context.Context, id string, updateOpts OA
}

// DeleteOAuthClient deletes the OAuthClient with the specified id
func (c *Client) DeleteOAuthClient(ctx context.Context, id string) error {
e, err := c.OAuthClients.Endpoint()
if err != nil {
return err
}

e = fmt.Sprintf("%s/%s", e, id)

_, err = coupleAPIErrors(c.R(ctx).Delete(e))

func (c *Client) DeleteOAuthClient(ctx context.Context, clientID string) error {
e := fmt.Sprintf("account/oauth-clients/%s", clientID)
_, err := coupleAPIErrors(c.R(ctx).Delete(e))
return err
}
41 changes: 11 additions & 30 deletions account_payments.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,8 @@ type PaymentsPagedResponse struct {
}

// endpoint gets the endpoint URL for Payment
func (PaymentsPagedResponse) endpoint(c *Client, _ ...any) string {
endpoint, err := c.Payments.Endpoint()
if err != nil {
panic(err)
}

return endpoint
func (PaymentsPagedResponse) endpoint(_ ...any) string {
return "account/payments"
}

func (resp *PaymentsPagedResponse) castResult(r *resty.Request, e string) (int, int, error) {
Expand All @@ -95,14 +90,10 @@ func (c *Client) ListPayments(ctx context.Context, opts *ListOptions) ([]Payment
}

// GetPayment gets the payment with the provided ID
func (c *Client) GetPayment(ctx context.Context, id int) (*Payment, error) {
e, err := c.Payments.Endpoint()
if err != nil {
return nil, err
}

e = fmt.Sprintf("%s/%d", e, id)
r, err := coupleAPIErrors(c.R(ctx).SetResult(&Payment{}).Get(e))
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))
if err != nil {
return nil, err
}
Expand All @@ -111,25 +102,15 @@ func (c *Client) GetPayment(ctx context.Context, id int) (*Payment, error) {
}

// CreatePayment creates a Payment
func (c *Client) CreatePayment(ctx context.Context, createOpts PaymentCreateOptions) (*Payment, error) {
var body string

e, err := c.Payments.Endpoint()
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{})

if bodyData, err := json.Marshal(createOpts); err == nil {
body = string(bodyData)
} else {
return nil, NewError(err)
}

r, err := coupleAPIErrors(req.
SetBody(body).
Post(e))
req := c.R(ctx).SetResult(&Payment{}).SetBody(string(body))
e := "accounts/payments"
r, err := coupleAPIErrors(req.Post(e))
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit d72adb3

Please sign in to comment.