-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponse.go
113 lines (92 loc) · 2.18 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package easy_http
import (
"io/ioutil"
"net/http"
)
type BuildResponse func(resp *http.Response, err error) IResponse
//使用client发去请求后,返回一个实现了这个接口的对象
//只要实现这个接口,就能作为返回值
//在 `BuildResponse` 函数中构造出返回的对象
//默认提供了 `HttpResponse` 实现了这个接口
//可以根据自己的需求自己重新实现这个接口
type IResponse interface {
//返回这个请求的错误
Error() error
//返回这个请求的http状态码
StatusCode() int
//返回HTTP请求的header信息
Header() http.Header
//返回HTTP内容长度
ContentLength() int64
//返回HTTP的内容
Content() []byte
//返回HTTP包中的 response信息
Resp() *http.Response
//返回这次请求的request信息
Request() *http.Request
//根据name 返回response的cookie
Cookie(name string) *http.Cookie
}
type HttpResponse struct {
err error
ResponseContent []byte
httpResp *http.Response
}
func (h *HttpResponse) Error() error {
return h.err
}
func (h *HttpResponse) StatusCode() int {
if h.httpResp == nil {
return 0
}
return h.httpResp.StatusCode
}
func (h *HttpResponse) Header() http.Header {
if h.httpResp == nil {
return nil
}
return h.httpResp.Header
}
func (h *HttpResponse) ContentLength() int64 {
if h.httpResp == nil {
return 0
}
return h.httpResp.ContentLength
}
func (h *HttpResponse) Content() []byte {
return h.ResponseContent
}
func (h *HttpResponse) Resp() *http.Response {
return h.httpResp
}
func (h *HttpResponse) Request() *http.Request {
if h.httpResp == nil {
return nil
}
return h.httpResp.Request
}
func (h *HttpResponse) Cookie(name string) *http.Cookie {
for _, cookie := range h.httpResp.Cookies() {
if cookie.Name == name {
return cookie
}
}
return nil
}
//默认构造HTTP response的函数
func EasyBuildResponse(resp *http.Response, err error) IResponse {
response := new(HttpResponse)
if err != nil {
response.err = err
return response
}
response.httpResp = resp
all, err := ioutil.ReadAll(resp.Body)
if err != nil {
response.err = err
return response
}
response.ResponseContent = all
_ = resp.Body.Close()
return response
}