|
| 1 | +// |
| 2 | +// Copyright (C) 2024 IOTech Ltd |
| 3 | +// |
| 4 | +// SPDX-License-Identifier: Apache-2.0 |
| 5 | + |
| 6 | +package http |
| 7 | + |
| 8 | +import ( |
| 9 | + "context" |
| 10 | + |
| 11 | + "github.com/edgexfoundry/go-mod-core-contracts/v4/clients/http/utils" |
| 12 | + "github.com/edgexfoundry/go-mod-core-contracts/v4/clients/interfaces" |
| 13 | + "github.com/edgexfoundry/go-mod-core-contracts/v4/common" |
| 14 | + dtoCommon "github.com/edgexfoundry/go-mod-core-contracts/v4/dtos/common" |
| 15 | + "github.com/edgexfoundry/go-mod-core-contracts/v4/dtos/requests" |
| 16 | + "github.com/edgexfoundry/go-mod-core-contracts/v4/dtos/responses" |
| 17 | + "github.com/edgexfoundry/go-mod-core-contracts/v4/errors" |
| 18 | +) |
| 19 | + |
| 20 | +type AuthClient struct { |
| 21 | + baseUrl string |
| 22 | + authInjector interfaces.AuthenticationInjector |
| 23 | +} |
| 24 | + |
| 25 | +// NewAuthClient creates an instance of AuthClient |
| 26 | +func NewAuthClient(baseUrl string, authInjector interfaces.AuthenticationInjector) interfaces.AuthClient { |
| 27 | + return &AuthClient{ |
| 28 | + baseUrl: baseUrl, |
| 29 | + authInjector: authInjector, |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +// AddKey adds new key |
| 34 | +func (ac *AuthClient) AddKey(ctx context.Context, req requests.AddKeyDataRequest) (dtoCommon.BaseResponse, errors.EdgeX) { |
| 35 | + var response dtoCommon.BaseResponse |
| 36 | + err := utils.PostRequestWithRawData(ctx, &response, ac.baseUrl, common.ApiKeyRoute, nil, req, ac.authInjector) |
| 37 | + if err != nil { |
| 38 | + return response, errors.NewCommonEdgeXWrapper(err) |
| 39 | + } |
| 40 | + return response, nil |
| 41 | +} |
| 42 | + |
| 43 | +func (ac *AuthClient) VerificationKeyByIssuer(ctx context.Context, issuer string) (res responses.KeyDataResponse, err errors.EdgeX) { |
| 44 | + path := common.NewPathBuilder().SetPath(common.ApiKeyRoute).SetPath(common.VerificationKeyType).SetPath(common.Issuer).SetNameFieldPath(issuer).BuildPath() |
| 45 | + err = utils.GetRequest(ctx, &res, ac.baseUrl, path, nil, ac.authInjector) |
| 46 | + if err != nil { |
| 47 | + return res, errors.NewCommonEdgeXWrapper(err) |
| 48 | + } |
| 49 | + return res, nil |
| 50 | +} |
0 commit comments