Skip to content

Commit

Permalink
Merge pull request #14 from devilcove/develop
Browse files Browse the repository at this point in the history
return both response and errResponse
  • Loading branch information
mattkasun authored Nov 19, 2022
2 parents a422db0 + 352fd3c commit 0e2e747
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 15 deletions.
2 changes: 1 addition & 1 deletion examples/functions/json/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion examples/method/json/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
23 changes: 11 additions & 12 deletions httpclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
3 changes: 2 additions & 1 deletion httpclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}

0 comments on commit 0e2e747

Please sign in to comment.