Total Lines | 34 |
Duplicated Lines | 0 % |
Coverage | 40% |
Changes | 0 |
1 | package devto |
||
2 | |||
3 | import ( |
||
4 | "encoding/json" |
||
5 | "fmt" |
||
6 | "io/ioutil" |
||
7 | "net/http" |
||
8 | ) |
||
9 | |||
10 | 1 | func non2xx(res *http.Response) bool { return res.StatusCode/100 != 2 } |
|
11 | |||
12 | // attempt to deserialize the error response; if it succeeds, |
||
13 | // the error will be an ErrorResponse, otherwise it will be |
||
14 | // an error indicating that the error response could not be |
||
15 | // deserialized. |
||
16 | func unmarshalErrorResponse(res *http.Response) error { |
||
17 | 1 | var e ErrorResponse |
|
18 | 1 | if err := json.NewDecoder(res.Body).Decode(&e); err != nil { |
|
19 | return fmt.Errorf( |
||
20 | `unexpected error deserializing %d response: "%v"`, |
||
21 | res.StatusCode, |
||
22 | err, |
||
23 | ) |
||
24 | } |
||
25 | 1 | return &e |
|
26 | } |
||
27 | |||
28 | func decodeResponse(r *http.Response) []byte { |
||
29 | c, err := ioutil.ReadAll(r.Body) |
||
30 | if err != nil { |
||
31 | return []byte("") |
||
32 | } |
||
33 | defer r.Body.Close() |
||
34 | return c |
||
35 | } |
||
36 |