-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrest.go
106 lines (100 loc) · 2.96 KB
/
rest.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
package postmaster
import (
"github.com/jmcvetta/restclient"
)
// get makes a HTTP GET request. Parameters must be provided in params.
var get = func(p *Postmaster, version string, endpoint string, params map[string]string, result interface{}) (status int, e error) {
err := new(PostmasterError)
rr := restclient.RequestResponse{
Url: p.makeUrl(version, endpoint),
Userinfo: p.userinfo,
Method: "GET",
Params: params,
Result: result,
Error: &err,
Header: p.headers,
}
status, e = p.client.Do(&rr)
if status >= 300 {
e = err
}
return
}
// put makes a HTTP PUT request. Parameters must be provided in params, and will
// be translated into query string.
var put = func(p *Postmaster, version string, endpoint string, params interface{}, result interface{}) (status int, e error) {
err := new(PostmasterError)
rr := restclient.RequestResponse{
Url: p.makeUrl(version, endpoint),
Userinfo: p.userinfo,
Method: "PUT",
Data: params,
Result: result,
Error: &err,
Header: p.headers,
}
status, e = p.client.Do(&rr)
if status >= 300 {
e = err
}
return
}
// post makes a HTTP POST request. Parameters must be provided in params, and will
// be translated into query string.
var post = func(p *Postmaster, version string, endpoint string, params interface{}, result interface{}) (status int, e error) {
err := new(PostmasterError)
rr := restclient.RequestResponse{
Url: p.makeUrl(version, endpoint),
Userinfo: p.userinfo,
Method: "POST",
Data: params,
Result: result,
Error: &err,
Header: p.headers,
}
status, e = p.client.Do(&rr)
if status >= 300 {
e = err
}
return
}
// postJson makes a HTTP POST request, with parameters as struct and being encoded to JSON.
// Currently the only function that utilizes this is *postmaster.Fit(), but it may change in future.
// Remember that every field of params structure must have a "json" comment, or json.Marshal will
// use its tentacles to make bad things to your data!
var postJson = func(p *Postmaster, version string, endpoint string, params interface{}, result interface{}) (status int, e error) {
err := new(PostmasterError)
rr := restclient.RequestResponse{
Url: p.makeUrl(version, endpoint),
Userinfo: p.userinfo,
Method: "POST",
Data: params,
Result: result,
Error: &err,
Header: p.headers,
}
status, e = p.client.Do(&rr)
if status >= 300 {
e = err
}
return
}
// delete makes a HTTP DELETE request. Parameters must be provided in params, and will
// be translated into query string.
var del = func(p *Postmaster, version string, endpoint string, params interface{}, result interface{}) (status int, e error) {
err := new(PostmasterError)
rr := restclient.RequestResponse{
Url: p.makeUrl(version, endpoint),
Userinfo: p.userinfo,
Method: "DELETE",
Data: params,
Result: result,
Error: &err,
Header: p.headers,
}
status, e = p.client.Do(&rr)
if status >= 300 {
e = err
}
return
}