Completed
Pull Request — master (#5)
by
unknown
02:01
created

devto/utilities.go   A

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 40%

Importance

Changes 0
Metric Value
cc 5
eloc 21
dl 0
loc 34
ccs 4
cts 10
cp 0.4
crap 10.3999
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A devto.non2xx 0 1 1
A devto.decodeResponse 0 7 2
A devto.unmarshalErrorResponse 0 10 2
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