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

add ctx context.Context parameter in every endpoint #192

Open
wants to merge 1 commit into
base: v2
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion mockns1/example_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mockns1_test

import (
"context"
"net/http"
"net/url"
"testing"
Expand Down Expand Up @@ -29,7 +30,7 @@ func Example() {
[]*dns.Zone{{Zone: "foo.bar"}}))

// Perform your tests
zones, _, err := ns1.Zones.List()
zones, _, err := ns1.Zones.List(context.Background())
require.Nil(t, err)
require.Equal(t, 1, len(zones))
require.Equal(t, "foo.bar", zones[0].Zone)
Expand Down
3 changes: 2 additions & 1 deletion mockns1/network_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mockns1_test

import (
"context"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -37,7 +38,7 @@ func TestNetwork(t *testing.T) {

require.Nil(t, mock.NetworkGetTestCase(nil, nil, networks))

resp, _, err := client.Network.Get()
resp, _, err := client.Network.Get(context.Background())
require.Nil(t, err)
require.NotNil(t, resp)
require.Equal(t, len(networks), len(resp))
Expand Down
11 changes: 6 additions & 5 deletions mockns1/zone_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mockns1_test

import (
"context"
"encoding/json"
"testing"

Expand All @@ -27,7 +28,7 @@ func TestZone(t *testing.T) {

require.Nil(t, mock.AddZoneListTestCase(nil, nil, zones))

resp, _, err := client.Zones.List()
resp, _, err := client.Zones.List(context.Background())
require.Nil(t, err)
require.NotNil(t, resp)
require.Equal(t, len(zones), len(resp))
Expand All @@ -48,7 +49,7 @@ func TestZone(t *testing.T) {

require.Nil(t, mock.AddZoneGetTestCase(zone.Zone, nil, nil, zone))

resp, _, err := client.Zones.Get(zone.Zone)
resp, _, err := client.Zones.Get(context.Background(), zone.Zone)
require.Nil(t, err)
require.NotNil(t, resp)
require.Equal(t, zone.Zone, resp.Zone)
Expand All @@ -71,7 +72,7 @@ func TestZone(t *testing.T) {
require.Nil(t, mock.AddZoneCreateTestCase(nil, nil, zone, resp))
require.Zero(t, zone.TTL)

_, err := client.Zones.Create(zone)
_, err := client.Zones.Create(context.Background(), zone)
require.Nil(t, err)
require.Equal(t, zone.TTL, resp.TTL)
})
Expand All @@ -84,14 +85,14 @@ func TestZone(t *testing.T) {

require.Nil(t, mock.AddZoneUpdateTestCase(nil, nil, zone, zone))

_, err := client.Zones.Update(zone)
_, err := client.Zones.Update(context.Background(), zone)
require.Nil(t, err)
})

t.Run("AddZoneDeleteTestCase", func(t *testing.T) {
require.Nil(t, mock.AddZoneDeleteTestCase("delete.zone", nil, nil))

_, err := client.Zones.Delete("delete.zone")
_, err := client.Zones.Delete(context.Background(), "delete.zone")
require.Nil(t, err)
})
}
Expand Down
25 changes: 13 additions & 12 deletions rest/account_apikey.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package rest

import (
"context"
"errors"
"fmt"
"net/http"
Expand All @@ -14,8 +15,8 @@ type APIKeysService service
// List returns all api keys in the account.
//
// NS1 API docs: https://ns1.com/api/#apikeys-get
func (s *APIKeysService) List() ([]*account.APIKey, *http.Response, error) {
req, err := s.client.NewRequest("GET", "account/apikeys", nil)
func (s *APIKeysService) List(ctx context.Context) ([]*account.APIKey, *http.Response, error) {
req, err := s.client.NewRequest(ctx, "GET", "account/apikeys", nil)
if err != nil {
return nil, nil, err
}
Expand All @@ -33,10 +34,10 @@ func (s *APIKeysService) List() ([]*account.APIKey, *http.Response, error) {
// Note: do not use the API Key itself as the keyid in the URL — use the id of the key.
//
// NS1 API docs: https://ns1.com/api/#apikeys-id-get
func (s *APIKeysService) Get(keyID string) (*account.APIKey, *http.Response, error) {
func (s *APIKeysService) Get(ctx context.Context, keyID string) (*account.APIKey, *http.Response, error) {
path := fmt.Sprintf("account/apikeys/%s", keyID)

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 @@ -60,7 +61,7 @@ func (s *APIKeysService) Get(keyID string) (*account.APIKey, *http.Response, err
// Create takes a *APIKey and creates a new account apikey.
//
// NS1 API docs: https://ns1.com/api/#apikeys-put
func (s *APIKeysService) Create(a *account.APIKey) (*http.Response, error) {
func (s *APIKeysService) Create(ctx context.Context, a *account.APIKey) (*http.Response, error) {
var (
req *http.Request
err error
Expand All @@ -69,12 +70,12 @@ func (s *APIKeysService) Create(a *account.APIKey) (*http.Response, error) {
// If this is DDI then the permissions need to be transformed to DDI-compatible permissions.
if s.client.DDI && a != nil {
ddiAPIKey := apiKeyToDDIAPIKey(a)
req, err = s.client.NewRequest("PUT", "account/apikeys", ddiAPIKey)
req, err = s.client.NewRequest(ctx, "PUT", "account/apikeys", ddiAPIKey)
if err != nil {
return nil, err
}
} else {
req, err = s.client.NewRequest("PUT", "account/apikeys", a)
req, err = s.client.NewRequest(ctx, "PUT", "account/apikeys", a)
if err != nil {
return nil, err
}
Expand All @@ -98,7 +99,7 @@ func (s *APIKeysService) Create(a *account.APIKey) (*http.Response, error) {
// Update changes the name or access rights for an API Key.
//
// NS1 API docs: https://ns1.com/api/#apikeys-id-post
func (s *APIKeysService) Update(a *account.APIKey) (*http.Response, error) {
func (s *APIKeysService) Update(ctx context.Context, a *account.APIKey) (*http.Response, error) {
path := fmt.Sprintf("account/apikeys/%s", a.ID)

var (
Expand All @@ -109,12 +110,12 @@ func (s *APIKeysService) Update(a *account.APIKey) (*http.Response, error) {
// If this is DDI then the permissions need to be transformed to DDI-compatible permissions.
if s.client.DDI && a != nil {
ddiAPIKey := apiKeyToDDIAPIKey(a)
req, err = s.client.NewRequest("POST", path, ddiAPIKey)
req, err = s.client.NewRequest(ctx, "POST", path, ddiAPIKey)
if err != nil {
return nil, err
}
} else {
req, err = s.client.NewRequest("POST", path, a)
req, err = s.client.NewRequest(ctx, "POST", path, a)
if err != nil {
return nil, err
}
Expand All @@ -138,10 +139,10 @@ func (s *APIKeysService) Update(a *account.APIKey) (*http.Response, error) {
// Delete deletes an apikey.
//
// NS1 API docs: https://ns1.com/api/#apikeys-id-delete
func (s *APIKeysService) Delete(keyID string) (*http.Response, error) {
func (s *APIKeysService) Delete(ctx context.Context, keyID string) (*http.Response, error) {
path := fmt.Sprintf("account/apikeys/%s", keyID)

req, err := s.client.NewRequest("DELETE", path, nil)
req, err := s.client.NewRequest(ctx, "DELETE", path, nil)
if err != nil {
return nil, err
}
Expand Down
7 changes: 4 additions & 3 deletions rest/account_apikey_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package rest

import (
"context"
"encoding/json"
"io/ioutil"
"net/http"
Expand Down Expand Up @@ -36,7 +37,7 @@ func TestCreateAPIKey(t *testing.T) {
Permissions: account.PermissionsMap{},
}

_, err := c.APIKeys.Create(k)
_, err := c.APIKeys.Create(context.Background(), k)
require.NoError(t, err)
}

Expand Down Expand Up @@ -95,7 +96,7 @@ func TestCreateDDIAPIKey(t *testing.T) {
Permissions: account.PermissionsMap{},
}

_, err := c.APIKeys.Create(k)
_, err := c.APIKeys.Create(context.Background(), k)
require.NoError(t, err)
// Create a key with auth tags
k = &account.APIKey{
Expand Down Expand Up @@ -150,6 +151,6 @@ func TestCreateDDIAPIKey(t *testing.T) {
},
}

_, err = c.APIKeys.Create(k)
_, err = c.APIKeys.Create(context.Background(), k)
require.NoError(t, err)
}
9 changes: 5 additions & 4 deletions rest/account_setting.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package rest

import (
"context"
"net/http"

"gopkg.in/ns1/ns1-go.v2/rest/model/account"
Expand All @@ -12,8 +13,8 @@ type SettingsService service
// Get returns the basic contact details associated with the account.
//
// NS1 API docs: https://ns1.com/api/#settings-get
func (s *SettingsService) Get() (*account.Setting, *http.Response, error) {
req, err := s.client.NewRequest("GET", "account/settings", nil)
func (s *SettingsService) Get(ctx context.Context) (*account.Setting, *http.Response, error) {
req, err := s.client.NewRequest(ctx, "GET", "account/settings", nil)
if err != nil {
return nil, nil, err
}
Expand All @@ -30,8 +31,8 @@ func (s *SettingsService) Get() (*account.Setting, *http.Response, error) {
// Update changes most of the basic contact details, except customerid.
//
// NS1 API docs: https://ns1.com/api/#settings-post
func (s *SettingsService) Update(us *account.Setting) (*http.Response, error) {
req, err := s.client.NewRequest("POST", "account/settings", &us)
func (s *SettingsService) Update(ctx context.Context, us *account.Setting) (*http.Response, error) {
req, err := s.client.NewRequest(ctx, "POST", "account/settings", &us)
if err != nil {
return nil, err
}
Expand Down
25 changes: 13 additions & 12 deletions rest/account_team.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package rest

import (
"context"
"errors"
"fmt"
"net/http"
Expand All @@ -14,8 +15,8 @@ type TeamsService service
// List returns all teams in the account.
//
// NS1 API docs: https://ns1.com/api/#teams-get
func (s *TeamsService) List() ([]*account.Team, *http.Response, error) {
req, err := s.client.NewRequest("GET", "account/teams", nil)
func (s *TeamsService) List(ctx context.Context) ([]*account.Team, *http.Response, error) {
req, err := s.client.NewRequest(ctx, "GET", "account/teams", nil)
if err != nil {
return nil, nil, err
}
Expand All @@ -32,10 +33,10 @@ func (s *TeamsService) List() ([]*account.Team, *http.Response, error) {
// Get returns details of a single team.
//
// NS1 API docs: https://ns1.com/api/#teams-id-get
func (s *TeamsService) Get(id string) (*account.Team, *http.Response, error) {
func (s *TeamsService) Get(ctx context.Context, id string) (*account.Team, *http.Response, error) {
path := fmt.Sprintf("account/teams/%s", 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 @@ -58,7 +59,7 @@ func (s *TeamsService) Get(id string) (*account.Team, *http.Response, error) {
// Create takes a *Team and creates a new account team.
//
// NS1 API docs: https://ns1.com/api/#teams-put
func (s *TeamsService) Create(t *account.Team) (*http.Response, error) {
func (s *TeamsService) Create(ctx context.Context, t *account.Team) (*http.Response, error) {
var (
req *http.Request
err error
Expand All @@ -67,12 +68,12 @@ func (s *TeamsService) Create(t *account.Team) (*http.Response, error) {
// If this is DDI then the permissions need to be transformed to DDI-compatible permissions.
if s.client.DDI && t != nil {
ddiTeam := teamToDDITeam(t)
req, err = s.client.NewRequest("PUT", "account/teams", ddiTeam)
req, err = s.client.NewRequest(ctx, "PUT", "account/teams", ddiTeam)
if err != nil {
return nil, err
}
} else {
req, err = s.client.NewRequest("PUT", "account/teams", t)
req, err = s.client.NewRequest(ctx, "PUT", "account/teams", t)
if err != nil {
return nil, err
}
Expand All @@ -96,7 +97,7 @@ func (s *TeamsService) Create(t *account.Team) (*http.Response, error) {
// Update changes the name or access rights for a team.
//
// NS1 API docs: https://ns1.com/api/#teams-id-post
func (s *TeamsService) Update(t *account.Team) (*http.Response, error) {
func (s *TeamsService) Update(ctx context.Context, t *account.Team) (*http.Response, error) {
path := fmt.Sprintf("account/teams/%s", t.ID)

var (
Expand All @@ -107,12 +108,12 @@ func (s *TeamsService) Update(t *account.Team) (*http.Response, error) {
// If this is DDI then the permissions need to be transformed to DDI-compatible permissions.
if s.client.DDI && t != nil {
ddiTeam := teamToDDITeam(t)
req, err = s.client.NewRequest("POST", path, ddiTeam)
req, err = s.client.NewRequest(ctx, "POST", path, ddiTeam)
if err != nil {
return nil, err
}
} else {
req, err = s.client.NewRequest("POST", path, t)
req, err = s.client.NewRequest(ctx, "POST", path, t)
if err != nil {
return nil, err
}
Expand All @@ -136,10 +137,10 @@ func (s *TeamsService) Update(t *account.Team) (*http.Response, error) {
// Delete deletes a team.
//
// NS1 API docs: https://ns1.com/api/#teams-id-delete
func (s *TeamsService) Delete(id string) (*http.Response, error) {
func (s *TeamsService) Delete(ctx context.Context, id string) (*http.Response, error) {
path := fmt.Sprintf("account/teams/%s", 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 Down
5 changes: 3 additions & 2 deletions rest/account_team_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package rest

import (
"context"
"encoding/json"
"io/ioutil"
"net/http"
Expand Down Expand Up @@ -34,7 +35,7 @@ func TestCreateTeam(t *testing.T) {
Permissions: account.PermissionsMap{},
}

_, err := c.Teams.Create(tm)
_, err := c.Teams.Create(context.Background(), tm)
require.NoError(t, err)
}

Expand Down Expand Up @@ -64,6 +65,6 @@ func TestCreateDDITeam(t *testing.T) {
Permissions: account.PermissionsMap{},
}

_, err := c.Teams.Create(tm)
_, err := c.Teams.Create(context.Background(), tm)
require.NoError(t, err)
}
Loading