| Total Lines | 34 | 
| Duplicated Lines | 0 % | 
| Changes | 0 | ||
| 1 | package ynote | ||
| 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 |