diff --git a/clients/command.go b/clients/command.go index 909d2638..0d0b99ef 100644 --- a/clients/command.go +++ b/clients/command.go @@ -12,7 +12,6 @@ import ( "errors" "net/http" "strconv" - "strings" "time" "github.com/edgexfoundry/go-mod-core-contracts/v3/clients/interfaces" @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) diff --git a/clients/command_test.go b/clients/command_test.go index ecf43d56..fc023cb9 100644 --- a/clients/command_test.go +++ b/clients/command_test.go @@ -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() @@ -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 } diff --git a/go.mod b/go.mod index 2559675a..25d98ef8 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 9e61f7b3..6e1b7cc2 100644 --- a/go.sum +++ b/go.sum @@ -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=