Skip to content

Commit

Permalink
Merge pull request #970 from jinlinGuan/bootstrap-issue-804
Browse files Browse the repository at this point in the history
feat: use baseUrlFunc instead baseUrl for Clients
  • Loading branch information
cloudxxx8 authored Jan 15, 2025
2 parents 8f6de19 + 3ce3929 commit 0d481f2
Show file tree
Hide file tree
Showing 12 changed files with 704 additions and 172 deletions.
22 changes: 22 additions & 0 deletions clients/common.go
Original file line number Diff line number Diff line change
@@ -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()
}
52 changes: 43 additions & 9 deletions clients/http/command.go
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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"
Expand All @@ -20,15 +21,24 @@ import (
)

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

// 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,
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down
82 changes: 68 additions & 14 deletions clients/http/device.go
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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"
Expand All @@ -23,22 +24,35 @@ import (
)

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

// 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)
}
Expand All @@ -50,15 +64,23 @@ 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)
}
return res, nil
}

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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down
Loading

0 comments on commit 0d481f2

Please sign in to comment.