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 | |
elinuxhenrik | 65a53d2 | 2021-09-29 15:41:26 +0200 | [diff] [blame] | 46 | func Get(url string, client HTTPClient) ([]byte, error) { |
| 47 | if response, err := client.Get(url); err == nil { |
| 48 | if isResponseSuccess(response.StatusCode) { |
| 49 | defer response.Body.Close() |
| 50 | if responseData, err := io.ReadAll(response.Body); err == nil { |
elinuxhenrik | cce95ff | 2021-09-05 17:27:02 +0200 | [diff] [blame] | 51 | return responseData, nil |
| 52 | } else { |
elinuxhenrik | 65a53d2 | 2021-09-29 15:41:26 +0200 | [diff] [blame] | 53 | return nil, err |
elinuxhenrik | cce95ff | 2021-09-05 17:27:02 +0200 | [diff] [blame] | 54 | } |
| 55 | } else { |
elinuxhenrik | 65a53d2 | 2021-09-29 15:41:26 +0200 | [diff] [blame] | 56 | return nil, getRequestError(response) |
elinuxhenrik | cce95ff | 2021-09-05 17:27:02 +0200 | [diff] [blame] | 57 | } |
| 58 | } else { |
| 59 | return nil, err |
| 60 | } |
| 61 | } |
| 62 | |
elinuxhenrik | 65a53d2 | 2021-09-29 15:41:26 +0200 | [diff] [blame] | 63 | func Put(url string, body []byte, client HTTPClient) error { |
| 64 | return do(http.MethodPut, url, body, client) |
elinuxhenrik | 2803856 | 2021-09-21 15:43:11 +0200 | [diff] [blame] | 65 | } |
| 66 | |
elinuxhenrik | 65a53d2 | 2021-09-29 15:41:26 +0200 | [diff] [blame] | 67 | func Post(url string, body []byte, client HTTPClient) error { |
| 68 | return do(http.MethodPost, url, body, client) |
elinuxhenrik | 2803856 | 2021-09-21 15:43:11 +0200 | [diff] [blame] | 69 | } |
| 70 | |
elinuxhenrik | 65a53d2 | 2021-09-29 15:41:26 +0200 | [diff] [blame] | 71 | func do(method string, url string, body []byte, client HTTPClient) error { |
elinuxhenrik | 2803856 | 2021-09-21 15:43:11 +0200 | [diff] [blame] | 72 | if req, reqErr := http.NewRequest(method, url, bytes.NewBuffer(body)); reqErr == nil { |
elinuxhenrik | c4960f1 | 2021-10-28 16:27:57 +0200 | [diff] [blame^] | 73 | req.Header.Set("Content-Type", "application/json") |
elinuxhenrik | 65a53d2 | 2021-09-29 15:41:26 +0200 | [diff] [blame] | 74 | if response, respErr := client.Do(req); respErr == nil { |
elinuxhenrik | cce95ff | 2021-09-05 17:27:02 +0200 | [diff] [blame] | 75 | if isResponseSuccess(response.StatusCode) { |
| 76 | return nil |
| 77 | } else { |
| 78 | return getRequestError(response) |
| 79 | } |
| 80 | } else { |
| 81 | return respErr |
| 82 | } |
| 83 | } else { |
| 84 | return reqErr |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | func isResponseSuccess(statusCode int) bool { |
| 89 | return statusCode >= http.StatusOK && statusCode <= 299 |
| 90 | } |
| 91 | |
| 92 | func getRequestError(response *http.Response) RequestError { |
elinuxhenrik | b1fb1d8 | 2021-09-07 02:58:52 +0200 | [diff] [blame] | 93 | defer response.Body.Close() |
elinuxhenrik | cce95ff | 2021-09-05 17:27:02 +0200 | [diff] [blame] | 94 | responseData, _ := io.ReadAll(response.Body) |
| 95 | putError := RequestError{ |
| 96 | StatusCode: response.StatusCode, |
| 97 | Body: responseData, |
| 98 | } |
| 99 | return putError |
| 100 | } |