From b39eaf448c5c5729eea1b65a42b27c980053a1e0 Mon Sep 17 00:00:00 2001 From: Jesse Haka Date: Tue, 15 Nov 2022 13:27:57 +0200 Subject: [PATCH 1/3] return statuscode and interface for output --- request.go | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/request.go b/request.go index cb45906..1b3bc24 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 %v", 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 } From c905e8ab4959ea60a9bb7761207b47ab515856dc Mon Sep 17 00:00:00 2001 From: Jesse Haka Date: Tue, 15 Nov 2022 13:41:57 +0200 Subject: [PATCH 2/3] lint fix --- request.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/request.go b/request.go index 1b3bc24..7b87eee 100644 --- a/request.go +++ b/request.go @@ -63,7 +63,7 @@ func MakeRequest(request HTTPRequest, output interface{}, client *http.Client, b if ContainsInteger(request.OKCode, resp.StatusCode) { err = json.Unmarshal(httpresp.Body, &output) if err != nil { - return true, fmt.Errorf("could not marshal as json %v", err) + return true, fmt.Errorf("could not marshal as json %w", err) } return true, nil } From 1535511831d9a42c0cc70d35c5672f0f7098bfd2 Mon Sep 17 00:00:00 2001 From: Jesse Haka Date: Tue, 15 Nov 2022 13:43:55 +0200 Subject: [PATCH 3/3] allow 140 chars in line --- .golangci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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.