response.go   A
last analyzed

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 23
dl 0
loc 34
c 0
b 0
f 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A client.*Response.Error 0 6 3
A client.*Response.errorMessage 0 9 1
1
package client
2
3
import (
4
	"bytes"
5
	"errors"
6
	"net/http"
7
	"strconv"
8
)
9
10
// Response captures the http response
11
type Response struct {
12
	HTTPResponse *http.Response
13
	Body         *[]byte
14
}
15
16
// Error ensures that the response can be decoded into a string inc ase it's an error response
17
func (r *Response) Error() error {
18
	switch r.HTTPResponse.StatusCode {
19
	case 200, 201, 202, 204, 205:
20
		return nil
21
	default:
22
		return errors.New(r.errorMessage())
23
	}
24
}
25
26
func (r *Response) errorMessage() string {
27
	var buf bytes.Buffer
28
	buf.WriteString(strconv.Itoa(r.HTTPResponse.StatusCode))
29
	buf.WriteString(": ")
30
	buf.WriteString(http.StatusText(r.HTTPResponse.StatusCode))
31
	buf.WriteString(", Body: ")
32
	buf.Write(*r.Body)
33
34
	return buf.String()
35
}
36