Skip to content

Commit

Permalink
edgexfoundry#196: Refactor metadata clients.
Browse files Browse the repository at this point in the history
Signed-off-by: Brandon Forster <brandonforster@gmail.com>
  • Loading branch information
brandonforster committed Jan 15, 2020
1 parent ab84fcc commit 5aa1de2
Show file tree
Hide file tree
Showing 11 changed files with 252 additions and 212 deletions.
62 changes: 31 additions & 31 deletions clients/metadata/addressable.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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)
Expand All @@ -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
}
Expand All @@ -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)
}
13 changes: 7 additions & 6 deletions clients/metadata/addressable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
}
}

Expand Down
71 changes: 39 additions & 32 deletions clients/metadata/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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)
}
10 changes: 5 additions & 5 deletions clients/metadata/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
}
}
4 changes: 1 addition & 3 deletions clients/metadata/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading

0 comments on commit 5aa1de2

Please sign in to comment.