-
Notifications
You must be signed in to change notification settings - Fork 1
/
request.go
74 lines (59 loc) · 2.17 KB
/
request.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
package gobs
import (
"encoding/json"
"net/http"
"bytes"
)
type request struct {
method string
url string
filter getFilter
data interface{}
expectedCode int
}
var client *http.Client = &http.Client{}
func createRequest(url string, dataObject interface{}, filter getFilter, expectedCode int) (byteResponse []byte, err error) {
request := request{url: url, expectedCode: expectedCode, method: "POST", data: dataObject, filter: filter}
return processRequest(request)
}
func readRequest(url string, filter getFilter, expectedCode int) (byteResponse []byte, err error) {
request := request{url: url, expectedCode: expectedCode, method: "GET", data: nil, filter: filter}
return processRequest(request)
}
func removeRequest(url string, filter getFilter, expectedCode int) (byteResponse []byte, err error) {
request := request{url: url, expectedCode: expectedCode, method: "DELETE", data: nil, filter: filter}
return processRequest(request)
}
func updateRequest(url string, updateExpression interface{}, filter getFilter, expectedCode int) (byteResponse []byte, err error) {
request := request{url: url, expectedCode: expectedCode, method: "PUT", data: updateExpression, filter: filter}
return processRequest(request)
}
func processRequest(request request) (body []byte, err error) {
byteBody, err := json.Marshal(request.data)
if err != nil {
return nil, err
}
req, err := http.NewRequest(request.method, request.url, bytes.NewReader(byteBody))
if err != nil {
return nil, err
}
if request.filter != nil {
filterMap := request.filter.getFilter()
filterObject, _ := json.Marshal(filterMap)
req.Header.Add("X-Everlive-Filter", string(filterObject))
}
req.Header.Add("Content-type", "application/json")
response, error := client.Do(req)
if error != nil {
return nil, error
}
if response.StatusCode != request.expectedCode {
gobsError := gobsErrorFromHttpRequest(response)
return nil, gobsError
}
body, requestError := getResponseBody(response)
if requestError != nil {
return nil, requestError
}
return body, nil
}