2 // ========================LICENSE_START=================================
5 // Copyright (C) 2021: 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{}, userName string, password string) error {
98 req, err := c.newRequest(http.MethodPut, path, payload, userName, password)
100 return fmt.Errorf("failed to create PUT request: %w", err)
103 if err := c.doRequest(req, v); err != nil {
110 func (c *Client) newRequest(method, path string, payload interface{}, userInfo ...string) (*http.Request, error) {
111 var reqBody io.Reader
114 bodyBytes, err := json.Marshal(payload)
116 return nil, fmt.Errorf("failed to marshal request body: %w", err)
118 reqBody = bytes.NewReader(bodyBytes)
121 req, err := http.NewRequest(method, path, reqBody)
124 return nil, fmt.Errorf("failed to create HTTP request: %w", err)
127 if len(userInfo) > 0 {
128 req.SetBasicAuth(userInfo[0], userInfo[1])
132 req.Header.Set("Content-Type", "application/json")
136 if reqDump, error := httputil.DumpRequest(req, true); error != nil {
139 fmt.Println(string(reqDump))
146 func (c *Client) doRequest(r *http.Request, v interface{}) error {
155 defer resp.Body.Close()
161 dec := json.NewDecoder(resp.Body)
162 if err := dec.Decode(&v); err != nil {
163 return fmt.Errorf("could not parse response body: %w [%s:%s]", err, r.Method, r.URL.String())
165 log.Debugf("Http Client Response: %v\n", v)
169 func (c *Client) do(r *http.Request) (*http.Response, error) {
170 resp, err := c.httpClient.Do(r)
172 return nil, fmt.Errorf("failed to make request [%s:%s]: %w", r.Method, r.URL.String(), err)
176 if responseDump, error := httputil.DumpResponse(resp, true); error != nil {
179 fmt.Println(string(responseDump))
183 if resp.StatusCode >= http.StatusOK && resp.StatusCode <= 299 {
187 defer resp.Body.Close()
188 responseData, _ := io.ReadAll(resp.Body)
190 putError := RequestError{
191 StatusCode: resp.StatusCode,
195 return resp, putError