Skip to content

Commit

Permalink
Merge pull request #15 from zetaab/outputinterface
Browse files Browse the repository at this point in the history
return statuscode and interface for output
  • Loading branch information
zetaab authored Nov 15, 2022
2 parents 44cd5be + 1535511 commit 7c7e463
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ linters:

linters-settings:
lll:
line-length: 120
line-length: 140
tab-width: 4
nolintlint:
# Enable to ensure that nolint directives are all used. Default is true.
Expand Down
27 changes: 18 additions & 9 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package common

import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
Expand All @@ -20,9 +21,15 @@ type HTTPRequest struct {
OKCode []int
}

// HTTPResponse ...
type HTTPResponse struct {
Body []byte
StatusCode int
}

// MakeRequest ...
func MakeRequest(request HTTPRequest, client *http.Client, backoff wait.Backoff) ([]byte, error) {
response := []byte{}
func MakeRequest(request HTTPRequest, output interface{}, client *http.Client, backoff wait.Backoff) (*HTTPResponse, error) {
httpresp := &HTTPResponse{}
err := SleepUntil(backoff, func() (bool, error) {
httpreq, err := http.NewRequest(request.Method, request.URL, nil)
if err != nil {
Expand All @@ -47,21 +54,23 @@ func MakeRequest(request HTTPRequest, client *http.Client, backoff wait.Backoff)
return false, err
}
defer resp.Body.Close()
httpresp.StatusCode = resp.StatusCode
responseBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return false, err
}
httpresp.Body = responseBody
if ContainsInteger(request.OKCode, resp.StatusCode) {
response, err = ioutil.ReadAll(resp.Body)
err = json.Unmarshal(httpresp.Body, &output)
if err != nil {
return false, err
return true, fmt.Errorf("could not marshal as json %w", err)
}
return true, nil
}
responseBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return false, err
}
err = fmt.Errorf("got http code %v from [%s] %s: %s... retrying",
resp.StatusCode, request.Method, request.URL, responseBody)
glog.Error(err)
return false, err
})
return response, err
return httpresp, err
}

0 comments on commit 7c7e463

Please sign in to comment.