From 3ce3929bd576846c79dfa7f4a059a6488c314b80 Mon Sep 17 00:00:00 2001 From: Ginny Guan Date: Tue, 14 Jan 2025 19:09:05 +0800 Subject: [PATCH] feat: use baseUrlFunc instead baseUrl for Clients relate: edgexfoundry/go-mod-bootstrap#804 Signed-off-by: Ginny Guan --- clients/common.go | 22 ++++ clients/http/command.go | 52 ++++++-- clients/http/device.go | 82 ++++++++++-- clients/http/deviceprofile.go | 184 ++++++++++++++++++++------- clients/http/deviceservice.go | 45 ++++++- clients/http/event.go | 79 +++++++++--- clients/http/notification.go | 112 ++++++++++++---- clients/http/provisionwatcher.go | 58 +++++++-- clients/http/reading.go | 76 +++++++++-- clients/http/scheduleactionrecord.go | 46 +++++-- clients/http/schedulejob.go | 52 ++++++-- clients/http/subscription.go | 68 ++++++++-- 12 files changed, 704 insertions(+), 172 deletions(-) create mode 100644 clients/common.go diff --git a/clients/common.go b/clients/common.go new file mode 100644 index 00000000..1b6d81b1 --- /dev/null +++ b/clients/common.go @@ -0,0 +1,22 @@ +// Copyright (C) 2025 IOTech Ltd + +package clients + +import goErrors "errors" + +type ClientBaseUrlFunc func() (string, error) + +// GetDefaultClientBaseUrlFunc returns a ClientBaseUrlFunc that always returns the provided baseUrl. +func GetDefaultClientBaseUrlFunc(baseUrl string) ClientBaseUrlFunc { + return func() (string, error) { + return baseUrl, nil + } +} + +// GetBaseUrl retrieves the base URL using the provided ClientBaseUrlFunc. +func GetBaseUrl(baseUrlFunc ClientBaseUrlFunc) (string, error) { + if baseUrlFunc == nil { + return "", goErrors.New("could not find ClientBaseUrlFunc to get base url") + } + return baseUrlFunc() +} diff --git a/clients/http/command.go b/clients/http/command.go index 1e590ee8..8a7d38cb 100644 --- a/clients/http/command.go +++ b/clients/http/command.go @@ -1,5 +1,5 @@ // -// Copyright (C) 2021-2023 IOTech Ltd +// Copyright (C) 2021-2025 IOTech Ltd // Copyright (C) 2023 Intel Corporation // // SPDX-License-Identifier: Apache-2.0 @@ -11,6 +11,7 @@ import ( "net/url" "strconv" + "github.com/edgexfoundry/go-mod-core-contracts/v4/clients" "github.com/edgexfoundry/go-mod-core-contracts/v4/clients/http/utils" "github.com/edgexfoundry/go-mod-core-contracts/v4/clients/interfaces" "github.com/edgexfoundry/go-mod-core-contracts/v4/common" @@ -20,7 +21,7 @@ import ( ) type CommandClient struct { - baseUrl string + baseUrlFunc clients.ClientBaseUrlFunc authInjector interfaces.AuthenticationInjector enableNameFieldEscape bool } @@ -28,7 +29,16 @@ type CommandClient struct { // NewCommandClient creates an instance of CommandClient func NewCommandClient(baseUrl string, authInjector interfaces.AuthenticationInjector, enableNameFieldEscape bool) interfaces.CommandClient { return &CommandClient{ - baseUrl: baseUrl, + baseUrlFunc: clients.GetDefaultClientBaseUrlFunc(baseUrl), + authInjector: authInjector, + enableNameFieldEscape: enableNameFieldEscape, + } +} + +// NewCommandClientWithUrlCallback creates an instance of CommandClient with ClientBaseUrlFunc. +func NewCommandClientWithUrlCallback(baseUrlFunc clients.ClientBaseUrlFunc, authInjector interfaces.AuthenticationInjector, enableNameFieldEscape bool) interfaces.CommandClient { + return &CommandClient{ + baseUrlFunc: baseUrlFunc, authInjector: authInjector, enableNameFieldEscape: enableNameFieldEscape, } @@ -40,7 +50,11 @@ func (client *CommandClient) AllDeviceCoreCommands(ctx context.Context, offset i requestParams := url.Values{} requestParams.Set(common.Offset, strconv.Itoa(offset)) requestParams.Set(common.Limit, strconv.Itoa(limit)) - err = utils.GetRequest(ctx, &res, client.baseUrl, common.ApiAllDeviceRoute, requestParams, client.authInjector) + baseUrl, goErr := clients.GetBaseUrl(client.baseUrlFunc) + if goErr != nil { + return res, errors.NewCommonEdgeXWrapper(goErr) + } + err = utils.GetRequest(ctx, &res, baseUrl, common.ApiAllDeviceRoute, requestParams, client.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } @@ -52,7 +66,11 @@ func (client *CommandClient) DeviceCoreCommandsByDeviceName(ctx context.Context, res responses.DeviceCoreCommandResponse, err errors.EdgeX) { 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) + baseUrl, goErr := clients.GetBaseUrl(client.baseUrlFunc) + if goErr != nil { + return res, errors.NewCommonEdgeXWrapper(goErr) + } + err = utils.GetRequest(ctx, &res, baseUrl, path, nil, client.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } @@ -66,7 +84,11 @@ func (client *CommandClient) IssueGetCommandByName(ctx context.Context, deviceNa requestParams.Set(common.ReturnEvent, strconv.FormatBool(dsReturnEvent)) 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) + baseUrl, goErr := clients.GetBaseUrl(client.baseUrlFunc) + if goErr != nil { + return res, errors.NewCommonEdgeXWrapper(goErr) + } + err = utils.GetRequest(ctx, &res, baseUrl, requestPath, requestParams, client.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } @@ -80,7 +102,11 @@ func (client *CommandClient) IssueGetCommandByNameWithQueryParams(ctx context.Co } 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) + baseUrl, goErr := clients.GetBaseUrl(client.baseUrlFunc) + if goErr != nil { + return res, errors.NewCommonEdgeXWrapper(goErr) + } + err = utils.GetRequest(ctx, &res, baseUrl, requestPath, requestParams, client.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } @@ -91,7 +117,11 @@ func (client *CommandClient) IssueGetCommandByNameWithQueryParams(ctx context.Co func (client *CommandClient) IssueSetCommandByName(ctx context.Context, deviceName string, commandName string, settings map[string]string) (res dtoCommon.BaseResponse, err errors.EdgeX) { 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) + baseUrl, goErr := clients.GetBaseUrl(client.baseUrlFunc) + if goErr != nil { + return res, errors.NewCommonEdgeXWrapper(goErr) + } + err = utils.PutRequest(ctx, &res, baseUrl, requestPath, nil, settings, client.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } @@ -102,7 +132,11 @@ func (client *CommandClient) IssueSetCommandByName(ctx context.Context, deviceNa func (client *CommandClient) IssueSetCommandByNameWithObject(ctx context.Context, deviceName string, commandName string, settings map[string]interface{}) (res dtoCommon.BaseResponse, err errors.EdgeX) { 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) + baseUrl, goErr := clients.GetBaseUrl(client.baseUrlFunc) + if goErr != nil { + return res, errors.NewCommonEdgeXWrapper(goErr) + } + err = utils.PutRequest(ctx, &res, baseUrl, requestPath, nil, settings, client.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } diff --git a/clients/http/device.go b/clients/http/device.go index dd604e73..99e801f2 100644 --- a/clients/http/device.go +++ b/clients/http/device.go @@ -1,7 +1,7 @@ // // Copyright (C) 2020-2021 Unknown author // Copyright (C) 2023 Intel Corporation -// Copyright (C) 2024 IOTech Ltd +// Copyright (C) 2024-2025 IOTech Ltd // // SPDX-License-Identifier: Apache-2.0 @@ -13,6 +13,7 @@ import ( "strconv" "strings" + "github.com/edgexfoundry/go-mod-core-contracts/v4/clients" "github.com/edgexfoundry/go-mod-core-contracts/v4/clients/http/utils" "github.com/edgexfoundry/go-mod-core-contracts/v4/clients/interfaces" "github.com/edgexfoundry/go-mod-core-contracts/v4/common" @@ -23,7 +24,7 @@ import ( ) type DeviceClient struct { - baseUrl string + baseUrlFunc clients.ClientBaseUrlFunc authInjector interfaces.AuthenticationInjector enableNameFieldEscape bool } @@ -31,14 +32,27 @@ type DeviceClient struct { // NewDeviceClient creates an instance of DeviceClient func NewDeviceClient(baseUrl string, authInjector interfaces.AuthenticationInjector, enableNameFieldEscape bool) interfaces.DeviceClient { return &DeviceClient{ - baseUrl: baseUrl, + baseUrlFunc: clients.GetDefaultClientBaseUrlFunc(baseUrl), + authInjector: authInjector, + enableNameFieldEscape: enableNameFieldEscape, + } +} + +// NewDeviceClientWithUrlCallback creates an instance of DeviceClient with ClientBaseUrlFunc. +func NewDeviceClientWithUrlCallback(baseUrlFunc clients.ClientBaseUrlFunc, authInjector interfaces.AuthenticationInjector, enableNameFieldEscape bool) interfaces.DeviceClient { + return &DeviceClient{ + baseUrlFunc: baseUrlFunc, authInjector: authInjector, enableNameFieldEscape: enableNameFieldEscape, } } func (dc DeviceClient) Add(ctx context.Context, reqs []requests.AddDeviceRequest) (res []dtoCommon.BaseWithIdResponse, err errors.EdgeX) { - err = utils.PostRequestWithRawData(ctx, &res, dc.baseUrl, common.ApiDeviceRoute, nil, reqs, dc.authInjector) + baseUrl, goErr := clients.GetBaseUrl(dc.baseUrlFunc) + if goErr != nil { + return res, errors.NewCommonEdgeXWrapper(goErr) + } + err = utils.PostRequestWithRawData(ctx, &res, baseUrl, common.ApiDeviceRoute, nil, reqs, dc.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } @@ -50,7 +64,11 @@ func (dc DeviceClient) AddWithQueryParams(ctx context.Context, reqs []requests.A for k, v := range queryParams { requestParams.Set(k, v) } - err = utils.PostRequestWithRawData(ctx, &res, dc.baseUrl, common.ApiDeviceRoute, requestParams, reqs, dc.authInjector) + baseUrl, goErr := clients.GetBaseUrl(dc.baseUrlFunc) + if goErr != nil { + return res, errors.NewCommonEdgeXWrapper(goErr) + } + err = utils.PostRequestWithRawData(ctx, &res, baseUrl, common.ApiDeviceRoute, requestParams, reqs, dc.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } @@ -58,7 +76,11 @@ func (dc DeviceClient) AddWithQueryParams(ctx context.Context, reqs []requests.A } func (dc DeviceClient) Update(ctx context.Context, reqs []requests.UpdateDeviceRequest) (res []dtoCommon.BaseResponse, err errors.EdgeX) { - err = utils.PatchRequest(ctx, &res, dc.baseUrl, common.ApiDeviceRoute, nil, reqs, dc.authInjector) + baseUrl, goErr := clients.GetBaseUrl(dc.baseUrlFunc) + if goErr != nil { + return res, errors.NewCommonEdgeXWrapper(goErr) + } + err = utils.PatchRequest(ctx, &res, baseUrl, common.ApiDeviceRoute, nil, reqs, dc.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } @@ -70,7 +92,11 @@ func (dc DeviceClient) UpdateWithQueryParams(ctx context.Context, reqs []request for k, v := range queryParams { requestParams.Set(k, v) } - err = utils.PatchRequest(ctx, &res, dc.baseUrl, common.ApiDeviceRoute, requestParams, reqs, dc.authInjector) + baseUrl, goErr := clients.GetBaseUrl(dc.baseUrlFunc) + if goErr != nil { + return res, errors.NewCommonEdgeXWrapper(goErr) + } + err = utils.PatchRequest(ctx, &res, baseUrl, common.ApiDeviceRoute, requestParams, reqs, dc.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } @@ -84,7 +110,11 @@ func (dc DeviceClient) AllDevices(ctx context.Context, labels []string, offset i } requestParams.Set(common.Offset, strconv.Itoa(offset)) requestParams.Set(common.Limit, strconv.Itoa(limit)) - err = utils.GetRequest(ctx, &res, dc.baseUrl, common.ApiAllDeviceRoute, requestParams, dc.authInjector) + baseUrl, goErr := clients.GetBaseUrl(dc.baseUrlFunc) + if goErr != nil { + return res, errors.NewCommonEdgeXWrapper(goErr) + } + err = utils.GetRequest(ctx, &res, baseUrl, common.ApiAllDeviceRoute, requestParams, dc.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } @@ -100,7 +130,11 @@ func (dc DeviceClient) AllDevicesWithChildren(ctx context.Context, parent string requestParams.Set(common.MaxLevels, strconv.FormatUint(uint64(maxLevels), 10)) requestParams.Set(common.Offset, strconv.Itoa(offset)) requestParams.Set(common.Limit, strconv.Itoa(limit)) - err = utils.GetRequest(ctx, &res, dc.baseUrl, common.ApiAllDeviceRoute, requestParams, dc.authInjector) + baseUrl, goErr := clients.GetBaseUrl(dc.baseUrlFunc) + if goErr != nil { + return res, errors.NewCommonEdgeXWrapper(goErr) + } + err = utils.GetRequest(ctx, &res, baseUrl, common.ApiAllDeviceRoute, requestParams, dc.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } @@ -110,7 +144,11 @@ func (dc DeviceClient) AllDevicesWithChildren(ctx context.Context, parent string func (dc DeviceClient) DeviceNameExists(ctx context.Context, name string) (res dtoCommon.BaseResponse, err errors.EdgeX) { 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) + baseUrl, goErr := clients.GetBaseUrl(dc.baseUrlFunc) + if goErr != nil { + return res, errors.NewCommonEdgeXWrapper(goErr) + } + err = utils.GetRequest(ctx, &res, baseUrl, path, nil, dc.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } @@ -120,7 +158,11 @@ 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 := 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) + baseUrl, goErr := clients.GetBaseUrl(dc.baseUrlFunc) + if goErr != nil { + return res, errors.NewCommonEdgeXWrapper(goErr) + } + err = utils.GetRequest(ctx, &res, baseUrl, path, nil, dc.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } @@ -130,7 +172,11 @@ 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 := common.NewPathBuilder().EnableNameFieldEscape(dc.enableNameFieldEscape). SetPath(common.ApiDeviceRoute).SetPath(common.Name).SetNameFieldPath(name).BuildPath() - err = utils.DeleteRequest(ctx, &res, dc.baseUrl, path, dc.authInjector) + baseUrl, goErr := clients.GetBaseUrl(dc.baseUrlFunc) + if goErr != nil { + return res, errors.NewCommonEdgeXWrapper(goErr) + } + err = utils.DeleteRequest(ctx, &res, baseUrl, path, dc.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } @@ -143,7 +189,11 @@ func (dc DeviceClient) DevicesByProfileName(ctx context.Context, name string, of requestParams := url.Values{} requestParams.Set(common.Offset, strconv.Itoa(offset)) requestParams.Set(common.Limit, strconv.Itoa(limit)) - err = utils.GetRequest(ctx, &res, dc.baseUrl, requestPath, requestParams, dc.authInjector) + baseUrl, goErr := clients.GetBaseUrl(dc.baseUrlFunc) + if goErr != nil { + return res, errors.NewCommonEdgeXWrapper(goErr) + } + err = utils.GetRequest(ctx, &res, baseUrl, requestPath, requestParams, dc.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } @@ -156,7 +206,11 @@ func (dc DeviceClient) DevicesByServiceName(ctx context.Context, name string, of requestParams := url.Values{} requestParams.Set(common.Offset, strconv.Itoa(offset)) requestParams.Set(common.Limit, strconv.Itoa(limit)) - err = utils.GetRequest(ctx, &res, dc.baseUrl, requestPath, requestParams, dc.authInjector) + baseUrl, goErr := clients.GetBaseUrl(dc.baseUrlFunc) + if goErr != nil { + return res, errors.NewCommonEdgeXWrapper(goErr) + } + err = utils.GetRequest(ctx, &res, baseUrl, requestPath, requestParams, dc.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } diff --git a/clients/http/deviceprofile.go b/clients/http/deviceprofile.go index 0863e00f..6fc9ae2d 100644 --- a/clients/http/deviceprofile.go +++ b/clients/http/deviceprofile.go @@ -1,5 +1,5 @@ // -// Copyright (C) 2020-2024 IOTech Ltd +// Copyright (C) 2020-2025 IOTech Ltd // Copyright (C) 2023 Intel Corporation // // SPDX-License-Identifier: Apache-2.0 @@ -15,6 +15,7 @@ import ( "strings" "sync" + "github.com/edgexfoundry/go-mod-core-contracts/v4/clients" "github.com/edgexfoundry/go-mod-core-contracts/v4/clients/http/utils" "github.com/edgexfoundry/go-mod-core-contracts/v4/clients/interfaces" "github.com/edgexfoundry/go-mod-core-contracts/v4/common" @@ -25,7 +26,7 @@ import ( ) type DeviceProfileClient struct { - baseUrl string + baseUrlFunc clients.ClientBaseUrlFunc authInjector interfaces.AuthenticationInjector resourcesCache map[string]responses.DeviceResourceResponse mux sync.RWMutex @@ -35,51 +36,76 @@ type DeviceProfileClient struct { // NewDeviceProfileClient creates an instance of DeviceProfileClient func NewDeviceProfileClient(baseUrl string, authInjector interfaces.AuthenticationInjector, enableNameFieldEscape bool) interfaces.DeviceProfileClient { return &DeviceProfileClient{ - baseUrl: baseUrl, + baseUrlFunc: clients.GetDefaultClientBaseUrlFunc(baseUrl), authInjector: authInjector, resourcesCache: make(map[string]responses.DeviceResourceResponse), enableNameFieldEscape: enableNameFieldEscape, } } +// NewDeviceProfileClientWithUrlCallback creates an instance of DeviceProfileClient with ClientBaseUrlFunc. +func NewDeviceProfileClientWithUrlCallback(baseUrlFunc clients.ClientBaseUrlFunc, authInjector interfaces.AuthenticationInjector, enableNameFieldEscape bool) interfaces.DeviceProfileClient { + return &DeviceProfileClient{ + baseUrlFunc: baseUrlFunc, + authInjector: authInjector, + enableNameFieldEscape: enableNameFieldEscape, + } +} + // Add adds new device profile func (client *DeviceProfileClient) Add(ctx context.Context, reqs []requests.DeviceProfileRequest) ([]dtoCommon.BaseWithIdResponse, errors.EdgeX) { - var responses []dtoCommon.BaseWithIdResponse - err := utils.PostRequestWithRawData(ctx, &responses, client.baseUrl, common.ApiDeviceProfileRoute, nil, reqs, client.authInjector) + var res []dtoCommon.BaseWithIdResponse + baseUrl, err := clients.GetBaseUrl(client.baseUrlFunc) if err != nil { - return responses, errors.NewCommonEdgeXWrapper(err) + return res, errors.NewCommonEdgeXWrapper(err) } - return responses, nil + err = utils.PostRequestWithRawData(ctx, &res, baseUrl, common.ApiDeviceProfileRoute, nil, reqs, client.authInjector) + if err != nil { + return res, errors.NewCommonEdgeXWrapper(err) + } + return res, nil } // Update updates device profile func (client *DeviceProfileClient) Update(ctx context.Context, reqs []requests.DeviceProfileRequest) ([]dtoCommon.BaseResponse, errors.EdgeX) { - var responses []dtoCommon.BaseResponse - err := utils.PutRequest(ctx, &responses, client.baseUrl, common.ApiDeviceProfileRoute, nil, reqs, client.authInjector) + var res []dtoCommon.BaseResponse + baseUrl, err := clients.GetBaseUrl(client.baseUrlFunc) if err != nil { - return responses, errors.NewCommonEdgeXWrapper(err) + return res, errors.NewCommonEdgeXWrapper(err) } - return responses, nil + err = utils.PutRequest(ctx, &res, baseUrl, common.ApiDeviceProfileRoute, nil, reqs, client.authInjector) + if err != nil { + return res, errors.NewCommonEdgeXWrapper(err) + } + return res, nil } // AddByYaml adds new device profile by uploading a yaml file func (client *DeviceProfileClient) AddByYaml(ctx context.Context, yamlFilePath string) (dtoCommon.BaseWithIdResponse, errors.EdgeX) { - var responses dtoCommon.BaseWithIdResponse - err := utils.PostByFileRequest(ctx, &responses, client.baseUrl, common.ApiDeviceProfileUploadFileRoute, yamlFilePath, client.authInjector) + var res dtoCommon.BaseWithIdResponse + baseUrl, err := clients.GetBaseUrl(client.baseUrlFunc) if err != nil { - return responses, errors.NewCommonEdgeXWrapper(err) + return res, errors.NewCommonEdgeXWrapper(err) } - return responses, nil + err = utils.PostByFileRequest(ctx, &res, baseUrl, common.ApiDeviceProfileUploadFileRoute, yamlFilePath, client.authInjector) + if err != nil { + return res, errors.NewCommonEdgeXWrapper(err) + } + return res, nil } // UpdateByYaml updates device profile by uploading a yaml file func (client *DeviceProfileClient) UpdateByYaml(ctx context.Context, yamlFilePath string) (dtoCommon.BaseResponse, errors.EdgeX) { - var responses dtoCommon.BaseResponse - err := utils.PutByFileRequest(ctx, &responses, client.baseUrl, common.ApiDeviceProfileUploadFileRoute, yamlFilePath, client.authInjector) + var res dtoCommon.BaseResponse + baseUrl, err := clients.GetBaseUrl(client.baseUrlFunc) if err != nil { - return responses, errors.NewCommonEdgeXWrapper(err) + return res, errors.NewCommonEdgeXWrapper(err) } - return responses, nil + err = utils.PutByFileRequest(ctx, &res, baseUrl, common.ApiDeviceProfileUploadFileRoute, yamlFilePath, client.authInjector) + if err != nil { + return res, errors.NewCommonEdgeXWrapper(err) + } + return res, nil } // DeleteByName deletes the device profile by name @@ -87,7 +113,11 @@ func (client *DeviceProfileClient) DeleteByName(ctx context.Context, name string var response dtoCommon.BaseResponse requestPath := common.NewPathBuilder().EnableNameFieldEscape(client.enableNameFieldEscape). SetPath(common.ApiDeviceProfileRoute).SetPath(common.Name).SetNameFieldPath(name).BuildPath() - err := utils.DeleteRequest(ctx, &response, client.baseUrl, requestPath, client.authInjector) + baseUrl, err := clients.GetBaseUrl(client.baseUrlFunc) + if err != nil { + return response, errors.NewCommonEdgeXWrapper(err) + } + err = utils.DeleteRequest(ctx, &response, baseUrl, requestPath, client.authInjector) if err != nil { return response, errors.NewCommonEdgeXWrapper(err) } @@ -98,7 +128,11 @@ func (client *DeviceProfileClient) DeleteByName(ctx context.Context, name string func (client *DeviceProfileClient) DeviceProfileByName(ctx context.Context, name string) (res responses.DeviceProfileResponse, edgexError errors.EdgeX) { requestPath := common.NewPathBuilder().EnableNameFieldEscape(client.enableNameFieldEscape). SetPath(common.ApiDeviceProfileRoute).SetPath(common.Name).SetNameFieldPath(name).BuildPath() - err := utils.GetRequest(ctx, &res, client.baseUrl, requestPath, nil, client.authInjector) + baseUrl, err := clients.GetBaseUrl(client.baseUrlFunc) + if err != nil { + return res, errors.NewCommonEdgeXWrapper(err) + } + err = utils.GetRequest(ctx, &res, baseUrl, requestPath, nil, client.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } @@ -113,7 +147,11 @@ func (client *DeviceProfileClient) AllDeviceProfiles(ctx context.Context, labels } requestParams.Set(common.Offset, strconv.Itoa(offset)) requestParams.Set(common.Limit, strconv.Itoa(limit)) - err := utils.GetRequest(ctx, &res, client.baseUrl, common.ApiAllDeviceProfileRoute, requestParams, client.authInjector) + baseUrl, err := clients.GetBaseUrl(client.baseUrlFunc) + if err != nil { + return res, errors.NewCommonEdgeXWrapper(err) + } + err = utils.GetRequest(ctx, &res, baseUrl, common.ApiAllDeviceProfileRoute, requestParams, client.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } @@ -128,7 +166,11 @@ func (client *DeviceProfileClient) AllDeviceProfileBasicInfos(ctx context.Contex } requestParams.Set(common.Offset, strconv.Itoa(offset)) requestParams.Set(common.Limit, strconv.Itoa(limit)) - err := utils.GetRequest(ctx, &res, client.baseUrl, common.ApiAllDeviceProfileBasicInfoRoute, requestParams, client.authInjector) + baseUrl, err := clients.GetBaseUrl(client.baseUrlFunc) + if err != nil { + return res, errors.NewCommonEdgeXWrapper(err) + } + err = utils.GetRequest(ctx, &res, baseUrl, common.ApiAllDeviceProfileBasicInfoRoute, requestParams, client.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } @@ -141,7 +183,11 @@ func (client *DeviceProfileClient) DeviceProfilesByModel(ctx context.Context, mo requestParams := url.Values{} requestParams.Set(common.Offset, strconv.Itoa(offset)) requestParams.Set(common.Limit, strconv.Itoa(limit)) - err := utils.GetRequest(ctx, &res, client.baseUrl, requestPath, requestParams, client.authInjector) + baseUrl, err := clients.GetBaseUrl(client.baseUrlFunc) + if err != nil { + return res, errors.NewCommonEdgeXWrapper(err) + } + err = utils.GetRequest(ctx, &res, baseUrl, requestPath, requestParams, client.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } @@ -154,7 +200,11 @@ func (client *DeviceProfileClient) DeviceProfilesByManufacturer(ctx context.Cont requestParams := url.Values{} requestParams.Set(common.Offset, strconv.Itoa(offset)) requestParams.Set(common.Limit, strconv.Itoa(limit)) - err := utils.GetRequest(ctx, &res, client.baseUrl, requestPath, requestParams, client.authInjector) + baseUrl, err := clients.GetBaseUrl(client.baseUrlFunc) + if err != nil { + return res, errors.NewCommonEdgeXWrapper(err) + } + err = utils.GetRequest(ctx, &res, baseUrl, requestPath, requestParams, client.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } @@ -167,7 +217,11 @@ func (client *DeviceProfileClient) DeviceProfilesByManufacturerAndModel(ctx cont requestParams := url.Values{} requestParams.Set(common.Offset, strconv.Itoa(offset)) requestParams.Set(common.Limit, strconv.Itoa(limit)) - err := utils.GetRequest(ctx, &res, client.baseUrl, requestPath, requestParams, client.authInjector) + baseUrl, err := clients.GetBaseUrl(client.baseUrlFunc) + if err != nil { + return res, errors.NewCommonEdgeXWrapper(err) + } + err = utils.GetRequest(ctx, &res, baseUrl, requestPath, requestParams, client.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } @@ -183,7 +237,11 @@ func (client *DeviceProfileClient) DeviceResourceByProfileNameAndResourceName(ct } requestPath := common.NewPathBuilder().EnableNameFieldEscape(client.enableNameFieldEscape). SetPath(common.ApiDeviceResourceRoute).SetPath(common.Profile).SetNameFieldPath(profileName).SetPath(common.Resource).SetNameFieldPath(resourceName).BuildPath() - err := utils.GetRequest(ctx, &res, client.baseUrl, requestPath, nil, client.authInjector) + baseUrl, err := clients.GetBaseUrl(client.baseUrlFunc) + if err != nil { + return res, errors.NewCommonEdgeXWrapper(err) + } + err = utils.GetRequest(ctx, &res, baseUrl, requestPath, nil, client.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } @@ -212,32 +270,44 @@ func (client *DeviceProfileClient) CleanResourcesCache() { // UpdateDeviceProfileBasicInfo updates existing profile's basic info func (client *DeviceProfileClient) UpdateDeviceProfileBasicInfo(ctx context.Context, reqs []requests.DeviceProfileBasicInfoRequest) ([]dtoCommon.BaseResponse, errors.EdgeX) { - var responses []dtoCommon.BaseResponse - err := utils.PatchRequest(ctx, &responses, client.baseUrl, common.ApiDeviceProfileBasicInfoRoute, nil, reqs, client.authInjector) + var res []dtoCommon.BaseResponse + baseUrl, err := clients.GetBaseUrl(client.baseUrlFunc) + if err != nil { + return res, errors.NewCommonEdgeXWrapper(err) + } + err = utils.PatchRequest(ctx, &res, baseUrl, common.ApiDeviceProfileBasicInfoRoute, nil, reqs, client.authInjector) if err != nil { - return responses, errors.NewCommonEdgeXWrapper(err) + return res, errors.NewCommonEdgeXWrapper(err) } - return responses, nil + return res, nil } // AddDeviceProfileResource adds new device resource to an existing profile func (client *DeviceProfileClient) AddDeviceProfileResource(ctx context.Context, reqs []requests.AddDeviceResourceRequest) ([]dtoCommon.BaseResponse, errors.EdgeX) { - var responses []dtoCommon.BaseResponse - err := utils.PostRequestWithRawData(ctx, &responses, client.baseUrl, common.ApiDeviceProfileResourceRoute, nil, reqs, client.authInjector) + var res []dtoCommon.BaseResponse + baseUrl, err := clients.GetBaseUrl(client.baseUrlFunc) + if err != nil { + return res, errors.NewCommonEdgeXWrapper(err) + } + err = utils.PostRequestWithRawData(ctx, &res, baseUrl, common.ApiDeviceProfileResourceRoute, nil, reqs, client.authInjector) if err != nil { - return responses, errors.NewCommonEdgeXWrapper(err) + return res, errors.NewCommonEdgeXWrapper(err) } - return responses, nil + return res, nil } // UpdateDeviceProfileResource updates existing device resource func (client *DeviceProfileClient) UpdateDeviceProfileResource(ctx context.Context, reqs []requests.UpdateDeviceResourceRequest) ([]dtoCommon.BaseResponse, errors.EdgeX) { - var responses []dtoCommon.BaseResponse - err := utils.PatchRequest(ctx, &responses, client.baseUrl, common.ApiDeviceProfileResourceRoute, nil, reqs, client.authInjector) + var res []dtoCommon.BaseResponse + baseUrl, err := clients.GetBaseUrl(client.baseUrlFunc) + if err != nil { + return res, errors.NewCommonEdgeXWrapper(err) + } + err = utils.PatchRequest(ctx, &res, baseUrl, common.ApiDeviceProfileResourceRoute, nil, reqs, client.authInjector) if err != nil { - return responses, errors.NewCommonEdgeXWrapper(err) + return res, errors.NewCommonEdgeXWrapper(err) } - return responses, nil + return res, nil } // DeleteDeviceResourceByName deletes device resource by name @@ -245,7 +315,11 @@ func (client *DeviceProfileClient) DeleteDeviceResourceByName(ctx context.Contex var response dtoCommon.BaseResponse requestPath := common.NewPathBuilder().EnableNameFieldEscape(client.enableNameFieldEscape). SetPath(common.ApiDeviceProfileRoute).SetPath(common.Name).SetNameFieldPath(profileName).SetPath(common.Resource).SetNameFieldPath(resourceName).BuildPath() - err := utils.DeleteRequest(ctx, &response, client.baseUrl, requestPath, client.authInjector) + baseUrl, err := clients.GetBaseUrl(client.baseUrlFunc) + if err != nil { + return response, errors.NewCommonEdgeXWrapper(err) + } + err = utils.DeleteRequest(ctx, &response, baseUrl, requestPath, client.authInjector) if err != nil { return response, errors.NewCommonEdgeXWrapper(err) } @@ -254,22 +328,30 @@ func (client *DeviceProfileClient) DeleteDeviceResourceByName(ctx context.Contex // AddDeviceProfileDeviceCommand adds new device command to an existing profile func (client *DeviceProfileClient) AddDeviceProfileDeviceCommand(ctx context.Context, reqs []requests.AddDeviceCommandRequest) ([]dtoCommon.BaseResponse, errors.EdgeX) { - var responses []dtoCommon.BaseResponse - err := utils.PostRequestWithRawData(ctx, &responses, client.baseUrl, common.ApiDeviceProfileDeviceCommandRoute, nil, reqs, client.authInjector) + var res []dtoCommon.BaseResponse + baseUrl, err := clients.GetBaseUrl(client.baseUrlFunc) + if err != nil { + return res, errors.NewCommonEdgeXWrapper(err) + } + err = utils.PostRequestWithRawData(ctx, &res, baseUrl, common.ApiDeviceProfileDeviceCommandRoute, nil, reqs, client.authInjector) if err != nil { - return responses, errors.NewCommonEdgeXWrapper(err) + return res, errors.NewCommonEdgeXWrapper(err) } - return responses, nil + return res, nil } // UpdateDeviceProfileDeviceCommand updates existing device command func (client *DeviceProfileClient) UpdateDeviceProfileDeviceCommand(ctx context.Context, reqs []requests.UpdateDeviceCommandRequest) ([]dtoCommon.BaseResponse, errors.EdgeX) { - var responses []dtoCommon.BaseResponse - err := utils.PatchRequest(ctx, &responses, client.baseUrl, common.ApiDeviceProfileDeviceCommandRoute, nil, reqs, client.authInjector) + var res []dtoCommon.BaseResponse + baseUrl, err := clients.GetBaseUrl(client.baseUrlFunc) + if err != nil { + return res, errors.NewCommonEdgeXWrapper(err) + } + err = utils.PatchRequest(ctx, &res, baseUrl, common.ApiDeviceProfileDeviceCommandRoute, nil, reqs, client.authInjector) if err != nil { - return responses, errors.NewCommonEdgeXWrapper(err) + return res, errors.NewCommonEdgeXWrapper(err) } - return responses, nil + return res, nil } // DeleteDeviceCommandByName deletes device command by name @@ -277,7 +359,11 @@ func (client *DeviceProfileClient) DeleteDeviceCommandByName(ctx context.Context var response dtoCommon.BaseResponse requestPath := common.NewPathBuilder().EnableNameFieldEscape(client.enableNameFieldEscape). SetPath(common.ApiDeviceProfileRoute).SetPath(common.Name).SetNameFieldPath(profileName).SetPath(common.DeviceCommand).SetNameFieldPath(commandName).BuildPath() - err := utils.DeleteRequest(ctx, &response, client.baseUrl, requestPath, client.authInjector) + baseUrl, err := clients.GetBaseUrl(client.baseUrlFunc) + if err != nil { + return response, errors.NewCommonEdgeXWrapper(err) + } + err = utils.DeleteRequest(ctx, &response, baseUrl, requestPath, client.authInjector) if err != nil { return response, errors.NewCommonEdgeXWrapper(err) } diff --git a/clients/http/deviceservice.go b/clients/http/deviceservice.go index 6d57c385..3c3ebdf8 100644 --- a/clients/http/deviceservice.go +++ b/clients/http/deviceservice.go @@ -1,6 +1,7 @@ // // Copyright (C) 2020-2021 Unknown author // Copyright (C) 2023 Intel Corporation +// Copyright (C) 2025 IOTech Ltd // // SPDX-License-Identifier: Apache-2.0 @@ -12,6 +13,7 @@ import ( "strconv" "strings" + "github.com/edgexfoundry/go-mod-core-contracts/v4/clients" "github.com/edgexfoundry/go-mod-core-contracts/v4/clients/http/utils" "github.com/edgexfoundry/go-mod-core-contracts/v4/clients/interfaces" "github.com/edgexfoundry/go-mod-core-contracts/v4/common" @@ -22,7 +24,7 @@ import ( ) type DeviceServiceClient struct { - baseUrl string + baseUrlFunc clients.ClientBaseUrlFunc authInjector interfaces.AuthenticationInjector enableNameFieldEscape bool } @@ -30,7 +32,16 @@ type DeviceServiceClient struct { // NewDeviceServiceClient creates an instance of DeviceServiceClient func NewDeviceServiceClient(baseUrl string, authInjector interfaces.AuthenticationInjector, enableNameFieldEscape bool) interfaces.DeviceServiceClient { return &DeviceServiceClient{ - baseUrl: baseUrl, + baseUrlFunc: clients.GetDefaultClientBaseUrlFunc(baseUrl), + authInjector: authInjector, + enableNameFieldEscape: enableNameFieldEscape, + } +} + +// NewDeviceServiceClientWithUrlCallback creates an instance of DeviceServiceClient with ClientBaseUrlFunc. +func NewDeviceServiceClientWithUrlCallback(baseUrlFunc clients.ClientBaseUrlFunc, authInjector interfaces.AuthenticationInjector, enableNameFieldEscape bool) interfaces.DeviceServiceClient { + return &DeviceServiceClient{ + baseUrlFunc: baseUrlFunc, authInjector: authInjector, enableNameFieldEscape: enableNameFieldEscape, } @@ -38,7 +49,11 @@ func NewDeviceServiceClient(baseUrl string, authInjector interfaces.Authenticati func (dsc DeviceServiceClient) Add(ctx context.Context, reqs []requests.AddDeviceServiceRequest) ( res []dtoCommon.BaseWithIdResponse, err errors.EdgeX) { - err = utils.PostRequestWithRawData(ctx, &res, dsc.baseUrl, common.ApiDeviceServiceRoute, nil, reqs, dsc.authInjector) + baseUrl, goErr := clients.GetBaseUrl(dsc.baseUrlFunc) + if goErr != nil { + return res, errors.NewCommonEdgeXWrapper(goErr) + } + err = utils.PostRequestWithRawData(ctx, &res, baseUrl, common.ApiDeviceServiceRoute, nil, reqs, dsc.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } @@ -47,7 +62,11 @@ func (dsc DeviceServiceClient) Add(ctx context.Context, reqs []requests.AddDevic func (dsc DeviceServiceClient) Update(ctx context.Context, reqs []requests.UpdateDeviceServiceRequest) ( res []dtoCommon.BaseResponse, err errors.EdgeX) { - err = utils.PatchRequest(ctx, &res, dsc.baseUrl, common.ApiDeviceServiceRoute, nil, reqs, dsc.authInjector) + baseUrl, goErr := clients.GetBaseUrl(dsc.baseUrlFunc) + if goErr != nil { + return res, errors.NewCommonEdgeXWrapper(goErr) + } + err = utils.PatchRequest(ctx, &res, baseUrl, common.ApiDeviceServiceRoute, nil, reqs, dsc.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } @@ -62,7 +81,11 @@ func (dsc DeviceServiceClient) AllDeviceServices(ctx context.Context, labels []s } requestParams.Set(common.Offset, strconv.Itoa(offset)) requestParams.Set(common.Limit, strconv.Itoa(limit)) - err = utils.GetRequest(ctx, &res, dsc.baseUrl, common.ApiAllDeviceServiceRoute, requestParams, dsc.authInjector) + baseUrl, goErr := clients.GetBaseUrl(dsc.baseUrlFunc) + if goErr != nil { + return res, errors.NewCommonEdgeXWrapper(goErr) + } + err = utils.GetRequest(ctx, &res, baseUrl, common.ApiAllDeviceServiceRoute, requestParams, dsc.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } @@ -73,7 +96,11 @@ func (dsc DeviceServiceClient) DeviceServiceByName(ctx context.Context, name str res responses.DeviceServiceResponse, err errors.EdgeX) { path := common.NewPathBuilder().EnableNameFieldEscape(dsc.enableNameFieldEscape). SetPath(common.ApiDeviceServiceRoute).SetPath(common.Name).SetNameFieldPath(name).BuildPath() - err = utils.GetRequest(ctx, &res, dsc.baseUrl, path, nil, dsc.authInjector) + baseUrl, goErr := clients.GetBaseUrl(dsc.baseUrlFunc) + if goErr != nil { + return res, errors.NewCommonEdgeXWrapper(goErr) + } + err = utils.GetRequest(ctx, &res, baseUrl, path, nil, dsc.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } @@ -84,7 +111,11 @@ func (dsc DeviceServiceClient) DeleteByName(ctx context.Context, name string) ( res dtoCommon.BaseResponse, err errors.EdgeX) { path := common.NewPathBuilder().EnableNameFieldEscape(dsc.enableNameFieldEscape). SetPath(common.ApiDeviceServiceRoute).SetPath(common.Name).SetNameFieldPath(name).BuildPath() - err = utils.DeleteRequest(ctx, &res, dsc.baseUrl, path, dsc.authInjector) + baseUrl, goErr := clients.GetBaseUrl(dsc.baseUrlFunc) + if goErr != nil { + return res, errors.NewCommonEdgeXWrapper(goErr) + } + err = utils.DeleteRequest(ctx, &res, baseUrl, path, dsc.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } diff --git a/clients/http/event.go b/clients/http/event.go index 4bfa9510..d5f8f328 100644 --- a/clients/http/event.go +++ b/clients/http/event.go @@ -1,5 +1,5 @@ // -// Copyright (C) 2021-2023 IOTech Ltd +// Copyright (C) 2021-2025 IOTech Ltd // Copyright (C) 2023 Intel Corporation // // SPDX-License-Identifier: Apache-2.0 @@ -12,6 +12,7 @@ import ( "path" "strconv" + "github.com/edgexfoundry/go-mod-core-contracts/v4/clients" "github.com/edgexfoundry/go-mod-core-contracts/v4/clients/http/utils" "github.com/edgexfoundry/go-mod-core-contracts/v4/clients/interfaces" "github.com/edgexfoundry/go-mod-core-contracts/v4/common" @@ -22,7 +23,7 @@ import ( ) type eventClient struct { - baseUrl string + baseUrlFunc clients.ClientBaseUrlFunc authInjector interfaces.AuthenticationInjector enableNameFieldEscape bool } @@ -30,7 +31,16 @@ type eventClient struct { // NewEventClient creates an instance of EventClient func NewEventClient(baseUrl string, authInjector interfaces.AuthenticationInjector, enableNameFieldEscape bool) interfaces.EventClient { return &eventClient{ - baseUrl: baseUrl, + baseUrlFunc: clients.GetDefaultClientBaseUrlFunc(baseUrl), + authInjector: authInjector, + enableNameFieldEscape: enableNameFieldEscape, + } +} + +// NewEventClientWithUrlCallback creates an instance of EventClient with ClientBaseUrlFunc. +func NewEventClientWithUrlCallback(baseUrlFunc clients.ClientBaseUrlFunc, authInjector interfaces.AuthenticationInjector, enableNameFieldEscape bool) interfaces.EventClient { + return &eventClient{ + baseUrlFunc: baseUrlFunc, authInjector: authInjector, enableNameFieldEscape: enableNameFieldEscape, } @@ -38,7 +48,7 @@ func NewEventClient(baseUrl string, authInjector interfaces.AuthenticationInject func (ec *eventClient) Add(ctx context.Context, serviceName string, req requests.AddEventRequest) ( dtoCommon.BaseWithIdResponse, errors.EdgeX) { - path := common.NewPathBuilder().EnableNameFieldEscape(ec.enableNameFieldEscape). + requestPath := common.NewPathBuilder().EnableNameFieldEscape(ec.enableNameFieldEscape). SetPath(common.ApiEventRoute).SetNameFieldPath(serviceName).SetNameFieldPath(req.Event.ProfileName).SetNameFieldPath(req.Event.DeviceName).SetNameFieldPath(req.Event.SourceName).BuildPath() var br dtoCommon.BaseWithIdResponse @@ -46,8 +56,11 @@ func (ec *eventClient) Add(ctx context.Context, serviceName string, req requests if err != nil { return br, errors.NewCommonEdgeXWrapper(err) } - - err = utils.PostRequest(ctx, &br, ec.baseUrl, path, bytes, encoding, ec.authInjector) + baseUrl, err := clients.GetBaseUrl(ec.baseUrlFunc) + if err != nil { + return br, errors.NewCommonEdgeXWrapper(err) + } + err = utils.PostRequest(ctx, &br, baseUrl, requestPath, bytes, encoding, ec.authInjector) if err != nil { return br, errors.NewCommonEdgeXWrapper(err) } @@ -59,7 +72,11 @@ func (ec *eventClient) AllEvents(ctx context.Context, offset, limit int) (respon requestParams.Set(common.Offset, strconv.Itoa(offset)) requestParams.Set(common.Limit, strconv.Itoa(limit)) res := responses.MultiEventsResponse{} - err := utils.GetRequest(ctx, &res, ec.baseUrl, common.ApiAllEventRoute, requestParams, ec.authInjector) + baseUrl, err := clients.GetBaseUrl(ec.baseUrlFunc) + if err != nil { + return res, errors.NewCommonEdgeXWrapper(err) + } + err = utils.GetRequest(ctx, &res, baseUrl, common.ApiAllEventRoute, requestParams, ec.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } @@ -68,7 +85,11 @@ func (ec *eventClient) AllEvents(ctx context.Context, offset, limit int) (respon func (ec *eventClient) EventCount(ctx context.Context) (dtoCommon.CountResponse, errors.EdgeX) { res := dtoCommon.CountResponse{} - err := utils.GetRequest(ctx, &res, ec.baseUrl, common.ApiEventCountRoute, nil, ec.authInjector) + baseUrl, err := clients.GetBaseUrl(ec.baseUrlFunc) + if err != nil { + return res, errors.NewCommonEdgeXWrapper(err) + } + err = utils.GetRequest(ctx, &res, baseUrl, common.ApiEventCountRoute, nil, ec.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } @@ -78,7 +99,11 @@ func (ec *eventClient) EventCount(ctx context.Context) (dtoCommon.CountResponse, func (ec *eventClient) EventCountByDeviceName(ctx context.Context, name string) (dtoCommon.CountResponse, errors.EdgeX) { requestPath := path.Join(common.ApiEventCountRoute, common.Device, common.Name, name) res := dtoCommon.CountResponse{} - err := utils.GetRequest(ctx, &res, ec.baseUrl, requestPath, nil, ec.authInjector) + baseUrl, err := clients.GetBaseUrl(ec.baseUrlFunc) + if err != nil { + return res, errors.NewCommonEdgeXWrapper(err) + } + err = utils.GetRequest(ctx, &res, baseUrl, requestPath, nil, ec.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } @@ -92,7 +117,11 @@ func (ec *eventClient) EventsByDeviceName(ctx context.Context, name string, offs requestParams.Set(common.Offset, strconv.Itoa(offset)) requestParams.Set(common.Limit, strconv.Itoa(limit)) res := responses.MultiEventsResponse{} - err := utils.GetRequest(ctx, &res, ec.baseUrl, requestPath, requestParams, ec.authInjector) + baseUrl, err := clients.GetBaseUrl(ec.baseUrlFunc) + if err != nil { + return res, errors.NewCommonEdgeXWrapper(err) + } + err = utils.GetRequest(ctx, &res, baseUrl, requestPath, requestParams, ec.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } @@ -100,9 +129,13 @@ func (ec *eventClient) EventsByDeviceName(ctx context.Context, name string, offs } func (ec *eventClient) DeleteByDeviceName(ctx context.Context, name string) (dtoCommon.BaseResponse, errors.EdgeX) { - path := path.Join(common.ApiEventRoute, common.Device, common.Name, name) + requestPath := path.Join(common.ApiEventRoute, common.Device, common.Name, name) res := dtoCommon.BaseResponse{} - err := utils.DeleteRequest(ctx, &res, ec.baseUrl, path, ec.authInjector) + baseUrl, err := clients.GetBaseUrl(ec.baseUrlFunc) + if err != nil { + return res, errors.NewCommonEdgeXWrapper(err) + } + err = utils.DeleteRequest(ctx, &res, baseUrl, requestPath, ec.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } @@ -116,7 +149,11 @@ func (ec *eventClient) EventsByTimeRange(ctx context.Context, start, end int64, requestParams.Set(common.Offset, strconv.Itoa(offset)) requestParams.Set(common.Limit, strconv.Itoa(limit)) res := responses.MultiEventsResponse{} - err := utils.GetRequest(ctx, &res, ec.baseUrl, requestPath, requestParams, ec.authInjector) + baseUrl, err := clients.GetBaseUrl(ec.baseUrlFunc) + if err != nil { + return res, errors.NewCommonEdgeXWrapper(err) + } + err = utils.GetRequest(ctx, &res, baseUrl, requestPath, requestParams, ec.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } @@ -124,9 +161,13 @@ func (ec *eventClient) EventsByTimeRange(ctx context.Context, start, end int64, } func (ec *eventClient) DeleteByAge(ctx context.Context, age int) (dtoCommon.BaseResponse, errors.EdgeX) { - path := path.Join(common.ApiEventRoute, common.Age, strconv.Itoa(age)) + requestPath := path.Join(common.ApiEventRoute, common.Age, strconv.Itoa(age)) res := dtoCommon.BaseResponse{} - err := utils.DeleteRequest(ctx, &res, ec.baseUrl, path, ec.authInjector) + baseUrl, err := clients.GetBaseUrl(ec.baseUrlFunc) + if err != nil { + return res, errors.NewCommonEdgeXWrapper(err) + } + err = utils.DeleteRequest(ctx, &res, baseUrl, requestPath, ec.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } @@ -134,9 +175,13 @@ func (ec *eventClient) DeleteByAge(ctx context.Context, age int) (dtoCommon.Base } func (ec *eventClient) DeleteById(ctx context.Context, id string) (dtoCommon.BaseResponse, errors.EdgeX) { - path := path.Join(common.ApiEventRoute, common.Id, id) + requestPath := path.Join(common.ApiEventRoute, common.Id, id) res := dtoCommon.BaseResponse{} - err := utils.DeleteRequest(ctx, &res, ec.baseUrl, path, ec.authInjector) + baseUrl, err := clients.GetBaseUrl(ec.baseUrlFunc) + if err != nil { + return res, errors.NewCommonEdgeXWrapper(err) + } + err = utils.DeleteRequest(ctx, &res, baseUrl, requestPath, ec.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } diff --git a/clients/http/notification.go b/clients/http/notification.go index e5c6b269..3a910088 100644 --- a/clients/http/notification.go +++ b/clients/http/notification.go @@ -1,5 +1,5 @@ // -// Copyright (C) 2021 IOTech Ltd +// Copyright (C) 2021-2025 IOTech Ltd // Copyright (C) 2023 Intel Corporation // // SPDX-License-Identifier: Apache-2.0 @@ -13,6 +13,7 @@ import ( "strconv" "strings" + "github.com/edgexfoundry/go-mod-core-contracts/v4/clients" "github.com/edgexfoundry/go-mod-core-contracts/v4/clients/http/utils" "github.com/edgexfoundry/go-mod-core-contracts/v4/clients/interfaces" "github.com/edgexfoundry/go-mod-core-contracts/v4/common" @@ -23,7 +24,7 @@ import ( ) type NotificationClient struct { - baseUrl string + baseUrlFunc clients.ClientBaseUrlFunc authInjector interfaces.AuthenticationInjector enableNameFieldEscape bool } @@ -31,7 +32,16 @@ type NotificationClient struct { // NewNotificationClient creates an instance of NotificationClient func NewNotificationClient(baseUrl string, authInjector interfaces.AuthenticationInjector, enableNameFieldEscape bool) interfaces.NotificationClient { return &NotificationClient{ - baseUrl: baseUrl, + baseUrlFunc: clients.GetDefaultClientBaseUrlFunc(baseUrl), + authInjector: authInjector, + enableNameFieldEscape: enableNameFieldEscape, + } +} + +// NewNotificationClientWithUrlCallback creates an instance of NotificationClient with ClientBaseUrlFunc. +func NewNotificationClientWithUrlCallback(baseUrlFunc clients.ClientBaseUrlFunc, authInjector interfaces.AuthenticationInjector, enableNameFieldEscape bool) interfaces.NotificationClient { + return &NotificationClient{ + baseUrlFunc: baseUrlFunc, authInjector: authInjector, enableNameFieldEscape: enableNameFieldEscape, } @@ -39,7 +49,11 @@ func NewNotificationClient(baseUrl string, authInjector interfaces.Authenticatio // SendNotification sends new notifications. func (client *NotificationClient) SendNotification(ctx context.Context, reqs []requests.AddNotificationRequest) (res []dtoCommon.BaseWithIdResponse, err errors.EdgeX) { - err = utils.PostRequestWithRawData(ctx, &res, client.baseUrl, common.ApiNotificationRoute, nil, reqs, client.authInjector) + baseUrl, goErr := clients.GetBaseUrl(client.baseUrlFunc) + if goErr != nil { + return res, errors.NewCommonEdgeXWrapper(goErr) + } + err = utils.PostRequestWithRawData(ctx, &res, baseUrl, common.ApiNotificationRoute, nil, reqs, client.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } @@ -48,8 +62,12 @@ func (client *NotificationClient) SendNotification(ctx context.Context, reqs []r // NotificationById query notification by id. func (client *NotificationClient) NotificationById(ctx context.Context, id string) (res responses.NotificationResponse, err errors.EdgeX) { - path := path.Join(common.ApiNotificationRoute, common.Id, id) - err = utils.GetRequest(ctx, &res, client.baseUrl, path, nil, client.authInjector) + requestPath := path.Join(common.ApiNotificationRoute, common.Id, id) + baseUrl, goErr := clients.GetBaseUrl(client.baseUrlFunc) + if goErr != nil { + return res, errors.NewCommonEdgeXWrapper(goErr) + } + err = utils.GetRequest(ctx, &res, baseUrl, requestPath, nil, client.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } @@ -58,8 +76,12 @@ func (client *NotificationClient) NotificationById(ctx context.Context, id strin // DeleteNotificationById deletes a notification by id. func (client *NotificationClient) DeleteNotificationById(ctx context.Context, id string) (res dtoCommon.BaseResponse, err errors.EdgeX) { - path := path.Join(common.ApiNotificationRoute, common.Id, id) - err = utils.DeleteRequest(ctx, &res, client.baseUrl, path, client.authInjector) + requestPath := path.Join(common.ApiNotificationRoute, common.Id, id) + baseUrl, goErr := clients.GetBaseUrl(client.baseUrlFunc) + if goErr != nil { + return res, errors.NewCommonEdgeXWrapper(goErr) + } + err = utils.DeleteRequest(ctx, &res, baseUrl, requestPath, client.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } @@ -73,7 +95,11 @@ func (client *NotificationClient) NotificationsByCategory(ctx context.Context, c requestParams.Set(common.Offset, strconv.Itoa(offset)) requestParams.Set(common.Limit, strconv.Itoa(limit)) requestParams.Set(common.Ack, ack) - err = utils.GetRequest(ctx, &res, client.baseUrl, requestPath, requestParams, client.authInjector) + baseUrl, goErr := clients.GetBaseUrl(client.baseUrlFunc) + if goErr != nil { + return res, errors.NewCommonEdgeXWrapper(goErr) + } + err = utils.GetRequest(ctx, &res, baseUrl, requestPath, requestParams, client.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } @@ -87,7 +113,11 @@ func (client *NotificationClient) NotificationsByLabel(ctx context.Context, labe requestParams.Set(common.Offset, strconv.Itoa(offset)) requestParams.Set(common.Limit, strconv.Itoa(limit)) requestParams.Set(common.Ack, ack) - err = utils.GetRequest(ctx, &res, client.baseUrl, requestPath, requestParams, client.authInjector) + baseUrl, goErr := clients.GetBaseUrl(client.baseUrlFunc) + if goErr != nil { + return res, errors.NewCommonEdgeXWrapper(goErr) + } + err = utils.GetRequest(ctx, &res, baseUrl, requestPath, requestParams, client.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } @@ -101,7 +131,11 @@ func (client *NotificationClient) NotificationsByStatus(ctx context.Context, sta requestParams.Set(common.Offset, strconv.Itoa(offset)) requestParams.Set(common.Limit, strconv.Itoa(limit)) requestParams.Set(common.Ack, ack) - err = utils.GetRequest(ctx, &res, client.baseUrl, requestPath, requestParams, client.authInjector) + baseUrl, goErr := clients.GetBaseUrl(client.baseUrlFunc) + if goErr != nil { + return res, errors.NewCommonEdgeXWrapper(goErr) + } + err = utils.GetRequest(ctx, &res, baseUrl, requestPath, requestParams, client.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } @@ -115,7 +149,11 @@ func (client *NotificationClient) NotificationsByTimeRange(ctx context.Context, requestParams.Set(common.Offset, strconv.Itoa(offset)) requestParams.Set(common.Limit, strconv.Itoa(limit)) requestParams.Set(common.Ack, ack) - err = utils.GetRequest(ctx, &res, client.baseUrl, requestPath, requestParams, client.authInjector) + baseUrl, goErr := clients.GetBaseUrl(client.baseUrlFunc) + if goErr != nil { + return res, errors.NewCommonEdgeXWrapper(goErr) + } + err = utils.GetRequest(ctx, &res, baseUrl, requestPath, requestParams, client.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } @@ -129,7 +167,11 @@ func (client *NotificationClient) NotificationsBySubscriptionName(ctx context.Co requestParams.Set(common.Offset, strconv.Itoa(offset)) requestParams.Set(common.Limit, strconv.Itoa(limit)) requestParams.Set(common.Ack, ack) - err = utils.GetRequest(ctx, &res, client.baseUrl, requestPath, requestParams, client.authInjector) + baseUrl, goErr := clients.GetBaseUrl(client.baseUrlFunc) + if goErr != nil { + return res, errors.NewCommonEdgeXWrapper(goErr) + } + err = utils.GetRequest(ctx, &res, baseUrl, requestPath, requestParams, client.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } @@ -139,8 +181,12 @@ func (client *NotificationClient) NotificationsBySubscriptionName(ctx context.Co // CleanupNotificationsByAge removes notifications that are older than age. And the corresponding transmissions will also be deleted. // Age is supposed in milliseconds since modified timestamp func (client *NotificationClient) CleanupNotificationsByAge(ctx context.Context, age int) (res dtoCommon.BaseResponse, err errors.EdgeX) { - path := path.Join(common.ApiNotificationCleanupRoute, common.Age, strconv.Itoa(age)) - err = utils.DeleteRequest(ctx, &res, client.baseUrl, path, client.authInjector) + requestPath := path.Join(common.ApiNotificationCleanupRoute, common.Age, strconv.Itoa(age)) + baseUrl, goErr := clients.GetBaseUrl(client.baseUrlFunc) + if goErr != nil { + return res, errors.NewCommonEdgeXWrapper(goErr) + } + err = utils.DeleteRequest(ctx, &res, baseUrl, requestPath, client.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } @@ -149,7 +195,11 @@ func (client *NotificationClient) CleanupNotificationsByAge(ctx context.Context, // CleanupNotifications removes notifications and the corresponding transmissions. func (client *NotificationClient) CleanupNotifications(ctx context.Context) (res dtoCommon.BaseResponse, err errors.EdgeX) { - err = utils.DeleteRequest(ctx, &res, client.baseUrl, common.ApiNotificationCleanupRoute, client.authInjector) + baseUrl, goErr := clients.GetBaseUrl(client.baseUrlFunc) + if goErr != nil { + return res, errors.NewCommonEdgeXWrapper(goErr) + } + err = utils.DeleteRequest(ctx, &res, baseUrl, common.ApiNotificationCleanupRoute, client.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } @@ -160,8 +210,12 @@ func (client *NotificationClient) CleanupNotifications(ctx context.Context) (res // Age is supposed in milliseconds since modified timestamp // Please notice that this API is only for processed notifications (status = PROCESSED). If the deletion purpose includes each kind of notifications, please refer to cleanup API. func (client *NotificationClient) DeleteProcessedNotificationsByAge(ctx context.Context, age int) (res dtoCommon.BaseResponse, err errors.EdgeX) { - path := path.Join(common.ApiNotificationRoute, common.Age, strconv.Itoa(age)) - err = utils.DeleteRequest(ctx, &res, client.baseUrl, path, client.authInjector) + requestPath := path.Join(common.ApiNotificationRoute, common.Age, strconv.Itoa(age)) + baseUrl, goErr := clients.GetBaseUrl(client.baseUrlFunc) + if goErr != nil { + return res, errors.NewCommonEdgeXWrapper(goErr) + } + err = utils.DeleteRequest(ctx, &res, baseUrl, requestPath, client.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } @@ -174,7 +228,11 @@ func (client *NotificationClient) NotificationsByQueryConditions(ctx context.Con requestParams.Set(common.Offset, strconv.Itoa(offset)) requestParams.Set(common.Limit, strconv.Itoa(limit)) requestParams.Set(common.Ack, ack) - err = utils.GetRequestWithBodyRawData(ctx, &res, client.baseUrl, common.ApiNotificationRoute, requestParams, conditionReq, client.authInjector) + baseUrl, goErr := clients.GetBaseUrl(client.baseUrlFunc) + if goErr != nil { + return res, errors.NewCommonEdgeXWrapper(goErr) + } + err = utils.GetRequestWithBodyRawData(ctx, &res, baseUrl, common.ApiNotificationRoute, requestParams, conditionReq, client.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } @@ -183,8 +241,12 @@ func (client *NotificationClient) NotificationsByQueryConditions(ctx context.Con // DeleteNotificationByIds deletes notifications by ids func (client *NotificationClient) DeleteNotificationByIds(ctx context.Context, ids []string) (res dtoCommon.BaseResponse, err errors.EdgeX) { - path := utils.EscapeAndJoinPath(common.ApiNotificationRoute, common.Ids, strings.Join(ids, common.CommaSeparator)) - err = utils.DeleteRequest(ctx, &res, client.baseUrl, path, client.authInjector) + requestPath := utils.EscapeAndJoinPath(common.ApiNotificationRoute, common.Ids, strings.Join(ids, common.CommaSeparator)) + baseUrl, goErr := clients.GetBaseUrl(client.baseUrlFunc) + if goErr != nil { + return res, errors.NewCommonEdgeXWrapper(goErr) + } + err = utils.DeleteRequest(ctx, &res, baseUrl, requestPath, client.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } @@ -197,8 +259,12 @@ func (client *NotificationClient) UpdateNotificationAckStatusByIds(ctx context.C if ack { pathAck = common.Acknowledge } - path := utils.EscapeAndJoinPath(common.ApiNotificationRoute, pathAck, common.Ids, strings.Join(ids, common.CommaSeparator)) - err = utils.PutRequest(ctx, &res, client.baseUrl, path, nil, nil, client.authInjector) + requestPath := utils.EscapeAndJoinPath(common.ApiNotificationRoute, pathAck, common.Ids, strings.Join(ids, common.CommaSeparator)) + baseUrl, goErr := clients.GetBaseUrl(client.baseUrlFunc) + if goErr != nil { + return res, errors.NewCommonEdgeXWrapper(goErr) + } + err = utils.PutRequest(ctx, &res, baseUrl, requestPath, nil, nil, client.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } diff --git a/clients/http/provisionwatcher.go b/clients/http/provisionwatcher.go index e651926f..65400323 100644 --- a/clients/http/provisionwatcher.go +++ b/clients/http/provisionwatcher.go @@ -1,5 +1,5 @@ // -// Copyright (C) 2021 IOTech Ltd +// Copyright (C) 2021-2025 IOTech Ltd // Copyright (C) 2023 Intel Corporation // // SPDX-License-Identifier: Apache-2.0 @@ -12,6 +12,7 @@ import ( "strconv" "strings" + "github.com/edgexfoundry/go-mod-core-contracts/v4/clients" "github.com/edgexfoundry/go-mod-core-contracts/v4/clients/http/utils" "github.com/edgexfoundry/go-mod-core-contracts/v4/clients/interfaces" "github.com/edgexfoundry/go-mod-core-contracts/v4/common" @@ -22,7 +23,7 @@ import ( ) type ProvisionWatcherClient struct { - baseUrl string + baseUrlFunc clients.ClientBaseUrlFunc authInjector interfaces.AuthenticationInjector enableNameFieldEscape bool } @@ -30,14 +31,27 @@ type ProvisionWatcherClient struct { // NewProvisionWatcherClient creates an instance of ProvisionWatcherClient func NewProvisionWatcherClient(baseUrl string, authInjector interfaces.AuthenticationInjector, enableNameFieldEscape bool) interfaces.ProvisionWatcherClient { return &ProvisionWatcherClient{ - baseUrl: baseUrl, + baseUrlFunc: clients.GetDefaultClientBaseUrlFunc(baseUrl), + authInjector: authInjector, + enableNameFieldEscape: enableNameFieldEscape, + } +} + +// NewProvisionWatcherClientWithUrlCallback creates an instance of ProvisionWatcherClient with ClientBaseUrlFunc. +func NewProvisionWatcherClientWithUrlCallback(baseUrlFunc clients.ClientBaseUrlFunc, authInjector interfaces.AuthenticationInjector, enableNameFieldEscape bool) interfaces.ProvisionWatcherClient { + return &ProvisionWatcherClient{ + baseUrlFunc: baseUrlFunc, authInjector: authInjector, enableNameFieldEscape: enableNameFieldEscape, } } func (pwc ProvisionWatcherClient) Add(ctx context.Context, reqs []requests.AddProvisionWatcherRequest) (res []dtoCommon.BaseWithIdResponse, err errors.EdgeX) { - err = utils.PostRequestWithRawData(ctx, &res, pwc.baseUrl, common.ApiProvisionWatcherRoute, nil, reqs, pwc.authInjector) + baseUrl, goErr := clients.GetBaseUrl(pwc.baseUrlFunc) + if goErr != nil { + return res, errors.NewCommonEdgeXWrapper(goErr) + } + err = utils.PostRequestWithRawData(ctx, &res, baseUrl, common.ApiProvisionWatcherRoute, nil, reqs, pwc.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } @@ -46,7 +60,11 @@ func (pwc ProvisionWatcherClient) Add(ctx context.Context, reqs []requests.AddPr } func (pwc ProvisionWatcherClient) Update(ctx context.Context, reqs []requests.UpdateProvisionWatcherRequest) (res []dtoCommon.BaseResponse, err errors.EdgeX) { - err = utils.PatchRequest(ctx, &res, pwc.baseUrl, common.ApiProvisionWatcherRoute, nil, reqs, pwc.authInjector) + baseUrl, goErr := clients.GetBaseUrl(pwc.baseUrlFunc) + if goErr != nil { + return res, errors.NewCommonEdgeXWrapper(goErr) + } + err = utils.PatchRequest(ctx, &res, baseUrl, common.ApiProvisionWatcherRoute, nil, reqs, pwc.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } @@ -61,7 +79,11 @@ func (pwc ProvisionWatcherClient) AllProvisionWatchers(ctx context.Context, labe } requestParams.Set(common.Offset, strconv.Itoa(offset)) requestParams.Set(common.Limit, strconv.Itoa(limit)) - err = utils.GetRequest(ctx, &res, pwc.baseUrl, common.ApiAllProvisionWatcherRoute, requestParams, pwc.authInjector) + baseUrl, goErr := clients.GetBaseUrl(pwc.baseUrlFunc) + if goErr != nil { + return res, errors.NewCommonEdgeXWrapper(goErr) + } + err = utils.GetRequest(ctx, &res, baseUrl, common.ApiAllProvisionWatcherRoute, requestParams, pwc.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } @@ -72,7 +94,11 @@ func (pwc ProvisionWatcherClient) AllProvisionWatchers(ctx context.Context, labe func (pwc ProvisionWatcherClient) ProvisionWatcherByName(ctx context.Context, name string) (res responses.ProvisionWatcherResponse, err errors.EdgeX) { path := common.NewPathBuilder().EnableNameFieldEscape(pwc.enableNameFieldEscape). SetPath(common.ApiProvisionWatcherRoute).SetPath(common.Name).SetNameFieldPath(name).BuildPath() - err = utils.GetRequest(ctx, &res, pwc.baseUrl, path, nil, pwc.authInjector) + baseUrl, goErr := clients.GetBaseUrl(pwc.baseUrlFunc) + if goErr != nil { + return res, errors.NewCommonEdgeXWrapper(goErr) + } + err = utils.GetRequest(ctx, &res, baseUrl, path, nil, pwc.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } @@ -83,7 +109,11 @@ func (pwc ProvisionWatcherClient) ProvisionWatcherByName(ctx context.Context, na func (pwc ProvisionWatcherClient) DeleteProvisionWatcherByName(ctx context.Context, name string) (res dtoCommon.BaseResponse, err errors.EdgeX) { path := common.NewPathBuilder().EnableNameFieldEscape(pwc.enableNameFieldEscape). SetPath(common.ApiProvisionWatcherRoute).SetPath(common.Name).SetNameFieldPath(name).BuildPath() - err = utils.DeleteRequest(ctx, &res, pwc.baseUrl, path, pwc.authInjector) + baseUrl, goErr := clients.GetBaseUrl(pwc.baseUrlFunc) + if goErr != nil { + return res, errors.NewCommonEdgeXWrapper(goErr) + } + err = utils.DeleteRequest(ctx, &res, baseUrl, path, pwc.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } @@ -97,7 +127,11 @@ func (pwc ProvisionWatcherClient) ProvisionWatchersByProfileName(ctx context.Con requestParams := url.Values{} requestParams.Set(common.Offset, strconv.Itoa(offset)) requestParams.Set(common.Limit, strconv.Itoa(limit)) - err = utils.GetRequest(ctx, &res, pwc.baseUrl, requestPath, requestParams, pwc.authInjector) + baseUrl, goErr := clients.GetBaseUrl(pwc.baseUrlFunc) + if goErr != nil { + return res, errors.NewCommonEdgeXWrapper(goErr) + } + err = utils.GetRequest(ctx, &res, baseUrl, requestPath, requestParams, pwc.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } @@ -111,7 +145,11 @@ func (pwc ProvisionWatcherClient) ProvisionWatchersByServiceName(ctx context.Con requestParams := url.Values{} requestParams.Set(common.Offset, strconv.Itoa(offset)) requestParams.Set(common.Limit, strconv.Itoa(limit)) - err = utils.GetRequest(ctx, &res, pwc.baseUrl, requestPath, requestParams, pwc.authInjector) + baseUrl, goErr := clients.GetBaseUrl(pwc.baseUrlFunc) + if goErr != nil { + return res, errors.NewCommonEdgeXWrapper(goErr) + } + err = utils.GetRequest(ctx, &res, baseUrl, requestPath, requestParams, pwc.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } diff --git a/clients/http/reading.go b/clients/http/reading.go index 43865404..3afdbaf1 100644 --- a/clients/http/reading.go +++ b/clients/http/reading.go @@ -1,5 +1,5 @@ // -// Copyright (C) 2020-2023 IOTech Ltd +// Copyright (C) 2020-2025 IOTech Ltd // Copyright (C) 2023 Intel Corporation // // SPDX-License-Identifier: Apache-2.0 @@ -12,6 +12,7 @@ import ( "path" "strconv" + "github.com/edgexfoundry/go-mod-core-contracts/v4/clients" "github.com/edgexfoundry/go-mod-core-contracts/v4/clients/http/utils" "github.com/edgexfoundry/go-mod-core-contracts/v4/clients/interfaces" "github.com/edgexfoundry/go-mod-core-contracts/v4/common" @@ -21,7 +22,7 @@ import ( ) type readingClient struct { - baseUrl string + baseUrlFunc clients.ClientBaseUrlFunc authInjector interfaces.AuthenticationInjector enableNameFieldEscape bool } @@ -29,7 +30,16 @@ type readingClient struct { // NewReadingClient creates an instance of ReadingClient func NewReadingClient(baseUrl string, authInjector interfaces.AuthenticationInjector, enableNameFieldEscape bool) interfaces.ReadingClient { return &readingClient{ - baseUrl: baseUrl, + baseUrlFunc: clients.GetDefaultClientBaseUrlFunc(baseUrl), + authInjector: authInjector, + enableNameFieldEscape: enableNameFieldEscape, + } +} + +// NewReadingClientWithUrlCallback creates an instance of ReadingClient with ClientBaseUrlFunc. +func NewReadingClientWithUrlCallback(baseUrlFunc clients.ClientBaseUrlFunc, authInjector interfaces.AuthenticationInjector, enableNameFieldEscape bool) interfaces.ReadingClient { + return &readingClient{ + baseUrlFunc: baseUrlFunc, authInjector: authInjector, enableNameFieldEscape: enableNameFieldEscape, } @@ -40,7 +50,11 @@ func (rc readingClient) AllReadings(ctx context.Context, offset, limit int) (res requestParams.Set(common.Offset, strconv.Itoa(offset)) requestParams.Set(common.Limit, strconv.Itoa(limit)) res := responses.MultiReadingsResponse{} - err := utils.GetRequest(ctx, &res, rc.baseUrl, common.ApiAllReadingRoute, requestParams, rc.authInjector) + baseUrl, err := clients.GetBaseUrl(rc.baseUrlFunc) + if err != nil { + return res, errors.NewCommonEdgeXWrapper(err) + } + err = utils.GetRequest(ctx, &res, baseUrl, common.ApiAllReadingRoute, requestParams, rc.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } @@ -49,7 +63,11 @@ func (rc readingClient) AllReadings(ctx context.Context, offset, limit int) (res func (rc readingClient) ReadingCount(ctx context.Context) (dtoCommon.CountResponse, errors.EdgeX) { res := dtoCommon.CountResponse{} - err := utils.GetRequest(ctx, &res, rc.baseUrl, common.ApiReadingCountRoute, nil, rc.authInjector) + baseUrl, err := clients.GetBaseUrl(rc.baseUrlFunc) + if err != nil { + return res, errors.NewCommonEdgeXWrapper(err) + } + err = utils.GetRequest(ctx, &res, baseUrl, common.ApiReadingCountRoute, nil, rc.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } @@ -59,7 +77,11 @@ func (rc readingClient) ReadingCount(ctx context.Context) (dtoCommon.CountRespon func (rc readingClient) ReadingCountByDeviceName(ctx context.Context, name string) (dtoCommon.CountResponse, errors.EdgeX) { requestPath := path.Join(common.ApiReadingCountRoute, common.Device, common.Name, name) res := dtoCommon.CountResponse{} - err := utils.GetRequest(ctx, &res, rc.baseUrl, requestPath, nil, rc.authInjector) + baseUrl, err := clients.GetBaseUrl(rc.baseUrlFunc) + if err != nil { + return res, errors.NewCommonEdgeXWrapper(err) + } + err = utils.GetRequest(ctx, &res, baseUrl, requestPath, nil, rc.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } @@ -72,7 +94,11 @@ func (rc readingClient) ReadingsByDeviceName(ctx context.Context, name string, o requestParams.Set(common.Offset, strconv.Itoa(offset)) requestParams.Set(common.Limit, strconv.Itoa(limit)) res := responses.MultiReadingsResponse{} - err := utils.GetRequest(ctx, &res, rc.baseUrl, requestPath, requestParams, rc.authInjector) + baseUrl, err := clients.GetBaseUrl(rc.baseUrlFunc) + if err != nil { + return res, errors.NewCommonEdgeXWrapper(err) + } + err = utils.GetRequest(ctx, &res, baseUrl, requestPath, requestParams, rc.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } @@ -86,7 +112,11 @@ func (rc readingClient) ReadingsByResourceName(ctx context.Context, name string, requestParams.Set(common.Offset, strconv.Itoa(offset)) requestParams.Set(common.Limit, strconv.Itoa(limit)) res := responses.MultiReadingsResponse{} - err := utils.GetRequest(ctx, &res, rc.baseUrl, requestPath, requestParams, rc.authInjector) + baseUrl, err := clients.GetBaseUrl(rc.baseUrlFunc) + if err != nil { + return res, errors.NewCommonEdgeXWrapper(err) + } + err = utils.GetRequest(ctx, &res, baseUrl, requestPath, requestParams, rc.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } @@ -99,7 +129,11 @@ func (rc readingClient) ReadingsByTimeRange(ctx context.Context, start, end int6 requestParams.Set(common.Offset, strconv.Itoa(offset)) requestParams.Set(common.Limit, strconv.Itoa(limit)) res := responses.MultiReadingsResponse{} - err := utils.GetRequest(ctx, &res, rc.baseUrl, requestPath, requestParams, rc.authInjector) + baseUrl, err := clients.GetBaseUrl(rc.baseUrlFunc) + if err != nil { + return res, errors.NewCommonEdgeXWrapper(err) + } + err = utils.GetRequest(ctx, &res, baseUrl, requestPath, requestParams, rc.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } @@ -114,7 +148,11 @@ func (rc readingClient) ReadingsByResourceNameAndTimeRange(ctx context.Context, requestParams.Set(common.Offset, strconv.Itoa(offset)) requestParams.Set(common.Limit, strconv.Itoa(limit)) res := responses.MultiReadingsResponse{} - err := utils.GetRequest(ctx, &res, rc.baseUrl, requestPath, requestParams, rc.authInjector) + baseUrl, err := clients.GetBaseUrl(rc.baseUrlFunc) + if err != nil { + return res, errors.NewCommonEdgeXWrapper(err) + } + err = utils.GetRequest(ctx, &res, baseUrl, requestPath, requestParams, rc.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } @@ -128,7 +166,11 @@ func (rc readingClient) ReadingsByDeviceNameAndResourceName(ctx context.Context, requestParams.Set(common.Offset, strconv.Itoa(offset)) requestParams.Set(common.Limit, strconv.Itoa(limit)) res := responses.MultiReadingsResponse{} - err := utils.GetRequest(ctx, &res, rc.baseUrl, requestPath, requestParams, rc.authInjector) + baseUrl, err := clients.GetBaseUrl(rc.baseUrlFunc) + if err != nil { + return res, errors.NewCommonEdgeXWrapper(err) + } + err = utils.GetRequest(ctx, &res, baseUrl, requestPath, requestParams, rc.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } @@ -144,7 +186,11 @@ func (rc readingClient) ReadingsByDeviceNameAndResourceNameAndTimeRange(ctx cont requestParams.Set(common.Offset, strconv.Itoa(offset)) requestParams.Set(common.Limit, strconv.Itoa(limit)) res := responses.MultiReadingsResponse{} - err := utils.GetRequest(ctx, &res, rc.baseUrl, requestPath, requestParams, rc.authInjector) + baseUrl, err := clients.GetBaseUrl(rc.baseUrlFunc) + if err != nil { + return res, errors.NewCommonEdgeXWrapper(err) + } + err = utils.GetRequest(ctx, &res, baseUrl, requestPath, requestParams, rc.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } @@ -164,7 +210,11 @@ func (rc readingClient) ReadingsByDeviceNameAndResourceNamesAndTimeRange(ctx con queryPayload[common.ResourceNames] = resourceNames } res := responses.MultiReadingsResponse{} - err := utils.GetRequestWithBodyRawData(ctx, &res, rc.baseUrl, requestPath, requestParams, queryPayload, rc.authInjector) + baseUrl, err := clients.GetBaseUrl(rc.baseUrlFunc) + if err != nil { + return res, errors.NewCommonEdgeXWrapper(err) + } + err = utils.GetRequestWithBodyRawData(ctx, &res, baseUrl, requestPath, requestParams, queryPayload, rc.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } diff --git a/clients/http/scheduleactionrecord.go b/clients/http/scheduleactionrecord.go index 2a97e777..304b05cc 100644 --- a/clients/http/scheduleactionrecord.go +++ b/clients/http/scheduleactionrecord.go @@ -1,5 +1,5 @@ // -// Copyright (C) 2024 IOTech Ltd +// Copyright (C) 2024-2025 IOTech Ltd // // SPDX-License-Identifier: Apache-2.0 @@ -11,6 +11,7 @@ import ( "path" "strconv" + "github.com/edgexfoundry/go-mod-core-contracts/v4/clients" "github.com/edgexfoundry/go-mod-core-contracts/v4/clients/http/utils" "github.com/edgexfoundry/go-mod-core-contracts/v4/clients/interfaces" "github.com/edgexfoundry/go-mod-core-contracts/v4/common" @@ -19,7 +20,7 @@ import ( ) type ScheduleActionRecordClient struct { - baseUrl string + baseUrlFunc clients.ClientBaseUrlFunc authInjector interfaces.AuthenticationInjector enableNameFieldEscape bool } @@ -27,7 +28,16 @@ type ScheduleActionRecordClient struct { // NewScheduleActionRecordClient creates an instance of ScheduleActionRecordClient func NewScheduleActionRecordClient(baseUrl string, authInjector interfaces.AuthenticationInjector, enableNameFieldEscape bool) interfaces.ScheduleActionRecordClient { return &ScheduleActionRecordClient{ - baseUrl: baseUrl, + baseUrlFunc: clients.GetDefaultClientBaseUrlFunc(baseUrl), + authInjector: authInjector, + enableNameFieldEscape: enableNameFieldEscape, + } +} + +// NewScheduleActionRecordClientWithUrlCallback creates an instance of ScheduleActionRecordClient with ClientBaseUrlFunc. +func NewScheduleActionRecordClientWithUrlCallback(baseUrlFunc clients.ClientBaseUrlFunc, authInjector interfaces.AuthenticationInjector, enableNameFieldEscape bool) interfaces.ScheduleActionRecordClient { + return &ScheduleActionRecordClient{ + baseUrlFunc: baseUrlFunc, authInjector: authInjector, enableNameFieldEscape: enableNameFieldEscape, } @@ -40,7 +50,11 @@ func (client *ScheduleActionRecordClient) AllScheduleActionRecords(ctx context.C requestParams.Set(common.End, strconv.FormatInt(end, 10)) requestParams.Set(common.Offset, strconv.Itoa(offset)) requestParams.Set(common.Limit, strconv.Itoa(limit)) - err = utils.GetRequest(ctx, &res, client.baseUrl, common.ApiAllScheduleActionRecordRoute, requestParams, client.authInjector) + baseUrl, goErr := clients.GetBaseUrl(client.baseUrlFunc) + if goErr != nil { + return res, errors.NewCommonEdgeXWrapper(goErr) + } + err = utils.GetRequest(ctx, &res, baseUrl, common.ApiAllScheduleActionRecordRoute, requestParams, client.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } @@ -52,7 +66,11 @@ func (client *ScheduleActionRecordClient) LatestScheduleActionRecordsByJobName(c requestPath := path.Join(common.ApiScheduleActionRecordRoute, common.Latest, common.Job, common.Name, jobName) requestParams := url.Values{} requestParams.Set(common.Name, jobName) - err = utils.GetRequest(ctx, &res, client.baseUrl, requestPath, requestParams, client.authInjector) + baseUrl, goErr := clients.GetBaseUrl(client.baseUrlFunc) + if goErr != nil { + return res, errors.NewCommonEdgeXWrapper(goErr) + } + err = utils.GetRequest(ctx, &res, baseUrl, requestPath, requestParams, client.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } @@ -67,7 +85,11 @@ func (client *ScheduleActionRecordClient) ScheduleActionRecordsByStatus(ctx cont requestParams.Set(common.End, strconv.FormatInt(end, 10)) requestParams.Set(common.Offset, strconv.Itoa(offset)) requestParams.Set(common.Limit, strconv.Itoa(limit)) - err = utils.GetRequest(ctx, &res, client.baseUrl, requestPath, requestParams, client.authInjector) + baseUrl, goErr := clients.GetBaseUrl(client.baseUrlFunc) + if goErr != nil { + return res, errors.NewCommonEdgeXWrapper(goErr) + } + err = utils.GetRequest(ctx, &res, baseUrl, requestPath, requestParams, client.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } @@ -82,7 +104,11 @@ func (client *ScheduleActionRecordClient) ScheduleActionRecordsByJobName(ctx con requestParams.Set(common.End, strconv.FormatInt(end, 10)) requestParams.Set(common.Offset, strconv.Itoa(offset)) requestParams.Set(common.Limit, strconv.Itoa(limit)) - err = utils.GetRequest(ctx, &res, client.baseUrl, requestPath, requestParams, client.authInjector) + baseUrl, goErr := clients.GetBaseUrl(client.baseUrlFunc) + if goErr != nil { + return res, errors.NewCommonEdgeXWrapper(goErr) + } + err = utils.GetRequest(ctx, &res, baseUrl, requestPath, requestParams, client.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } @@ -97,7 +123,11 @@ func (client *ScheduleActionRecordClient) ScheduleActionRecordsByJobNameAndStatu requestParams.Set(common.End, strconv.FormatInt(end, 10)) requestParams.Set(common.Offset, strconv.Itoa(offset)) requestParams.Set(common.Limit, strconv.Itoa(limit)) - err = utils.GetRequest(ctx, &res, client.baseUrl, requestPath, requestParams, client.authInjector) + baseUrl, goErr := clients.GetBaseUrl(client.baseUrlFunc) + if goErr != nil { + return res, errors.NewCommonEdgeXWrapper(goErr) + } + err = utils.GetRequest(ctx, &res, baseUrl, requestPath, requestParams, client.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } diff --git a/clients/http/schedulejob.go b/clients/http/schedulejob.go index d6f12015..f9e827a9 100644 --- a/clients/http/schedulejob.go +++ b/clients/http/schedulejob.go @@ -1,5 +1,5 @@ // -// Copyright (C) 2024 IOTech Ltd +// Copyright (C) 2024-2025 IOTech Ltd // // SPDX-License-Identifier: Apache-2.0 @@ -11,6 +11,7 @@ import ( "strconv" "strings" + "github.com/edgexfoundry/go-mod-core-contracts/v4/clients" "github.com/edgexfoundry/go-mod-core-contracts/v4/clients/http/utils" "github.com/edgexfoundry/go-mod-core-contracts/v4/clients/interfaces" "github.com/edgexfoundry/go-mod-core-contracts/v4/common" @@ -21,7 +22,7 @@ import ( ) type ScheduleJobClient struct { - baseUrl string + baseUrlFunc clients.ClientBaseUrlFunc authInjector interfaces.AuthenticationInjector enableNameFieldEscape bool } @@ -29,7 +30,16 @@ type ScheduleJobClient struct { // NewScheduleJobClient creates an instance of ScheduleJobClient func NewScheduleJobClient(baseUrl string, authInjector interfaces.AuthenticationInjector, enableNameFieldEscape bool) interfaces.ScheduleJobClient { return &ScheduleJobClient{ - baseUrl: baseUrl, + baseUrlFunc: clients.GetDefaultClientBaseUrlFunc(baseUrl), + authInjector: authInjector, + enableNameFieldEscape: enableNameFieldEscape, + } +} + +// NewScheduleJobClientWithUrlCallback creates an instance of ScheduleJobClient with ClientBaseUrlFunc. +func NewScheduleJobClientWithUrlCallback(baseUrlFunc clients.ClientBaseUrlFunc, authInjector interfaces.AuthenticationInjector, enableNameFieldEscape bool) interfaces.ScheduleJobClient { + return &ScheduleJobClient{ + baseUrlFunc: baseUrlFunc, authInjector: authInjector, enableNameFieldEscape: enableNameFieldEscape, } @@ -38,7 +48,11 @@ func NewScheduleJobClient(baseUrl string, authInjector interfaces.Authentication // Add adds new schedule jobs func (client ScheduleJobClient) Add(ctx context.Context, reqs []requests.AddScheduleJobRequest) ( res []dtoCommon.BaseWithIdResponse, err errors.EdgeX) { - err = utils.PostRequestWithRawData(ctx, &res, client.baseUrl, common.ApiScheduleJobRoute, nil, reqs, client.authInjector) + baseUrl, goErr := clients.GetBaseUrl(client.baseUrlFunc) + if goErr != nil { + return res, errors.NewCommonEdgeXWrapper(goErr) + } + err = utils.PostRequestWithRawData(ctx, &res, baseUrl, common.ApiScheduleJobRoute, nil, reqs, client.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } @@ -48,7 +62,11 @@ func (client ScheduleJobClient) Add(ctx context.Context, reqs []requests.AddSche // Update updates schedule jobs func (client ScheduleJobClient) Update(ctx context.Context, reqs []requests.UpdateScheduleJobRequest) ( res []dtoCommon.BaseResponse, err errors.EdgeX) { - err = utils.PatchRequest(ctx, &res, client.baseUrl, common.ApiScheduleJobRoute, nil, reqs, client.authInjector) + baseUrl, goErr := clients.GetBaseUrl(client.baseUrlFunc) + if goErr != nil { + return res, errors.NewCommonEdgeXWrapper(goErr) + } + err = utils.PatchRequest(ctx, &res, baseUrl, common.ApiScheduleJobRoute, nil, reqs, client.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } @@ -64,7 +82,11 @@ func (client ScheduleJobClient) AllScheduleJobs(ctx context.Context, labels []st } requestParams.Set(common.Offset, strconv.Itoa(offset)) requestParams.Set(common.Limit, strconv.Itoa(limit)) - err = utils.GetRequest(ctx, &res, client.baseUrl, common.ApiAllScheduleJobRoute, requestParams, client.authInjector) + baseUrl, goErr := clients.GetBaseUrl(client.baseUrlFunc) + if goErr != nil { + return res, errors.NewCommonEdgeXWrapper(goErr) + } + err = utils.GetRequest(ctx, &res, baseUrl, common.ApiAllScheduleJobRoute, requestParams, client.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } @@ -76,7 +98,11 @@ func (client ScheduleJobClient) ScheduleJobByName(ctx context.Context, name stri res responses.ScheduleJobResponse, err errors.EdgeX) { requestPath := common.NewPathBuilder().EnableNameFieldEscape(client.enableNameFieldEscape). SetPath(common.ApiScheduleJobRoute).SetPath(common.Name).SetNameFieldPath(name).BuildPath() - err = utils.GetRequest(ctx, &res, client.baseUrl, requestPath, nil, client.authInjector) + baseUrl, goErr := clients.GetBaseUrl(client.baseUrlFunc) + if goErr != nil { + return res, errors.NewCommonEdgeXWrapper(goErr) + } + err = utils.GetRequest(ctx, &res, baseUrl, requestPath, nil, client.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } @@ -88,7 +114,11 @@ func (client ScheduleJobClient) DeleteScheduleJobByName(ctx context.Context, nam res dtoCommon.BaseResponse, err errors.EdgeX) { requestPath := common.NewPathBuilder().EnableNameFieldEscape(client.enableNameFieldEscape). SetPath(common.ApiScheduleJobRoute).SetPath(common.Name).SetNameFieldPath(name).BuildPath() - err = utils.DeleteRequest(ctx, &res, client.baseUrl, requestPath, client.authInjector) + baseUrl, goErr := clients.GetBaseUrl(client.baseUrlFunc) + if goErr != nil { + return res, errors.NewCommonEdgeXWrapper(goErr) + } + err = utils.DeleteRequest(ctx, &res, baseUrl, requestPath, client.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } @@ -100,7 +130,11 @@ func (client ScheduleJobClient) TriggerScheduleJobByName(ctx context.Context, na res dtoCommon.BaseResponse, err errors.EdgeX) { requestPath := common.NewPathBuilder().EnableNameFieldEscape(client.enableNameFieldEscape). SetPath(common.ApiTriggerScheduleJobRoute).SetPath(common.Name).SetNameFieldPath(name).BuildPath() - err = utils.PostRequestWithRawData(ctx, &res, client.baseUrl, requestPath, nil, nil, client.authInjector) + baseUrl, goErr := clients.GetBaseUrl(client.baseUrlFunc) + if goErr != nil { + return res, errors.NewCommonEdgeXWrapper(goErr) + } + err = utils.PostRequestWithRawData(ctx, &res, baseUrl, requestPath, nil, nil, client.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } diff --git a/clients/http/subscription.go b/clients/http/subscription.go index daa2212c..d0bba0f1 100644 --- a/clients/http/subscription.go +++ b/clients/http/subscription.go @@ -1,5 +1,5 @@ // -// Copyright (C) 2021 IOTech Ltd +// Copyright (C) 2021-2025 IOTech Ltd // Copyright (C) 2023 Intel Corporation // // SPDX-License-Identifier: Apache-2.0 @@ -12,6 +12,7 @@ import ( "path" "strconv" + "github.com/edgexfoundry/go-mod-core-contracts/v4/clients" "github.com/edgexfoundry/go-mod-core-contracts/v4/clients/http/utils" "github.com/edgexfoundry/go-mod-core-contracts/v4/clients/interfaces" "github.com/edgexfoundry/go-mod-core-contracts/v4/common" @@ -22,7 +23,7 @@ import ( ) type SubscriptionClient struct { - baseUrl string + baseUrlFunc clients.ClientBaseUrlFunc authInjector interfaces.AuthenticationInjector enableNameFieldEscape bool } @@ -30,7 +31,16 @@ type SubscriptionClient struct { // NewSubscriptionClient creates an instance of SubscriptionClient func NewSubscriptionClient(baseUrl string, authInjector interfaces.AuthenticationInjector, enableNameFieldEscape bool) interfaces.SubscriptionClient { return &SubscriptionClient{ - baseUrl: baseUrl, + baseUrlFunc: clients.GetDefaultClientBaseUrlFunc(baseUrl), + authInjector: authInjector, + enableNameFieldEscape: enableNameFieldEscape, + } +} + +// NewSubscriptionClientWithUrlCallback creates an instance of SubscriptionClient with ClientBaseUrlFunc. +func NewSubscriptionClientWithUrlCallback(baseUrlFunc clients.ClientBaseUrlFunc, authInjector interfaces.AuthenticationInjector, enableNameFieldEscape bool) interfaces.SubscriptionClient { + return &SubscriptionClient{ + baseUrlFunc: baseUrlFunc, authInjector: authInjector, enableNameFieldEscape: enableNameFieldEscape, } @@ -38,7 +48,11 @@ func NewSubscriptionClient(baseUrl string, authInjector interfaces.Authenticatio // Add adds new subscriptions. func (client *SubscriptionClient) Add(ctx context.Context, reqs []requests.AddSubscriptionRequest) (res []dtoCommon.BaseWithIdResponse, err errors.EdgeX) { - err = utils.PostRequestWithRawData(ctx, &res, client.baseUrl, common.ApiSubscriptionRoute, nil, reqs, client.authInjector) + baseUrl, goErr := clients.GetBaseUrl(client.baseUrlFunc) + if goErr != nil { + return res, errors.NewCommonEdgeXWrapper(goErr) + } + err = utils.PostRequestWithRawData(ctx, &res, baseUrl, common.ApiSubscriptionRoute, nil, reqs, client.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } @@ -47,7 +61,11 @@ func (client *SubscriptionClient) Add(ctx context.Context, reqs []requests.AddSu // Update updates subscriptions. func (client *SubscriptionClient) Update(ctx context.Context, reqs []requests.UpdateSubscriptionRequest) (res []dtoCommon.BaseResponse, err errors.EdgeX) { - err = utils.PatchRequest(ctx, &res, client.baseUrl, common.ApiSubscriptionRoute, nil, reqs, client.authInjector) + baseUrl, goErr := clients.GetBaseUrl(client.baseUrlFunc) + if goErr != nil { + return res, errors.NewCommonEdgeXWrapper(goErr) + } + err = utils.PatchRequest(ctx, &res, baseUrl, common.ApiSubscriptionRoute, nil, reqs, client.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } @@ -59,7 +77,11 @@ func (client *SubscriptionClient) AllSubscriptions(ctx context.Context, offset i requestParams := url.Values{} requestParams.Set(common.Offset, strconv.Itoa(offset)) requestParams.Set(common.Limit, strconv.Itoa(limit)) - err = utils.GetRequest(ctx, &res, client.baseUrl, common.ApiAllSubscriptionRoute, requestParams, client.authInjector) + baseUrl, goErr := clients.GetBaseUrl(client.baseUrlFunc) + if goErr != nil { + return res, errors.NewCommonEdgeXWrapper(goErr) + } + err = utils.GetRequest(ctx, &res, baseUrl, common.ApiAllSubscriptionRoute, requestParams, client.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } @@ -72,7 +94,11 @@ func (client *SubscriptionClient) SubscriptionsByCategory(ctx context.Context, c requestParams := url.Values{} requestParams.Set(common.Offset, strconv.Itoa(offset)) requestParams.Set(common.Limit, strconv.Itoa(limit)) - err = utils.GetRequest(ctx, &res, client.baseUrl, requestPath, requestParams, client.authInjector) + baseUrl, goErr := clients.GetBaseUrl(client.baseUrlFunc) + if goErr != nil { + return res, errors.NewCommonEdgeXWrapper(goErr) + } + err = utils.GetRequest(ctx, &res, baseUrl, requestPath, requestParams, client.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } @@ -85,7 +111,11 @@ func (client *SubscriptionClient) SubscriptionsByLabel(ctx context.Context, labe requestParams := url.Values{} requestParams.Set(common.Offset, strconv.Itoa(offset)) requestParams.Set(common.Limit, strconv.Itoa(limit)) - err = utils.GetRequest(ctx, &res, client.baseUrl, requestPath, requestParams, client.authInjector) + baseUrl, goErr := clients.GetBaseUrl(client.baseUrlFunc) + if goErr != nil { + return res, errors.NewCommonEdgeXWrapper(goErr) + } + err = utils.GetRequest(ctx, &res, baseUrl, requestPath, requestParams, client.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } @@ -98,7 +128,11 @@ func (client *SubscriptionClient) SubscriptionsByReceiver(ctx context.Context, r requestParams := url.Values{} requestParams.Set(common.Offset, strconv.Itoa(offset)) requestParams.Set(common.Limit, strconv.Itoa(limit)) - err = utils.GetRequest(ctx, &res, client.baseUrl, requestPath, requestParams, client.authInjector) + baseUrl, goErr := clients.GetBaseUrl(client.baseUrlFunc) + if goErr != nil { + return res, errors.NewCommonEdgeXWrapper(goErr) + } + err = utils.GetRequest(ctx, &res, baseUrl, requestPath, requestParams, client.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } @@ -107,9 +141,13 @@ func (client *SubscriptionClient) SubscriptionsByReceiver(ctx context.Context, r // SubscriptionByName query subscription by name. func (client *SubscriptionClient) SubscriptionByName(ctx context.Context, name string) (res responses.SubscriptionResponse, err errors.EdgeX) { - path := common.NewPathBuilder().EnableNameFieldEscape(client.enableNameFieldEscape). + requestPath := common.NewPathBuilder().EnableNameFieldEscape(client.enableNameFieldEscape). SetPath(common.ApiSubscriptionRoute).SetPath(common.Name).SetNameFieldPath(name).BuildPath() - err = utils.GetRequest(ctx, &res, client.baseUrl, path, nil, client.authInjector) + baseUrl, goErr := clients.GetBaseUrl(client.baseUrlFunc) + if goErr != nil { + return res, errors.NewCommonEdgeXWrapper(goErr) + } + err = utils.GetRequest(ctx, &res, baseUrl, requestPath, nil, client.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) } @@ -118,9 +156,13 @@ func (client *SubscriptionClient) SubscriptionByName(ctx context.Context, name s // DeleteSubscriptionByName deletes a subscription by name. func (client *SubscriptionClient) DeleteSubscriptionByName(ctx context.Context, name string) (res dtoCommon.BaseResponse, err errors.EdgeX) { - path := common.NewPathBuilder().EnableNameFieldEscape(client.enableNameFieldEscape). + requestPath := common.NewPathBuilder().EnableNameFieldEscape(client.enableNameFieldEscape). SetPath(common.ApiSubscriptionRoute).SetPath(common.Name).SetNameFieldPath(name).BuildPath() - err = utils.DeleteRequest(ctx, &res, client.baseUrl, path, client.authInjector) + baseUrl, goErr := clients.GetBaseUrl(client.baseUrlFunc) + if goErr != nil { + return res, errors.NewCommonEdgeXWrapper(goErr) + } + err = utils.DeleteRequest(ctx, &res, baseUrl, requestPath, client.authInjector) if err != nil { return res, errors.NewCommonEdgeXWrapper(err) }