-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsession.go
84 lines (69 loc) · 2.2 KB
/
session.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
package go_requests
type Session struct {
Config *Config `json:"config"` // 请求配置
cookies map[string]string
}
func NewSession(config *Config) *Session {
return &Session{Config: config}
}
func (s *Session) SendRequest(req *Request) *Response {
if s.cookies != nil {
updateMap(req.Cookies, s.cookies)
}
resp := req.Send()
if resp.Cookies != nil {
updateMap(s.cookies, resp.Cookies)
//s.cookies = resp.Cookies // TODO Merge cookies
}
return resp
}
func (s *Session) Get(url string, headers map[string]string) *Response {
req := NewRequestWithConfig(s.Config, "GET", url).
SetHeaders(headers)
return s.SendRequest(req)
}
func (s *Session) Post(url, data string, headers map[string]string) *Response {
req := NewRequestWithConfig(s.Config, "POST", url).
SetRawData(data).
SetHeaders(headers)
return req.Send()
}
func (s *Session) Put(url, data string, headers map[string]string) *Response {
req := NewRequestWithConfig(s.Config, "Put", url).
SetRawData(data).
SetHeaders(headers)
return req.Send()
}
func (s *Session) Delete(url string, headers map[string]string) *Response {
req := NewRequestWithConfig(s.Config, "DELETE", url).
SetHeaders(headers)
return s.SendRequest(req)
}
func (s *Session) Head(url string, headers map[string]string) *Response {
req := NewRequestWithConfig(s.Config, "HEAD", url).
SetHeaders(headers)
return s.SendRequest(req)
}
func (s *Session) Options(url string, headers map[string]string) *Response {
req := NewRequestWithConfig(s.Config, "OPTIONS", url).
SetHeaders(headers)
return s.SendRequest(req)
}
func Get(url string, headers map[string]string) *Response {
return NewSession(nil).Get(url, headers)
}
func Post(url, data string, headers map[string]string) *Response {
return NewSession(nil).Post(url, data, headers)
}
func Put(url, data string, headers map[string]string) *Response {
return NewSession(nil).Post(url, data, headers)
}
func Delete(url string, headers map[string]string) *Response {
return NewSession(nil).Get(url, headers)
}
func Head(url string, headers map[string]string) *Response {
return NewSession(nil).Head(url, headers)
}
func Options(url string, headers map[string]string) *Response {
return NewSession(nil).Options(url, headers)
}