-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresponse.go
94 lines (83 loc) · 1.51 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
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
package requests
import (
"io/ioutil"
"net/http"
"compress/gzip"
"encoding/json"
"io"
)
type Response struct {
*http.Response
err error
}
func (r *Response) Err() error {
return r.err
}
func (r *Response) GetCookie() map[string]string {
cs := r.Cookies()
if len(cs) == 0 {
return nil
}
cookie := make(map[string]string)
for _, v := range cs {
cookie[v.Name] = v.Value
}
return cookie
}
func (r *Response) GetHeader() map[string]string {
var header map[string]string
for k, values := range r.Header {
value := ""
for _, v := range values {
if value != "" {
value = value + ";" + v
} else {
value = v
}
}
if header == nil {
header = make(map[string]string)
}
header[k] = value
}
return header
}
func (r *Response) Bytes() []byte {
if r.err != nil || r.Body == nil {
return nil
}
var body []byte
if r.Header.Get("Content-Encoding") == "gzip" {
var reader io.Reader
reader, r.err = gzip.NewReader(r.Body)
if r.err != nil {
return nil
}
body, r.err = ioutil.ReadAll(reader)
return body
}
body, r.err = ioutil.ReadAll(r.Body)
return body
}
func (r *Response) BindJson(obj interface{}) error {
body := r.Bytes()
if r.err != nil {
return r.err
}
return json.Unmarshal(body, obj)
}
func (r *Response) StoreCookie(req *Request) *Response{
c := r.GetCookie()
if c != nil {
req.SetCookie(c)
}
return nil
}
func (r *Response) DumpToFile(filename string) error {
// TODO
return nil
}
func (r *Response) SaveToFile(filename string) error {
// TODO
return nil
}