Skip to content

Commit 7a839f5

Browse files
committed
return both response and errResponse
1 parent f40d7b8 commit 7a839f5

File tree

4 files changed

+15
-15
lines changed

4 files changed

+15
-15
lines changed

examples/functions/json/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type Answer struct {
1717
func main() {
1818
var answer Answer
1919
var errResp any
20-
ip, err := httpclient.GetJSON(nil, answer, errResp, http.MethodGet, "http://api.ipify.org?format=json", "", nil)
20+
ip, _, err := httpclient.GetJSON(nil, answer, errResp, http.MethodGet, "http://api.ipify.org?format=json", "", nil)
2121
if err != nil {
2222
if strings.Contains(err.Error(), "non ok status") {
2323
fmt.Println(ip, err)

examples/method/json/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func main() {
2424
Response: response,
2525
ErrorResponse: errResponse,
2626
}
27-
answer, err := endpoint.GetJSON(response, errResponse)
27+
answer, _, err := endpoint.GetJSON(response, errResponse)
2828
if err != nil {
2929
log.Fatal(err)
3030
}

httpclient.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -76,27 +76,26 @@ func GetResponse(data any, method, url, auth string, headers []Header) (*http.Re
7676
}
7777

7878
// JSON returns JSON response from http request
79-
// if response.code is http.StatusOK (200) returns body decoded to resp
80-
// if response.code is not http.Status ok returns response(*http.Response) with err set to
81-
// 'non ok response code'
82-
// if json decode err returns response and err set to 'json decode err'
83-
// for any other err returns nil and err
84-
func GetJSON[T any, R any](data any, resp T, errResponse R, method, url, auth string, headers []Header) (any, error) {
79+
// if response.code is http.StatusOK (200) returns resp, nil, nil
80+
// if response.code is not http.Status ok returns nil, errResponse, err='non ok response code'
81+
// if json decode err returns nil, nil, err set to 'json decode err'
82+
// for any other err returns nil, nil, err
83+
func GetJSON[T any, R any](data any, resp T, errResponse R, method, url, auth string, headers []Header) (T, R, error) {
8584
response, err := GetResponse(data, method, url, auth, headers)
8685
if err != nil {
87-
return nil, err
86+
return resp, errResponse, err
8887
}
8988
defer response.Body.Close()
9089
if response.StatusCode != http.StatusOK {
9190
if err := json.NewDecoder(response.Body).Decode(&errResponse); err != nil {
92-
return response, ErrJSON
91+
return resp, errResponse, ErrJSON
9392
}
94-
return errResponse, ErrStatus
93+
return resp, errResponse, ErrStatus
9594
}
9695
if err := json.NewDecoder(response.Body).Decode(&resp); err != nil {
97-
return response, ErrJSON
96+
return resp, errResponse, ErrJSON
9897
}
99-
return resp, nil
98+
return resp, errResponse, nil
10099
}
101100

102101
// GetResponse returns response from http endpoint
@@ -105,6 +104,6 @@ func (e *Endpoint) GetResponse() (*http.Response, error) {
105104
}
106105

107106
// GetJSON returns JSON received from http endpoint
108-
func (e *JSONEndpoint[T, R]) GetJSON(response T, errResponse R) (any, error) {
107+
func (e *JSONEndpoint[T, R]) GetJSON(response T, errResponse R) (T, R, error) {
109108
return GetJSON(e.Data, response, errResponse, e.Method, e.URL+e.Route, e.Authorization, e.Headers)
110109
}

httpclient_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,10 @@ func TestGetJSON(t *testing.T) {
105105
Response: response,
106106
ErrorResponse: errResponse,
107107
}
108-
answer, err := e.GetJSON(response, errResponse)
108+
answer, errs, err := e.GetJSON(response, errResponse)
109109
is.NoErr(err)
110110
answerType := fmt.Sprintf("%T", answer)
111111
is.True(answerType == "struct { JWT string }")
112+
is.Equal(errs, nil)
112113
})
113114
}

0 commit comments

Comments
 (0)