Adding misssing go.mod file for provider
[nonrtric/plt/sme.git] / provider / handler / common_handler.go
1 // -
2 //
3 //      ========================LICENSE_START=================================
4 //      O-RAN-SC
5 //      %%
6 //      Copyright (C) 2023: Nordix Foundation
7 //      %%
8 //      Licensed under the Apache License, Version 2.0 (the "License");
9 //      you may not use this file except in compliance with the License.
10 //      You may obtain a copy of the License at
11 //
12 //           http://www.apache.org/licenses/LICENSE-2.0
13 //
14 //      Unless required by applicable law or agreed to in writing, software
15 //      distributed under the License is distributed on an "AS IS" BASIS,
16 //      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 //      See the License for the specific language governing permissions and
18 //      limitations under the License.
19 //      ========================LICENSE_END===================================
20 package handler
21
22 import (
23         "bytes"
24         "encoding/json"
25         "fmt"
26         "io"
27         "net/http"
28 )
29
30 func makeRequest(method, url string, headers map[string]string, data interface{}) ([]byte, error) {
31         client := &http.Client{}
32
33         // Create a new HTTP request with the specified method and URL
34         req, err := http.NewRequest(method, url, nil)
35         if err != nil {
36                 return nil, err
37         }
38
39         // Set any headers specified in the headers map
40         for k, v := range headers {
41                 req.Header.Set(k, v)
42         }
43
44         // If there is data to send, marshal it to JSON and set it as the request body
45         if data != nil {
46                 jsonBytes, err := json.Marshal(data)
47                 if err != nil {
48                         return nil, err
49                 }
50                 req.Body = io.NopCloser(bytes.NewReader(jsonBytes))
51         }
52
53         // Send the request and get the response
54         if resp, err := client.Do(req); err == nil {
55                 if isResponseSuccess(resp.StatusCode) {
56                         defer resp.Body.Close()
57
58                         // Read the response body
59                         respBody, err := io.ReadAll(resp.Body)
60                         if err != nil {
61                                 return nil, err
62                         }
63                         return respBody, nil
64                 } else {
65                         return nil, getRequestError(resp)
66                 }
67         } else {
68                 return nil, err
69         }
70 }
71
72 func isResponseSuccess(statusCode int) bool {
73         return statusCode >= http.StatusOK && statusCode <= 299
74 }
75
76 func getRequestError(response *http.Response) error {
77         defer response.Body.Close()
78         responseData, _ := io.ReadAll(response.Body)
79
80         return fmt.Errorf("message:  %v code: %v", string(responseData), response.StatusCode)
81 }