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