Skip to content

Commit

Permalink
refactor!: Replace topics from config with new constants
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Topics no longer in config. Client factory method has additional baseTopic parameter

Signed-off-by: Leonard Goodell <leonard.goodell@intel.com>
  • Loading branch information
Leonard Goodell committed Feb 14, 2023
1 parent a04e229 commit 3646279
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 25 deletions.
22 changes: 11 additions & 11 deletions clients/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"errors"
"net/http"
"strconv"
"strings"
"time"

"github.com/edgexfoundry/go-mod-core-contracts/v3/clients/interfaces"
Expand All @@ -27,26 +26,27 @@ import (

type CommandClient struct {
messageBus messaging.MessageClient
topics map[string]string
baseTopic string
responseTopicPrefix string
timeout time.Duration
}

func NewCommandClient(messageBus messaging.MessageClient, topics map[string]string, timeout time.Duration) (interfaces.CommandClient, error) {
func NewCommandClient(messageBus messaging.MessageClient, baseTopic string, timeout time.Duration) interfaces.CommandClient {
client := &CommandClient{
messageBus: messageBus,
topics: topics,
responseTopicPrefix: strings.Join([]string{topics[common.ResponseTopicPrefixKey], common.CoreCommandServiceKey}, "/"),
baseTopic: baseTopic,
responseTopicPrefix: common.BuildTopic(baseTopic, common.ResponseTopic, common.CoreCommandServiceKey),
timeout: timeout,
}

return client, nil
return client
}

func (c *CommandClient) AllDeviceCoreCommands(_ context.Context, offset int, limit int) (responses.MultiDeviceCoreCommandsResponse, edgexErr.EdgeX) {
queryParams := map[string]string{common.Offset: strconv.Itoa(offset), common.Limit: strconv.Itoa(limit)}
requestEnvelope := types.NewMessageEnvelopeForRequest(nil, queryParams)
requestTopic := strings.Join([]string{c.topics[common.CommandQueryRequestTopicPrefixKey], common.All}, "/")

requestTopic := common.BuildTopic(c.baseTopic, common.CoreCommandQueryRequestPublishTopic, common.All)
responseEnvelope, err := c.messageBus.Request(requestEnvelope, common.CoreCommandServiceKey, requestTopic, c.timeout)
if err != nil {
return responses.MultiDeviceCoreCommandsResponse{}, edgexErr.NewCommonEdgeXWrapper(err)
Expand All @@ -67,7 +67,7 @@ func (c *CommandClient) AllDeviceCoreCommands(_ context.Context, offset int, lim

func (c *CommandClient) DeviceCoreCommandsByDeviceName(_ context.Context, deviceName string) (responses.DeviceCoreCommandResponse, edgexErr.EdgeX) {
requestEnvelope := types.NewMessageEnvelopeForRequest(nil, nil)
requestTopic := strings.Join([]string{c.topics[common.CommandQueryRequestTopicPrefixKey], deviceName}, "/")
requestTopic := common.BuildTopic(c.baseTopic, common.CoreCommandQueryRequestPublishTopic, deviceName)
responseEnvelope, err := c.messageBus.Request(requestEnvelope, requestTopic, c.responseTopicPrefix, c.timeout)
if err != nil {
return responses.DeviceCoreCommandResponse{}, edgexErr.NewCommonEdgeXWrapper(err)
Expand All @@ -93,7 +93,7 @@ func (c *CommandClient) IssueGetCommandByName(ctx context.Context, deviceName st

func (c *CommandClient) IssueGetCommandByNameWithQueryParams(_ context.Context, deviceName string, commandName string, queryParams map[string]string) (*responses.EventResponse, edgexErr.EdgeX) {
requestEnvelope := types.NewMessageEnvelopeForRequest(nil, queryParams)
requestTopic := strings.Join([]string{c.topics[common.CommandRequestTopicPrefixKey], deviceName, commandName, "get"}, "/")
requestTopic := common.BuildTopic(c.baseTopic, common.CoreCommandRequestPublishTopic, deviceName, commandName, "get")
responseEnvelope, err := c.messageBus.Request(requestEnvelope, requestTopic, c.responseTopicPrefix, c.timeout)
if err != nil {
return nil, edgexErr.NewCommonEdgeXWrapper(err)
Expand Down Expand Up @@ -126,7 +126,7 @@ func (c *CommandClient) IssueSetCommandByName(_ context.Context, deviceName stri
}

requestEnvelope := types.NewMessageEnvelopeForRequest(payloadBytes, nil)
requestTopic := strings.Join([]string{c.topics[common.CommandRequestTopicPrefixKey], deviceName, commandName, "set"}, "/")
requestTopic := common.BuildTopic(c.baseTopic, common.CoreCommandRequestPublishTopic, deviceName, commandName, "set")
responseEnvelope, err := c.messageBus.Request(requestEnvelope, requestTopic, c.responseTopicPrefix, c.timeout)
if err != nil {
return commonDTO.BaseResponse{}, edgexErr.NewCommonEdgeXWrapper(err)
Expand All @@ -147,7 +147,7 @@ func (c *CommandClient) IssueSetCommandByNameWithObject(_ context.Context, devic
}

requestEnvelope := types.NewMessageEnvelopeForRequest(payloadBytes, nil)
requestTopic := strings.Join([]string{c.topics[common.CommandRequestTopicPrefixKey], deviceName, commandName, "set"}, "/")
requestTopic := common.BuildTopic(c.baseTopic, common.CoreCommandRequestPublishTopic, deviceName, commandName, "set")
responseEnvelope, err := c.messageBus.Request(requestEnvelope, requestTopic, c.responseTopicPrefix, c.timeout)
if err != nil {
return commonDTO.BaseResponse{}, edgexErr.NewCommonEdgeXWrapper(err)
Expand Down
14 changes: 3 additions & 11 deletions clients/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@ import (
)

const (
testDeviceName = "test-device"
testCommandName = "test-command"
testQueryRequestTopic = "test/commandquery/request"
testCommandRequestTopicPrefix = "test/command/request"
testDeviceName = "test-device"
testCommandName = "test-command"
)

var expectedRequestID = uuid.NewString()
Expand Down Expand Up @@ -265,13 +263,7 @@ func getCommandClientWithMockMessaging(t *testing.T, expectedResponse *types.Mes
mockMessageClient := &mocks.MessageClient{}
mockMessageClient.On("Request", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(expectedResponse, expectedRequestError)

topics := map[string]string{
common.CommandQueryRequestTopicPrefixKey: testQueryRequestTopic,
common.CommandRequestTopicPrefixKey: testCommandRequestTopicPrefix,
}

client, err := NewCommandClient(mockMessageClient, topics, 10*time.Second)
require.NoError(t, err)
client := NewCommandClient(mockMessageClient, "edgex", 10*time.Second)

return client
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.18

require (
github.com/eclipse/paho.mqtt.golang v1.4.2
github.com/edgexfoundry/go-mod-core-contracts/v3 v3.0.0-dev.10
github.com/edgexfoundry/go-mod-core-contracts/v3 v3.0.0-dev.13
github.com/go-redis/redis/v7 v7.3.0
github.com/google/uuid v1.3.0
github.com/hashicorp/go-multierror v1.1.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/eclipse/paho.mqtt.golang v1.4.2 h1:66wOzfUHSSI1zamx7jR6yMEI5EuHnT1G6rNA5PM12m4=
github.com/eclipse/paho.mqtt.golang v1.4.2/go.mod h1:JGt0RsEwEX+Xa/agj90YJ9d9DH2b7upDZMK9HRbFvCA=
github.com/edgexfoundry/go-mod-core-contracts/v3 v3.0.0-dev.10 h1:o5yenvmLn8+0AOz0d5GIek011Tt5ZRbvPlgE4VhozEU=
github.com/edgexfoundry/go-mod-core-contracts/v3 v3.0.0-dev.10/go.mod h1:4lpZUM54ZareGU/yuAJvLEw0BoJ43SvCj1LO+gsKm9c=
github.com/edgexfoundry/go-mod-core-contracts/v3 v3.0.0-dev.13 h1:AYi0PX6S13bJBJPl3c5XiIKPCUlnkQIzLvIza2r0U+8=
github.com/edgexfoundry/go-mod-core-contracts/v3 v3.0.0-dev.13/go.mod h1:4lpZUM54ZareGU/yuAJvLEw0BoJ43SvCj1LO+gsKm9c=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/fxamacker/cbor/v2 v2.4.0 h1:ri0ArlOR+5XunOP8CRUowT0pSJOwhW098ZCUyskZD88=
github.com/fxamacker/cbor/v2 v2.4.0/go.mod h1:TA1xS00nchWmaBnEIxPSE5oHLuJBAVvqrtAnWBwBCVo=
Expand Down

0 comments on commit 3646279

Please sign in to comment.