Skip to content

Commit

Permalink
add_with_context_methods
Browse files Browse the repository at this point in the history
  • Loading branch information
James Goodhouse committed Dec 21, 2022
1 parent 2d590aa commit 5c0fe57
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 0 deletions.
37 changes: 37 additions & 0 deletions rest/monitor_job.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package rest

import (
"context"
"fmt"
"net/http"
"net/url"
Expand All @@ -15,10 +16,16 @@ type JobsService service
//
// NS1 API docs: https://ns1.com/api/#jobs-get
func (s *JobsService) List() ([]*monitor.Job, *http.Response, error) {
return s.ListWithContext(context.Background())
}

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

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

// GetWithContext is the same as Get, but takes a context.
func (s *JobsService) GetWithContext(ctx context.Context, id string) (*monitor.Job, *http.Response, error) {
path := fmt.Sprintf("%s/%s", "monitoring/jobs", id)

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

var mj monitor.Job
resp, err := s.client.Do(req, &mj)
Expand All @@ -53,12 +66,18 @@ func (s *JobsService) Get(id string) (*monitor.Job, *http.Response, error) {
//
// NS1 API docs: https://ns1.com/api/#jobs-put
func (s *JobsService) Create(mj *monitor.Job) (*http.Response, error) {
return s.CreateWithContext(context.Background(), mj)
}

// CreateWithContext is the same as Create, but takes a context.
func (s *JobsService) CreateWithContext(ctx context.Context, mj *monitor.Job) (*http.Response, error) {
path := fmt.Sprintf("%s/%s", "monitoring/jobs", mj.ID)

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

// Update mon jobs' fields with data from api(ensure consistent)
resp, err := s.client.Do(req, &mj)
Expand All @@ -73,12 +92,18 @@ func (s *JobsService) Create(mj *monitor.Job) (*http.Response, error) {
//
// NS1 API docs: https://ns1.com/api/#jobs-jobid-post
func (s *JobsService) Update(mj *monitor.Job) (*http.Response, error) {
return s.UpdateWithContext(context.Background(), mj)
}

// UpdateWithContext is the same as Update, but takes a context.
func (s *JobsService) UpdateWithContext(ctx context.Context, mj *monitor.Job) (*http.Response, error) {
path := fmt.Sprintf("%s/%s", "monitoring/jobs", mj.ID)

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

// Update mon jobs' fields with data from api(ensure consistent)
resp, err := s.client.Do(req, &mj)
Expand All @@ -93,12 +118,18 @@ func (s *JobsService) Update(mj *monitor.Job) (*http.Response, error) {
//
// NS1 API docs: https://ns1.com/api/#jobs-jobid-delete
func (s *JobsService) Delete(id string) (*http.Response, error) {
return s.DeleteWithContext(context.Background(), id)
}

// DeleteWithContext is the same as Delete, but takes a context.
func (s *JobsService) DeleteWithContext(ctx context.Context, id string) (*http.Response, error) {
path := fmt.Sprintf("%s/%s", "monitoring/jobs", 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 All @@ -112,6 +143,11 @@ func (s *JobsService) Delete(id string) (*http.Response, error) {
//
// NS1 API docs: https://ns1.com/api/#history-get
func (s *JobsService) History(id string, opts ...func(*url.Values)) ([]*monitor.StatusLog, *http.Response, error) {
return s.HistoryWithContext(context.Background(), id, opts...)
}

// HistoryWithContext is the same as History, but takes a context.
func (s *JobsService) HistoryWithContext(ctx context.Context, id string, opts ...func(*url.Values)) ([]*monitor.StatusLog, *http.Response, error) {
v := url.Values{}
for _, opt := range opts {
opt(&v)
Expand All @@ -123,6 +159,7 @@ func (s *JobsService) History(id string, opts ...func(*url.Values)) ([]*monitor.
if err != nil {
return nil, nil, err
}
req = req.WithContext(ctx)

var slgs []*monitor.StatusLog
resp, err := s.client.Do(req, &slgs)
Expand Down
31 changes: 31 additions & 0 deletions rest/monitor_notify.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 NotificationsService service
//
// NS1 API docs: https://ns1.com/api/#lists-get
func (s *NotificationsService) List() ([]*monitor.NotifyList, *http.Response, error) {
return s.ListWithContext(context.Background())
}

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

nl := []*monitor.NotifyList{}
resp, err := s.client.Do(req, &nl)
Expand All @@ -33,12 +40,18 @@ func (s *NotificationsService) List() ([]*monitor.NotifyList, *http.Response, er
//
// NS1 API docs: https://ns1.com/api/#lists-listid-get
func (s *NotificationsService) Get(listID string) (*monitor.NotifyList, *http.Response, error) {
return s.GetWithContext(context.Background(), listID)
}

// GetWithContext is the same as Get, but takes a context.
func (s *NotificationsService) GetWithContext(ctx context.Context, listID string) (*monitor.NotifyList, *http.Response, error) {
path := fmt.Sprintf("%s/%s", "lists", listID)

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

var nl monitor.NotifyList
resp, err := s.client.Do(req, &nl)
Expand All @@ -59,10 +72,16 @@ func (s *NotificationsService) Get(listID string) (*monitor.NotifyList, *http.Re
//
// NS1 API docs: https://ns1.com/api/#lists-put
func (s *NotificationsService) Create(nl *monitor.NotifyList) (*http.Response, error) {
return s.CreateWithContext(context.Background(), nl)
}

// CreateWithContext is the same as Create, but takes a context.
func (s *NotificationsService) CreateWithContext(ctx context.Context, nl *monitor.NotifyList) (*http.Response, error) {
req, err := s.client.NewRequest("PUT", "lists", &nl)
if err != nil {
return nil, err
}
req = req.WithContext(ctx)

// Update notify list fields with data from api(ensure consistent)
resp, err := s.client.Do(req, &nl)
Expand All @@ -83,12 +102,18 @@ func (s *NotificationsService) Create(nl *monitor.NotifyList) (*http.Response, e
//
// NS1 API docs: https://ns1.com/api/#list-listid-post
func (s *NotificationsService) Update(nl *monitor.NotifyList) (*http.Response, error) {
return s.UpdateWithContext(context.Background(), nl)
}

// UpdateWithContext is the same as Update, but takes a context.
func (s *NotificationsService) UpdateWithContext(ctx context.Context, nl *monitor.NotifyList) (*http.Response, error) {
path := fmt.Sprintf("%s/%s", "lists", nl.ID)

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

// Update mon lists' fields with data from api(ensure consistent)
resp, err := s.client.Do(req, &nl)
Expand All @@ -103,12 +128,18 @@ func (s *NotificationsService) Update(nl *monitor.NotifyList) (*http.Response, e
//
// NS1 API docs: https://ns1.com/api/#lists-listid-delete
func (s *NotificationsService) Delete(listID string) (*http.Response, error) {
return s.DeleteWithContext(context.Background(), listID)
}

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

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
25 changes: 25 additions & 0 deletions rest/optiondef.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package rest

import (
"context"
"errors"
"fmt"
"gopkg.in/ns1/ns1-go.v2/rest/model/dhcp"
Expand All @@ -14,10 +15,16 @@ type OptionDefService service
//
// NS1 API docs: https://ns1.com/api#getlist-dhcp-option-definitions
func (s *OptionDefService) List() ([]dhcp.OptionDef, *http.Response, error) {
return s.ListWithContext(context.Background())
}

// ListWithContext is the same as List, but takes a context.
func (s *OptionDefService) ListWithContext(ctx context.Context) ([]dhcp.OptionDef, *http.Response, error) {
req, err := s.client.NewRequest(http.MethodGet, "dhcp/optiondef", nil)
if err != nil {
return nil, nil, err
}
req = req.WithContext(ctx)

ods := make([]dhcp.OptionDef, 0)
resp, err := s.client.Do(req, &ods)
Expand All @@ -28,11 +35,17 @@ func (s *OptionDefService) List() ([]dhcp.OptionDef, *http.Response, error) {
//
// NS1 API docs: https://ns1.com/api#getview-dhcp-option-definition
func (s *OptionDefService) Get(odSpace, odKey string) (*dhcp.OptionDef, *http.Response, error) {
return s.GetWithContext(context.Background(), odSpace, odKey)
}

// GetWithContext is the same as Get, but takes a context.
func (s *OptionDefService) GetWithContext(ctx context.Context, odSpace, odKey string) (*dhcp.OptionDef, *http.Response, error) {
reqPath := fmt.Sprintf("dhcp/optiondef/%s/%s", odSpace, odKey)
req, err := s.client.NewRequest(http.MethodGet, reqPath, nil)
if err != nil {
return nil, nil, err
}
req = req.WithContext(ctx)

od := &dhcp.OptionDef{}
var resp *http.Response
Expand All @@ -49,6 +62,11 @@ func (s *OptionDefService) Get(odSpace, odKey string) (*dhcp.OptionDef, *http.Re
//
// NS1 API docs: https://ns1.com/api#putcreate-an-custom-dhcp-option-definition
func (s *OptionDefService) Create(od *dhcp.OptionDef, odSpace, odKey string) (*dhcp.OptionDef, *http.Response, error) {
return s.CreateWithContext(context.Background(), od, odSpace, odKey)
}

// CreateWithContext is the same as Create, but takes a context.
func (s *OptionDefService) CreateWithContext(ctx context.Context, od *dhcp.OptionDef, odSpace, odKey string) (*dhcp.OptionDef, *http.Response, error) {
switch {
case od.FriendlyName == "":
return nil, nil, errors.New("the FriendlyName field is required")
Expand All @@ -65,6 +83,7 @@ func (s *OptionDefService) Create(od *dhcp.OptionDef, odSpace, odKey string) (*d
if err != nil {
return nil, nil, err
}
req = req.WithContext(ctx)

respOd := new(dhcp.OptionDef)
var resp *http.Response
Expand All @@ -80,11 +99,17 @@ func (s *OptionDefService) Create(od *dhcp.OptionDef, odSpace, odKey string) (*d
//
// NS1 API docs: https://ns1.com/api#deletedelete-a-custom-dhcp-option-definition
func (s *OptionDefService) Delete(odSpace, odKey string) (*http.Response, error) {
return s.DeleteWithContext(context.Background(), odSpace, odKey)
}

// DeleteWithContext is the same as Delete, but takes a context.
func (s *OptionDefService) DeleteWithContext(ctx context.Context, odSpace, odKey string) (*http.Response, error) {
reqPath := fmt.Sprintf("dhcp/optiondef/%s/%s", odSpace, odKey)
req, err := s.client.NewRequest(http.MethodDelete, reqPath, nil)
if err != nil {
return nil, err
}
req = req.WithContext(ctx)

return s.client.Do(req, nil)
}
Loading

0 comments on commit 5c0fe57

Please sign in to comment.