-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathresponse.go
More file actions
100 lines (83 loc) · 1.98 KB
/
response.go
File metadata and controls
100 lines (83 loc) · 1.98 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package gohttp
import (
"encoding/json"
"io"
"io/ioutil"
"net/http"
"net/url"
)
// Response is a http response struct
type Response struct {
resp *http.Response
}
// AsyncResponse is a response struct for asynchronous request
type AsyncResponse struct {
Resp *Response
Err error
}
// GetResp get net/http original response
func (res *Response) GetResp() *http.Response {
return res.resp
}
// GetStatusCode returns http status code
// if Response is not returned from a Request
// the status code will be 0
func (res *Response) GetStatusCode() int {
if res.resp == nil {
return 0
}
return res.resp.StatusCode
}
// GetBody returns response body
// It is the caller's responsibility to close Body
func (res *Response) GetBody() io.ReadCloser {
if res.resp == nil {
return nil
}
return res.resp.Body
}
// GetBodyAsByte returns response body as byte
func (res *Response) GetBodyAsByte() ([]byte, error) {
body := res.GetBody()
if body == nil {
return nil, nil
}
defer body.Close()
byts, err := ioutil.ReadAll(body)
if err != nil {
return nil, err
}
return byts, nil
}
// GetBodyAsString returns response body as string
func (res *Response) GetBodyAsString() (string, error) {
body, err := res.GetBodyAsByte()
if err != nil || body == nil {
return "", err
}
return string(body), nil
}
// GetBodyAsJSONRawMessage returns response body as json.RawMessage
func (res *Response) GetBodyAsJSONRawMessage() (json.RawMessage, error) {
body, err := res.GetBodyAsByte()
if err != nil || body == nil {
return nil, err
}
return json.RawMessage(body), nil
}
// UnmarshalBody unmarshal response body
func (res *Response) UnmarshalBody(v interface{}) error {
body, err := res.GetBodyAsByte()
if err != nil || body == nil {
return err
}
return json.Unmarshal(body, &v)
}
//Protocol returns response proto
func (res *Response) Protocol() string{
return res.resp.Proto
}
//URL returns response Location
func (res *Response) URL() (*url.URL, error) {
return res.resp.Location()
}