-
Notifications
You must be signed in to change notification settings - Fork 3
/
api.go
121 lines (112 loc) · 3.15 KB
/
api.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
114
115
116
117
118
119
120
121
/*
Package direwolf is a convenient and easy to use http client written in Golang.
*/
package direwolf
// Default global session
var defaultSession *Session
func init() {
sessionOptions := DefaultSessionOptions() // New default global session
sessionOptions.DisableCookieJar = true
defaultSession = NewSession(sessionOptions)
}
// Send is different with Get and Post method, you should pass a
// Request to it. You can construct Request by use NewRequest
// method.
func Send(req *Request) (*Response, error) {
resp, err := defaultSession.Send(req)
if err != nil {
return nil, err
}
return resp, nil
}
// Get is the most common method of direwolf to constructs and sends a
// Get request.
//
// You can construct this request by passing the following parameters:
// url: URL for the request.
// http.Header: HTTP Headers to send.
// direwolf.Params: Parameters to send in the query string.
// direwolf.Cookies: Cookies to send.
// direwolf.PostForm: Post data form to send.
// direwolf.Body: Post body to send.
// direwolf.Proxy: Proxy url to use.
// direwolf.Timeout: Request Timeout. Default value is 30.
// direwolf.RedirectNum: Number of Request allowed to redirect. Default value is 5.
func Get(URL string, args ...RequestOption) (*Response, error) {
req, err := NewRequest("GET", URL, args...)
if err != nil {
return nil, err
}
resp, err := Send(req)
if err != nil {
return nil, err
}
return resp, nil
}
// Post is the method to constructs and sends a Post request. Parameters are
// the same with direwolf.Get()
//
// Note: direwolf.Body can`t existed with direwolf.PostForm.
func Post(URL string, args ...RequestOption) (*Response, error) {
req, err := NewRequest("POST", URL, args...)
if err != nil {
return nil, err
}
resp, err := Send(req)
if err != nil {
return nil, err
}
return resp, nil
}
// Head is the method to constructs and sends a Head request. Parameters are
// the same with direwolf.Get()
func Head(URL string, args ...RequestOption) (*Response, error) {
req, err := NewRequest("HEAD", URL, args...)
if err != nil {
return nil, err
}
resp, err := Send(req)
if err != nil {
return nil, err
}
return resp, nil
}
// Put is the method to constructs and sends a Put request. Parameters are
// the same with direwolf.Get()
func Put(URL string, args ...RequestOption) (*Response, error) {
req, err := NewRequest("Put", URL, args...)
if err != nil {
return nil, err
}
resp, err := Send(req)
if err != nil {
return nil, err
}
return resp, nil
}
// Patch is the method to constructs and sends a Patch request. Parameters are
// the same with direwolf.Get()
func Patch(URL string, args ...RequestOption) (*Response, error) {
req, err := NewRequest("Patch", URL, args...)
if err != nil {
return nil, err
}
resp, err := Send(req)
if err != nil {
return nil, err
}
return resp, nil
}
// Delete is the method to constructs and sends a Delete request. Parameters are
// the same with direwolf.Get()
func Delete(URL string, args ...RequestOption) (*Response, error) {
req, err := NewRequest("Delete", URL, args...)
if err != nil {
return nil, err
}
resp, err := Send(req)
if err != nil {
return nil, err
}
return resp, nil
}