diff --git a/clients/scheduler/interval.go b/clients/scheduler/interval.go index d9ff989d..04356d4b 100644 --- a/clients/scheduler/interval.go +++ b/clients/scheduler/interval.go @@ -12,9 +12,7 @@ * the License. *******************************************************************************/ -/* -Package scheduler provides clients used for integration with the support-scheduler service. -*/ +// scheduler provides clients used for integration with the support-scheduler service. package scheduler import ( @@ -25,12 +23,11 @@ import ( "github.com/edgexfoundry/go-mod-core-contracts/clients" "github.com/edgexfoundry/go-mod-core-contracts/clients/interfaces" "github.com/edgexfoundry/go-mod-core-contracts/clients/types" + "github.com/edgexfoundry/go-mod-core-contracts/clients/urlclient" "github.com/edgexfoundry/go-mod-core-contracts/models" ) -/* -IntervalClient defines the interface for interactions with the Interval endpoint on the EdgeX Foundry support-scheduler service. -*/ +// IntervalClient defines the interface for interactions with the Interval endpoint on support-scheduler. type IntervalClient interface { // Add a new scheduling interval Add(dev *models.Interval, ctx context.Context) (string, error) @@ -49,84 +46,100 @@ type IntervalClient interface { } type intervalRestClient struct { - url string - endpoint interfaces.Endpointer + urlClient interfaces.URLClient } // NewIntervalClient creates an instance of IntervalClient func NewIntervalClient(params types.EndpointParams, m interfaces.Endpointer) IntervalClient { - s := intervalRestClient{endpoint: m} - s.init(params) - return &s + return &intervalRestClient{urlClient: urlclient.New(params, m)} } -func (s *intervalRestClient) init(params types.EndpointParams) { - if params.UseRegistry { - go func(ch chan string) { - for { - select { - case url := <-ch: - s.url = url - } - } - }(s.endpoint.Monitor(params)) - } else { - s.url = params.Url +func (ic *intervalRestClient) Add(interval *models.Interval, ctx context.Context) (string, error) { + url, err := ic.urlClient.Prefix() + if err != nil { + return "", err } -} -func (s *intervalRestClient) Add(interval *models.Interval, ctx context.Context) (string, error) { - return clients.PostJsonRequest(s.url, interval, ctx) + return clients.PostJsonRequest(url, interval, ctx) } -func (s *intervalRestClient) Delete(id string, ctx context.Context) error { - return clients.DeleteRequest(s.url+"/id/"+id, ctx) -} +func (ic *intervalRestClient) Delete(id string, ctx context.Context) error { + urlPrefix, err := ic.urlClient.Prefix() + if err != nil { + return err + } -func (s *intervalRestClient) DeleteByName(name string, ctx context.Context) error { - return clients.DeleteRequest(s.url+"/name/"+url.QueryEscape(name), ctx) + return clients.DeleteRequest(urlPrefix+"/id/"+id, ctx) } -func (s *intervalRestClient) Interval(id string, ctx context.Context) (models.Interval, error) { - return s.requestInterval(s.url+"/"+id, ctx) +func (ic *intervalRestClient) DeleteByName(name string, ctx context.Context) error { + urlPrefix, err := ic.urlClient.Prefix() + if err != nil { + return err + } + + return clients.DeleteRequest(urlPrefix+"/name/"+url.QueryEscape(name), ctx) } -func (s *intervalRestClient) IntervalForName(name string, ctx context.Context) (models.Interval, error) { - return s.requestInterval(s.url+"/name/"+url.QueryEscape(name), ctx) +func (ic *intervalRestClient) Interval(id string, ctx context.Context) (models.Interval, error) { + return ic.requestInterval("/"+id, ctx) } -func (s *intervalRestClient) Intervals(ctx context.Context) ([]models.Interval, error) { - return s.requestIntervalSlice(s.url, ctx) +func (ic *intervalRestClient) IntervalForName(name string, ctx context.Context) (models.Interval, error) { + return ic.requestInterval("/name/"+url.QueryEscape(name), ctx) } -func (s *intervalRestClient) Update(interval models.Interval, ctx context.Context) error { - return clients.UpdateRequest(s.url, interval, ctx) +func (ic *intervalRestClient) Intervals(ctx context.Context) ([]models.Interval, error) { + return ic.requestIntervalSlice("", ctx) } -// -// Helper functions -// +func (ic *intervalRestClient) Update(interval models.Interval, ctx context.Context) error { + url, err := ic.urlClient.Prefix() + if err != nil { + return err + } + + return clients.UpdateRequest(url, interval, ctx) +} // helper request and decode an interval -func (s *intervalRestClient) requestInterval(url string, ctx context.Context) (models.Interval, error) { - data, err := clients.GetRequest(url, ctx) +func (ic *intervalRestClient) requestInterval(urlSuffix string, ctx context.Context) (models.Interval, error) { + urlPrefix, err := ic.urlClient.Prefix() + if err != nil { + return models.Interval{}, err + } + + data, err := clients.GetRequest(urlPrefix+urlSuffix, ctx) if err != nil { return models.Interval{}, err } interval := models.Interval{} err = json.Unmarshal(data, &interval) - return interval, err + if err != nil { + return models.Interval{}, err + } + + return interval, nil } // helper returns a slice of intervals -func (s *intervalRestClient) requestIntervalSlice(url string, ctx context.Context) ([]models.Interval, error) { - data, err := clients.GetRequest(url, ctx) +func (ic *intervalRestClient) requestIntervalSlice(urlSuffix string, ctx context.Context) ([]models.Interval, error) { + urlPrefix, err := ic.urlClient.Prefix() + if err != nil { + return []models.Interval{}, err + } + + data, err := clients.GetRequest(urlPrefix+urlSuffix, ctx) if err != nil { return []models.Interval{}, err } sSlice := make([]models.Interval, 0) err = json.Unmarshal(data, &sSlice) - return sSlice, err + if err != nil { + return []models.Interval{}, err + } + + return sSlice, nil } diff --git a/clients/scheduler/interval_action.go b/clients/scheduler/interval_action.go index d69e5c72..bae18ea5 100644 --- a/clients/scheduler/interval_action.go +++ b/clients/scheduler/interval_action.go @@ -22,12 +22,11 @@ import ( "github.com/edgexfoundry/go-mod-core-contracts/clients" "github.com/edgexfoundry/go-mod-core-contracts/clients/interfaces" "github.com/edgexfoundry/go-mod-core-contracts/clients/types" + "github.com/edgexfoundry/go-mod-core-contracts/clients/urlclient" "github.com/edgexfoundry/go-mod-core-contracts/models" ) -/* -IntervalActionClient defines the interface for interactions with the IntervalAction endpoint on the EdgeX Foundry support-scheduler service. -*/ +// IntervalActionClient defines the interface for interactions with the IntervalAction endpoint on support-scheduler. type IntervalActionClient interface { // Add a new schedule interval action Add(dev *models.IntervalAction, ctx context.Context) (string, error) @@ -48,84 +47,110 @@ type IntervalActionClient interface { } type intervalActionRestClient struct { - url string - endpoint interfaces.Endpointer + urlClient interfaces.URLClient } // NewIntervalActionClient creates an instance of IntervalActionClient func NewIntervalActionClient(params types.EndpointParams, m interfaces.Endpointer) IntervalActionClient { - s := intervalActionRestClient{endpoint: m} - s.init(params) - return &s + return &intervalActionRestClient{urlClient: urlclient.New(params, m)} } -func (s *intervalActionRestClient) init(params types.EndpointParams) { - if params.UseRegistry { - go func(ch chan string) { - for { - select { - case url := <-ch: - s.url = url - } - } - }(s.endpoint.Monitor(params)) - } else { - s.url = params.Url +// Helper method to request and decode an interval action +func (iac *intervalActionRestClient) requestIntervalAction( + urlSuffix string, + ctx context.Context) (models.IntervalAction, error) { + + urlPrefix, err := iac.urlClient.Prefix() + if err != nil { + return models.IntervalAction{}, err } -} -// Helper method to request and decode an interval action -func (s *intervalActionRestClient) requestIntervalAction(url string, ctx context.Context) (models.IntervalAction, error) { - data, err := clients.GetRequest(url, ctx) + data, err := clients.GetRequest(urlPrefix+urlSuffix, ctx) if err != nil { return models.IntervalAction{}, err } ia := models.IntervalAction{} err = json.Unmarshal(data, &ia) - return ia, err + if err != nil { + return models.IntervalAction{}, err + } + + return ia, nil } // Helper method to request and decode an interval action slice -func (s *intervalActionRestClient) requestIntervalActionSlice(url string, ctx context.Context) ([]models.IntervalAction, error) { - data, err := clients.GetRequest(url, ctx) +func (iac *intervalActionRestClient) requestIntervalActionSlice( + urlSuffix string, + ctx context.Context) ([]models.IntervalAction, error) { + + urlPrefix, err := iac.urlClient.Prefix() + if err != nil { + return nil, err + } + + data, err := clients.GetRequest(urlPrefix+urlSuffix, ctx) if err != nil { return []models.IntervalAction{}, err } iaSlice := make([]models.IntervalAction, 0) err = json.Unmarshal(data, &iaSlice) - return iaSlice, err + if err != nil { + return []models.IntervalAction{}, err + } + + return iaSlice, nil } -func (s *intervalActionRestClient) Add(ia *models.IntervalAction, ctx context.Context) (string, error) { - return clients.PostJsonRequest(s.url, ia, ctx) +func (iac *intervalActionRestClient) Add(ia *models.IntervalAction, ctx context.Context) (string, error) { + url, err := iac.urlClient.Prefix() + if err != nil { + return "", err + } + + return clients.PostJsonRequest(url, ia, ctx) } -func (s *intervalActionRestClient) Delete(id string, ctx context.Context) error { - return clients.DeleteRequest(s.url+"/id/"+id, ctx) +func (iac *intervalActionRestClient) Delete(id string, ctx context.Context) error { + urlPrefix, err := iac.urlClient.Prefix() + if err != nil { + return err + } + + return clients.DeleteRequest(urlPrefix+"/id/"+id, ctx) } -func (s *intervalActionRestClient) DeleteByName(name string, ctx context.Context) error { - return clients.DeleteRequest(s.url+"/name/"+url.QueryEscape(name), ctx) +func (iac *intervalActionRestClient) DeleteByName(name string, ctx context.Context) error { + urlPrefix, err := iac.urlClient.Prefix() + if err != nil { + return err + } + + return clients.DeleteRequest(urlPrefix+"/name/"+url.QueryEscape(name), ctx) } -func (s *intervalActionRestClient) IntervalAction(id string, ctx context.Context) (models.IntervalAction, error) { - return s.requestIntervalAction(s.url+"/"+id, ctx) +func (iac *intervalActionRestClient) IntervalAction(id string, ctx context.Context) (models.IntervalAction, error) { + return iac.requestIntervalAction("/"+id, ctx) } -func (s *intervalActionRestClient) IntervalActionForName(name string, ctx context.Context) (models.IntervalAction, error) { - return s.requestIntervalAction(s.url+"/name/"+url.QueryEscape(name), ctx) +func (iac *intervalActionRestClient) IntervalActionForName(name string, ctx context.Context) (models.IntervalAction, error) { + return iac.requestIntervalAction("/name/"+url.QueryEscape(name), ctx) } -func (s *intervalActionRestClient) IntervalActions(ctx context.Context) ([]models.IntervalAction, error) { - return s.requestIntervalActionSlice(s.url, ctx) +func (iac *intervalActionRestClient) IntervalActions(ctx context.Context) ([]models.IntervalAction, error) { + return iac.requestIntervalActionSlice("", ctx) } -func (s *intervalActionRestClient) IntervalActionsForTargetByName(name string, ctx context.Context) ([]models.IntervalAction, error) { - return s.requestIntervalActionSlice(s.url+"/target/"+url.QueryEscape(name), ctx) +func (iac *intervalActionRestClient) IntervalActionsForTargetByName(name string, ctx context.Context) ([]models.IntervalAction, error) { + return iac.requestIntervalActionSlice("/target/"+url.QueryEscape(name), ctx) } -func (s *intervalActionRestClient) Update(ia models.IntervalAction, ctx context.Context) error { - return clients.UpdateRequest(s.url, ia, ctx) +func (iac *intervalActionRestClient) Update(ia models.IntervalAction, ctx context.Context) error { + url, err := iac.urlClient.Prefix() + if err != nil { + return err + } + + return clients.UpdateRequest(url, ia, ctx) }