26
var zeroByte = new([]byte) //pointer to empty []byte
28
// PostJSON sends an Http Request with using the "POST" method and with
29
// a "Content-Type" header with application/json and X-Auth-Token" header
30
// set to the specified token value. The inputValue is encoded to json
31
// and sent in the body of the request. The response json body is
32
// decoded into the outputValue. If the response does sends an invalid
33
// or error status code then an error will be returned. If the Content-Type
34
// value of the response is not "application/json" an error is returned.
35
func PostJSON(url string, token string, client http.Client, inputValue interface{}, outputValue interface{}) (err error) {
36
body, err := json.Marshal(inputValue)
41
req, err := http.NewRequest("POST", url, bytes.NewBuffer(body))
46
req.Header.Set("Content-Type", "application/json")
47
req.Header.Set("Accept", "application/json")
48
req.Header.Set("X-Auth-Token", token)
50
resp, err := client.Do(req)
55
if resp.StatusCode != 201 && resp.StatusCode != 202 {
56
err = errors.New("Error: status code != 201 or 202, actual status code '" + resp.Status + "'")
60
contentTypeValue := resp.Header.Get("Content-Type")
61
if contentTypeValue != "application/json" {
62
err = errors.New("Error: Expected a json payload but instead recieved '" + contentTypeValue + "'")
66
err = json.NewDecoder(resp.Body).Decode(&outputValue)
67
defer resp.Body.Close()
75
// Delete sends an Http Request with using the "DELETE" method and with
76
// an "X-Auth-Token" header set to the specified token value. The request
77
// is made by the specified client.
78
func Delete(url string, token string, client http.Client) (err error) {
79
req, err := http.NewRequest("DELETE", url, nil)
84
req.Header.Set("X-Auth-Token", token)
86
resp, err := client.Do(req)
91
// Expecting a successful delete
92
if !(resp.StatusCode == 200 || resp.StatusCode == 202 || resp.StatusCode == 204) {
93
err = fmt.Errorf("Unexpected server response status code on Delete '%s'", resp.StatusCode)
100
//GetJSON sends an Http Request with using the "GET" method and with
101
//an "Accept" header set to "application/json" and the authenication token
102
//set to the specified token value. The request is made by the
103
//specified client. The val interface should be a pointer to the
104
//structure that the json response should be decoded into.
105
func GetJSON(url string, token string, client http.Client, val interface{}) (err error) {
106
req, err := createJSONGetRequest(url, token)
111
err = executeRequestCheckStatusDecodeJSONResponse(client, req, val)
24
119
//CallAPI sends an HTTP request using "method" to "url".
25
120
//For uploading / sending file, caller needs to set the "content". Otherwise,
26
121
//set it to zero length []byte. If Header fields need to be set, then set it in
74
//CheckStatusCode compares http response header StatusCode against expected
75
//statuses. Primary function is to ensure StatusCode is in the 20x (return nil).
76
//Ok: 200. Created: 201. Accepted: 202. No Content: 204.
77
//Otherwise return error message.
78
func CheckHttpResponseStatusCode(resp *http.Response) error {
169
// CheckHTTPResponseStatusCode compares http response header StatusCode against expected
170
// statuses. Primary function is to ensure StatusCode is in the 20x (return nil).
171
// Ok: 200. Created: 201. Accepted: 202. No Content: 204.
172
// Otherwise return error message.
173
func CheckHTTPResponseStatusCode(resp *http.Response) error {
79
174
switch resp.StatusCode {
80
175
case 200, 201, 202, 204:
109
204
return errors.New("Error: unexpected response status code")
207
func createJSONGetRequest(url string, token string) (req *http.Request, err error) {
208
req, err = http.NewRequest("GET", url, nil)
213
req.Header.Set("Accept", "application/json")
214
req.Header.Set("X-Auth-Token", token)
219
func executeRequestCheckStatusDecodeJSONResponse(client http.Client, req *http.Request, val interface{}) (err error) {
220
resp, err := client.Do(req)
225
err = CheckHTTPResponseStatusCode(resp)
230
err = json.NewDecoder(resp.Body).Decode(&val)
231
defer resp.Body.Close()