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 additional WithContext methods to services #182

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
31 changes: 31 additions & 0 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 @@ -15,10 +16,16 @@ type APIKeysService service
//
// NS1 API docs: https://ns1.com/api/#apikeys-get
func (s *APIKeysService) List() ([]*account.APIKey, *http.Response, error) {
return s.ListWithContext(context.Background())
}

// ListWithContext is the same as List, but accepts a context.
func (s *APIKeysService) ListWithContext(ctx context.Context) ([]*account.APIKey, *http.Response, error) {
req, err := s.client.NewRequest("GET", "account/apikeys", nil)
if err != nil {
return nil, nil, err
}
req = req.WithContext(ctx)

kl := []*account.APIKey{}
resp, err := s.client.Do(req, &kl)
Expand All @@ -34,12 +41,18 @@ func (s *APIKeysService) List() ([]*account.APIKey, *http.Response, error) {
//
// NS1 API docs: https://ns1.com/api/#apikeys-id-get
func (s *APIKeysService) Get(keyID string) (*account.APIKey, *http.Response, error) {
return s.GetWithContext(context.Background(), keyID)
}

// GetWithContext is the same as Get, but accepts a context.
func (s *APIKeysService) GetWithContext(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)
if err != nil {
return nil, nil, err
}
req = req.WithContext(ctx)

var a account.APIKey
resp, err := s.client.Do(req, &a)
Expand All @@ -61,6 +74,11 @@ func (s *APIKeysService) Get(keyID string) (*account.APIKey, *http.Response, err
//
// NS1 API docs: https://ns1.com/api/#apikeys-put
func (s *APIKeysService) Create(a *account.APIKey) (*http.Response, error) {
return s.CreateWithContext(context.Background(), a)
}

// CreateWithContext is the same as Create, but accepts a context.
func (s *APIKeysService) CreateWithContext(ctx context.Context, a *account.APIKey) (*http.Response, error) {
var (
req *http.Request
err error
Expand All @@ -79,6 +97,7 @@ func (s *APIKeysService) Create(a *account.APIKey) (*http.Response, error) {
return nil, err
}
}
req = req.WithContext(ctx)

// Update account fields with data from api(ensure consistent)
resp, err := s.client.Do(req, &a)
Expand All @@ -99,6 +118,11 @@ func (s *APIKeysService) Create(a *account.APIKey) (*http.Response, error) {
//
// NS1 API docs: https://ns1.com/api/#apikeys-id-post
func (s *APIKeysService) Update(a *account.APIKey) (*http.Response, error) {
return s.UpdateWithContext(context.Background(), a)
}

// UpdateWithContext is the same as Update, but accepts a context.
func (s *APIKeysService) UpdateWithContext(ctx context.Context, a *account.APIKey) (*http.Response, error) {
path := fmt.Sprintf("account/apikeys/%s", a.ID)

var (
Expand All @@ -119,6 +143,7 @@ func (s *APIKeysService) Update(a *account.APIKey) (*http.Response, error) {
return nil, err
}
}
req = req.WithContext(ctx)

// Update apikey fields with data from api(ensure consistent)
resp, err := s.client.Do(req, &a)
Expand All @@ -139,12 +164,18 @@ func (s *APIKeysService) Update(a *account.APIKey) (*http.Response, error) {
//
// NS1 API docs: https://ns1.com/api/#apikeys-id-delete
func (s *APIKeysService) Delete(keyID string) (*http.Response, error) {
return s.DeleteWithContext(context.Background(), keyID)
}

// DeleteWithContext is the same as Update, but accepts a context.
func (s *APIKeysService) DeleteWithContext(ctx context.Context, keyID string) (*http.Response, error) {
path := fmt.Sprintf("account/apikeys/%s", keyID)

req, err := s.client.NewRequest("DELETE", path, nil)
if err != nil {
return nil, err
}
req = req.WithContext(ctx)

resp, err := s.client.Do(req, nil)
if err != nil {
Expand Down
13 changes: 13 additions & 0 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 @@ -13,10 +14,16 @@ type SettingsService service
//
// NS1 API docs: https://ns1.com/api/#settings-get
func (s *SettingsService) Get() (*account.Setting, *http.Response, error) {
return s.GetWithContext(context.Background())
}

// GetWithContext is the same as Get, but takes a context.
func (s *SettingsService) GetWithContext(ctx context.Context) (*account.Setting, *http.Response, error) {
req, err := s.client.NewRequest("GET", "account/settings", nil)
if err != nil {
return nil, nil, err
}
req = req.WithContext(ctx)

var us account.Setting
resp, err := s.client.Do(req, &us)
Expand All @@ -31,10 +38,16 @@ func (s *SettingsService) Get() (*account.Setting, *http.Response, error) {
//
// NS1 API docs: https://ns1.com/api/#settings-post
func (s *SettingsService) Update(us *account.Setting) (*http.Response, error) {
return s.UpdateWithContext(context.Background(), us)
}

// UpdateWithContext is the same as Update, but takes a context.
func (s *SettingsService) UpdateWithContext(ctx context.Context, us *account.Setting) (*http.Response, error) {
req, err := s.client.NewRequest("POST", "account/settings", &us)
if err != nil {
return nil, err
}
req = req.WithContext(ctx)

// Update usagewarnings fields with data from api(ensure consistent)
resp, err := s.client.Do(req, &us)
Expand Down
31 changes: 31 additions & 0 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 @@ -15,10 +16,16 @@ type TeamsService service
//
// NS1 API docs: https://ns1.com/api/#teams-get
func (s *TeamsService) List() ([]*account.Team, *http.Response, error) {
return s.ListWithContext(context.Background())
}

// ListWithContext is the same as List, but takes a context.
func (s *TeamsService) ListWithContext(ctx context.Context) ([]*account.Team, *http.Response, error) {
req, err := s.client.NewRequest("GET", "account/teams", nil)
if err != nil {
return nil, nil, err
}
req = req.WithContext(ctx)

tl := []*account.Team{}
resp, err := s.client.Do(req, &tl)
Expand All @@ -33,12 +40,18 @@ func (s *TeamsService) List() ([]*account.Team, *http.Response, error) {
//
// NS1 API docs: https://ns1.com/api/#teams-id-get
func (s *TeamsService) Get(id string) (*account.Team, *http.Response, error) {
return s.GetWithContext(context.Background(), id)
}

// GetWithContext is the same as Get, but takes a context.
func (s *TeamsService) GetWithContext(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)
if err != nil {
return nil, nil, err
}
req = req.WithContext(ctx)

var t account.Team
resp, err := s.client.Do(req, &t)
Expand All @@ -59,6 +72,11 @@ func (s *TeamsService) Get(id string) (*account.Team, *http.Response, error) {
//
// NS1 API docs: https://ns1.com/api/#teams-put
func (s *TeamsService) Create(t *account.Team) (*http.Response, error) {
return s.CreateWithContext(context.Background(), t)
}

// CreateWithContext is the same as Create, but takes a context.
func (s *TeamsService) CreateWithContext(ctx context.Context, t *account.Team) (*http.Response, error) {
var (
req *http.Request
err error
Expand All @@ -77,6 +95,7 @@ func (s *TeamsService) Create(t *account.Team) (*http.Response, error) {
return nil, err
}
}
req = req.WithContext(ctx)

// Update team fields with data from api(ensure consistent)
resp, err := s.client.Do(req, &t)
Expand All @@ -97,6 +116,11 @@ func (s *TeamsService) Create(t *account.Team) (*http.Response, error) {
//
// NS1 API docs: https://ns1.com/api/#teams-id-post
func (s *TeamsService) Update(t *account.Team) (*http.Response, error) {
return s.UpdateWithContext(context.Background(), t)
}

// UpdateWithContext is the same as Update, but takes a context.
func (s *TeamsService) UpdateWithContext(ctx context.Context, t *account.Team) (*http.Response, error) {
path := fmt.Sprintf("account/teams/%s", t.ID)

var (
Expand All @@ -117,6 +141,7 @@ func (s *TeamsService) Update(t *account.Team) (*http.Response, error) {
return nil, err
}
}
req = req.WithContext(ctx)

// Update team fields with data from api(ensure consistent)
resp, err := s.client.Do(req, &t)
Expand All @@ -137,12 +162,18 @@ func (s *TeamsService) Update(t *account.Team) (*http.Response, error) {
//
// NS1 API docs: https://ns1.com/api/#teams-id-delete
func (s *TeamsService) Delete(id string) (*http.Response, error) {
return s.DeleteWithContext(context.Background(), id)
}

// DeleteWithContext is the same as Delete, but takes a context.
func (s *TeamsService) DeleteWithContext(ctx context.Context, id string) (*http.Response, error) {
path := fmt.Sprintf("account/teams/%s", id)

req, err := s.client.NewRequest("DELETE", path, nil)
if err != nil {
return nil, err
}
req = req.WithContext(ctx)

resp, err := s.client.Do(req, nil)
if err != nil {
Expand Down
31 changes: 31 additions & 0 deletions rest/account_user.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 @@ -15,10 +16,16 @@ type UsersService service
//
// NS1 API docs: https://ns1.com/api/#users-get
func (s *UsersService) List() ([]*account.User, *http.Response, error) {
return s.ListWithContext(context.Background())
}

// ListWithContext is the same as List, but takes a context.
func (s *UsersService) ListWithContext(ctx context.Context) ([]*account.User, *http.Response, error) {
req, err := s.client.NewRequest("GET", "account/users", nil)
if err != nil {
return nil, nil, err
}
req = req.WithContext(ctx)

ul := []*account.User{}
resp, err := s.client.Do(req, &ul)
Expand All @@ -33,12 +40,18 @@ func (s *UsersService) List() ([]*account.User, *http.Response, error) {
//
// NS1 API docs: https://ns1.com/api/#users-user-get
func (s *UsersService) Get(username string) (*account.User, *http.Response, error) {
return s.GetWithContext(context.Background(), username)
}

// GetWithContext is the same as Get, but takes a context.
func (s *UsersService) GetWithContext(ctx context.Context, username string) (*account.User, *http.Response, error) {
path := fmt.Sprintf("account/users/%s", username)

req, err := s.client.NewRequest("GET", path, nil)
if err != nil {
return nil, nil, err
}
req = req.WithContext(ctx)

var u account.User
resp, err := s.client.Do(req, &u)
Expand All @@ -59,6 +72,11 @@ func (s *UsersService) Get(username string) (*account.User, *http.Response, erro
//
// NS1 API docs: https://ns1.com/api/#users-put
func (s *UsersService) Create(u *account.User) (*http.Response, error) {
return s.CreateWithContext(context.Background(), u)
}

// CreateWithContext is the same as Create, but takes a context.
func (s *UsersService) CreateWithContext(ctx context.Context, u *account.User) (*http.Response, error) {
var (
req *http.Request
err error
Expand All @@ -77,6 +95,7 @@ func (s *UsersService) Create(u *account.User) (*http.Response, error) {
return nil, err
}
}
req = req.WithContext(ctx)

// Update user fields with data from api(ensure consistent)
resp, err := s.client.Do(req, &u)
Expand All @@ -97,6 +116,11 @@ func (s *UsersService) Create(u *account.User) (*http.Response, error) {
//
// NS1 API docs: https://ns1.com/api/#users-user-post
func (s *UsersService) Update(u *account.User) (*http.Response, error) {
return s.UpdateWithContext(context.Background(), u)
}

// UpdateWithContext is the same as Update, but takes a context
func (s *UsersService) UpdateWithContext(ctx context.Context, u *account.User) (*http.Response, error) {
path := fmt.Sprintf("account/users/%s", u.Username)

var (
Expand All @@ -117,6 +141,7 @@ func (s *UsersService) Update(u *account.User) (*http.Response, error) {
return nil, err
}
}
req = req.WithContext(ctx)

// Update user fields with data from api(ensure consistent)
resp, err := s.client.Do(req, &u)
Expand All @@ -137,12 +162,18 @@ func (s *UsersService) Update(u *account.User) (*http.Response, error) {
//
// NS1 API docs: https://ns1.com/api/#users-user-delete
func (s *UsersService) Delete(username string) (*http.Response, error) {
return s.DeleteWithContext(context.Background(), username)
}

// DeleteWithContext is the same as Delete, but takes a context.
func (s *UsersService) DeleteWithContext(ctx context.Context, username string) (*http.Response, error) {
path := fmt.Sprintf("account/users/%s", username)

req, err := s.client.NewRequest("DELETE", path, nil)
if err != nil {
return nil, err
}
req = req.WithContext(ctx)

resp, err := s.client.Do(req, nil)
if err != nil {
Expand Down
13 changes: 13 additions & 0 deletions rest/account_warning.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 @@ -14,10 +15,16 @@ type WarningsService service
//
// NS1 API docs: https://ns1.com/api/#usagewarnings-get
func (s *WarningsService) Get() (*account.UsageWarning, *http.Response, error) {
return s.GetWithContext(context.Background())
}

// GetWithContext is the same as Get, but takes a context.
func (s *WarningsService) GetWithContext(ctx context.Context) (*account.UsageWarning, *http.Response, error) {
req, err := s.client.NewRequest("GET", "account/usagewarnings", nil)
if err != nil {
return nil, nil, err
}
req = req.WithContext(ctx)

var uw account.UsageWarning
resp, err := s.client.Do(req, &uw)
Expand All @@ -32,10 +39,16 @@ func (s *WarningsService) Get() (*account.UsageWarning, *http.Response, error) {
//
// NS1 API docs: https://ns1.com/api/#usagewarnings-post
func (s *WarningsService) Update(uw *account.UsageWarning) (*http.Response, error) {
return s.UpdateWithContext(context.Background(), uw)
}

// UpdateWithContext is the same as Update, but takes a context.
func (s *WarningsService) UpdateWithContext(ctx context.Context, uw *account.UsageWarning) (*http.Response, error) {
req, err := s.client.NewRequest("POST", "account/usagewarnings", &uw)
if err != nil {
return nil, err
}
req = req.WithContext(ctx)

// Update usagewarnings fields with data from api(ensure consistent)
resp, err := s.client.Do(req, &uw)
Expand Down
Loading