Skip to content

Commit

Permalink
edgexfoundry#196: Updated command client with URLClient
Browse files Browse the repository at this point in the history
Signed-off-by: Brandon Forster <me@brandonforster.com>
  • Loading branch information
brandonforster committed Jan 21, 2020
1 parent dd15615 commit d38330e
Showing 1 changed file with 38 additions and 26 deletions.
64 changes: 38 additions & 26 deletions clients/command/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ 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"
)

// The CommandClient interface defines interactions with the EdgeX Core Command microservice.
// CommandClient interface defines interactions with the EdgeX Core Command microservice.
type CommandClient interface {
// Get issues a GET command targeting the specified device, using the specified command id
Get(deviceId string, commandId string, ctx context.Context) (string, error)
Expand All @@ -38,46 +39,57 @@ type CommandClient interface {
}

type commandRestClient struct {
url string
endpoint interfaces.Endpointer
urlClient interfaces.URLClient
}

// NewCommandClient creates an instance of CommandClient
func NewCommandClient(params types.EndpointParams, m interfaces.Endpointer) CommandClient {
c := commandRestClient{endpoint: m}
c.init(params)
return &c
return &commandRestClient{urlClient: urlclient.New(params, m)}
}

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
func (cc *commandRestClient) Get(deviceId string, commandId string, ctx context.Context) (string, error) {
url, err := cc.urlClient.Prefix()
if err != nil {
return "", err
}
}

func (cc *commandRestClient) Get(deviceId string, commandId string, ctx context.Context) (string, error) {
body, err := clients.GetRequest(cc.url+"/"+deviceId+"/command/"+commandId, ctx)
body, err := clients.GetRequest(url+"/"+deviceId+"/command/"+commandId, ctx)
return string(body), err
}

func (cc *commandRestClient) Put(deviceId string, commandId string, body string, ctx context.Context) (string, error) {
return clients.PutRequest(cc.url+"/"+deviceId+"/command/"+commandId, []byte(body), ctx)
url, err := cc.urlClient.Prefix()
if err != nil {
return "", err
}

return clients.PutRequest(url+"/"+deviceId+"/command/"+commandId, []byte(body), ctx)
}

func (cc *commandRestClient) GetDeviceCommandByNames(deviceName string, commandName string, ctx context.Context) (string, error) {
body, err := clients.GetRequest(cc.url+"/name/"+deviceName+"/command/"+commandName, ctx)
func (cc *commandRestClient) GetDeviceCommandByNames(
deviceName string,
commandName string,
ctx context.Context) (string, error) {

url, err := cc.urlClient.Prefix()
if err != nil {
return "", err
}

body, err := clients.GetRequest(url+"/name/"+deviceName+"/command/"+commandName, ctx)
return string(body), err
}

func (cc *commandRestClient) PutDeviceCommandByNames(deviceName string, commandName string, body string, ctx context.Context) (string, error) {
return clients.PutRequest(cc.url+"/name/"+deviceName+"/command/"+commandName, []byte(body), ctx)
func (cc *commandRestClient) PutDeviceCommandByNames(
deviceName string,
commandName string,
body string,
ctx context.Context) (string, error) {

url, err := cc.urlClient.Prefix()
if err != nil {
return "", err
}

return clients.PutRequest(url+"/name/"+deviceName+"/command/"+commandName, []byte(body), ctx)
}

0 comments on commit d38330e

Please sign in to comment.