-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.go
48 lines (39 loc) · 1.02 KB
/
response.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package snorlax
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
)
// Response is a type alias for http.Response.
type Response struct {
http.Response
}
// IsSuccess returns whether the response code is within the 2XX range.
func (r *Response) IsSuccess() bool {
return r.StatusCode < http.StatusMultipleChoices
}
// JSON reads and unmarshals the response body into out.
func (r *Response) JSON(out interface{}) error {
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return fmt.Errorf("failed to read response body: %w", err)
}
if err = json.Unmarshal(body, &out); err != nil {
return fmt.Errorf("failed to unmarshal response body: %w", err)
}
return nil
}
// RawBody returns an io.Reader containing the data returned in the response
// body.
func (r *Response) RawBody() (io.Reader, error) {
defer r.Body.Close()
data, err := ioutil.ReadAll(r.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response body: %w", err)
}
return bytes.NewBuffer(data), nil
}