Skip to content

Commit

Permalink
add context.Context to all calls
Browse files Browse the repository at this point in the history
  • Loading branch information
aybabtme committed Jan 9, 2017
1 parent 3ab73e7 commit e25f50a
Show file tree
Hide file tree
Showing 38 changed files with 583 additions and 519 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
@@ -1,6 +1,5 @@
language: go

go:
- 1.3
- 1.4
- 1.7
- tip
9 changes: 6 additions & 3 deletions account.go
@@ -1,10 +1,12 @@
package godo

import "context"

// AccountService is an interface for interfacing with the Account
// endpoints of the DigitalOcean API
// See: https://developers.digitalocean.com/documentation/v2/#account
type AccountService interface {
Get() (*Account, *Response, error)
Get(context.Context) (*Account, *Response, error)
}

// AccountServiceOp handles communication with the Account related methods of
Expand Down Expand Up @@ -35,10 +37,11 @@ func (r Account) String() string {
}

// Get DigitalOcean account info
func (s *AccountServiceOp) Get() (*Account, *Response, error) {
func (s *AccountServiceOp) Get(ctx context.Context) (*Account, *Response, error) {

path := "v2/account"

req, err := s.client.NewRequest("GET", path, nil)
req, err := s.client.NewRequest(ctx, "GET", path, nil)
if err != nil {
return nil, nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion account_test.go
Expand Up @@ -27,7 +27,7 @@ func TestAccountGet(t *testing.T) {
fmt.Fprint(w, response)
})

acct, _, err := client.Account.Get()
acct, _, err := client.Account.Get(ctx)
if err != nil {
t.Errorf("Account.Get returned error: %v", err)
}
Expand Down
17 changes: 10 additions & 7 deletions action.go
@@ -1,6 +1,9 @@
package godo

import "fmt"
import (
"context"
"fmt"
)

const (
actionsBasePath = "v2/actions"
Expand All @@ -15,8 +18,8 @@ const (
// ActionsService handles communction with action related methods of the
// DigitalOcean API: https://developers.digitalocean.com/documentation/v2#actions
type ActionsService interface {
List(*ListOptions) ([]Action, *Response, error)
Get(int) (*Action, *Response, error)
List(context.Context, *ListOptions) ([]Action, *Response, error)
Get(context.Context, int) (*Action, *Response, error)
}

// ActionsServiceOp handles communition with the image action related methods of the
Expand Down Expand Up @@ -50,14 +53,14 @@ type Action struct {
}

// List all actions
func (s *ActionsServiceOp) List(opt *ListOptions) ([]Action, *Response, error) {
func (s *ActionsServiceOp) List(ctx context.Context, opt *ListOptions) ([]Action, *Response, error) {
path := actionsBasePath
path, err := addOptions(path, opt)
if err != nil {
return nil, nil, err
}

req, err := s.client.NewRequest("GET", path, nil)
req, err := s.client.NewRequest(ctx, "GET", path, nil)
if err != nil {
return nil, nil, err
}
Expand All @@ -75,13 +78,13 @@ func (s *ActionsServiceOp) List(opt *ListOptions) ([]Action, *Response, error) {
}

// Get an action by ID.
func (s *ActionsServiceOp) Get(id int) (*Action, *Response, error) {
func (s *ActionsServiceOp) Get(ctx context.Context, id int) (*Action, *Response, error) {
if id < 1 {
return nil, nil, NewArgError("id", "cannot be less than 1")
}

path := fmt.Sprintf("%s/%d", actionsBasePath, id)
req, err := s.client.NewRequest("GET", path, nil)
req, err := s.client.NewRequest(ctx, "GET", path, nil)
if err != nil {
return nil, nil, err
}
Expand Down
8 changes: 4 additions & 4 deletions action_test.go
Expand Up @@ -17,7 +17,7 @@ func TestAction_List(t *testing.T) {
testMethod(t, r, "GET")
})

actions, _, err := client.Actions.List(nil)
actions, _, err := client.Actions.List(ctx, nil)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
Expand All @@ -37,7 +37,7 @@ func TestAction_ListActionMultiplePages(t *testing.T) {
testMethod(t, r, "GET")
})

_, resp, err := client.Actions.List(nil)
_, resp, err := client.Actions.List(ctx, nil)
if err != nil {
t.Fatal(nil)
}
Expand Down Expand Up @@ -68,7 +68,7 @@ func TestAction_RetrievePageByNumber(t *testing.T) {
})

opt := &ListOptions{Page: 2}
_, resp, err := client.Actions.List(opt)
_, resp, err := client.Actions.List(ctx, opt)
if err != nil {
t.Fatal(err)
}
Expand All @@ -85,7 +85,7 @@ func TestAction_Get(t *testing.T) {
testMethod(t, r, "GET")
})

action, _, err := client.Actions.Get(12345)
action, _, err := client.Actions.Get(ctx, 12345)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
Expand Down
61 changes: 32 additions & 29 deletions domains.go
@@ -1,23 +1,26 @@
package godo

import "fmt"
import (
"context"
"fmt"
)

const domainsBasePath = "v2/domains"

// DomainsService is an interface for managing DNS with the DigitalOcean API.
// See: https://developers.digitalocean.com/documentation/v2#domains and
// https://developers.digitalocean.com/documentation/v2#domain-records
type DomainsService interface {
List(*ListOptions) ([]Domain, *Response, error)
Get(string) (*Domain, *Response, error)
Create(*DomainCreateRequest) (*Domain, *Response, error)
Delete(string) (*Response, error)

Records(string, *ListOptions) ([]DomainRecord, *Response, error)
Record(string, int) (*DomainRecord, *Response, error)
DeleteRecord(string, int) (*Response, error)
EditRecord(string, int, *DomainRecordEditRequest) (*DomainRecord, *Response, error)
CreateRecord(string, *DomainRecordEditRequest) (*DomainRecord, *Response, error)
List(context.Context, *ListOptions) ([]Domain, *Response, error)
Get(context.Context, string) (*Domain, *Response, error)
Create(context.Context, *DomainCreateRequest) (*Domain, *Response, error)
Delete(context.Context, string) (*Response, error)

Records(context.Context, string, *ListOptions) ([]DomainRecord, *Response, error)
Record(context.Context, string, int) (*DomainRecord, *Response, error)
DeleteRecord(context.Context, string, int) (*Response, error)
EditRecord(context.Context, string, int, *DomainRecordEditRequest) (*DomainRecord, *Response, error)
CreateRecord(context.Context, string, *DomainRecordEditRequest) (*DomainRecord, *Response, error)
}

// DomainsServiceOp handles communication with the domain related methods of the
Expand Down Expand Up @@ -88,14 +91,14 @@ func (d Domain) String() string {
}

// List all domains.
func (s DomainsServiceOp) List(opt *ListOptions) ([]Domain, *Response, error) {
func (s DomainsServiceOp) List(ctx context.Context, opt *ListOptions) ([]Domain, *Response, error) {
path := domainsBasePath
path, err := addOptions(path, opt)
if err != nil {
return nil, nil, err
}

req, err := s.client.NewRequest("GET", path, nil)
req, err := s.client.NewRequest(ctx, "GET", path, nil)
if err != nil {
return nil, nil, err
}
Expand All @@ -113,14 +116,14 @@ func (s DomainsServiceOp) List(opt *ListOptions) ([]Domain, *Response, error) {
}

// Get individual domain. It requires a non-empty domain name.
func (s *DomainsServiceOp) Get(name string) (*Domain, *Response, error) {
func (s *DomainsServiceOp) Get(ctx context.Context, name string) (*Domain, *Response, error) {
if len(name) < 1 {
return nil, nil, NewArgError("name", "cannot be an empty string")
}

path := fmt.Sprintf("%s/%s", domainsBasePath, name)

req, err := s.client.NewRequest("GET", path, nil)
req, err := s.client.NewRequest(ctx, "GET", path, nil)
if err != nil {
return nil, nil, err
}
Expand All @@ -135,14 +138,14 @@ func (s *DomainsServiceOp) Get(name string) (*Domain, *Response, error) {
}

// Create a new domain
func (s *DomainsServiceOp) Create(createRequest *DomainCreateRequest) (*Domain, *Response, error) {
func (s *DomainsServiceOp) Create(ctx context.Context, createRequest *DomainCreateRequest) (*Domain, *Response, error) {
if createRequest == nil {
return nil, nil, NewArgError("createRequest", "cannot be nil")
}

path := domainsBasePath

req, err := s.client.NewRequest("POST", path, createRequest)
req, err := s.client.NewRequest(ctx, "POST", path, createRequest)
if err != nil {
return nil, nil, err
}
Expand All @@ -156,14 +159,14 @@ func (s *DomainsServiceOp) Create(createRequest *DomainCreateRequest) (*Domain,
}

// Delete domain
func (s *DomainsServiceOp) Delete(name string) (*Response, error) {
func (s *DomainsServiceOp) Delete(ctx context.Context, name string) (*Response, error) {
if len(name) < 1 {
return nil, NewArgError("name", "cannot be an empty string")
}

path := fmt.Sprintf("%s/%s", domainsBasePath, name)

req, err := s.client.NewRequest("DELETE", path, nil)
req, err := s.client.NewRequest(ctx, "DELETE", path, nil)
if err != nil {
return nil, err
}
Expand All @@ -184,7 +187,7 @@ func (d DomainRecordEditRequest) String() string {
}

// Records returns a slice of DomainRecords for a domain
func (s *DomainsServiceOp) Records(domain string, opt *ListOptions) ([]DomainRecord, *Response, error) {
func (s *DomainsServiceOp) Records(ctx context.Context, domain string, opt *ListOptions) ([]DomainRecord, *Response, error) {
if len(domain) < 1 {
return nil, nil, NewArgError("domain", "cannot be an empty string")
}
Expand All @@ -195,7 +198,7 @@ func (s *DomainsServiceOp) Records(domain string, opt *ListOptions) ([]DomainRec
return nil, nil, err
}

req, err := s.client.NewRequest("GET", path, nil)
req, err := s.client.NewRequest(ctx, "GET", path, nil)
if err != nil {
return nil, nil, err
}
Expand All @@ -213,7 +216,7 @@ func (s *DomainsServiceOp) Records(domain string, opt *ListOptions) ([]DomainRec
}

// Record returns the record id from a domain
func (s *DomainsServiceOp) Record(domain string, id int) (*DomainRecord, *Response, error) {
func (s *DomainsServiceOp) Record(ctx context.Context, domain string, id int) (*DomainRecord, *Response, error) {
if len(domain) < 1 {
return nil, nil, NewArgError("domain", "cannot be an empty string")
}
Expand All @@ -224,7 +227,7 @@ func (s *DomainsServiceOp) Record(domain string, id int) (*DomainRecord, *Respon

path := fmt.Sprintf("%s/%s/records/%d", domainsBasePath, domain, id)

req, err := s.client.NewRequest("GET", path, nil)
req, err := s.client.NewRequest(ctx, "GET", path, nil)
if err != nil {
return nil, nil, err
}
Expand All @@ -239,7 +242,7 @@ func (s *DomainsServiceOp) Record(domain string, id int) (*DomainRecord, *Respon
}

// DeleteRecord deletes a record from a domain identified by id
func (s *DomainsServiceOp) DeleteRecord(domain string, id int) (*Response, error) {
func (s *DomainsServiceOp) DeleteRecord(ctx context.Context, domain string, id int) (*Response, error) {
if len(domain) < 1 {
return nil, NewArgError("domain", "cannot be an empty string")
}
Expand All @@ -250,7 +253,7 @@ func (s *DomainsServiceOp) DeleteRecord(domain string, id int) (*Response, error

path := fmt.Sprintf("%s/%s/records/%d", domainsBasePath, domain, id)

req, err := s.client.NewRequest("DELETE", path, nil)
req, err := s.client.NewRequest(ctx, "DELETE", path, nil)
if err != nil {
return nil, err
}
Expand All @@ -261,7 +264,7 @@ func (s *DomainsServiceOp) DeleteRecord(domain string, id int) (*Response, error
}

// EditRecord edits a record using a DomainRecordEditRequest
func (s *DomainsServiceOp) EditRecord(
func (s *DomainsServiceOp) EditRecord(ctx context.Context,
domain string,
id int,
editRequest *DomainRecordEditRequest,
Expand All @@ -280,7 +283,7 @@ func (s *DomainsServiceOp) EditRecord(

path := fmt.Sprintf("%s/%s/records/%d", domainsBasePath, domain, id)

req, err := s.client.NewRequest("PUT", path, editRequest)
req, err := s.client.NewRequest(ctx, "PUT", path, editRequest)
if err != nil {
return nil, nil, err
}
Expand All @@ -295,7 +298,7 @@ func (s *DomainsServiceOp) EditRecord(
}

// CreateRecord creates a record using a DomainRecordEditRequest
func (s *DomainsServiceOp) CreateRecord(
func (s *DomainsServiceOp) CreateRecord(ctx context.Context,
domain string,
createRequest *DomainRecordEditRequest) (*DomainRecord, *Response, error) {
if len(domain) < 1 {
Expand All @@ -307,7 +310,7 @@ func (s *DomainsServiceOp) CreateRecord(
}

path := fmt.Sprintf("%s/%s/records", domainsBasePath, domain)
req, err := s.client.NewRequest("POST", path, createRequest)
req, err := s.client.NewRequest(ctx, "POST", path, createRequest)

if err != nil {
return nil, nil, err
Expand Down

0 comments on commit e25f50a

Please sign in to comment.