forked from erinpentecost/gou
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
205 lines (189 loc) · 4.53 KB
/
http.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package gou
import (
"bytes"
"encoding/json"
"io"
"io/ioutil"
"net/http"
"net/url"
)
// Simple Fetch Wrapper, given a url it returns bytes
func Fetch(url string) (ret []byte, err error) {
resp, err := http.Get(url)
defer func() {
if resp != nil && resp.Body != nil {
resp.Body.Close()
}
}()
if err != nil {
Log(WARN, err.Error())
return
}
ret, err = ioutil.ReadAll(resp.Body)
if err != nil {
return
}
return
}
// Simple Fetch Wrapper, given a url it returns bytes and response
func FetchResp(url string) (ret []byte, err error, resp *http.Response) {
resp, err = http.Get(url)
defer func() {
if resp != nil && resp.Body != nil {
resp.Body.Close()
}
}()
if err != nil {
Log(WARN, err.Error())
}
if resp == nil || resp.Body == nil {
return
}
ret, err = ioutil.ReadAll(resp.Body)
return
}
// Simple Fetch Wrapper, given a url it returns Helper, error
// Sends as type application/json, interprets whatever datatype is sent in appropriately
func JsonHelperHttp(method, urlStr string, data interface{}) (JsonHelper, error) {
var body io.Reader
if data != nil {
switch val := data.(type) {
case string:
body = bytes.NewReader([]byte(val))
case io.Reader:
body = val
case url.Values:
body = bytes.NewReader([]byte(val.Encode()))
default:
by, err := json.Marshal(data)
if err != nil {
return nil, err
}
body = bytes.NewReader(by)
}
}
req, err := http.NewRequest(method, urlStr, body)
if err != nil {
return nil, err
}
req.Header.Add("Accept", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
jh, err := NewJsonHelperReader(resp.Body)
return jh, err
}
// posts an application/json to url with body
// ie: type = application/json
func PostJson(postUrl string, data interface{}) (ret string, err error, resp *http.Response) {
var buf io.Reader
if data != nil {
switch val := data.(type) {
case string:
buf = bytes.NewBufferString(val)
case []byte:
buf = bytes.NewReader(val)
case json.RawMessage:
buf = bytes.NewReader([]byte(val))
case io.Reader:
buf = val
case url.Values:
buf = bytes.NewBufferString(val.Encode())
default:
by, err := json.Marshal(data)
if err != nil {
return "", err, nil
}
buf = bytes.NewReader(by)
}
}
resp, err = http.Post(postUrl, "application/json", buf)
defer func() {
if resp != nil && resp.Body != nil {
resp.Body.Close()
}
}()
if err != nil {
Log(WARN, err.Error())
return "", err, resp
}
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err, resp
}
return string(bodyBytes), nil, resp
}
// issues http delete an application/json to url with body
func DeleteJson(url, body string) (ret string, err error, resp *http.Response) {
//Post(url string, bodyType string, body io.Reader)
buf := bytes.NewBufferString(body)
Debug(buf.Len())
req, err := http.NewRequest("DELETE", url, buf)
if err != nil {
Debug(err)
return
}
req.Header.Add("Content-Type", "application/json")
resp, err = http.DefaultClient.Do(req) //(url, "application/json", buf)
defer func() {
if resp != nil && resp.Body != nil {
resp.Body.Close()
}
}()
if err != nil {
Log(WARN, err.Error())
return "", err, resp
}
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err, resp
}
return string(data), nil, resp
}
// posts a www-form encoded form to url with body
func PostForm(url, body string) (ret string, err error, resp *http.Response) {
//Post(url string, bodyType string, body io.Reader)
buf := bytes.NewBufferString(body)
resp, err = http.Post(url, "application/x-www-form-urlencoded", buf)
defer func() {
if resp != nil && resp.Body != nil {
resp.Body.Close()
}
}()
if err != nil {
Log(WARN, url, " ", body, " ", err.Error())
return "", err, resp
}
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err, resp
}
return string(data), nil, resp
}
// issues http put an application/json to url with optional body
func PutJson(url, body string) (ret string, err error, resp *http.Response) {
buf := bytes.NewBufferString(body)
req, err := http.NewRequest("PUT", url, buf)
if err != nil {
Debug(err)
return
}
req.Header.Add("Content-Type", "application/json")
resp, err = http.DefaultClient.Do(req)
defer func() {
if resp != nil && resp.Body != nil {
resp.Body.Close()
}
}()
if err != nil {
Log(WARN, err.Error())
return "", err, resp
}
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err, resp
}
return string(data), nil, resp
}