Merge "NONRTRIC - Implement DMaaP mediator producer service in Java"
[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 )
35
36 // HTTPClient interface
37 type HTTPClient interface {
38         Get(url string) (*http.Response, error)
39
40         Do(*http.Request) (*http.Response, error)
41 }
42
43 type RequestError struct {
44         StatusCode int
45         Body       []byte
46 }
47
48 func (pe RequestError) Error() string {
49         return fmt.Sprintf("Request failed due to error response with status: %v and body: %v", pe.StatusCode, string(pe.Body))
50 }
51
52 func Get(url string, client HTTPClient) ([]byte, error) {
53         if response, err := client.Get(url); err == nil {
54                 if isResponseSuccess(response.StatusCode) {
55                         defer response.Body.Close()
56                         if responseData, err := io.ReadAll(response.Body); err == nil {
57                                 return responseData, nil
58                         } else {
59                                 return nil, err
60                         }
61                 } else {
62                         return nil, getRequestError(response)
63                 }
64         } else {
65                 return nil, err
66         }
67 }
68
69 func Put(url string, body []byte, client HTTPClient) error {
70         return do(http.MethodPut, url, body, client)
71 }
72
73 func Post(url string, body []byte, client HTTPClient) error {
74         return do(http.MethodPost, url, body, client)
75 }
76
77 func do(method string, url string, body []byte, client HTTPClient) error {
78         if req, reqErr := http.NewRequest(method, url, bytes.NewBuffer(body)); reqErr == nil {
79                 req.Header.Set("Content-Type", "application/json")
80                 if response, respErr := client.Do(req); respErr == nil {
81                         if isResponseSuccess(response.StatusCode) {
82                                 return nil
83                         } else {
84                                 return getRequestError(response)
85                         }
86                 } else {
87                         return respErr
88                 }
89         } else {
90                 return reqErr
91         }
92 }
93
94 func isResponseSuccess(statusCode int) bool {
95         return statusCode >= http.StatusOK && statusCode <= 299
96 }
97
98 func getRequestError(response *http.Response) RequestError {
99         defer response.Body.Close()
100         responseData, _ := io.ReadAll(response.Body)
101         putError := RequestError{
102                 StatusCode: response.StatusCode,
103                 Body:       responseData,
104         }
105         return putError
106 }
107
108 func CreateClientCertificate(certPath string, keyPath string) (tls.Certificate, error) {
109         if cert, err := tls.LoadX509KeyPair(certPath, keyPath); err == nil {
110                 return cert, nil
111         } else {
112                 return tls.Certificate{}, fmt.Errorf("cannot create x509 keypair from cert file %s and key file %s due to: %v", certPath, keyPath, err)
113         }
114 }
115
116 func CreateRetryClient(cert tls.Certificate) *http.Client {
117         rawRetryClient := retryablehttp.NewClient()
118         rawRetryClient.RetryWaitMax = time.Minute
119         rawRetryClient.RetryMax = math.MaxInt
120         rawRetryClient.HTTPClient.Transport = getSecureTransportWithoutVerify(cert)
121
122         client := rawRetryClient.StandardClient()
123         return client
124 }
125
126 func CreateClientWithoutRetry(cert tls.Certificate, timeout time.Duration) *http.Client {
127         return &http.Client{
128                 Timeout:   timeout,
129                 Transport: getSecureTransportWithoutVerify(cert),
130         }
131 }
132
133 func getSecureTransportWithoutVerify(cert tls.Certificate) *http.Transport {
134         return &http.Transport{
135                 TLSClientConfig: &tls.Config{
136                         Certificates: []tls.Certificate{
137                                 cert,
138                         },
139                         InsecureSkipVerify: true,
140                 },
141         }
142 }
143
144 func IsUrlSecure(configUrl string) bool {
145         u, _ := url.Parse(configUrl)
146         return u.Scheme == "https"
147 }