dccf3c5e870abf426d699152c30ebc453c7314f5
[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 //
34 //lint:ignore S100
35 func Send_http_request(json []byte, method string, url string, retry bool, useAuth bool) bool {
36
37         // set the HTTP method, url, and request body
38         var req *http.Request
39         var err error
40         if json == nil {
41                 req, err = http.NewRequest(method, url, http.NoBody)
42         } else {
43                 req, err = http.NewRequest(method, url, bytes.NewBuffer(json))
44                 req.Header.Set("Content-Type", "application/json; charset=utf-8")
45         }
46         if err != nil {
47                 log.Error("Cannot create http request, method: ", method, " url: ", url)
48                 return false
49         }
50
51         if useAuth {
52                 token, err := kafkacollector.Fetch_token()
53                 if err != nil {
54                         log.Error("Cannot fetch token for http request: ", err)
55                         return false
56                 }
57                 req.Header.Set("Authorization", "Bearer "+token.TokenValue)
58         }
59
60         log.Debug("HTTP request: ", req)
61
62         log.Debug("Sending http request")
63         resp, err2 := httpclient.Do(req)
64         if err2 != nil {
65                 log.Error("Http request error: ", err2)
66                 log.Error("Cannot send http request method: ", method, " url: ", url)
67         } else {
68                 if resp.StatusCode == 200 || resp.StatusCode == 201 || resp.StatusCode == 204 {
69                         log.Debug("Accepted http status: ", resp.StatusCode)
70                         resp.Body.Close()
71                         return true
72                 }
73                 log.Debug("HTTP resp: ", resp)
74                 resp.Body.Close()
75         }
76         return false
77 }