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 Feb 23, 2017
1 parent 19ceffc commit dc14e4b
Show file tree
Hide file tree
Showing 42 changed files with 642 additions and 574 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
language: go

go:
- 1.3
- 1.4
- 1.7
- tip
9 changes: 6 additions & 3 deletions account.go
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
29 changes: 16 additions & 13 deletions certificates.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package godo

import "path"
import (
"context"
"path"
)

const certificatesBasePath = "/v2/certificates"

// CertificatesService is an interface for managing certificates with the DigitalOcean API.
// See: https://developers.digitalocean.com/documentation/v2/#certificates
type CertificatesService interface {
Get(cID string) (*Certificate, *Response, error)
List(opt *ListOptions) ([]Certificate, *Response, error)
Create(cr *CertificateRequest) (*Certificate, *Response, error)
Delete(cID string) (*Response, error)
Get(context.Context, string) (*Certificate, *Response, error)
List(context.Context, *ListOptions) ([]Certificate, *Response, error)
Create(context.Context, *CertificateRequest) (*Certificate, *Response, error)
Delete(context.Context, string) (*Response, error)
}

// Certificate represents a DigitalOcean certificate configuration.
Expand Down Expand Up @@ -47,10 +50,10 @@ type CertificatesServiceOp struct {
var _ CertificatesService = &CertificatesServiceOp{}

// Get an existing certificate by its identifier.
func (c *CertificatesServiceOp) Get(cID string) (*Certificate, *Response, error) {
func (c *CertificatesServiceOp) Get(ctx context.Context, cID string) (*Certificate, *Response, error) {
urlStr := path.Join(certificatesBasePath, cID)

req, err := c.client.NewRequest("GET", urlStr, nil)
req, err := c.client.NewRequest(ctx, "GET", urlStr, nil)
if err != nil {
return nil, nil, err
}
Expand All @@ -65,13 +68,13 @@ func (c *CertificatesServiceOp) Get(cID string) (*Certificate, *Response, error)
}

// List all certificates.
func (c *CertificatesServiceOp) List(opt *ListOptions) ([]Certificate, *Response, error) {
func (c *CertificatesServiceOp) List(ctx context.Context, opt *ListOptions) ([]Certificate, *Response, error) {
urlStr, err := addOptions(certificatesBasePath, opt)
if err != nil {
return nil, nil, err
}

req, err := c.client.NewRequest("GET", urlStr, nil)
req, err := c.client.NewRequest(ctx, "GET", urlStr, nil)
if err != nil {
return nil, nil, err
}
Expand All @@ -89,8 +92,8 @@ func (c *CertificatesServiceOp) List(opt *ListOptions) ([]Certificate, *Response
}

// Create a new certificate with provided configuration.
func (c *CertificatesServiceOp) Create(cr *CertificateRequest) (*Certificate, *Response, error) {
req, err := c.client.NewRequest("POST", certificatesBasePath, cr)
func (c *CertificatesServiceOp) Create(ctx context.Context, cr *CertificateRequest) (*Certificate, *Response, error) {
req, err := c.client.NewRequest(ctx, "POST", certificatesBasePath, cr)
if err != nil {
return nil, nil, err
}
Expand All @@ -105,10 +108,10 @@ func (c *CertificatesServiceOp) Create(cr *CertificateRequest) (*Certificate, *R
}

// Delete a certificate by its identifier.
func (c *CertificatesServiceOp) Delete(cID string) (*Response, error) {
func (c *CertificatesServiceOp) Delete(ctx context.Context, cID string) (*Response, error) {
urlStr := path.Join(certificatesBasePath, cID)

req, err := c.client.NewRequest("DELETE", urlStr, nil)
req, err := c.client.NewRequest(ctx, "DELETE", urlStr, nil)
if err != nil {
return nil, err
}
Expand Down
8 changes: 4 additions & 4 deletions certificates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func TestCertificates_Get(t *testing.T) {
fmt.Fprint(w, certJSONResponse)
})

certificate, _, err := client.Certificates.Get(cID)
certificate, _, err := client.Certificates.Get(ctx, cID)
if err != nil {
t.Errorf("Certificates.Get returned error: %v", err)
}
Expand All @@ -85,7 +85,7 @@ func TestCertificates_List(t *testing.T) {
fmt.Fprint(w, certsJSONResponse)
})

certificates, _, err := client.Certificates.List(nil)
certificates, _, err := client.Certificates.List(ctx, nil)

if err != nil {
t.Errorf("Certificates.List returned error: %v", err)
Expand Down Expand Up @@ -136,7 +136,7 @@ func TestCertificates_Create(t *testing.T) {
fmt.Fprint(w, certJSONResponse)
})

certificate, _, err := client.Certificates.Create(createRequest)
certificate, _, err := client.Certificates.Create(ctx, createRequest)
if err != nil {
t.Errorf("Certificates.Create returned error: %v", err)
}
Expand All @@ -163,7 +163,7 @@ func TestCertificates_Delete(t *testing.T) {
testMethod(t, r, "DELETE")
})

_, err := client.Certificates.Delete(cID)
_, err := client.Certificates.Delete(ctx, cID)

if err != nil {
t.Errorf("Certificates.Delete returned error: %v", err)
Expand Down
Loading

0 comments on commit dc14e4b

Please sign in to comment.