dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts@v1.0.2/clients/http/utils/request.go (about) 1 // 2 // Copyright (C) 2020-2021 IOTech Ltd 3 // Copyright (C) 2023 Intel Corporation 4 // 5 // SPDX-License-Identifier: Apache-2.0 6 7 package utils 8 9 import ( 10 "context" 11 "encoding/json" 12 "fmt" 13 "net/http" 14 "net/url" 15 16 "dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/clients/interfaces" 17 "dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/common" 18 "dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/errors" 19 ) 20 21 // GetRequest makes the get request and return the body 22 func GetRequest(ctx context.Context, returnValuePointer interface{}, baseUrl string, requestPath string, requestParams url.Values, authInjector interfaces.AuthenticationInjector) errors.EdgeX { 23 req, err := createRequest(ctx, http.MethodGet, baseUrl, requestPath, requestParams) 24 if err != nil { 25 return errors.NewCommonEdgeXWrapper(err) 26 } 27 28 res, err := sendRequest(ctx, req, authInjector) 29 if err != nil { 30 return errors.NewCommonEdgeXWrapper(err) 31 } 32 // Check the response content length to avoid json unmarshal error 33 if len(res) == 0 { 34 return nil 35 } 36 if err := json.Unmarshal(res, returnValuePointer); err != nil { 37 return errors.NewCommonEdgeX(errors.KindContractInvalid, "failed to parse the response body", err) 38 } 39 return nil 40 } 41 42 // GetRequestAndReturnBinaryRes makes the get request and return the binary response and content type(i.e., application/json, application/cbor, ... ) 43 func GetRequestAndReturnBinaryRes(ctx context.Context, baseUrl string, requestPath string, requestParams url.Values, authInjector interfaces.AuthenticationInjector) (res []byte, contentType string, edgeXerr errors.EdgeX) { 44 req, edgeXerr := createRequest(ctx, http.MethodGet, baseUrl, requestPath, requestParams) 45 if edgeXerr != nil { 46 return nil, "", errors.NewCommonEdgeXWrapper(edgeXerr) 47 } 48 49 resp, edgeXerr := makeRequest(req, authInjector) 50 if edgeXerr != nil { 51 return nil, "", errors.NewCommonEdgeXWrapper(edgeXerr) 52 } 53 defer resp.Body.Close() 54 55 res, edgeXerr = getBody(resp) 56 if edgeXerr != nil { 57 return nil, "", errors.NewCommonEdgeXWrapper(edgeXerr) 58 } 59 60 if resp.StatusCode <= http.StatusMultiStatus { 61 return res, resp.Header.Get(common.ContentType), nil 62 } 63 64 // Handle error response 65 return nil, 66 "", 67 errors.NewCommonEdgeX( 68 errors.KindMapping(resp.StatusCode), 69 fmt.Sprintf("request failed, status code: %d, err: %s", resp.StatusCode, string(res)), nil) 70 } 71 72 // GetRequestWithBodyRawData makes the GET request with JSON raw data as request body and return the response 73 func GetRequestWithBodyRawData(ctx context.Context, returnValuePointer interface{}, baseUrl string, requestPath string, requestParams url.Values, data interface{}, authInjector interfaces.AuthenticationInjector) errors.EdgeX { 74 req, err := createRequestWithRawDataAndParams(ctx, http.MethodGet, baseUrl, requestPath, requestParams, data) 75 if err != nil { 76 return errors.NewCommonEdgeXWrapper(err) 77 } 78 79 res, err := sendRequest(ctx, req, authInjector) 80 if err != nil { 81 return errors.NewCommonEdgeXWrapper(err) 82 } 83 if err := json.Unmarshal(res, returnValuePointer); err != nil { 84 return errors.NewCommonEdgeX(errors.KindContractInvalid, "failed to parse the response body", err) 85 } 86 return nil 87 } 88 89 // PostRequest makes the post request with encoded data and return the body 90 func PostRequest( 91 ctx context.Context, 92 returnValuePointer interface{}, 93 baseUrl string, requestPath string, 94 data []byte, 95 encoding string, authInjector interfaces.AuthenticationInjector) errors.EdgeX { 96 97 req, err := createRequestWithEncodedData(ctx, http.MethodPost, baseUrl, requestPath, data, encoding) 98 if err != nil { 99 return errors.NewCommonEdgeXWrapper(err) 100 } 101 102 res, err := sendRequest(ctx, req, authInjector) 103 if err != nil { 104 return errors.NewCommonEdgeXWrapper(err) 105 } 106 if err := json.Unmarshal(res, returnValuePointer); err != nil { 107 return errors.NewCommonEdgeX(errors.KindContractInvalid, "failed to parse the response body", err) 108 } 109 return nil 110 } 111 112 // PostRequestWithRawData makes the post JSON request with raw data and return the body 113 func PostRequestWithRawData( 114 ctx context.Context, 115 returnValuePointer interface{}, 116 baseUrl string, requestPath string, 117 requestParams url.Values, 118 data interface{}, authInjector interfaces.AuthenticationInjector) errors.EdgeX { 119 120 req, err := createRequestWithRawData(ctx, http.MethodPost, baseUrl, requestPath, requestParams, data) 121 if err != nil { 122 return errors.NewCommonEdgeXWrapper(err) 123 } 124 125 res, err := sendRequest(ctx, req, authInjector) 126 if err != nil { 127 return errors.NewCommonEdgeXWrapper(err) 128 } 129 if err := json.Unmarshal(res, returnValuePointer); err != nil { 130 return errors.NewCommonEdgeX(errors.KindContractInvalid, "failed to parse the response body", err) 131 } 132 return nil 133 } 134 135 // PutRequest makes the put JSON request and return the body 136 func PutRequest( 137 ctx context.Context, 138 returnValuePointer interface{}, 139 baseUrl string, requestPath string, 140 requestParams url.Values, 141 data interface{}, authInjector interfaces.AuthenticationInjector) errors.EdgeX { 142 143 req, err := createRequestWithRawData(ctx, http.MethodPut, baseUrl, requestPath, requestParams, data) 144 if err != nil { 145 return errors.NewCommonEdgeXWrapper(err) 146 } 147 148 res, err := sendRequest(ctx, req, authInjector) 149 if err != nil { 150 return errors.NewCommonEdgeXWrapper(err) 151 } 152 if err := json.Unmarshal(res, returnValuePointer); err != nil { 153 return errors.NewCommonEdgeX(errors.KindContractInvalid, "failed to parse the response body", err) 154 } 155 return nil 156 } 157 158 // PatchRequest makes a PATCH request and unmarshals the response to the returnValuePointer 159 func PatchRequest( 160 ctx context.Context, 161 returnValuePointer interface{}, 162 baseUrl string, requestPath string, 163 requestParams url.Values, 164 data interface{}, authInjector interfaces.AuthenticationInjector) errors.EdgeX { 165 166 req, err := createRequestWithRawData(ctx, http.MethodPatch, baseUrl, requestPath, requestParams, data) 167 if err != nil { 168 return errors.NewCommonEdgeXWrapper(err) 169 } 170 171 res, err := sendRequest(ctx, req, authInjector) 172 if err != nil { 173 return errors.NewCommonEdgeXWrapper(err) 174 } 175 if err := json.Unmarshal(res, returnValuePointer); err != nil { 176 return errors.NewCommonEdgeX(errors.KindContractInvalid, "failed to parse the response body", err) 177 } 178 return nil 179 } 180 181 // PostByFileRequest makes the post file request and return the body 182 func PostByFileRequest( 183 ctx context.Context, 184 returnValuePointer interface{}, 185 baseUrl string, requestPath string, 186 filePath string, authInjector interfaces.AuthenticationInjector) errors.EdgeX { 187 188 req, err := createRequestFromFilePath(ctx, http.MethodPost, baseUrl, requestPath, filePath) 189 if err != nil { 190 return errors.NewCommonEdgeXWrapper(err) 191 } 192 193 res, err := sendRequest(ctx, req, authInjector) 194 if err != nil { 195 return errors.NewCommonEdgeXWrapper(err) 196 } 197 if err := json.Unmarshal(res, returnValuePointer); err != nil { 198 return errors.NewCommonEdgeX(errors.KindContractInvalid, "failed to parse the response body", err) 199 } 200 return nil 201 } 202 203 // PutByFileRequest makes the put file request and return the body 204 func PutByFileRequest( 205 ctx context.Context, 206 returnValuePointer interface{}, 207 baseUrl string, requestPath string, 208 filePath string, authInjector interfaces.AuthenticationInjector) errors.EdgeX { 209 210 req, err := createRequestFromFilePath(ctx, http.MethodPut, baseUrl, requestPath, filePath) 211 if err != nil { 212 return errors.NewCommonEdgeXWrapper(err) 213 } 214 215 res, err := sendRequest(ctx, req, authInjector) 216 if err != nil { 217 return errors.NewCommonEdgeXWrapper(err) 218 } 219 if err := json.Unmarshal(res, returnValuePointer); err != nil { 220 return errors.NewCommonEdgeX(errors.KindContractInvalid, "failed to parse the response body", err) 221 } 222 return nil 223 } 224 225 // DeleteRequest makes the delete request and return the body 226 func DeleteRequest(ctx context.Context, returnValuePointer interface{}, baseUrl string, requestPath string, authInjector interfaces.AuthenticationInjector) errors.EdgeX { 227 req, err := createRequest(ctx, http.MethodDelete, baseUrl, requestPath, nil) 228 if err != nil { 229 return errors.NewCommonEdgeXWrapper(err) 230 } 231 232 res, err := sendRequest(ctx, req, authInjector) 233 if err != nil { 234 return errors.NewCommonEdgeXWrapper(err) 235 } 236 if err := json.Unmarshal(res, returnValuePointer); err != nil { 237 return errors.NewCommonEdgeX(errors.KindContractInvalid, "failed to parse the response body", err) 238 } 239 return nil 240 }