dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts@v1.0.2/clients/http/deviceservicecommand.go (about) 1 // 2 // Copyright (C) 2021-2023 IOTech Ltd 3 // Copyright (C) 2023 Intel Corporation 4 // 5 // SPDX-License-Identifier: Apache-2.0 6 7 package http 8 9 import ( 10 "context" 11 "encoding/json" 12 "net/url" 13 14 "dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/clients/http/utils" 15 "dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/clients/interfaces" 16 "dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/common" 17 dtoCommon "dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/dtos/common" 18 "dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/dtos/responses" 19 "dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/errors" 20 21 "github.com/fxamacker/cbor/v2" 22 ) 23 24 type deviceServiceCommandClient struct { 25 authInjector interfaces.AuthenticationInjector 26 enableNameFieldEscape bool 27 } 28 29 // NewDeviceServiceCommandClient creates an instance of deviceServiceCommandClient 30 func NewDeviceServiceCommandClient(authInjector interfaces.AuthenticationInjector, enableNameFieldEscape bool) interfaces.DeviceServiceCommandClient { 31 return &deviceServiceCommandClient{ 32 authInjector: authInjector, 33 enableNameFieldEscape: enableNameFieldEscape, 34 } 35 } 36 37 // GetCommand sends HTTP request to execute the Get command 38 func (client *deviceServiceCommandClient) GetCommand(ctx context.Context, baseUrl string, deviceName string, commandName string, queryParams string) (*responses.EventResponse, errors.EdgeX) { 39 requestPath := common.NewPathBuilder().EnableNameFieldEscape(client.enableNameFieldEscape). 40 SetPath(common.ApiDeviceRoute).SetPath(common.Name).SetNameFieldPath(deviceName).SetNameFieldPath(commandName).BuildPath() 41 params, err := url.ParseQuery(queryParams) 42 if err != nil { 43 return nil, errors.NewCommonEdgeXWrapper(err) 44 } 45 res, contentType, edgeXerr := utils.GetRequestAndReturnBinaryRes(ctx, baseUrl, requestPath, params, client.authInjector) 46 if edgeXerr != nil { 47 return nil, errors.NewCommonEdgeXWrapper(edgeXerr) 48 } 49 // If execute GetCommand with dsReturnEvent query parameter 'no', there will be no content returned in the http response. 50 // So we can use the nil pointer to indicate that the HTTP response content is empty 51 if len(res) == 0 { 52 return nil, nil 53 } 54 response := &responses.EventResponse{} 55 if contentType == common.ContentTypeCBOR { 56 if err = cbor.Unmarshal(res, response); err != nil { 57 return nil, errors.NewCommonEdgeX(errors.KindContractInvalid, "failed to decode the cbor response", err) 58 } 59 } else { 60 if err = json.Unmarshal(res, response); err != nil { 61 return nil, errors.NewCommonEdgeX(errors.KindContractInvalid, "failed to decode the json response", err) 62 } 63 } 64 return response, nil 65 } 66 67 // SetCommand sends HTTP request to execute the Set command 68 func (client *deviceServiceCommandClient) SetCommand(ctx context.Context, baseUrl string, deviceName string, commandName string, queryParams string, settings map[string]string) (dtoCommon.BaseResponse, errors.EdgeX) { 69 var response dtoCommon.BaseResponse 70 requestPath := common.NewPathBuilder().EnableNameFieldEscape(client.enableNameFieldEscape). 71 SetPath(common.ApiDeviceRoute).SetPath(common.Name).SetNameFieldPath(deviceName).SetNameFieldPath(commandName).BuildPath() 72 params, err := url.ParseQuery(queryParams) 73 if err != nil { 74 return response, errors.NewCommonEdgeXWrapper(err) 75 } 76 err = utils.PutRequest(ctx, &response, baseUrl, requestPath, params, settings, client.authInjector) 77 if err != nil { 78 return response, errors.NewCommonEdgeXWrapper(err) 79 } 80 return response, nil 81 } 82 83 // SetCommandWithObject invokes device service's set command API and the settings supports object value type 84 func (client *deviceServiceCommandClient) SetCommandWithObject(ctx context.Context, baseUrl string, deviceName string, commandName string, queryParams string, settings map[string]interface{}) (dtoCommon.BaseResponse, errors.EdgeX) { 85 var response dtoCommon.BaseResponse 86 requestPath := common.NewPathBuilder().EnableNameFieldEscape(client.enableNameFieldEscape). 87 SetPath(common.ApiDeviceRoute).SetPath(common.Name).SetNameFieldPath(deviceName).SetNameFieldPath(commandName).BuildPath() 88 params, err := url.ParseQuery(queryParams) 89 if err != nil { 90 return response, errors.NewCommonEdgeXWrapper(err) 91 } 92 err = utils.PutRequest(ctx, &response, baseUrl, requestPath, params, settings, client.authInjector) 93 if err != nil { 94 return response, errors.NewCommonEdgeXWrapper(err) 95 } 96 return response, nil 97 }