Skip to content

Commit

Permalink
Merge pull request #971 from jinlinGuan/bootstrap-issue-804
Browse files Browse the repository at this point in the history
feat: use baseUrlFunc for AuthClient
  • Loading branch information
cloudxxx8 authored Jan 15, 2025
2 parents 0d481f2 + 86fdbe0 commit c739c72
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions clients/http/auth.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright (C) 2024 IOTech Ltd
// Copyright (C) 2024-2025 IOTech Ltd
//
// SPDX-License-Identifier: Apache-2.0

Expand All @@ -8,6 +8,7 @@ package http
import (
"context"

"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 @@ -18,22 +19,34 @@ import (
)

type AuthClient struct {
baseUrl string
baseUrlFunc clients.ClientBaseUrlFunc
authInjector interfaces.AuthenticationInjector
}

// NewAuthClient creates an instance of AuthClient
func NewAuthClient(baseUrl string, authInjector interfaces.AuthenticationInjector) interfaces.AuthClient {
return &AuthClient{
baseUrl: baseUrl,
baseUrlFunc: clients.GetDefaultClientBaseUrlFunc(baseUrl),
authInjector: authInjector,
}
}

// NewAuthClientWithUrlCallback creates an instance of AuthClient with ClientBaseUrlFunc.
func NewAuthClientWithUrlCallback(baseUrlFunc clients.ClientBaseUrlFunc, authInjector interfaces.AuthenticationInjector) interfaces.AuthClient {
return &AuthClient{
baseUrlFunc: baseUrlFunc,
authInjector: authInjector,
}
}

// AddKey adds new key
func (ac *AuthClient) AddKey(ctx context.Context, req requests.AddKeyDataRequest) (dtoCommon.BaseResponse, errors.EdgeX) {
var response dtoCommon.BaseResponse
err := utils.PostRequestWithRawData(ctx, &response, ac.baseUrl, common.ApiKeyRoute, nil, req, ac.authInjector)
baseUrl, err := clients.GetBaseUrl(ac.baseUrlFunc)
if err != nil {
return response, errors.NewCommonEdgeXWrapper(err)
}
err = utils.PostRequestWithRawData(ctx, &response, baseUrl, common.ApiKeyRoute, nil, req, ac.authInjector)
if err != nil {
return response, errors.NewCommonEdgeXWrapper(err)
}
Expand All @@ -42,7 +55,11 @@ func (ac *AuthClient) AddKey(ctx context.Context, req requests.AddKeyDataRequest

func (ac *AuthClient) VerificationKeyByIssuer(ctx context.Context, issuer string) (res responses.KeyDataResponse, err errors.EdgeX) {
path := common.NewPathBuilder().SetPath(common.ApiKeyRoute).SetPath(common.VerificationKeyType).SetPath(common.Issuer).SetNameFieldPath(issuer).BuildPath()
err = utils.GetRequest(ctx, &res, ac.baseUrl, path, nil, ac.authInjector)
baseUrl, goErr := clients.GetBaseUrl(ac.baseUrlFunc)
if goErr != nil {
return res, errors.NewCommonEdgeXWrapper(goErr)
}
err = utils.GetRequest(ctx, &res, baseUrl, path, nil, ac.authInjector)
if err != nil {
return res, errors.NewCommonEdgeXWrapper(err)
}
Expand Down

0 comments on commit c739c72

Please sign in to comment.