a7582c2ba71150e5a9608792a2ef88892c1f480d
[nonrtric.git] / dmaap-mediator-producer / internal / restclient / HTTPClient.go
1 // -
2 //   ========================LICENSE_START=================================
3 //   O-RAN-SC
4 //   %%
5 //   Copyright (C) 2021: Nordix Foundation
6 //   %%
7 //   Licensed under the Apache License, Version 2.0 (the "License");
8 //   you may not use this file except in compliance with the License.
9 //   You may obtain a copy of the License at
10 //
11 //        http://www.apache.org/licenses/LICENSE-2.0
12 //
13 //   Unless required by applicable law or agreed to in writing, software
14 //   distributed under the License is distributed on an "AS IS" BASIS,
15 //   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 //   See the License for the specific language governing permissions and
17 //   limitations under the License.
18 //   ========================LICENSE_END===================================
19 //
20
21 package restclient
22
23 import (
24         "bytes"
25         "crypto/tls"
26         "fmt"
27         "io"
28         "math"
29         "net/http"
30         "net/url"
31         "time"
32
33         "github.com/hashicorp/go-retryablehttp"
34         log "github.com/sirupsen/logrus"
35 )
36
37 const ContentTypeJSON = "application/json"
38 const ContentTypePlain = "text/plain"
39
40 // HTTPClient interface
41 type HTTPClient interface {
42         Get(url string) (*http.Response, error)
43
44         Do(*http.Request) (*http.Response, error)
45 }
46
47 type RequestError struct {
48         StatusCode int
49         Body       []byte
50 }
51
52 func (pe RequestError) Error() string {
53         return fmt.Sprintf("Request failed due to error response with status: %v and body: %v", pe.StatusCode, string(pe.Body))
54 }
55
56 func Get(url string, client HTTPClient) ([]byte, error) {
57         if response, err := client.Get(url); err == nil {
58                 if isResponseSuccess(response.StatusCode) {
59                         defer response.Body.Close()
60                         if responseData, err := io.ReadAll(response.Body); err == nil {
61                                 return responseData, nil
62                         } else {
63                                 return nil, err
64                         }
65                 } else {
66                         return nil, getRequestError(response)
67                 }
68         } else {
69                 return nil, err
70         }
71 }
72
73 func Put(url string, body []byte, client HTTPClient) error {
74         return do(http.MethodPut, url, body, ContentTypeJSON, client)
75 }
76
77 func Post(url string, body []byte, contentType string, client HTTPClient) error {
78         return do(http.MethodPost, url, body, contentType, client)
79 }
80
81 func do(method string, url string, body []byte, contentType string, client HTTPClient) error {
82         if req, reqErr := http.NewRequest(method, url, bytes.NewBuffer(body)); reqErr == nil {
83                 req.Header.Set("Content-Type", contentType)
84                 if response, respErr := client.Do(req); respErr == nil {
85                         if isResponseSuccess(response.StatusCode) {
86                                 return nil
87                         } else {
88                                 return getRequestError(response)
89                         }
90                 } else {
91                         return respErr
92                 }
93         } else {
94                 return reqErr
95         }
96 }
97
98 func isResponseSuccess(statusCode int) bool {
99         return statusCode >= http.StatusOK && statusCode <= 299
100 }
101
102 func getRequestError(response *http.Response) RequestError {
103         defer response.Body.Close()
104         responseData, _ := io.ReadAll(response.Body)
105         putError := RequestError{
106                 StatusCode: response.StatusCode,
107                 Body:       responseData,
108         }
109         return putError
110 }
111
112 func CreateClientCertificate(certPath string, keyPath string) (tls.Certificate, error) {
113         if cert, err := tls.LoadX509KeyPair(certPath, keyPath); err == nil {
114                 return cert, nil
115         } else {
116                 return tls.Certificate{}, fmt.Errorf("cannot create x509 keypair from cert file %s and key file %s due to: %v", certPath, keyPath, err)
117         }
118 }
119
120 func CreateRetryClient(cert tls.Certificate) *http.Client {
121         rawRetryClient := retryablehttp.NewClient()
122         rawRetryClient.Logger = leveledLogger{}
123         rawRetryClient.RetryWaitMax = time.Minute
124         rawRetryClient.RetryMax = math.MaxInt
125         rawRetryClient.HTTPClient.Transport = getSecureTransportWithoutVerify(cert)
126
127         client := rawRetryClient.StandardClient()
128         return client
129 }
130
131 func CreateClientWithoutRetry(cert tls.Certificate, timeout time.Duration) *http.Client {
132         return &http.Client{
133                 Timeout:   timeout,
134                 Transport: getSecureTransportWithoutVerify(cert),
135         }
136 }
137
138 func getSecureTransportWithoutVerify(cert tls.Certificate) *http.Transport {
139         return &http.Transport{
140                 TLSClientConfig: &tls.Config{
141                         Certificates: []tls.Certificate{
142                                 cert,
143                         },
144                         InsecureSkipVerify: true,
145                 },
146         }
147 }
148
149 func IsUrlSecure(configUrl string) bool {
150         u, _ := url.Parse(configUrl)
151         return u.Scheme == "https"
152 }
153
154 // Used to get leveled logging in the RetryClient
155 type leveledLogger struct {
156 }
157
158 func (ll leveledLogger) Error(msg string, keysAndValues ...interface{}) {
159         log.WithFields(getFields(keysAndValues)).Error(msg)
160 }
161 func (ll leveledLogger) Info(msg string, keysAndValues ...interface{}) {
162         log.WithFields(getFields(keysAndValues)).Info(msg)
163 }
164 func (ll leveledLogger) Debug(msg string, keysAndValues ...interface{}) {
165         log.WithFields(getFields(keysAndValues)).Debug(msg)
166 }
167 func (ll leveledLogger) Warn(msg string, keysAndValues ...interface{}) {
168         log.WithFields(getFields(keysAndValues)).Warn(msg)
169 }
170
171 func getFields(keysAndValues []interface{}) log.Fields {
172         fields := log.Fields{}
173         for i := 0; i < len(keysAndValues); i = i + 2 {
174                 fields[fmt.Sprint(keysAndValues[i])] = keysAndValues[i+1]
175         }
176         return fields
177 }