diff --git a/clients/metadata/addressable.go b/clients/metadata/addressable.go index ba713425..9e985bfc 100644 --- a/clients/metadata/addressable.go +++ b/clients/metadata/addressable.go @@ -12,9 +12,7 @@ * the License. *******************************************************************************/ -/* - Package metadata provides clients used for integration with the core-metadata service. -*/ +// metadata provides clients used for integration with the core-metadata service. package metadata import ( @@ -24,13 +22,12 @@ 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/rest" "github.com/edgexfoundry/go-mod-core-contracts/clients/types" "github.com/edgexfoundry/go-mod-core-contracts/models" ) -/* -AddressableClient defines the interface for interactions with the Addressable endpoint on the EdgeX Foundry core-metadata service. -*/ +// AddressableClient defines the interface for interactions with the Addressable endpoint on core-metadata. type AddressableClient interface { // Add creates a new Addressable and returns the ID of the new item if successful. Add(addr *models.Addressable, ctx context.Context) (string, error) @@ -45,35 +42,23 @@ type AddressableClient interface { } type addressableRestClient struct { - url string - endpoint interfaces.Endpointer + client interfaces.RestClientBuilder } // NewAddressableClient creates an instance of AddressableClient func NewAddressableClient(params types.EndpointParams, m interfaces.Endpointer) AddressableClient { - a := addressableRestClient{endpoint: m} - a.init(params) + a := addressableRestClient{client: rest.ClientFactory(params, m)} return &a } -func (a *addressableRestClient) init(params types.EndpointParams) { - if params.UseRegistry { - go func(ch chan string) { - for { - select { - case url := <-ch: - a.url = url - } - } - }(a.endpoint.Monitor(params)) - } else { - a.url = params.Url +// Helper method to request and decode an addressable +func (a *addressableRestClient) requestAddressable(urlSuffix string, ctx context.Context) (models.Addressable, error) { + urlPrefix, err := a.client.URLPrefix() + if err != nil { + return models.Addressable{}, err } -} -// Helper method to request and decode an addressable -func (a *addressableRestClient) requestAddressable(url string, ctx context.Context) (models.Addressable, error) { - data, err := clients.GetRequest(url, ctx) + data, err := clients.GetRequest(urlPrefix+urlSuffix, ctx) if err != nil { return models.Addressable{}, err } @@ -84,21 +69,36 @@ func (a *addressableRestClient) requestAddressable(url string, ctx context.Conte } func (a *addressableRestClient) Add(addr *models.Addressable, ctx context.Context) (string, error) { - return clients.PostJsonRequest(a.url, addr, ctx) + serviceURL, err := a.client.URLPrefix() + if err != nil { + return "", err + } + + return clients.PostJsonRequest(serviceURL, addr, ctx) } func (a *addressableRestClient) Addressable(id string, ctx context.Context) (models.Addressable, error) { - return a.requestAddressable(a.url+"/"+id, ctx) + return a.requestAddressable("/"+id, ctx) } func (a *addressableRestClient) AddressableForName(name string, ctx context.Context) (models.Addressable, error) { - return a.requestAddressable(a.url+"/name/"+url.QueryEscape(name), ctx) + return a.requestAddressable("/name/"+url.QueryEscape(name), ctx) } func (a *addressableRestClient) Update(addr models.Addressable, ctx context.Context) error { - return clients.UpdateRequest(a.url, addr, ctx) + serviceURL, err := a.client.URLPrefix() + if err != nil { + return err + } + + return clients.UpdateRequest(serviceURL, addr, ctx) } func (a *addressableRestClient) Delete(id string, ctx context.Context) error { - return clients.DeleteRequest(a.url+"/id/"+id, ctx) + serviceURL, err := a.client.URLPrefix() + if err != nil { + return err + } + + return clients.DeleteRequest(serviceURL+"/id/"+id, ctx) } diff --git a/clients/metadata/addressable_test.go b/clients/metadata/addressable_test.go index eb66f2cb..d0cb69cb 100644 --- a/clients/metadata/addressable_test.go +++ b/clients/metadata/addressable_test.go @@ -20,12 +20,12 @@ import ( "net/http" "net/http/httptest" "testing" - "time" + + "github.com/google/uuid" "github.com/edgexfoundry/go-mod-core-contracts/clients" "github.com/edgexfoundry/go-mod-core-contracts/clients/types" "github.com/edgexfoundry/go-mod-core-contracts/models" - "github.com/google/uuid" ) func TestNewAddressableClientWithConsul(t *testing.T) { @@ -44,11 +44,12 @@ func TestNewAddressableClientWithConsul(t *testing.T) { t.Error("sc is not of expected type") } - time.Sleep(25 * time.Millisecond) - if len(r.url) == 0 { + url, err := r.client.URLPrefix() + + if err != nil { t.Error("url was not initialized") - } else if r.url != addressableURL { - t.Errorf("unexpected url value %s", r.url) + } else if url != addressableURL { + t.Errorf("unexpected url value %s", url) } } diff --git a/clients/metadata/command.go b/clients/metadata/command.go index 533b5c21..b70857e5 100644 --- a/clients/metadata/command.go +++ b/clients/metadata/command.go @@ -20,13 +20,12 @@ 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/rest" "github.com/edgexfoundry/go-mod-core-contracts/clients/types" "github.com/edgexfoundry/go-mod-core-contracts/models" ) -/* -CommandClient defines the interface for interactions with the Command endpoint on the EdgeX Foundry core-metadata service. -*/ +// CommandClient defines the interface for interactions with the Command endpoint on core-metadata. type CommandClient interface { // Add a new command Add(com *models.Command, ctx context.Context) (string, error) @@ -45,35 +44,23 @@ type CommandClient interface { } type commandRestClient struct { - url string - endpoint interfaces.Endpointer + client interfaces.RestClientBuilder } // NewCommandClient creates an instance of CommandClient func NewCommandClient(params types.EndpointParams, m interfaces.Endpointer) CommandClient { - c := commandRestClient{endpoint: m} - c.init(params) + c := commandRestClient{client: rest.ClientFactory(params, m)} return &c } -func (c *commandRestClient) init(params types.EndpointParams) { - if params.UseRegistry { - go func(ch chan string) { - for { - select { - case url := <-ch: - c.url = url - } - } - }(c.endpoint.Monitor(params)) - } else { - c.url = params.Url +// Helper method to request and decode a command +func (c *commandRestClient) requestCommand(urlSuffix string, ctx context.Context) (models.Command, error) { + urlPrefix, err := c.client.URLPrefix() + if err != nil { + return models.Command{}, err } -} -// Helper method to request and decode a command -func (c *commandRestClient) requestCommand(url string, ctx context.Context) (models.Command, error) { - data, err := clients.GetRequest(url, ctx) + data, err := clients.GetRequest(urlPrefix+urlSuffix, ctx) if err != nil { return models.Command{}, err } @@ -84,8 +71,13 @@ func (c *commandRestClient) requestCommand(url string, ctx context.Context) (mod } // Helper method to request and decode a command slice -func (c *commandRestClient) requestCommandSlice(url string, ctx context.Context) ([]models.Command, error) { - data, err := clients.GetRequest(url, ctx) +func (c *commandRestClient) requestCommandSlice(urlSuffix string, ctx context.Context) ([]models.Command, error) { + urlPrefix, err := c.client.URLPrefix() + if err != nil { + return nil, err + } + + data, err := clients.GetRequest(urlPrefix+urlSuffix, ctx) if err != nil { return []models.Command{}, err } @@ -96,29 +88,44 @@ func (c *commandRestClient) requestCommandSlice(url string, ctx context.Context) } func (c *commandRestClient) Command(id string, ctx context.Context) (models.Command, error) { - return c.requestCommand(c.url+"/"+id, ctx) + return c.requestCommand("/"+id, ctx) } func (c *commandRestClient) Commands(ctx context.Context) ([]models.Command, error) { - return c.requestCommandSlice(c.url, ctx) + return c.requestCommandSlice("", ctx) } func (c *commandRestClient) CommandsForName(name string, ctx context.Context) ([]models.Command, error) { - return c.requestCommandSlice(c.url+"/name/"+name, ctx) + return c.requestCommandSlice("/name/"+name, ctx) } func (c *commandRestClient) CommandsForDeviceId(id string, ctx context.Context) ([]models.Command, error) { - return c.requestCommandSlice(c.url+"/device/"+id, ctx) + return c.requestCommandSlice("/device/"+id, ctx) } func (c *commandRestClient) Add(com *models.Command, ctx context.Context) (string, error) { - return clients.PostJsonRequest(c.url, com, ctx) + serviceURL, err := c.client.URLPrefix() + if err != nil { + return "", err + } + + return clients.PostJsonRequest(serviceURL, com, ctx) } func (c *commandRestClient) Update(com models.Command, ctx context.Context) error { - return clients.UpdateRequest(c.url, com, ctx) + serviceURL, err := c.client.URLPrefix() + if err != nil { + return err + } + + return clients.UpdateRequest(serviceURL, com, ctx) } func (c *commandRestClient) Delete(id string, ctx context.Context) error { - return clients.DeleteRequest(c.url+"/id/"+id, ctx) + serviceURL, err := c.client.URLPrefix() + if err != nil { + return err + } + + return clients.DeleteRequest(serviceURL+"/id/"+id, ctx) } diff --git a/clients/metadata/command_test.go b/clients/metadata/command_test.go index 1a02f15d..ced4de37 100644 --- a/clients/metadata/command_test.go +++ b/clients/metadata/command_test.go @@ -16,7 +16,6 @@ package metadata import ( "testing" - "time" "github.com/edgexfoundry/go-mod-core-contracts/clients" "github.com/edgexfoundry/go-mod-core-contracts/clients/types" @@ -38,10 +37,11 @@ func TestNewCommandClientWithConsul(t *testing.T) { t.Error("cc is not of expected type") } - time.Sleep(25 * time.Millisecond) - if len(r.url) == 0 { + url, err := r.client.URLPrefix() + + if err != nil { t.Error("url was not initialized") - } else if r.url != deviceUrl { - t.Errorf("unexpected url value %s", r.url) + } else if url != deviceUrl { + t.Errorf("unexpected url value %s", url) } } diff --git a/clients/metadata/device.go b/clients/metadata/device.go index 2641d3fb..fc6b6d78 100644 --- a/clients/metadata/device.go +++ b/clients/metadata/device.go @@ -27,9 +27,7 @@ import ( "github.com/edgexfoundry/go-mod-core-contracts/models" ) -/* -DeviceClient defines the interface for interactions with the Device endpoint on the EdgeX Foundry core-metadata service. -*/ +// DeviceClient defines the interface for interactions with the Device endpoint on core-metadata. type DeviceClient interface { // Add creates a new device Add(dev *models.Device, ctx context.Context) (string, error) diff --git a/clients/metadata/device_profile.go b/clients/metadata/device_profile.go index e7581b18..ff7348c1 100644 --- a/clients/metadata/device_profile.go +++ b/clients/metadata/device_profile.go @@ -21,14 +21,12 @@ 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/rest" "github.com/edgexfoundry/go-mod-core-contracts/clients/types" "github.com/edgexfoundry/go-mod-core-contracts/models" ) -/* -DeviceProfileClient defines the interface for interactions with the DeviceProfile endpoint on the EdgeX Foundry -core-metadata service. -*/ +// DeviceProfileClient defines the interface for interactions with the DeviceProfile endpoint on metadata. type DeviceProfileClient interface { // Add a new device profile Add(dp *models.DeviceProfile, ctx context.Context) (string, error) @@ -51,35 +49,26 @@ type DeviceProfileClient interface { } type deviceProfileRestClient struct { - url string - endpoint interfaces.Endpointer + client interfaces.RestClientBuilder } // Return an instance of DeviceProfileClient func NewDeviceProfileClient(params types.EndpointParams, m interfaces.Endpointer) DeviceProfileClient { - d := deviceProfileRestClient{endpoint: m} - d.init(params) + d := deviceProfileRestClient{client: rest.ClientFactory(params, m)} return &d } -func (d *deviceProfileRestClient) init(params types.EndpointParams) { - if params.UseRegistry { - go func(ch chan string) { - for { - select { - case url := <-ch: - d.url = url - } - } - }(d.endpoint.Monitor(params)) - } else { - d.url = params.Url +// Helper method to request and decode a device profile +func (dpc *deviceProfileRestClient) requestDeviceProfile( + urlSuffix string, + ctx context.Context) (models.DeviceProfile, error) { + + urlPrefix, err := dpc.client.URLPrefix() + if err != nil { + return models.DeviceProfile{}, err } -} -// Helper method to request and decode a device profile -func (dpc *deviceProfileRestClient) requestDeviceProfile(url string, ctx context.Context) (models.DeviceProfile, error) { - data, err := clients.GetRequest(url, ctx) + data, err := clients.GetRequest(urlPrefix+urlSuffix, ctx) if err != nil { return models.DeviceProfile{}, err } @@ -90,8 +79,11 @@ func (dpc *deviceProfileRestClient) requestDeviceProfile(url string, ctx context } // Helper method to request and decode a device profile slice -func (dpc *deviceProfileRestClient) requestDeviceProfileSlice(url string, ctx context.Context) ([]models.DeviceProfile, error) { - data, err := clients.GetRequest(url, ctx) +func (dpc *deviceProfileRestClient) requestDeviceProfileSlice( + urlSuffix string, + ctx context.Context) ([]models.DeviceProfile, error) { + + data, err := clients.GetRequest(urlSuffix, ctx) if err != nil { return []models.DeviceProfile{}, err } @@ -102,39 +94,69 @@ func (dpc *deviceProfileRestClient) requestDeviceProfileSlice(url string, ctx co } func (dpc *deviceProfileRestClient) Add(dp *models.DeviceProfile, ctx context.Context) (string, error) { - return clients.PostJsonRequest(dpc.url, dp, ctx) + serviceURL, err := dpc.client.URLPrefix() + if err != nil { + return "", err + } + + return clients.PostJsonRequest(serviceURL, dp, ctx) } func (dpc *deviceProfileRestClient) Delete(id string, ctx context.Context) error { - return clients.DeleteRequest(dpc.url+"/id/"+id, ctx) + serviceURL, err := dpc.client.URLPrefix() + if err != nil { + return err + } + + return clients.DeleteRequest(serviceURL+"/id/"+id, ctx) } func (dpc *deviceProfileRestClient) DeleteByName(name string, ctx context.Context) error { - return clients.DeleteRequest(dpc.url+"/name/"+url.QueryEscape(name), ctx) + serviceURL, err := dpc.client.URLPrefix() + if err != nil { + return err + } + + return clients.DeleteRequest(serviceURL+"/name/"+url.QueryEscape(name), ctx) } func (dpc *deviceProfileRestClient) DeviceProfile(id string, ctx context.Context) (models.DeviceProfile, error) { - return dpc.requestDeviceProfile(dpc.url+"/"+id, ctx) + return dpc.requestDeviceProfile("/"+id, ctx) } func (dpc *deviceProfileRestClient) DeviceProfiles(ctx context.Context) ([]models.DeviceProfile, error) { - return dpc.requestDeviceProfileSlice(dpc.url, ctx) + return dpc.requestDeviceProfileSlice("", ctx) } func (dpc *deviceProfileRestClient) DeviceProfileForName(name string, ctx context.Context) (models.DeviceProfile, error) { - return dpc.requestDeviceProfile(dpc.url+"/name/"+name, ctx) + return dpc.requestDeviceProfile("/name/"+name, ctx) } func (dpc *deviceProfileRestClient) Update(dp models.DeviceProfile, ctx context.Context) error { - return clients.UpdateRequest(dpc.url, dp, ctx) + serviceURL, err := dpc.client.URLPrefix() + if err != nil { + return err + } + + return clients.UpdateRequest(serviceURL, dp, ctx) } func (dpc *deviceProfileRestClient) Upload(yamlString string, ctx context.Context) (string, error) { + serviceURL, err := dpc.client.URLPrefix() + if err != nil { + return "", err + } + ctx = context.WithValue(ctx, clients.ContentType, clients.ContentTypeYAML) - return clients.PostRequest(dpc.url+"/upload", []byte(yamlString), ctx) + return clients.PostRequest(serviceURL+"/upload", []byte(yamlString), ctx) } func (dpc *deviceProfileRestClient) UploadFile(yamlFilePath string, ctx context.Context) (string, error) { - return clients.UploadFileRequest(dpc.url+"/uploadfile", yamlFilePath, ctx) + serviceURL, err := dpc.client.URLPrefix() + if err != nil { + return "", err + } + + return clients.UploadFileRequest(serviceURL+"/uploadfile", yamlFilePath, ctx) } diff --git a/clients/metadata/device_profile_test.go b/clients/metadata/device_profile_test.go index 1c8f5e75..616ff870 100644 --- a/clients/metadata/device_profile_test.go +++ b/clients/metadata/device_profile_test.go @@ -19,7 +19,6 @@ import ( "net/http" "net/http/httptest" "testing" - "time" "github.com/edgexfoundry/go-mod-core-contracts/clients" "github.com/edgexfoundry/go-mod-core-contracts/clients/types" @@ -42,11 +41,12 @@ func TestNewDeviceProfileClientWithConsul(t *testing.T) { t.Error("cc is not of expected type") } - time.Sleep(25 * time.Millisecond) - if len(r.url) == 0 { + url, err := r.client.URLPrefix() + + if err != nil { t.Error("url was not initialized") - } else if r.url != deviceUrl { - t.Errorf("unexpected url value %s", r.url) + } else if url != deviceUrl { + t.Errorf("unexpected url value %s", url) } } diff --git a/clients/metadata/device_service.go b/clients/metadata/device_service.go index f1a5324a..b02786b2 100644 --- a/clients/metadata/device_service.go +++ b/clients/metadata/device_service.go @@ -21,14 +21,12 @@ 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/rest" "github.com/edgexfoundry/go-mod-core-contracts/clients/types" "github.com/edgexfoundry/go-mod-core-contracts/models" ) -/* -DeviceServiceClient defines the interface for interactions with the DeviceService endpoint on the EdgeX Foundry -core-metadata service. -*/ +// DeviceServiceClient defines the interface for interactions with the DeviceService endpoint on metadata. type DeviceServiceClient interface { // Add a new device service Add(ds *models.DeviceService, ctx context.Context) (string, error) @@ -41,58 +39,60 @@ type DeviceServiceClient interface { } type deviceServiceRestClient struct { - url string - endpoint interfaces.Endpointer + client interfaces.RestClientBuilder } // NewDeviceServiceClient creates an instance of DeviceServiceClient func NewDeviceServiceClient(params types.EndpointParams, m interfaces.Endpointer) DeviceServiceClient { - s := deviceServiceRestClient{endpoint: m} - s.init(params) + s := deviceServiceRestClient{client: rest.ClientFactory(params, m)} return &s } -func (d *deviceServiceRestClient) init(params types.EndpointParams) { - if params.UseRegistry { - go func(ch chan string) { - for { - select { - case url := <-ch: - d.url = url - } - } - }(d.endpoint.Monitor(params)) - } else { - d.url = params.Url +func (dsc *deviceServiceRestClient) UpdateLastConnected(id string, time int64, ctx context.Context) error { + serviceURL, err := dsc.client.URLPrefix() + if err != nil { + return err } + + _, err = clients.PutRequest(serviceURL+"/"+id+"/lastconnected/"+strconv.FormatInt(time, 10), nil, ctx) + return err } -// Helper method to request and decode a device service -func (s *deviceServiceRestClient) requestDeviceService(url string, ctx context.Context) (models.DeviceService, error) { - data, err := clients.GetRequest(url, ctx) +func (dsc *deviceServiceRestClient) UpdateLastReported(id string, time int64, ctx context.Context) error { + serviceURL, err := dsc.client.URLPrefix() if err != nil { - return models.DeviceService{}, err + return err } - ds := models.DeviceService{} - err = json.Unmarshal(data, &ds) - return ds, err -} - -func (s *deviceServiceRestClient) UpdateLastConnected(id string, time int64, ctx context.Context) error { - _, err := clients.PutRequest(s.url+"/"+id+"/lastconnected/"+strconv.FormatInt(time, 10), nil, ctx) + _, err = clients.PutRequest(serviceURL+"/"+id+"/lastreported/"+strconv.FormatInt(time, 10), nil, ctx) return err } -func (s *deviceServiceRestClient) UpdateLastReported(id string, time int64, ctx context.Context) error { - _, err := clients.PutRequest(s.url+"/"+id+"/lastreported/"+strconv.FormatInt(time, 10), nil, ctx) - return err -} +func (dsc *deviceServiceRestClient) Add(ds *models.DeviceService, ctx context.Context) (string, error) { + serviceURL, err := dsc.client.URLPrefix() + if err != nil { + return "", err + } -func (s *deviceServiceRestClient) Add(ds *models.DeviceService, ctx context.Context) (string, error) { - return clients.PostJsonRequest(s.url, ds, ctx) + return clients.PostJsonRequest(serviceURL, ds, ctx) } -func (s *deviceServiceRestClient) DeviceServiceForName(name string, ctx context.Context) (models.DeviceService, error) { - return s.requestDeviceService(s.url+"/name/"+name, ctx) +func (dsc *deviceServiceRestClient) DeviceServiceForName( + name string, + ctx context.Context) (models.DeviceService, error) { + + urlPrefix, err := dsc.client.URLPrefix() + if err != nil { + return models.DeviceService{}, err + } + + data, err := clients.GetRequest(urlPrefix+"/name/"+name, ctx) + if err != nil { + return models.DeviceService{}, err + } + + ds := models.DeviceService{} + err = json.Unmarshal(data, &ds) + + return ds, err } diff --git a/clients/metadata/device_service_test.go b/clients/metadata/device_service_test.go index b71e84fb..1551b6c9 100644 --- a/clients/metadata/device_service_test.go +++ b/clients/metadata/device_service_test.go @@ -16,7 +16,6 @@ package metadata import ( "testing" - "time" "github.com/edgexfoundry/go-mod-core-contracts/clients" "github.com/edgexfoundry/go-mod-core-contracts/clients/types" @@ -37,10 +36,11 @@ func TestNewDeviceServiceClientWithConsul(t *testing.T) { t.Error("dsc is not of expected type") } - time.Sleep(25 * time.Millisecond) - if len(r.url) == 0 { + url, err := r.client.URLPrefix() + + if err != nil { t.Error("url was not initialized") - } else if r.url != deviceServiceUrl { - t.Errorf("unexpected url value %s", r.url) + } else if url != deviceServiceUrl { + t.Errorf("unexpected url value %s", url) } } diff --git a/clients/metadata/provision_watcher.go b/clients/metadata/provision_watcher.go index bf79c0af..2601b9e5 100644 --- a/clients/metadata/provision_watcher.go +++ b/clients/metadata/provision_watcher.go @@ -21,14 +21,12 @@ 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/rest" "github.com/edgexfoundry/go-mod-core-contracts/clients/types" "github.com/edgexfoundry/go-mod-core-contracts/models" ) -/* -ProvisionWatcherClient defines the interface for interactions with the ProvisionWatcher endpoint on the EdgeX Foundry -core-metadata service. -*/ +// ProvisionWatcherClient defines the interface for interactions with the ProvisionWatcher endpoint on metadata. type ProvisionWatcherClient interface { // Add a new provision watcher Add(dev *models.ProvisionWatcher, ctx context.Context) (string, error) @@ -53,35 +51,26 @@ type ProvisionWatcherClient interface { } type provisionWatcherRestClient struct { - url string - endpoint interfaces.Endpointer + client interfaces.RestClientBuilder } // NewProvisionWatcherClient creates an instance of ProvisionWatcherClient func NewProvisionWatcherClient(params types.EndpointParams, m interfaces.Endpointer) ProvisionWatcherClient { - pw := provisionWatcherRestClient{endpoint: m} - pw.init(params) + pw := provisionWatcherRestClient{client: rest.ClientFactory(params, m)} return &pw } -func (pw *provisionWatcherRestClient) init(params types.EndpointParams) { - if params.UseRegistry { - go func(ch chan string) { - for { - select { - case url := <-ch: - pw.url = url - } - } - }(pw.endpoint.Monitor(params)) - } else { - pw.url = params.Url +// Helper method to request and decode a provision watcher +func (pwc *provisionWatcherRestClient) requestProvisionWatcher( + urlSuffix string, + ctx context.Context) (models.ProvisionWatcher, error) { + + urlPrefix, err := pwc.client.URLPrefix() + if err != nil { + return models.ProvisionWatcher{}, err } -} -// Helper method to request and decode a provision watcher -func (pw *provisionWatcherRestClient) requestProvisionWatcher(url string, ctx context.Context) (models.ProvisionWatcher, error) { - data, err := clients.GetRequest(url, ctx) + data, err := clients.GetRequest(urlPrefix+urlSuffix, ctx) if err != nil { return models.ProvisionWatcher{}, err } @@ -92,8 +81,16 @@ func (pw *provisionWatcherRestClient) requestProvisionWatcher(url string, ctx co } // Helper method to request and decode a provision watcher slice -func (pw *provisionWatcherRestClient) requestProvisionWatcherSlice(url string, ctx context.Context) ([]models.ProvisionWatcher, error) { - data, err := clients.GetRequest(url, ctx) +func (pwc *provisionWatcherRestClient) requestProvisionWatcherSlice( + urlSuffix string, + ctx context.Context) ([]models.ProvisionWatcher, error) { + + urlPrefix, err := pwc.client.URLPrefix() + if err != nil { + return nil, err + } + + data, err := clients.GetRequest(urlPrefix+urlSuffix, ctx) if err != nil { return []models.ProvisionWatcher{}, err } @@ -103,42 +100,57 @@ func (pw *provisionWatcherRestClient) requestProvisionWatcherSlice(url string, c return pwSlice, err } -func (pw *provisionWatcherRestClient) ProvisionWatcher(id string, ctx context.Context) (models.ProvisionWatcher, error) { - return pw.requestProvisionWatcher(pw.url+"/"+id, ctx) +func (pwc *provisionWatcherRestClient) ProvisionWatcher(id string, ctx context.Context) (models.ProvisionWatcher, error) { + return pwc.requestProvisionWatcher("/"+id, ctx) } -func (pw *provisionWatcherRestClient) ProvisionWatchers(ctx context.Context) ([]models.ProvisionWatcher, error) { - return pw.requestProvisionWatcherSlice(pw.url, ctx) +func (pwc *provisionWatcherRestClient) ProvisionWatchers(ctx context.Context) ([]models.ProvisionWatcher, error) { + return pwc.requestProvisionWatcherSlice("", ctx) } -func (pw *provisionWatcherRestClient) ProvisionWatcherForName(name string, ctx context.Context) (models.ProvisionWatcher, error) { - return pw.requestProvisionWatcher(pw.url+"/name/"+url.QueryEscape(name), ctx) +func (pwc *provisionWatcherRestClient) ProvisionWatcherForName(name string, ctx context.Context) (models.ProvisionWatcher, error) { + return pwc.requestProvisionWatcher("/name/"+url.QueryEscape(name), ctx) } -func (pw *provisionWatcherRestClient) ProvisionWatchersForService(serviceId string, ctx context.Context) ([]models.ProvisionWatcher, error) { - return pw.requestProvisionWatcherSlice(pw.url+"/service/"+serviceId, ctx) +func (pwc *provisionWatcherRestClient) ProvisionWatchersForService(serviceId string, ctx context.Context) ([]models.ProvisionWatcher, error) { + return pwc.requestProvisionWatcherSlice("/service/"+serviceId, ctx) } -func (pw *provisionWatcherRestClient) ProvisionWatchersForServiceByName(serviceName string, ctx context.Context) ([]models.ProvisionWatcher, error) { - return pw.requestProvisionWatcherSlice(pw.url+"/servicename/"+url.QueryEscape(serviceName), ctx) +func (pwc *provisionWatcherRestClient) ProvisionWatchersForServiceByName(serviceName string, ctx context.Context) ([]models.ProvisionWatcher, error) { + return pwc.requestProvisionWatcherSlice("/servicename/"+url.QueryEscape(serviceName), ctx) } -func (pw *provisionWatcherRestClient) ProvisionWatchersForProfile(profileId string, ctx context.Context) ([]models.ProvisionWatcher, error) { - return pw.requestProvisionWatcherSlice(pw.url+"/profile/"+profileId, ctx) +func (pwc *provisionWatcherRestClient) ProvisionWatchersForProfile(profileId string, ctx context.Context) ([]models.ProvisionWatcher, error) { + return pwc.requestProvisionWatcherSlice("/profile/"+profileId, ctx) } -func (pw *provisionWatcherRestClient) ProvisionWatchersForProfileByName(profileName string, ctx context.Context) ([]models.ProvisionWatcher, error) { - return pw.requestProvisionWatcherSlice(pw.url+"/profilename/"+url.QueryEscape(profileName), ctx) +func (pwc *provisionWatcherRestClient) ProvisionWatchersForProfileByName(profileName string, ctx context.Context) ([]models.ProvisionWatcher, error) { + return pwc.requestProvisionWatcherSlice("/profilename/"+url.QueryEscape(profileName), ctx) } -func (pw *provisionWatcherRestClient) Add(dev *models.ProvisionWatcher, ctx context.Context) (string, error) { - return clients.PostJsonRequest(pw.url, dev, ctx) +func (pwc *provisionWatcherRestClient) Add(dev *models.ProvisionWatcher, ctx context.Context) (string, error) { + serviceURL, err := pwc.client.URLPrefix() + if err != nil { + return "", err + } + + return clients.PostJsonRequest(serviceURL, dev, ctx) } -func (pw *provisionWatcherRestClient) Update(dev models.ProvisionWatcher, ctx context.Context) error { - return clients.UpdateRequest(pw.url, dev, ctx) +func (pwc *provisionWatcherRestClient) Update(dev models.ProvisionWatcher, ctx context.Context) error { + serviceURL, err := pwc.client.URLPrefix() + if err != nil { + return err + } + + return clients.UpdateRequest(serviceURL, dev, ctx) } -func (pw *provisionWatcherRestClient) Delete(id string, ctx context.Context) error { - return clients.DeleteRequest(pw.url+"/id/"+id, ctx) +func (pwc *provisionWatcherRestClient) Delete(id string, ctx context.Context) error { + serviceURL, err := pwc.client.URLPrefix() + if err != nil { + return err + } + + return clients.DeleteRequest(serviceURL+"/id/"+id, ctx) } diff --git a/clients/metadata/provision_watcher_test.go b/clients/metadata/provision_watcher_test.go index 1491eece..7c5206fb 100644 --- a/clients/metadata/provision_watcher_test.go +++ b/clients/metadata/provision_watcher_test.go @@ -20,7 +20,6 @@ import ( "net/http" "net/http/httptest" "testing" - "time" "github.com/edgexfoundry/go-mod-core-contracts/clients" "github.com/edgexfoundry/go-mod-core-contracts/clients/types" @@ -92,10 +91,11 @@ func TestNewProvisionWatcherClientWithConsul(t *testing.T) { t.Error("sc is not of expected type") } - time.Sleep(25 * time.Millisecond) - if len(r.url) == 0 { + url, err := r.client.URLPrefix() + + if err != nil { t.Error("url was not initialized") - } else if r.url != provisionWatcherURL { - t.Errorf("unexpected url value %s", r.url) + } else if url != provisionWatcherURL { + t.Errorf("unexpected url value %s", url) } }