2 // ========================LICENSE_START=================================
5 // Copyright (C) 2022: Nordix Foundation
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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===================================
31 log "github.com/sirupsen/logrus"
34 type RequestError struct {
39 func (e RequestError) Error() string {
40 return fmt.Sprintf("error response with status: %v and body: %v", e.StatusCode, string(e.Body))
44 httpClient *http.Client
48 func New(httpClient *http.Client, verbose bool) *Client {
50 httpClient: httpClient,
55 func (c *Client) Get(path string, v interface{}, userInfo ...string) error {
59 if len(userInfo) > 1 {
60 req, err = c.newRequest(http.MethodGet, path, nil, userInfo[0], userInfo[1])
62 req, err = c.newRequest(http.MethodGet, path, nil)
66 return fmt.Errorf("failed to create GET request: %w", err)
69 if err := c.doRequest(req, v); err != nil {
76 func (c *Client) Post(path string, payload interface{}, v interface{}, userInfo ...string) error {
80 if len(userInfo) > 1 {
81 req, err = c.newRequest(http.MethodPost, path, payload, userInfo[0], userInfo[1])
83 req, err = c.newRequest(http.MethodPost, path, payload)
87 return fmt.Errorf("failed to create POST request: %w", err)
90 if err := c.doRequest(req, v); err != nil {
97 func (c *Client) Put(path string, payload interface{}, v interface{}, userInfo ...string) error {
100 if len(userInfo) > 1 {
101 req, err = c.newRequest(http.MethodPut, path, payload, userInfo[0], userInfo[1])
103 req, err = c.newRequest(http.MethodPut, path, payload)
107 return fmt.Errorf("failed to create PUT request: %w", err)
110 if err := c.doRequest(req, v); err != nil {
117 func (c *Client) Delete(path string, payload interface{}, v interface{}, userInfo ...string) error {
118 var req *http.Request
120 if len(userInfo) > 1 {
121 req, err = c.newRequest(http.MethodDelete, path, payload, userInfo[0], userInfo[1])
123 req, err = c.newRequest(http.MethodDelete, path, payload)
127 return fmt.Errorf("failed to create Delete request: %w", err)
130 if err := c.doRequest(req, v); err != nil {
137 func (c *Client) newRequest(method, path string, payload interface{}, userInfo ...string) (*http.Request, error) {
138 var reqBody io.Reader
141 bodyBytes, err := json.Marshal(payload)
143 return nil, fmt.Errorf("failed to marshal request body: %w", err)
145 reqBody = bytes.NewReader(bodyBytes)
148 req, err := http.NewRequest(method, path, reqBody)
151 return nil, fmt.Errorf("failed to create HTTP request: %w", err)
154 if len(userInfo) > 0 {
155 req.SetBasicAuth(userInfo[0], userInfo[1])
159 req.Header.Set("Content-Type", "application/json")
163 if reqDump, error := httputil.DumpRequestOut(req, true); error != nil {
166 fmt.Println(string(reqDump))
173 func (c *Client) doRequest(r *http.Request, v interface{}) error {
182 defer resp.Body.Close()
188 dec := json.NewDecoder(resp.Body)
189 if err := dec.Decode(&v); err != nil {
190 return fmt.Errorf("could not parse response body: %w [%s:%s]", err, r.Method, r.URL.String())
192 log.Debugf("Http Client Response: %v\n", v)
196 func (c *Client) do(r *http.Request) (*http.Response, error) {
197 resp, err := c.httpClient.Do(r)
199 return nil, fmt.Errorf("failed to make request [%s:%s]: %w", r.Method, r.URL.String(), err)
203 if responseDump, error := httputil.DumpResponse(resp, true); error != nil {
206 fmt.Println(string(responseDump))
210 if resp.StatusCode >= http.StatusOK && resp.StatusCode <= 299 {
214 defer resp.Body.Close()
215 responseData, _ := io.ReadAll(resp.Body)
217 putError := RequestError{
218 StatusCode: resp.StatusCode,
222 return resp, putError