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