forked from wangluozhe/requests
/
Response.go
111 lines (99 loc) · 2.72 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
package models
import (
"encoding/json"
"errors"
"fmt"
"github.com/bitly/go-simplejson"
"github.com/wangluozhe/chttp"
"github.com/760644586/requests/url"
"io"
)
var RedirectStatusCodes = []int{
http.StatusMovedPermanently,
http.StatusFound,
http.StatusSeeOther,
http.StatusTemporaryRedirect,
http.StatusPermanentRedirect,
}
func inRedirectStatusCodes(statusCode int) bool {
for _, code := range RedirectStatusCodes {
if statusCode == code {
return true
}
}
return false
}
var PermanentRedirectStatusCodes = []int{
http.StatusMovedPermanently,
http.StatusPermanentRedirect,
}
func inPermanentRedirectStatusCodes(statusCode int) bool {
for _, code := range PermanentRedirectStatusCodes {
if code == statusCode {
return true
}
}
return false
}
// Response结构体
type Response struct {
Url string
Headers http.Header
Cookies []*http.Cookie
Text string
Content []byte
Body io.ReadCloser
StatusCode int
History []*Response
Request *url.Request
}
// 使用自带库JSON解析
func (res *Response) Json() (map[string]interface{}, error) {
js := make(map[string]interface{})
err := json.Unmarshal(res.Content, &js)
return js, err
}
// 使用go-simplejson解析
func (res *Response) SimpleJson() (*simplejson.Json, error) {
return simplejson.NewFromReader(res.Body)
}
// 状态码是否合格
func (res Response) Ok() bool {
// Returns True if :attr:`status_code` is less than 400, False if not.
//
// This attribute checks if the status code of the response is between
// 400 and 600 to see if there was a client error or a server error. If
// the status code is between 200 and 400, this will return True. This
// is **not** a check to see if the response code is ``200 OK``.
err := res.RaiseForStatus()
if err != nil {
return false
}
return true
}
func (res Response) IsRedirect() bool {
// True if this Response is a well-formed HTTP redirect that could have
// been processed automatically (by :meth:`Session.resolve_redirects`).
if res.Headers.Get("locaion") == "" || inRedirectStatusCodes(res.StatusCode) {
return false
}
return true
}
func (res Response) IsPermanentRedirect() bool {
// True if this Response one of the permanent versions of redirect.
if res.Headers.Get("locaion") == "" || inPermanentRedirectStatusCodes(res.StatusCode) {
return false
}
return true
}
// 状态码是否错误
func (res *Response) RaiseForStatus() error {
// Raises :class:`HTTPError`, if one occurred.
var err error
if res.StatusCode >= 400 && res.StatusCode < 500 {
err = errors.New(fmt.Sprintf("%d Client Error", res.StatusCode))
} else if res.StatusCode >= 500 && res.StatusCode < 600 {
err = errors.New(fmt.Sprintf("%d Server Error", res.StatusCode))
}
return err
}