Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: Allow NameFieldEscape configurable #855

Merged
merged 1 commit into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions clients/http/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,17 @@ import (
)

type CommandClient struct {
baseUrl string
authInjector interfaces.AuthenticationInjector
baseUrl string
authInjector interfaces.AuthenticationInjector
enableNameFieldEscape bool
}

// NewCommandClient creates an instance of CommandClient
func NewCommandClient(baseUrl string, authInjector interfaces.AuthenticationInjector) interfaces.CommandClient {
func NewCommandClient(baseUrl string, authInjector interfaces.AuthenticationInjector, enableNameFieldEscape bool) interfaces.CommandClient {
return &CommandClient{
baseUrl: baseUrl,
authInjector: authInjector,
baseUrl: baseUrl,
authInjector: authInjector,
enableNameFieldEscape: enableNameFieldEscape,
}
}

Expand All @@ -48,7 +50,8 @@ func (client *CommandClient) AllDeviceCoreCommands(ctx context.Context, offset i
// DeviceCoreCommandsByDeviceName returns all commands associated with the specified device name.
func (client *CommandClient) DeviceCoreCommandsByDeviceName(ctx context.Context, name string) (
res responses.DeviceCoreCommandResponse, err errors.EdgeX) {
path := utils.EscapeAndJoinPath(common.ApiDeviceRoute, common.Name, name)
path := common.NewPathBuilder().EnableNameFieldEscape(client.enableNameFieldEscape).
SetPath(common.ApiDeviceRoute).SetPath(common.Name).SetNameFieldPath(name).BuildPath()
err = utils.GetRequest(ctx, &res, client.baseUrl, path, nil, client.authInjector)
if err != nil {
return res, errors.NewCommonEdgeXWrapper(err)
Expand All @@ -61,7 +64,8 @@ func (client *CommandClient) IssueGetCommandByName(ctx context.Context, deviceNa
requestParams := url.Values{}
requestParams.Set(common.PushEvent, strconv.FormatBool(dsPushEvent))
requestParams.Set(common.ReturnEvent, strconv.FormatBool(dsReturnEvent))
requestPath := utils.EscapeAndJoinPath(common.ApiDeviceRoute, common.Name, deviceName, commandName)
requestPath := common.NewPathBuilder().EnableNameFieldEscape(client.enableNameFieldEscape).
SetPath(common.ApiDeviceRoute).SetPath(common.Name).SetNameFieldPath(deviceName).SetNameFieldPath(commandName).BuildPath()
err = utils.GetRequest(ctx, &res, client.baseUrl, requestPath, requestParams, client.authInjector)
if err != nil {
return res, errors.NewCommonEdgeXWrapper(err)
Expand All @@ -74,8 +78,8 @@ func (client *CommandClient) IssueGetCommandByNameWithQueryParams(ctx context.Co
for k, v := range queryParams {
requestParams.Set(k, v)
}

requestPath := utils.EscapeAndJoinPath(common.ApiDeviceRoute, common.Name, deviceName, commandName)
requestPath := common.NewPathBuilder().EnableNameFieldEscape(client.enableNameFieldEscape).
SetPath(common.ApiDeviceRoute).SetPath(common.Name).SetNameFieldPath(deviceName).SetNameFieldPath(commandName).BuildPath()
err = utils.GetRequest(ctx, &res, client.baseUrl, requestPath, requestParams, client.authInjector)
if err != nil {
return res, errors.NewCommonEdgeXWrapper(err)
Expand All @@ -85,7 +89,8 @@ func (client *CommandClient) IssueGetCommandByNameWithQueryParams(ctx context.Co

// IssueSetCommandByName issues the specified write command referenced by the command name to the device/sensor that is also referenced by name.
func (client *CommandClient) IssueSetCommandByName(ctx context.Context, deviceName string, commandName string, settings map[string]string) (res dtoCommon.BaseResponse, err errors.EdgeX) {
requestPath := utils.EscapeAndJoinPath(common.ApiDeviceRoute, common.Name, deviceName, commandName)
requestPath := common.NewPathBuilder().EnableNameFieldEscape(client.enableNameFieldEscape).
SetPath(common.ApiDeviceRoute).SetPath(common.Name).SetNameFieldPath(deviceName).SetNameFieldPath(commandName).BuildPath()
err = utils.PutRequest(ctx, &res, client.baseUrl, requestPath, nil, settings, client.authInjector)
if err != nil {
return res, errors.NewCommonEdgeXWrapper(err)
Expand All @@ -95,7 +100,8 @@ func (client *CommandClient) IssueSetCommandByName(ctx context.Context, deviceNa

// IssueSetCommandByNameWithObject issues the specified write command and the settings supports object value type
func (client *CommandClient) IssueSetCommandByNameWithObject(ctx context.Context, deviceName string, commandName string, settings map[string]interface{}) (res dtoCommon.BaseResponse, err errors.EdgeX) {
requestPath := utils.EscapeAndJoinPath(common.ApiDeviceRoute, common.Name, deviceName, commandName)
requestPath := common.NewPathBuilder().EnableNameFieldEscape(client.enableNameFieldEscape).
SetPath(common.ApiDeviceRoute).SetPath(common.Name).SetNameFieldPath(deviceName).SetNameFieldPath(commandName).BuildPath()
err = utils.PutRequest(ctx, &res, client.baseUrl, requestPath, nil, settings, client.authInjector)
if err != nil {
return res, errors.NewCommonEdgeXWrapper(err)
Expand Down
25 changes: 14 additions & 11 deletions clients/http/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package http
import (
"context"
"encoding/json"
"github.com/edgexfoundry/go-mod-core-contracts/v3/clients/http/utils"
"net/http"
"net/http/httptest"
"strconv"
Expand All @@ -24,18 +23,19 @@ import (
func TestQueryDeviceCoreCommands(t *testing.T) {
ts := newTestServer(http.MethodGet, common.ApiAllDeviceRoute, responses.MultiDeviceCoreCommandsResponse{})
defer ts.Close()
client := NewCommandClient(ts.URL, NewNullAuthenticationInjector())
client := NewCommandClient(ts.URL, NewNullAuthenticationInjector(), false)
res, err := client.AllDeviceCoreCommands(context.Background(), 0, 10)
require.NoError(t, err)
require.IsType(t, responses.MultiDeviceCoreCommandsResponse{}, res)
}

func TestQueryDeviceCoreCommandsByDeviceName(t *testing.T) {
deviceName := "Simple-Device01"
path := utils.EscapeAndJoinPath(common.ApiDeviceRoute, common.Name, deviceName)
path := common.NewPathBuilder().EnableNameFieldEscape(false).
SetPath(common.ApiDeviceRoute).SetPath(common.Name).SetNameFieldPath(deviceName).BuildPath()
ts := newTestServer(http.MethodGet, path, responses.DeviceCoreCommandResponse{})
defer ts.Close()
client := NewCommandClient(ts.URL, NewNullAuthenticationInjector())
client := NewCommandClient(ts.URL, NewNullAuthenticationInjector(), false)
res, err := client.DeviceCoreCommandsByDeviceName(context.Background(), deviceName)
require.NoError(t, err)
require.IsType(t, responses.DeviceCoreCommandResponse{}, res)
Expand All @@ -44,10 +44,11 @@ func TestQueryDeviceCoreCommandsByDeviceName(t *testing.T) {
func TestIssueGetCommandByName(t *testing.T) {
deviceName := "Simple-Device01"
cmdName := "SwitchButton"
path := utils.EscapeAndJoinPath(common.ApiDeviceRoute, common.Name, deviceName, cmdName)
path := common.NewPathBuilder().EnableNameFieldEscape(false).
SetPath(common.ApiDeviceRoute).SetPath(common.Name).SetNameFieldPath(deviceName).SetNameFieldPath(cmdName).BuildPath()
ts := newTestServer(http.MethodGet, path, &responses.EventResponse{})
defer ts.Close()
client := NewCommandClient(ts.URL, NewNullAuthenticationInjector())
client := NewCommandClient(ts.URL, NewNullAuthenticationInjector(), false)
pushEvent, err := strconv.ParseBool(common.ValueTrue)
require.NoError(t, err)
notReturnEvent, err := strconv.ParseBool(common.ValueFalse)
Expand Down Expand Up @@ -75,7 +76,7 @@ func TestIssueGetCommandByNameWithQueryParams(t *testing.T) {
}))
defer ts.Close()

client := NewCommandClient(ts.URL, NewNullAuthenticationInjector())
client := NewCommandClient(ts.URL, NewNullAuthenticationInjector(), false)
res, err := client.IssueGetCommandByNameWithQueryParams(context.Background(), deviceName, cmdName, testQueryParams)
require.NoError(t, err)
require.IsType(t, &responses.EventResponse{}, res)
Expand All @@ -87,10 +88,11 @@ func TestIssueIssueSetCommandByName(t *testing.T) {
settings := map[string]string{
"SwitchButton": "true",
}
path := utils.EscapeAndJoinPath(common.ApiDeviceRoute, common.Name, deviceName, cmdName)
path := common.NewPathBuilder().EnableNameFieldEscape(false).
SetPath(common.ApiDeviceRoute).SetPath(common.Name).SetNameFieldPath(deviceName).SetNameFieldPath(cmdName).BuildPath()
ts := newTestServer(http.MethodPut, path, dtoCommon.BaseResponse{})
defer ts.Close()
client := NewCommandClient(ts.URL, NewNullAuthenticationInjector())
client := NewCommandClient(ts.URL, NewNullAuthenticationInjector(), false)
res, err := client.IssueSetCommandByName(context.Background(), deviceName, cmdName, settings)
require.NoError(t, err)
require.IsType(t, dtoCommon.BaseResponse{}, res)
Expand All @@ -105,10 +107,11 @@ func TestIssueIssueSetCommandByNameWithObject(t *testing.T) {
"value": "on",
},
}
path := utils.EscapeAndJoinPath(common.ApiDeviceRoute, common.Name, deviceName, cmdName)
path := common.NewPathBuilder().EnableNameFieldEscape(false).
SetPath(common.ApiDeviceRoute).SetPath(common.Name).SetNameFieldPath(deviceName).SetNameFieldPath(cmdName).BuildPath()
ts := newTestServer(http.MethodPut, path, dtoCommon.BaseResponse{})
defer ts.Close()
client := NewCommandClient(ts.URL, NewNullAuthenticationInjector())
client := NewCommandClient(ts.URL, NewNullAuthenticationInjector(), false)
res, err := client.IssueSetCommandByNameWithObject(context.Background(), deviceName, cmdName, settings)
require.NoError(t, err)
require.IsType(t, dtoCommon.BaseResponse{}, res)
Expand Down
27 changes: 17 additions & 10 deletions clients/http/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,17 @@ import (
)

type DeviceClient struct {
baseUrl string
authInjector interfaces.AuthenticationInjector
baseUrl string
authInjector interfaces.AuthenticationInjector
enableNameFieldEscape bool
}

// NewDeviceClient creates an instance of DeviceClient
func NewDeviceClient(baseUrl string, authInjector interfaces.AuthenticationInjector) interfaces.DeviceClient {
func NewDeviceClient(baseUrl string, authInjector interfaces.AuthenticationInjector, enableNameFieldEscape bool) interfaces.DeviceClient {
return &DeviceClient{
baseUrl: baseUrl,
authInjector: authInjector,
baseUrl: baseUrl,
authInjector: authInjector,
enableNameFieldEscape: enableNameFieldEscape,
}
}

Expand Down Expand Up @@ -65,7 +67,8 @@ func (dc DeviceClient) AllDevices(ctx context.Context, labels []string, offset i
}

func (dc DeviceClient) DeviceNameExists(ctx context.Context, name string) (res dtoCommon.BaseResponse, err errors.EdgeX) {
path := utils.EscapeAndJoinPath(common.ApiDeviceRoute, common.Check, common.Name, name)
path := common.NewPathBuilder().EnableNameFieldEscape(dc.enableNameFieldEscape).
SetPath(common.ApiDeviceRoute).SetPath(common.Check).SetPath(common.Name).SetNameFieldPath(name).BuildPath()
err = utils.GetRequest(ctx, &res, dc.baseUrl, path, nil, dc.authInjector)
if err != nil {
return res, errors.NewCommonEdgeXWrapper(err)
Expand All @@ -74,7 +77,8 @@ func (dc DeviceClient) DeviceNameExists(ctx context.Context, name string) (res d
}

func (dc DeviceClient) DeviceByName(ctx context.Context, name string) (res responses.DeviceResponse, err errors.EdgeX) {
path := utils.EscapeAndJoinPath(common.ApiDeviceRoute, common.Name, name)
path := common.NewPathBuilder().EnableNameFieldEscape(dc.enableNameFieldEscape).
SetPath(common.ApiDeviceRoute).SetPath(common.Name).SetNameFieldPath(name).BuildPath()
err = utils.GetRequest(ctx, &res, dc.baseUrl, path, nil, dc.authInjector)
if err != nil {
return res, errors.NewCommonEdgeXWrapper(err)
Expand All @@ -83,7 +87,8 @@ func (dc DeviceClient) DeviceByName(ctx context.Context, name string) (res respo
}

func (dc DeviceClient) DeleteDeviceByName(ctx context.Context, name string) (res dtoCommon.BaseResponse, err errors.EdgeX) {
path := utils.EscapeAndJoinPath(common.ApiDeviceRoute, common.Name, name)
path := common.NewPathBuilder().EnableNameFieldEscape(dc.enableNameFieldEscape).
SetPath(common.ApiDeviceRoute).SetPath(common.Name).SetNameFieldPath(name).BuildPath()
err = utils.DeleteRequest(ctx, &res, dc.baseUrl, path, dc.authInjector)
if err != nil {
return res, errors.NewCommonEdgeXWrapper(err)
Expand All @@ -92,7 +97,8 @@ func (dc DeviceClient) DeleteDeviceByName(ctx context.Context, name string) (res
}

func (dc DeviceClient) DevicesByProfileName(ctx context.Context, name string, offset int, limit int) (res responses.MultiDevicesResponse, err errors.EdgeX) {
requestPath := utils.EscapeAndJoinPath(common.ApiDeviceRoute, common.Profile, common.Name, name)
requestPath := common.NewPathBuilder().EnableNameFieldEscape(dc.enableNameFieldEscape).
SetPath(common.ApiDeviceRoute).SetPath(common.Profile).SetPath(common.Name).SetNameFieldPath(name).BuildPath()
requestParams := url.Values{}
requestParams.Set(common.Offset, strconv.Itoa(offset))
requestParams.Set(common.Limit, strconv.Itoa(limit))
Expand All @@ -104,7 +110,8 @@ func (dc DeviceClient) DevicesByProfileName(ctx context.Context, name string, of
}

func (dc DeviceClient) DevicesByServiceName(ctx context.Context, name string, offset int, limit int) (res responses.MultiDevicesResponse, err errors.EdgeX) {
requestPath := utils.EscapeAndJoinPath(common.ApiDeviceRoute, common.Service, common.Name, name)
requestPath := common.NewPathBuilder().EnableNameFieldEscape(dc.enableNameFieldEscape).
SetPath(common.ApiDeviceRoute).SetPath(common.Service).SetPath(common.Name).SetNameFieldPath(name).BuildPath()
requestParams := url.Values{}
requestParams.Set(common.Offset, strconv.Itoa(offset))
requestParams.Set(common.Limit, strconv.Itoa(limit))
Expand Down
16 changes: 8 additions & 8 deletions clients/http/device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
func TestAddDevices(t *testing.T) {
ts := newTestServer(http.MethodPost, common.ApiDeviceRoute, []dtoCommon.BaseWithIdResponse{})
defer ts.Close()
client := NewDeviceClient(ts.URL, NewNullAuthenticationInjector())
client := NewDeviceClient(ts.URL, NewNullAuthenticationInjector(), false)
res, err := client.Add(context.Background(), []requests.AddDeviceRequest{})
require.NoError(t, err)
require.IsType(t, []dtoCommon.BaseWithIdResponse{}, res)
Expand All @@ -32,7 +32,7 @@ func TestAddDevices(t *testing.T) {
func TestPatchDevices(t *testing.T) {
ts := newTestServer(http.MethodPatch, common.ApiDeviceRoute, []dtoCommon.BaseResponse{})
defer ts.Close()
client := NewDeviceClient(ts.URL, NewNullAuthenticationInjector())
client := NewDeviceClient(ts.URL, NewNullAuthenticationInjector(), false)
res, err := client.Update(context.Background(), []requests.UpdateDeviceRequest{})
require.NoError(t, err)
require.IsType(t, []dtoCommon.BaseResponse{}, res)
Expand All @@ -41,7 +41,7 @@ func TestPatchDevices(t *testing.T) {
func TestQueryAllDevices(t *testing.T) {
ts := newTestServer(http.MethodGet, common.ApiAllDeviceRoute, responses.MultiDevicesResponse{})
defer ts.Close()
client := NewDeviceClient(ts.URL, NewNullAuthenticationInjector())
client := NewDeviceClient(ts.URL, NewNullAuthenticationInjector(), false)
res, err := client.AllDevices(context.Background(), []string{"label1", "label2"}, 1, 10)
require.NoError(t, err)
require.IsType(t, responses.MultiDevicesResponse{}, res)
Expand All @@ -52,7 +52,7 @@ func TestDeviceNameExists(t *testing.T) {
path := path.Join(common.ApiDeviceRoute, common.Check, common.Name, deviceName)
ts := newTestServer(http.MethodGet, path, dtoCommon.BaseResponse{})
defer ts.Close()
client := NewDeviceClient(ts.URL, NewNullAuthenticationInjector())
client := NewDeviceClient(ts.URL, NewNullAuthenticationInjector(), false)
res, err := client.DeviceNameExists(context.Background(), deviceName)
require.NoError(t, err)
require.IsType(t, dtoCommon.BaseResponse{}, res)
Expand All @@ -63,7 +63,7 @@ func TestQueryDeviceByName(t *testing.T) {
path := path.Join(common.ApiDeviceRoute, common.Name, deviceName)
ts := newTestServer(http.MethodGet, path, responses.DeviceResponse{})
defer ts.Close()
client := NewDeviceClient(ts.URL, NewNullAuthenticationInjector())
client := NewDeviceClient(ts.URL, NewNullAuthenticationInjector(), false)
res, err := client.DeviceByName(context.Background(), deviceName)
require.NoError(t, err)
require.IsType(t, responses.DeviceResponse{}, res)
Expand All @@ -74,7 +74,7 @@ func TestDeleteDeviceByName(t *testing.T) {
path := path.Join(common.ApiDeviceRoute, common.Name, deviceName)
ts := newTestServer(http.MethodDelete, path, dtoCommon.BaseResponse{})
defer ts.Close()
client := NewDeviceClient(ts.URL, NewNullAuthenticationInjector())
client := NewDeviceClient(ts.URL, NewNullAuthenticationInjector(), false)
res, err := client.DeleteDeviceByName(context.Background(), deviceName)
require.NoError(t, err)
require.IsType(t, dtoCommon.BaseResponse{}, res)
Expand All @@ -85,7 +85,7 @@ func TestQueryDevicesByProfileName(t *testing.T) {
urlPath := path.Join(common.ApiDeviceRoute, common.Profile, common.Name, profileName)
ts := newTestServer(http.MethodGet, urlPath, responses.MultiDevicesResponse{})
defer ts.Close()
client := NewDeviceClient(ts.URL, NewNullAuthenticationInjector())
client := NewDeviceClient(ts.URL, NewNullAuthenticationInjector(), false)
res, err := client.DevicesByProfileName(context.Background(), profileName, 1, 10)
require.NoError(t, err)
require.IsType(t, responses.MultiDevicesResponse{}, res)
Expand All @@ -96,7 +96,7 @@ func TestQueryDevicesByServiceName(t *testing.T) {
urlPath := path.Join(common.ApiDeviceRoute, common.Service, common.Name, serviceName)
ts := newTestServer(http.MethodGet, urlPath, responses.MultiDevicesResponse{})
defer ts.Close()
client := NewDeviceClient(ts.URL, NewNullAuthenticationInjector())
client := NewDeviceClient(ts.URL, NewNullAuthenticationInjector(), false)
res, err := client.DevicesByServiceName(context.Background(), serviceName, 1, 10)
require.NoError(t, err)
require.IsType(t, responses.MultiDevicesResponse{}, res)
Expand Down
Loading