68572099bfcd25d3a0daef209aa37d2d652d39d2
[nonrtric/rapp/orufhrecovery.git] / goversion / internal / restclient / client.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 type RequestError struct {
37         StatusCode int
38         Body       []byte
39 }
40
41 func (e RequestError) Error() string {
42         return fmt.Sprintf("error response with status: %v and body: %v", e.StatusCode, string(e.Body))
43 }
44
45 // HTTPClient interface
46 //go:generate mockery --name HTTPClient
47 type HTTPClient interface {
48         Get(url string) (*http.Response, error)
49
50         Do(*http.Request) (*http.Response, error)
51 }
52
53 func PutWithoutAuth(url string, body []byte, client HTTPClient) error {
54         return do(http.MethodPut, url, body, client)
55 }
56
57 func Put(url string, body string, client HTTPClient, userName string, password string) error {
58         return do(http.MethodPut, url, []byte(body), client, userName, password)
59 }
60
61 func Delete(url string, client HTTPClient) error {
62         return do(http.MethodDelete, url, nil, client)
63 }
64
65 func CreateClientCertificate(certPath string, keyPath string) (tls.Certificate, error) {
66         if cert, err := tls.LoadX509KeyPair(certPath, keyPath); err == nil {
67                 return cert, nil
68         } else {
69                 return tls.Certificate{}, fmt.Errorf("cannot create x509 keypair from cert file %s and key file %s due to: %v", certPath, keyPath, err)
70         }
71 }
72
73 func CreateRetryClient(cert tls.Certificate) *http.Client {
74         rawRetryClient := retryablehttp.NewClient()
75         rawRetryClient.RetryWaitMax = time.Minute
76         rawRetryClient.RetryMax = math.MaxInt
77         rawRetryClient.HTTPClient.Transport = getSecureTransportWithoutVerify(cert)
78
79         client := rawRetryClient.StandardClient()
80         return client
81 }
82
83 func IsUrlSecure(configUrl string) bool {
84         u, _ := url.Parse(configUrl)
85         return u.Scheme == "https"
86 }
87
88 func getSecureTransportWithoutVerify(cert tls.Certificate) *http.Transport {
89         return &http.Transport{
90                 TLSClientConfig: &tls.Config{
91                         Certificates: []tls.Certificate{
92                                 cert,
93                         },
94                         InsecureSkipVerify: true,
95                 },
96         }
97 }
98
99 func do(method string, url string, body []byte, client HTTPClient, userInfo ...string) error {
100         if req, reqErr := http.NewRequest(method, url, bytes.NewBuffer(body)); reqErr == nil {
101                 if body != nil {
102                         req.Header.Set("Content-Type", "application/json; charset=utf-8")
103                 }
104                 if len(userInfo) > 0 {
105                         req.SetBasicAuth(userInfo[0], userInfo[1])
106                 }
107                 if response, respErr := client.Do(req); respErr == nil {
108                         if isResponseSuccess(response.StatusCode) {
109                                 return nil
110                         } else {
111                                 return getResponseError(response)
112                         }
113                 } else {
114                         return respErr
115                 }
116         } else {
117                 return reqErr
118         }
119 }
120
121 func isResponseSuccess(statusCode int) bool {
122         return statusCode >= http.StatusOK && statusCode <= 299
123 }
124
125 func getResponseError(response *http.Response) RequestError {
126         defer response.Body.Close()
127         responseData, _ := io.ReadAll(response.Body)
128         putError := RequestError{
129                 StatusCode: response.StatusCode,
130                 Body:       responseData,
131         }
132         return putError
133 }