-
Notifications
You must be signed in to change notification settings - Fork 204
/
http_helpers.go
57 lines (46 loc) · 1.5 KB
/
http_helpers.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
package synchronization_server
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
func PerformPost(client *http.Client, url string, request, response interface{}) error {
reqBodyData, err := json.Marshal(request)
if err != nil {
return fmt.Errorf("unable to marshal request data: %w", err)
}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(reqBodyData))
if err != nil {
return fmt.Errorf("unable to create POST request for %q: %w", url, err)
}
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("error requesting url %q: %w", url, err)
}
defer resp.Body.Close()
respBodyData, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("error reading response of %q request: %w", url, err)
}
if resp.StatusCode != 200 {
return fmt.Errorf("got bad response %s by url %q request:\n%s", resp.Status, url, string(respBodyData))
}
if err := json.Unmarshal(respBodyData, response); err != nil {
return fmt.Errorf("unable to unmarshal json body by url %q request: %w", url, err)
}
return nil
}
func HandleRequest(w http.ResponseWriter, r *http.Request, request, response interface{}, actionFunc func()) {
if r.Method == "POST" {
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
http.Error(w, fmt.Sprintf("unable to unmarshal request json: %s", err), http.StatusBadRequest)
return
}
}
actionFunc()
if err := json.NewEncoder(w).Encode(response); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}