First go version of o-ru-closed-loop
[nonrtric.git] / test / usecases / oruclosedlooprecovery / 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         "fmt"
26         "io"
27         "net/http"
28         "time"
29 )
30
31 type RequestError struct {
32         StatusCode int
33         Body       []byte
34 }
35
36 func (pe RequestError) Error() string {
37         return fmt.Sprintf("Request failed due to error response with status: %v and body: %v", pe.StatusCode, string(pe.Body))
38 }
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 var (
48         Client HTTPClient
49 )
50
51 func init() {
52         Client = &http.Client{
53                 Timeout: time.Second * 5,
54         }
55 }
56
57 func Get(url string) ([]byte, error) {
58         if response, err := Client.Get(url); err == nil {
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, err
67         }
68 }
69
70 func PutWithoutAuth(url string, body []byte) error {
71         return do(http.MethodPut, url, body)
72 }
73
74 func Put(url string, body string, userName string, password string) error {
75         return do(http.MethodPut, url, []byte(body), userName, password)
76 }
77
78 func Delete(url string) error {
79         return do(http.MethodDelete, url, nil)
80 }
81
82 func do(method string, url string, body []byte, userInfo ...string) error {
83         if req, reqErr := http.NewRequest(method, url, bytes.NewBuffer(body)); reqErr == nil {
84                 if body != nil {
85                         req.Header.Set("Content-Type", "application/json; charset=utf-8")
86                 }
87                 if len(userInfo) > 0 {
88                         req.SetBasicAuth(userInfo[0], userInfo[1])
89                 }
90                 if response, respErr := Client.Do(req); respErr == nil {
91                         if isResponseSuccess(response.StatusCode) {
92                                 return nil
93                         } else {
94                                 return getResponseError(response)
95                         }
96                 } else {
97                         return respErr
98                 }
99         } else {
100                 return reqErr
101         }
102 }
103
104 func isResponseSuccess(statusCode int) bool {
105         return statusCode >= http.StatusOK && statusCode <= 299
106 }
107
108 func getResponseError(response *http.Response) RequestError {
109         defer response.Body.Close()
110         responseData, _ := io.ReadAll(response.Body)
111         putError := RequestError{
112                 StatusCode: response.StatusCode,
113                 Body:       responseData,
114         }
115         return putError
116 }