Skip to content

Commit a38ed1d

Browse files
authored
Merge pull request #12 from devilcove/refactor-jsonendpoint-struct
return http status code for JSON func/method
2 parents 0028e69 + da1cffa commit a38ed1d

4 files changed

Lines changed: 19 additions & 8 deletions

File tree

examples/functions/json/main.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@ type Answer struct {
1515
// equivalient of curl api.ipify.org?format=json
1616
func main() {
1717
var answer Answer
18-
ip, err := httpclient.GetJSON(nil, answer, http.MethodGet, "http://api.ipify.org?format=json", "")
18+
ip, code, err := httpclient.GetJSON(nil, answer, http.MethodGet, "http://api.ipify.org?format=json", "")
1919
if err != nil {
2020
log.Fatal(err)
2121
}
22+
if code != http.StatusOK {
23+
log.Fatal(err, code)
24+
}
2225
fmt.Println(ip)
2326
}

examples/method/json/main.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,13 @@ func main() {
2222
Data: nil,
2323
Response: response,
2424
}
25-
answer, err := endpoint.GetJSON(response)
25+
answer, code, err := endpoint.GetJSON(response)
2626
if err != nil {
2727
log.Fatal(err)
2828
}
29+
if code != http.StatusOK {
30+
log.Fatal(err, code)
31+
}
2932
fmt.Println(answer)
3033

3134
}

httpclient.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package httpclient
33
import (
44
"bytes"
55
"encoding/json"
6+
"errors"
67
"fmt"
78
"net/http"
89
"time"
@@ -62,16 +63,19 @@ func GetResponse(data any, method, url, auth string) (*http.Response, error) {
6263
}
6364

6465
// JSON returns JSON response from http request
65-
func GetJSON[T any](data any, resp T, method, url, auth string) (any, error) {
66+
func GetJSON[T any](data any, resp T, method, url, auth string) (any, int, error) {
6667
response, err := GetResponse(data, method, url, auth)
6768
if err != nil {
68-
return nil, err
69+
return nil, response.StatusCode, err
6970
}
7071
defer response.Body.Close()
72+
if response.StatusCode != http.StatusOK {
73+
return response, response.StatusCode, errors.New("non 200 response code")
74+
}
7175
if err := json.NewDecoder(response.Body).Decode(&resp); err != nil {
72-
return nil, err
76+
return nil, response.StatusCode, err
7377
}
74-
return resp, nil
78+
return resp, response.StatusCode, nil
7579
}
7680

7781
// GetResponse returns response from http endpoint
@@ -80,6 +84,6 @@ func (e *Endpoint) GetResponse() (*http.Response, error) {
8084
}
8185

8286
// GetJSON returns JSON received from http endpoint
83-
func (e *JSONEndpoint[T]) GetJSON(response T) (any, error) {
87+
func (e *JSONEndpoint[T]) GetJSON(response T) (any, int, error) {
8488
return GetJSON(e.Data, response, e.Method, e.URL+e.Route, e.Authorization)
8589
}

httpclient_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,9 @@ func TestGetJSON(t *testing.T) {
103103
Data: data,
104104
Response: response,
105105
}
106-
answer, err := e.GetJSON(response)
106+
answer, code, err := e.GetJSON(response)
107107
is.NoErr(err)
108+
is.Equal(code, http.StatusOK)
108109
answerType := fmt.Sprintf("%T", answer)
109110
is.True(answerType == "struct { JWT string }")
110111
})

0 commit comments

Comments
 (0)