From 5c0fe57d5f160e42227a77823996913e7fe6314c Mon Sep 17 00:00:00 2001 From: James Goodhouse Date: Wed, 21 Dec 2022 12:45:40 -0800 Subject: [PATCH] add_with_context_methods --- rest/monitor_job.go | 37 +++++++++++++++++++++++++++++++++++++ rest/monitor_notify.go | 31 +++++++++++++++++++++++++++++++ rest/optiondef.go | 25 +++++++++++++++++++++++++ rest/pulsar_job.go | 31 +++++++++++++++++++++++++++++++ rest/record.go | 25 +++++++++++++++++++++++++ 5 files changed, 149 insertions(+) diff --git a/rest/monitor_job.go b/rest/monitor_job.go index 9ff927e..981f8ec 100644 --- a/rest/monitor_job.go +++ b/rest/monitor_job.go @@ -1,6 +1,7 @@ package rest import ( + "context" "fmt" "net/http" "net/url" @@ -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) @@ -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) @@ -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) @@ -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) @@ -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 { @@ -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) @@ -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) diff --git a/rest/monitor_notify.go b/rest/monitor_notify.go index e1ddc36..3956250 100644 --- a/rest/monitor_notify.go +++ b/rest/monitor_notify.go @@ -1,6 +1,7 @@ package rest import ( + "context" "errors" "fmt" "net/http" @@ -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) @@ -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) @@ -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) @@ -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) @@ -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 { diff --git a/rest/optiondef.go b/rest/optiondef.go index a3ad0c0..8b62e5b 100644 --- a/rest/optiondef.go +++ b/rest/optiondef.go @@ -1,6 +1,7 @@ package rest import ( + "context" "errors" "fmt" "gopkg.in/ns1/ns1-go.v2/rest/model/dhcp" @@ -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) @@ -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 @@ -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") @@ -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 @@ -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) } diff --git a/rest/pulsar_job.go b/rest/pulsar_job.go index c65cf47..fc11a48 100644 --- a/rest/pulsar_job.go +++ b/rest/pulsar_job.go @@ -1,6 +1,7 @@ package rest import ( + "context" "errors" "fmt" "net/http" @@ -15,11 +16,17 @@ type PulsarJobsService service // // NS1 API docs: https://ns1.com/api/#getlist-jobs-within-an-app func (s *PulsarJobsService) List(appId string) ([]*pulsar.PulsarJob, *http.Response, error) { + return s.ListWithContext(context.Background(), appId) +} + +// ListWithContext is the same as List, but takes a context. +func (s *PulsarJobsService) ListWithContext(ctx context.Context, appId string) ([]*pulsar.PulsarJob, *http.Response, error) { path := fmt.Sprintf("pulsar/apps/%s/jobs", appId) req, err := s.client.NewRequest("GET", path, nil) if err != nil { return nil, nil, err } + req = req.WithContext(ctx) jl := []*pulsar.PulsarJob{} var resp *http.Response @@ -41,12 +48,18 @@ func (s *PulsarJobsService) List(appId string) ([]*pulsar.PulsarJob, *http.Respo // // NS1 API docs: https://ns1.com/api/#getview-job-details func (s *PulsarJobsService) Get(appId string, jobId string) (*pulsar.PulsarJob, *http.Response, error) { + return s.GetWithContext(context.Background(), appId, jobId) +} + +// GetWithContext is the same as Get, but takes a context. +func (s *PulsarJobsService) GetWithContext(ctx context.Context, appId string, jobId string) (*pulsar.PulsarJob, *http.Response, error) { path := fmt.Sprintf("pulsar/apps/%s/jobs/%s", appId, jobId) req, err := s.client.NewRequest("GET", path, nil) if err != nil { return nil, nil, err } + req = req.WithContext(ctx) var job pulsar.PulsarJob @@ -73,12 +86,18 @@ func (s *PulsarJobsService) Get(appId string, jobId string) (*pulsar.PulsarJob, // // NS1 API docs: https://ns1.com/api/#putcreate-a-pulsar-job func (s *PulsarJobsService) Create(j *pulsar.PulsarJob) (*http.Response, error) { + return s.CreateWithContext(context.Background(), j) +} + +// CreateWithContext is the same as Create, but takes a context. +func (s *PulsarJobsService) CreateWithContext(ctx context.Context, j *pulsar.PulsarJob) (*http.Response, error) { path := fmt.Sprintf("pulsar/apps/%s/jobs", j.AppID) req, err := s.client.NewRequest("PUT", path, j) if err != nil { return nil, err } + req = req.WithContext(ctx) // Update job fields with data from api(ensure consistent) resp, err := s.client.Do(req, j) @@ -100,12 +119,18 @@ func (s *PulsarJobsService) Create(j *pulsar.PulsarJob) (*http.Response, error) // Only the fields to be updated are required in the given job. // NS1 API docs: https://ns1.com/api/#postmodify-a-pulsar-job func (s *PulsarJobsService) Update(j *pulsar.PulsarJob) (*http.Response, error) { + return s.UpdateWithContext(context.Background(), j) +} + +// UpdateWithContext is the same as Update, but takes a context. +func (s *PulsarJobsService) UpdateWithContext(ctx context.Context, j *pulsar.PulsarJob) (*http.Response, error) { path := fmt.Sprintf("pulsar/apps/%s/jobs/%s", j.AppID, j.JobID) req, err := s.client.NewRequest("POST", path, j) if err != nil { return nil, err } + req = req.WithContext(ctx) // Update jobs fields with data from api(ensure consistent) resp, err := s.client.Do(req, j) @@ -130,12 +155,18 @@ func (s *PulsarJobsService) Update(j *pulsar.PulsarJob) (*http.Response, error) // // NS1 API docs: https://ns1.com/api/#deletedelete-a-pulsar-job func (s *PulsarJobsService) Delete(pulsarJob *pulsar.PulsarJob) (*http.Response, error) { + return s.DeleteWithContext(context.Background(), pulsarJob) +} + +// DeleteWithContext is the same as Delete, but takes a context. +func (s *PulsarJobsService) DeleteWithContext(ctx context.Context, pulsarJob *pulsar.PulsarJob) (*http.Response, error) { path := fmt.Sprintf("pulsar/apps/%s/jobs/%s", pulsarJob.AppID, pulsarJob.JobID) 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 { diff --git a/rest/record.go b/rest/record.go index 4aed429..d411bab 100644 --- a/rest/record.go +++ b/rest/record.go @@ -1,6 +1,7 @@ package rest import ( + "context" "errors" "fmt" "net/http" @@ -15,12 +16,18 @@ type RecordsService service // // NS1 API docs: https://ns1.com/api/#record-get func (s *RecordsService) Get(zone, domain, t string) (*dns.Record, *http.Response, error) { + return s.GetWithContext(context.Background(), zone, domain, t) +} + +// GetWithContext is the same as Get, but takes a context. +func (s *RecordsService) GetWithContext(ctx context.Context, zone, domain, t string) (*dns.Record, *http.Response, error) { path := fmt.Sprintf("zones/%s/%s/%s", zone, domain, t) req, err := s.client.NewRequest("GET", path, nil) if err != nil { return nil, nil, err } + req = req.WithContext(ctx) var r dns.Record resp, err := s.client.Do(req, &r) @@ -42,12 +49,18 @@ func (s *RecordsService) Get(zone, domain, t string) (*dns.Record, *http.Respons // The given record must have at least one answer. // NS1 API docs: https://ns1.com/api/#record-put func (s *RecordsService) Create(r *dns.Record) (*http.Response, error) { + return s.CreateWithContext(context.Background(), r) +} + +// CreateWithContext is the same as Create, but takes a context. +func (s *RecordsService) CreateWithContext(ctx context.Context, r *dns.Record) (*http.Response, error) { path := fmt.Sprintf("zones/%s/%s/%s", r.Zone, r.Domain, r.Type) req, err := s.client.NewRequest("PUT", path, &r) if err != nil { return nil, err } + req = req.WithContext(ctx) // Update record fields with data from api(ensure consistent) resp, err := s.client.Do(req, &r) @@ -72,12 +85,18 @@ func (s *RecordsService) Create(r *dns.Record) (*http.Response, error) { // Only the fields to be updated are required in the given record. // NS1 API docs: https://ns1.com/api/#record-post func (s *RecordsService) Update(r *dns.Record) (*http.Response, error) { + return s.UpdateWithContext(context.Background(), r) +} + +// UpdateWithContext is the same as Update, but takes a context. +func (s *RecordsService) UpdateWithContext(ctx context.Context, r *dns.Record) (*http.Response, error) { path := fmt.Sprintf("zones/%s/%s/%s", r.Zone, r.Domain, r.Type) req, err := s.client.NewRequest("POST", path, &r) if err != nil { return nil, err } + req = req.WithContext(ctx) // Update records fields with data from api(ensure consistent) resp, err := s.client.Do(req, &r) @@ -103,12 +122,18 @@ func (s *RecordsService) Update(r *dns.Record) (*http.Response, error) { // // NS1 API docs: https://ns1.com/api/#record-delete func (s *RecordsService) Delete(zone string, domain string, t string) (*http.Response, error) { + return s.DeleteWithContext(context.Background(), zone, domain, t) +} + +// DeleteWithContext is the same as Delete, but takes a context. +func (s *RecordsService) DeleteWithContext(ctx context.Context, zone string, domain string, t string) (*http.Response, error) { path := fmt.Sprintf("zones/%s/%s/%s", zone, domain, t) 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 {