Commit 8a81bda58b6aadf3955ead9e59eeecb0bec0a341
1 parent
6faf94f857
Exists in
master
Moved Get/Post JSON to seperate file
Showing
3 changed files
with
107 additions
and
70 deletions
Show diff stats
diff.go
... | ... | @@ -30,3 +30,34 @@ func Diff(old, new []int64) (added, removed []int64) { |
30 | 30 | |
31 | 31 | return added, removed |
32 | 32 | } |
33 | + | |
34 | +// DiffString ... | |
35 | +func DiffString(old, new []string) (added, removed []string) { | |
36 | + for i := range old { | |
37 | + isRemoved := true | |
38 | + for j := range new { | |
39 | + if old[i] == new[j] { | |
40 | + isRemoved = false | |
41 | + break | |
42 | + } | |
43 | + } | |
44 | + if isRemoved { | |
45 | + removed = append(removed, old[i]) | |
46 | + } | |
47 | + } | |
48 | + | |
49 | + for i := range new { | |
50 | + isAdded := true | |
51 | + for j := range old { | |
52 | + if new[i] == old[j] { | |
53 | + isAdded = false | |
54 | + break | |
55 | + } | |
56 | + } | |
57 | + if isAdded { | |
58 | + added = append(added, new[i]) | |
59 | + } | |
60 | + } | |
61 | + | |
62 | + return added, removed | |
63 | +} | ... | ... |
http.go
1 | 1 | package webutility |
2 | 2 | |
3 | 3 | import ( |
4 | - "bytes" | |
5 | 4 | "encoding/json" |
6 | 5 | "fmt" |
7 | 6 | "io" |
8 | 7 | "net/http" |
9 | - "net/url" | |
10 | 8 | ) |
11 | 9 | |
12 | 10 | // StatusRecorder ... |
... | ... | @@ -158,71 +156,3 @@ func InternalServerError(w http.ResponseWriter, r *http.Request, err string) { |
158 | 156 | func DecodeJSON(r io.Reader, v interface{}) error { |
159 | 157 | return json.NewDecoder(r).Decode(v) |
160 | 158 | } |
161 | - | |
162 | -func GetJSON(url string, v interface{}, params url.Values, headers http.Header) (status int, err error) { | |
163 | - p := params.Encode() | |
164 | - if p != "" { | |
165 | - url += "?" + p | |
166 | - } | |
167 | - | |
168 | - req, err := http.NewRequest(http.MethodGet, url, nil) | |
169 | - if err != nil { | |
170 | - return 0, err | |
171 | - } | |
172 | - | |
173 | - if headers != nil { | |
174 | - for k, head := range headers { | |
175 | - for i, h := range head { | |
176 | - if i == 0 { | |
177 | - req.Header.Set(k, h) | |
178 | - } else { | |
179 | - req.Header.Add(k, h) | |
180 | - } | |
181 | - } | |
182 | - } | |
183 | - } | |
184 | - | |
185 | - resp, err := http.DefaultClient.Do(req) | |
186 | - if err != nil { | |
187 | - return 0, err | |
188 | - } | |
189 | - status = resp.StatusCode | |
190 | - | |
191 | - return status, DecodeJSON(resp.Body, v) | |
192 | -} | |
193 | - | |
194 | -func PostJSON(url string, v, r interface{}, params url.Values, headers http.Header) (status int, err error) { | |
195 | - buffer := bytes.NewBuffer(make([]byte, 0)) | |
196 | - json.NewEncoder(buffer).Encode(v) | |
197 | - | |
198 | - p := params.Encode() | |
199 | - if p != "" { | |
200 | - url += "?" + p | |
201 | - } | |
202 | - | |
203 | - req, err := http.NewRequest(http.MethodPost, url, buffer) | |
204 | - if err != nil { | |
205 | - return 0, err | |
206 | - } | |
207 | - | |
208 | - if headers != nil { | |
209 | - for k, head := range headers { | |
210 | - for i, h := range head { | |
211 | - if i == 0 { | |
212 | - req.Header.Set(k, h) | |
213 | - } else { | |
214 | - req.Header.Add(k, h) | |
215 | - } | |
216 | - } | |
217 | - } | |
218 | - } | |
219 | - req.Header.Set("Content-Type", "application/json") | |
220 | - | |
221 | - resp, err := http.DefaultClient.Do(req) | |
222 | - if err != nil { | |
223 | - return 0, err | |
224 | - } | |
225 | - status = resp.StatusCode | |
226 | - | |
227 | - return status, DecodeJSON(resp.Body, r) | |
228 | -} | ... | ... |
json.go
... | ... | @@ -0,0 +1,76 @@ |
1 | +package webutility | |
2 | + | |
3 | +import ( | |
4 | + "bytes" | |
5 | + "encoding/json" | |
6 | + "net/http" | |
7 | + "net/url" | |
8 | +) | |
9 | + | |
10 | +func GetJSON(url string, v interface{}, params url.Values, headers http.Header) (status int, err error) { | |
11 | + p := params.Encode() | |
12 | + if p != "" { | |
13 | + url += "?" + p | |
14 | + } | |
15 | + | |
16 | + req, err := http.NewRequest(http.MethodGet, url, nil) | |
17 | + if err != nil { | |
18 | + return 0, err | |
19 | + } | |
20 | + | |
21 | + if headers != nil { | |
22 | + for k, head := range headers { | |
23 | + for i, h := range head { | |
24 | + if i == 0 { | |
25 | + req.Header.Set(k, h) | |
26 | + } else { | |
27 | + req.Header.Add(k, h) | |
28 | + } | |
29 | + } | |
30 | + } | |
31 | + } | |
32 | + | |
33 | + resp, err := http.DefaultClient.Do(req) | |
34 | + if err != nil { | |
35 | + return 0, err | |
36 | + } | |
37 | + status = resp.StatusCode | |
38 | + | |
39 | + return status, DecodeJSON(resp.Body, v) | |
40 | +} | |
41 | + | |
42 | +func PostJSON(url string, v, r interface{}, params url.Values, headers http.Header) (status int, err error) { | |
43 | + buffer := bytes.NewBuffer(make([]byte, 0)) | |
44 | + json.NewEncoder(buffer).Encode(v) | |
45 | + | |
46 | + p := params.Encode() | |
47 | + if p != "" { | |
48 | + url += "?" + p | |
49 | + } | |
50 | + | |
51 | + req, err := http.NewRequest(http.MethodPost, url, buffer) | |
52 | + if err != nil { | |
53 | + return 0, err | |
54 | + } | |
55 | + | |
56 | + if headers != nil { | |
57 | + for k, head := range headers { | |
58 | + for i, h := range head { | |
59 | + if i == 0 { | |
60 | + req.Header.Set(k, h) | |
61 | + } else { | |
62 | + req.Header.Add(k, h) | |
63 | + } | |
64 | + } | |
65 | + } | |
66 | + } | |
67 | + req.Header.Set("Content-Type", "application/json") | |
68 | + | |
69 | + resp, err := http.DefaultClient.Do(req) | |
70 | + if err != nil { | |
71 | + return 0, err | |
72 | + } | |
73 | + status = resp.StatusCode | |
74 | + | |
75 | + return status, DecodeJSON(resp.Body, r) | |
76 | +} | ... | ... |