dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts@v1.0.2/clients/http/deviceprofile.go (about)

     1  //
     2  // Copyright (C) 2020-2023 IOTech Ltd
     3  // Copyright (C) 2023 Intel Corporation
     4  //
     5  // SPDX-License-Identifier: Apache-2.0
     6  
     7  package http
     8  
     9  import (
    10  	"context"
    11  	"fmt"
    12  	"net/url"
    13  	"path"
    14  	"strconv"
    15  	"strings"
    16  	"sync"
    17  
    18  	"dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/clients/http/utils"
    19  	"dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/clients/interfaces"
    20  	"dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/common"
    21  	dtoCommon "dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/dtos/common"
    22  	"dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/dtos/requests"
    23  	"dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/dtos/responses"
    24  	"dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/errors"
    25  )
    26  
    27  type DeviceProfileClient struct {
    28  	baseUrl               string
    29  	authInjector          interfaces.AuthenticationInjector
    30  	resourcesCache        map[string]responses.DeviceResourceResponse
    31  	mux                   sync.RWMutex
    32  	enableNameFieldEscape bool
    33  }
    34  
    35  // NewDeviceProfileClient creates an instance of DeviceProfileClient
    36  func NewDeviceProfileClient(baseUrl string, authInjector interfaces.AuthenticationInjector, enableNameFieldEscape bool) interfaces.DeviceProfileClient {
    37  	return &DeviceProfileClient{
    38  		baseUrl:               baseUrl,
    39  		authInjector:          authInjector,
    40  		resourcesCache:        make(map[string]responses.DeviceResourceResponse),
    41  		enableNameFieldEscape: enableNameFieldEscape,
    42  	}
    43  }
    44  
    45  // Add adds new device profile
    46  func (client *DeviceProfileClient) Add(ctx context.Context, reqs []requests.DeviceProfileRequest) ([]dtoCommon.BaseWithIdResponse, errors.EdgeX) {
    47  	var responses []dtoCommon.BaseWithIdResponse
    48  	err := utils.PostRequestWithRawData(ctx, &responses, client.baseUrl, common.ApiDeviceProfileRoute, nil, reqs, client.authInjector)
    49  	if err != nil {
    50  		return responses, errors.NewCommonEdgeXWrapper(err)
    51  	}
    52  	return responses, nil
    53  }
    54  
    55  // Update updates device profile
    56  func (client *DeviceProfileClient) Update(ctx context.Context, reqs []requests.DeviceProfileRequest) ([]dtoCommon.BaseResponse, errors.EdgeX) {
    57  	var responses []dtoCommon.BaseResponse
    58  	err := utils.PutRequest(ctx, &responses, client.baseUrl, common.ApiDeviceProfileRoute, nil, reqs, client.authInjector)
    59  	if err != nil {
    60  		return responses, errors.NewCommonEdgeXWrapper(err)
    61  	}
    62  	return responses, nil
    63  }
    64  
    65  // AddByYaml adds new device profile by uploading a yaml file
    66  func (client *DeviceProfileClient) AddByYaml(ctx context.Context, yamlFilePath string) (dtoCommon.BaseWithIdResponse, errors.EdgeX) {
    67  	var responses dtoCommon.BaseWithIdResponse
    68  	err := utils.PostByFileRequest(ctx, &responses, client.baseUrl, common.ApiDeviceProfileUploadFileRoute, yamlFilePath, client.authInjector)
    69  	if err != nil {
    70  		return responses, errors.NewCommonEdgeXWrapper(err)
    71  	}
    72  	return responses, nil
    73  }
    74  
    75  // UpdateByYaml updates device profile by uploading a yaml file
    76  func (client *DeviceProfileClient) UpdateByYaml(ctx context.Context, yamlFilePath string) (dtoCommon.BaseResponse, errors.EdgeX) {
    77  	var responses dtoCommon.BaseResponse
    78  	err := utils.PutByFileRequest(ctx, &responses, client.baseUrl, common.ApiDeviceProfileUploadFileRoute, yamlFilePath, client.authInjector)
    79  	if err != nil {
    80  		return responses, errors.NewCommonEdgeXWrapper(err)
    81  	}
    82  	return responses, nil
    83  }
    84  
    85  // DeleteByName deletes the device profile by name
    86  func (client *DeviceProfileClient) DeleteByName(ctx context.Context, name string) (dtoCommon.BaseResponse, errors.EdgeX) {
    87  	var response dtoCommon.BaseResponse
    88  	requestPath := common.NewPathBuilder().EnableNameFieldEscape(client.enableNameFieldEscape).
    89  		SetPath(common.ApiDeviceProfileRoute).SetPath(common.Name).SetNameFieldPath(name).BuildPath()
    90  	err := utils.DeleteRequest(ctx, &response, client.baseUrl, requestPath, client.authInjector)
    91  	if err != nil {
    92  		return response, errors.NewCommonEdgeXWrapper(err)
    93  	}
    94  	return response, nil
    95  }
    96  
    97  // DeviceProfileByName queries the device profile by name
    98  func (client *DeviceProfileClient) DeviceProfileByName(ctx context.Context, name string) (res responses.DeviceProfileResponse, edgexError errors.EdgeX) {
    99  	requestPath := common.NewPathBuilder().EnableNameFieldEscape(client.enableNameFieldEscape).
   100  		SetPath(common.ApiDeviceProfileRoute).SetPath(common.Name).SetNameFieldPath(name).BuildPath()
   101  	err := utils.GetRequest(ctx, &res, client.baseUrl, requestPath, nil, client.authInjector)
   102  	if err != nil {
   103  		return res, errors.NewCommonEdgeXWrapper(err)
   104  	}
   105  	return res, nil
   106  }
   107  
   108  // AllDeviceProfiles queries the device profiles with offset, and limit
   109  func (client *DeviceProfileClient) AllDeviceProfiles(ctx context.Context, labels []string, offset int, limit int) (res responses.MultiDeviceProfilesResponse, edgexError errors.EdgeX) {
   110  	requestParams := url.Values{}
   111  	if len(labels) > 0 {
   112  		requestParams.Set(common.Labels, strings.Join(labels, common.CommaSeparator))
   113  	}
   114  	requestParams.Set(common.Offset, strconv.Itoa(offset))
   115  	requestParams.Set(common.Limit, strconv.Itoa(limit))
   116  	err := utils.GetRequest(ctx, &res, client.baseUrl, common.ApiAllDeviceProfileRoute, requestParams, client.authInjector)
   117  	if err != nil {
   118  		return res, errors.NewCommonEdgeXWrapper(err)
   119  	}
   120  	return res, nil
   121  }
   122  
   123  // DeviceProfilesByModel queries the device profiles with offset, limit and model
   124  func (client *DeviceProfileClient) DeviceProfilesByModel(ctx context.Context, model string, offset int, limit int) (res responses.MultiDeviceProfilesResponse, edgexError errors.EdgeX) {
   125  	requestPath := path.Join(common.ApiDeviceProfileRoute, common.Model, model)
   126  	requestParams := url.Values{}
   127  	requestParams.Set(common.Offset, strconv.Itoa(offset))
   128  	requestParams.Set(common.Limit, strconv.Itoa(limit))
   129  	err := utils.GetRequest(ctx, &res, client.baseUrl, requestPath, requestParams, client.authInjector)
   130  	if err != nil {
   131  		return res, errors.NewCommonEdgeXWrapper(err)
   132  	}
   133  	return res, nil
   134  }
   135  
   136  // DeviceProfilesByManufacturer queries the device profiles with offset, limit and manufacturer
   137  func (client *DeviceProfileClient) DeviceProfilesByManufacturer(ctx context.Context, manufacturer string, offset int, limit int) (res responses.MultiDeviceProfilesResponse, edgexError errors.EdgeX) {
   138  	requestPath := path.Join(common.ApiDeviceProfileRoute, common.Manufacturer, manufacturer)
   139  	requestParams := url.Values{}
   140  	requestParams.Set(common.Offset, strconv.Itoa(offset))
   141  	requestParams.Set(common.Limit, strconv.Itoa(limit))
   142  	err := utils.GetRequest(ctx, &res, client.baseUrl, requestPath, requestParams, client.authInjector)
   143  	if err != nil {
   144  		return res, errors.NewCommonEdgeXWrapper(err)
   145  	}
   146  	return res, nil
   147  }
   148  
   149  // DeviceProfilesByManufacturerAndModel queries the device profiles with offset, limit, manufacturer and model
   150  func (client *DeviceProfileClient) DeviceProfilesByManufacturerAndModel(ctx context.Context, manufacturer string, model string, offset int, limit int) (res responses.MultiDeviceProfilesResponse, edgexError errors.EdgeX) {
   151  	requestPath := path.Join(common.ApiDeviceProfileRoute, common.Manufacturer, manufacturer, common.Model, model)
   152  	requestParams := url.Values{}
   153  	requestParams.Set(common.Offset, strconv.Itoa(offset))
   154  	requestParams.Set(common.Limit, strconv.Itoa(limit))
   155  	err := utils.GetRequest(ctx, &res, client.baseUrl, requestPath, requestParams, client.authInjector)
   156  	if err != nil {
   157  		return res, errors.NewCommonEdgeXWrapper(err)
   158  	}
   159  	return res, nil
   160  }
   161  
   162  // DeviceResourceByProfileNameAndResourceName queries the device resource by profileName and resourceName
   163  func (client *DeviceProfileClient) DeviceResourceByProfileNameAndResourceName(ctx context.Context, profileName string, resourceName string) (res responses.DeviceResourceResponse, edgexError errors.EdgeX) {
   164  	resourceMapKey := fmt.Sprintf("%s:%s", profileName, resourceName)
   165  	res, exists := client.resourceByMapKey(resourceMapKey)
   166  	if exists {
   167  		return res, nil
   168  	}
   169  	requestPath := common.NewPathBuilder().EnableNameFieldEscape(client.enableNameFieldEscape).
   170  		SetPath(common.ApiDeviceResourceRoute).SetPath(common.Profile).SetNameFieldPath(profileName).SetPath(common.Resource).SetNameFieldPath(resourceName).BuildPath()
   171  	err := utils.GetRequest(ctx, &res, client.baseUrl, requestPath, nil, client.authInjector)
   172  	if err != nil {
   173  		return res, errors.NewCommonEdgeXWrapper(err)
   174  	}
   175  	client.setResourceWithMapKey(res, resourceMapKey)
   176  	return res, nil
   177  }
   178  
   179  func (client *DeviceProfileClient) resourceByMapKey(key string) (res responses.DeviceResourceResponse, exists bool) {
   180  	client.mux.RLock()
   181  	defer client.mux.RUnlock()
   182  	res, exists = client.resourcesCache[key]
   183  	return
   184  }
   185  
   186  func (client *DeviceProfileClient) setResourceWithMapKey(res responses.DeviceResourceResponse, key string) {
   187  	client.mux.Lock()
   188  	defer client.mux.Unlock()
   189  	client.resourcesCache[key] = res
   190  }
   191  
   192  func (client *DeviceProfileClient) CleanResourcesCache() {
   193  	client.mux.Lock()
   194  	defer client.mux.Unlock()
   195  	client.resourcesCache = make(map[string]responses.DeviceResourceResponse)
   196  }
   197  
   198  // UpdateDeviceProfileBasicInfo updates existing profile's basic info
   199  func (client *DeviceProfileClient) UpdateDeviceProfileBasicInfo(ctx context.Context, reqs []requests.DeviceProfileBasicInfoRequest) ([]dtoCommon.BaseResponse, errors.EdgeX) {
   200  	var responses []dtoCommon.BaseResponse
   201  	err := utils.PatchRequest(ctx, &responses, client.baseUrl, common.ApiDeviceProfileBasicInfoRoute, nil, reqs, client.authInjector)
   202  	if err != nil {
   203  		return responses, errors.NewCommonEdgeXWrapper(err)
   204  	}
   205  	return responses, nil
   206  }
   207  
   208  // AddDeviceProfileResource adds new device resource to an existing profile
   209  func (client *DeviceProfileClient) AddDeviceProfileResource(ctx context.Context, reqs []requests.AddDeviceResourceRequest) ([]dtoCommon.BaseResponse, errors.EdgeX) {
   210  	var responses []dtoCommon.BaseResponse
   211  	err := utils.PostRequestWithRawData(ctx, &responses, client.baseUrl, common.ApiDeviceProfileResourceRoute, nil, reqs, client.authInjector)
   212  	if err != nil {
   213  		return responses, errors.NewCommonEdgeXWrapper(err)
   214  	}
   215  	return responses, nil
   216  }
   217  
   218  // UpdateDeviceProfileResource updates existing device resource
   219  func (client *DeviceProfileClient) UpdateDeviceProfileResource(ctx context.Context, reqs []requests.UpdateDeviceResourceRequest) ([]dtoCommon.BaseResponse, errors.EdgeX) {
   220  	var responses []dtoCommon.BaseResponse
   221  	err := utils.PatchRequest(ctx, &responses, client.baseUrl, common.ApiDeviceProfileResourceRoute, nil, reqs, client.authInjector)
   222  	if err != nil {
   223  		return responses, errors.NewCommonEdgeXWrapper(err)
   224  	}
   225  	return responses, nil
   226  }
   227  
   228  // DeleteDeviceResourceByName deletes device resource by name
   229  func (client *DeviceProfileClient) DeleteDeviceResourceByName(ctx context.Context, profileName string, resourceName string) (dtoCommon.BaseResponse, errors.EdgeX) {
   230  	var response dtoCommon.BaseResponse
   231  	requestPath := common.NewPathBuilder().EnableNameFieldEscape(client.enableNameFieldEscape).
   232  		SetPath(common.ApiDeviceProfileRoute).SetPath(common.Name).SetNameFieldPath(profileName).SetPath(common.Resource).SetNameFieldPath(resourceName).BuildPath()
   233  	err := utils.DeleteRequest(ctx, &response, client.baseUrl, requestPath, client.authInjector)
   234  	if err != nil {
   235  		return response, errors.NewCommonEdgeXWrapper(err)
   236  	}
   237  	return response, nil
   238  }
   239  
   240  // AddDeviceProfileDeviceCommand adds new device command to an existing profile
   241  func (client *DeviceProfileClient) AddDeviceProfileDeviceCommand(ctx context.Context, reqs []requests.AddDeviceCommandRequest) ([]dtoCommon.BaseResponse, errors.EdgeX) {
   242  	var responses []dtoCommon.BaseResponse
   243  	err := utils.PostRequestWithRawData(ctx, &responses, client.baseUrl, common.ApiDeviceProfileDeviceCommandRoute, nil, reqs, client.authInjector)
   244  	if err != nil {
   245  		return responses, errors.NewCommonEdgeXWrapper(err)
   246  	}
   247  	return responses, nil
   248  }
   249  
   250  // UpdateDeviceProfileDeviceCommand updates existing device command
   251  func (client *DeviceProfileClient) UpdateDeviceProfileDeviceCommand(ctx context.Context, reqs []requests.UpdateDeviceCommandRequest) ([]dtoCommon.BaseResponse, errors.EdgeX) {
   252  	var responses []dtoCommon.BaseResponse
   253  	err := utils.PatchRequest(ctx, &responses, client.baseUrl, common.ApiDeviceProfileDeviceCommandRoute, nil, reqs, client.authInjector)
   254  	if err != nil {
   255  		return responses, errors.NewCommonEdgeXWrapper(err)
   256  	}
   257  	return responses, nil
   258  }
   259  
   260  // DeleteDeviceCommandByName deletes device command by name
   261  func (client *DeviceProfileClient) DeleteDeviceCommandByName(ctx context.Context, profileName string, commandName string) (dtoCommon.BaseResponse, errors.EdgeX) {
   262  	var response dtoCommon.BaseResponse
   263  	requestPath := common.NewPathBuilder().EnableNameFieldEscape(client.enableNameFieldEscape).
   264  		SetPath(common.ApiDeviceProfileRoute).SetPath(common.Name).SetNameFieldPath(profileName).SetPath(common.DeviceCommand).SetNameFieldPath(commandName).BuildPath()
   265  	err := utils.DeleteRequest(ctx, &response, client.baseUrl, requestPath, client.authInjector)
   266  	if err != nil {
   267  		return response, errors.NewCommonEdgeXWrapper(err)
   268  	}
   269  	return response, nil
   270  }
   271  
   272  func (client *DeviceProfileClient) GetDeviceProfilesModelList(ctx context.Context) (res responses.DeviceProfileListResponse, edgexError errors.EdgeX) {
   273  	requestParams := url.Values{}
   274  
   275  	err := utils.GetRequest(ctx, &res, client.baseUrl, common.ApiDeviceProfileModelListRoute, requestParams, client.authInjector)
   276  	if err != nil {
   277  		return res, errors.NewCommonEdgeXWrapper(err)
   278  	}
   279  	return res, nil
   280  }
   281  
   282  func (client *DeviceProfileClient) GetDeviceProfilesManufacturerList(ctx context.Context) (res responses.DeviceProfileListResponse, edgexError errors.EdgeX) {
   283  	requestParams := url.Values{}
   284  
   285  	err := utils.GetRequest(ctx, &res, client.baseUrl, common.ApiDeviceProfileManufacturerListRoute, requestParams, client.authInjector)
   286  	if err != nil {
   287  		return res, errors.NewCommonEdgeXWrapper(err)
   288  	}
   289  	return res, nil
   290  }
   291  
   292  func (client *DeviceProfileClient) GetDeviceProfilesNameList(ctx context.Context) (res responses.DeviceProfileListResponse, edgexError errors.EdgeX) {
   293  	requestParams := url.Values{}
   294  
   295  	err := utils.GetRequest(ctx, &res, client.baseUrl, common.ApiDeviceProfileNameListRoute, requestParams, client.authInjector)
   296  	if err != nil {
   297  		return res, errors.NewCommonEdgeXWrapper(err)
   298  	}
   299  	return res, nil
   300  }