Fixing Sonar Code Smell for PMConverter
[nonrtric/plt/ranpm.git] / pm-file-converter / common / utils / utils.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 utils
21
22 import (
23         "bytes"
24         "main/components/kafkacollector"
25         "net/http"
26
27         log "github.com/sirupsen/logrus"
28 )
29
30 var httpclient = &http.Client{}
31
32 // Send a http request with json (json may be nil)
33 func SendHttpRequest(json []byte, method string, url string, retry bool, useAuth bool) bool {
34
35         // set the HTTP method, url, and request body
36         var req *http.Request
37         var err error
38         if json == nil {
39                 req, err = http.NewRequest(method, url, http.NoBody)
40         } else {
41                 req, err = http.NewRequest(method, url, bytes.NewBuffer(json))
42                 req.Header.Set("Content-Type", "application/json; charset=utf-8")
43         }
44         if err != nil {
45                 log.Error("Cannot create http request, method: ", method, " url: ", url)
46                 return false
47         }
48
49         if useAuth {
50                 token, err := kafkacollector.FetchToken()
51                 if err != nil {
52                         log.Error("Cannot fetch token for http request: ", err)
53                         return false
54                 }
55                 req.Header.Set("Authorization", "Bearer "+token.TokenValue)
56         }
57
58         log.Debug("HTTP request: ", req)
59
60         log.Debug("Sending http request")
61         resp, err2 := httpclient.Do(req)
62         if err2 != nil {
63                 log.Error("Http request error: ", err2)
64                 log.Error("Cannot send http request method: ", method, " url: ", url)
65         } else {
66                 if resp.StatusCode == 200 || resp.StatusCode == 201 || resp.StatusCode == 204 {
67                         log.Debug("Accepted http status: ", resp.StatusCode)
68                         resp.Body.Close()
69                         return true
70                 }
71                 log.Debug("HTTP resp: ", resp)
72                 resp.Body.Close()
73         }
74         return false
75 }