elinuxhenrik | cce95ff | 2021-09-05 17:27:02 +0200 | [diff] [blame] | 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 | ) |
| 29 | |
| 30 | // HTTPClient interface |
| 31 | type HTTPClient interface { |
| 32 | Get(url string) (*http.Response, error) |
| 33 | |
| 34 | Do(*http.Request) (*http.Response, error) |
| 35 | } |
| 36 | |
| 37 | type RequestError struct { |
| 38 | StatusCode int |
| 39 | Body []byte |
| 40 | } |
| 41 | |
| 42 | func (pe RequestError) Error() string { |
| 43 | return fmt.Sprintf("Request failed due to error response with status: %v and body: %v", pe.StatusCode, string(pe.Body)) |
| 44 | } |
| 45 | |
| 46 | var ( |
| 47 | Client HTTPClient |
| 48 | ) |
| 49 | |
| 50 | func init() { |
| 51 | Client = &http.Client{} |
| 52 | } |
| 53 | |
| 54 | func Get(url string) ([]byte, error) { |
| 55 | if response, err := Client.Get(url); err == nil { |
| 56 | defer response.Body.Close() |
| 57 | if responseData, err := io.ReadAll(response.Body); err == nil { |
| 58 | if isResponseSuccess(response.StatusCode) { |
| 59 | return responseData, nil |
| 60 | } else { |
| 61 | requestError := RequestError{ |
| 62 | StatusCode: response.StatusCode, |
| 63 | Body: responseData, |
| 64 | } |
| 65 | return nil, requestError |
| 66 | } |
| 67 | } else { |
| 68 | return nil, err |
| 69 | } |
| 70 | } else { |
| 71 | return nil, err |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | func Put(url string, body []byte) error { |
elinuxhenrik | 2803856 | 2021-09-21 15:43:11 +0200 | [diff] [blame] | 76 | return do(http.MethodPut, url, body) |
| 77 | } |
| 78 | |
| 79 | func Post(url string, body []byte) error { |
| 80 | return do(http.MethodPost, url, body) |
| 81 | } |
| 82 | |
| 83 | func do(method string, url string, body []byte) error { |
| 84 | if req, reqErr := http.NewRequest(method, url, bytes.NewBuffer(body)); reqErr == nil { |
elinuxhenrik | cce95ff | 2021-09-05 17:27:02 +0200 | [diff] [blame] | 85 | req.Header.Set("Content-Type", "application/json; charset=utf-8") |
| 86 | if response, respErr := Client.Do(req); respErr == nil { |
| 87 | if isResponseSuccess(response.StatusCode) { |
| 88 | return nil |
| 89 | } else { |
| 90 | return getRequestError(response) |
| 91 | } |
| 92 | } else { |
| 93 | return respErr |
| 94 | } |
| 95 | } else { |
| 96 | return reqErr |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | func isResponseSuccess(statusCode int) bool { |
| 101 | return statusCode >= http.StatusOK && statusCode <= 299 |
| 102 | } |
| 103 | |
| 104 | func getRequestError(response *http.Response) RequestError { |
elinuxhenrik | b1fb1d8 | 2021-09-07 02:58:52 +0200 | [diff] [blame] | 105 | defer response.Body.Close() |
elinuxhenrik | cce95ff | 2021-09-05 17:27:02 +0200 | [diff] [blame] | 106 | responseData, _ := io.ReadAll(response.Body) |
| 107 | putError := RequestError{ |
| 108 | StatusCode: response.StatusCode, |
| 109 | Body: responseData, |
| 110 | } |
| 111 | return putError |
| 112 | } |