From 7a839f5e70b386f68447cd8fc4d836fdff6b080a Mon Sep 17 00:00:00 2001 From: "Matthew R. Kasun" Date: Sat, 19 Nov 2022 14:41:39 -0500 Subject: [PATCH] return both response and errResponse --- examples/functions/json/main.go | 2 +- examples/method/json/main.go | 2 +- httpclient.go | 23 +++++++++++------------ httpclient_test.go | 3 ++- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/examples/functions/json/main.go b/examples/functions/json/main.go index 88decb9..5d2eee3 100644 --- a/examples/functions/json/main.go +++ b/examples/functions/json/main.go @@ -17,7 +17,7 @@ type Answer struct { func main() { var answer Answer var errResp any - ip, err := httpclient.GetJSON(nil, answer, errResp, http.MethodGet, "http://api.ipify.org?format=json", "", nil) + ip, _, err := httpclient.GetJSON(nil, answer, errResp, http.MethodGet, "http://api.ipify.org?format=json", "", nil) if err != nil { if strings.Contains(err.Error(), "non ok status") { fmt.Println(ip, err) diff --git a/examples/method/json/main.go b/examples/method/json/main.go index 060da55..181960a 100644 --- a/examples/method/json/main.go +++ b/examples/method/json/main.go @@ -24,7 +24,7 @@ func main() { Response: response, ErrorResponse: errResponse, } - answer, err := endpoint.GetJSON(response, errResponse) + answer, _, err := endpoint.GetJSON(response, errResponse) if err != nil { log.Fatal(err) } diff --git a/httpclient.go b/httpclient.go index 04d143e..cc60c74 100644 --- a/httpclient.go +++ b/httpclient.go @@ -76,27 +76,26 @@ func GetResponse(data any, method, url, auth string, headers []Header) (*http.Re } // JSON returns JSON response from http request -// if response.code is http.StatusOK (200) returns body decoded to resp -// if response.code is not http.Status ok returns response(*http.Response) with err set to -// 'non ok response code' -// if json decode err returns response and err set to 'json decode err' -// for any other err returns nil and err -func GetJSON[T any, R any](data any, resp T, errResponse R, method, url, auth string, headers []Header) (any, error) { +// if response.code is http.StatusOK (200) returns resp, nil, nil +// if response.code is not http.Status ok returns nil, errResponse, err='non ok response code' +// if json decode err returns nil, nil, err set to 'json decode err' +// for any other err returns nil, nil, err +func GetJSON[T any, R any](data any, resp T, errResponse R, method, url, auth string, headers []Header) (T, R, error) { response, err := GetResponse(data, method, url, auth, headers) if err != nil { - return nil, err + return resp, errResponse, err } defer response.Body.Close() if response.StatusCode != http.StatusOK { if err := json.NewDecoder(response.Body).Decode(&errResponse); err != nil { - return response, ErrJSON + return resp, errResponse, ErrJSON } - return errResponse, ErrStatus + return resp, errResponse, ErrStatus } if err := json.NewDecoder(response.Body).Decode(&resp); err != nil { - return response, ErrJSON + return resp, errResponse, ErrJSON } - return resp, nil + return resp, errResponse, nil } // GetResponse returns response from http endpoint @@ -105,6 +104,6 @@ func (e *Endpoint) GetResponse() (*http.Response, error) { } // GetJSON returns JSON received from http endpoint -func (e *JSONEndpoint[T, R]) GetJSON(response T, errResponse R) (any, error) { +func (e *JSONEndpoint[T, R]) GetJSON(response T, errResponse R) (T, R, error) { return GetJSON(e.Data, response, errResponse, e.Method, e.URL+e.Route, e.Authorization, e.Headers) } diff --git a/httpclient_test.go b/httpclient_test.go index a931f2b..f06fe6b 100644 --- a/httpclient_test.go +++ b/httpclient_test.go @@ -105,9 +105,10 @@ func TestGetJSON(t *testing.T) { Response: response, ErrorResponse: errResponse, } - answer, err := e.GetJSON(response, errResponse) + answer, errs, err := e.GetJSON(response, errResponse) is.NoErr(err) answerType := fmt.Sprintf("%T", answer) is.True(answerType == "struct { JWT string }") + is.Equal(errs, nil) }) }