Total Lines | 37 |
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 | // nonSuccessfulResponse indicates that the status code for |
||
11 | // the HTTP response passed in was not one of the "success" |
||
12 | // (2XX) status codes |
||
13 | 1 | func nonSuccessfulResponse(res *http.Response) bool { return res.StatusCode/100 != 2 } |
|
14 | |||
15 | // attempt to deserialize the error response; if it succeeds, |
||
16 | // the error will be an ErrorResponse, otherwise it will be |
||
17 | // an error indicating that the error response could not be |
||
18 | // deserialized. |
||
19 | func unmarshalErrorResponse(res *http.Response) error { |
||
20 | 1 | var e ErrorResponse |
|
21 | 1 | if err := json.NewDecoder(res.Body).Decode(&e); err != nil { |
|
22 | return fmt.Errorf( |
||
23 | `unexpected error deserializing %d response: "%v"`, |
||
24 | res.StatusCode, |
||
25 | err, |
||
26 | ) |
||
27 | } |
||
28 | 1 | return &e |
|
29 | } |
||
30 | |||
31 | func decodeResponse(r *http.Response) []byte { |
||
32 | c, err := ioutil.ReadAll(r.Body) |
||
33 | if err != nil { |
||
34 | return []byte("") |
||
35 | } |
||
36 | defer r.Body.Close() |
||
37 | return c |
||
38 | } |
||
39 |