diff --git a/.golangci.yml b/.golangci.yml index 5d09cde..172ea8a 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -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. diff --git a/request.go b/request.go index cb45906..7b87eee 100644 --- a/request.go +++ b/request.go @@ -2,6 +2,7 @@ package common import ( "bytes" + "encoding/json" "fmt" "io/ioutil" "net/http" @@ -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 { @@ -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 }