~mattfarina/golang-client/master

« back to all changes in this revision

Viewing changes to misc/util.go

  • Committer: Chris Robinson
  • Date: 2014-10-27 21:09:26 UTC
  • Revision ID: git-v1:219cc1c2c1abac86d34faf5b1810478393af74cd
Added Capability to get images and images with details

        new file:   examples/30-image-v1.go
        modified:   examples/config.json.dist
        modified:   examples/setup.go
        modified:   identity/v2/auth.go
        new file:   image/v1/image.go
        new file:   image/v1/image_test.go
        new file:   misc/rfc8601DateTime.go
        new file:   misc/rfc8601DateTime_test.go
        modified:   misc/util.go
        modified:   misc/util_test.go
        modified:   objectstorage/v1/objectstorage.go
        modified:   objectstorage/v1/objectstorage_test.go
        new file:   testUtil/testUtil.go

Partially implements blueprint image-v1

Change-Id: I6277a5c8915f45f4b7585855d836015ffd9e1c12

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
import (
18
18
        "bytes"
 
19
        "encoding/json"
19
20
        "errors"
 
21
        "fmt"
20
22
        "io"
21
23
        "net/http"
22
24
)
23
25
 
 
26
var zeroByte = new([]byte) //pointer to empty []byte
 
27
 
 
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)
 
37
        if err != nil {
 
38
                return err
 
39
        }
 
40
 
 
41
        req, err := http.NewRequest("POST", url, bytes.NewBuffer(body))
 
42
        if err != nil {
 
43
                return err
 
44
        }
 
45
 
 
46
        req.Header.Set("Content-Type", "application/json")
 
47
        req.Header.Set("Accept", "application/json")
 
48
        req.Header.Set("X-Auth-Token", token)
 
49
 
 
50
        resp, err := client.Do(req)
 
51
        if err != nil {
 
52
                return err
 
53
        }
 
54
 
 
55
        if resp.StatusCode != 201 && resp.StatusCode != 202 {
 
56
                err = errors.New("Error: status code != 201 or 202, actual status code '" + resp.Status + "'")
 
57
                return
 
58
        }
 
59
 
 
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 + "'")
 
63
                return
 
64
        }
 
65
 
 
66
        err = json.NewDecoder(resp.Body).Decode(&outputValue)
 
67
        defer resp.Body.Close()
 
68
        if err != nil {
 
69
                return err
 
70
        }
 
71
 
 
72
        return nil
 
73
}
 
74
 
 
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)
 
80
        if err != nil {
 
81
                return err
 
82
        }
 
83
 
 
84
        req.Header.Set("X-Auth-Token", token)
 
85
 
 
86
        resp, err := client.Do(req)
 
87
        if err != nil {
 
88
                return err
 
89
        }
 
90
 
 
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)
 
94
                return
 
95
        }
 
96
 
 
97
        return nil
 
98
}
 
99
 
 
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)
 
107
        if err != nil {
 
108
                return err
 
109
        }
 
110
 
 
111
        err = executeRequestCheckStatusDecodeJSONResponse(client, req, val)
 
112
        if err != nil {
 
113
                return err
 
114
        }
 
115
 
 
116
        return nil
 
117
}
 
118
 
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
71
166
        return nil
72
167
}
73
168
 
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:
81
176
                return nil
108
203
        }
109
204
        return errors.New("Error: unexpected response status code")
110
205
}
 
206
 
 
207
func createJSONGetRequest(url string, token string) (req *http.Request, err error) {
 
208
        req, err = http.NewRequest("GET", url, nil)
 
209
        if err != nil {
 
210
                return nil, err
 
211
        }
 
212
 
 
213
        req.Header.Set("Accept", "application/json")
 
214
        req.Header.Set("X-Auth-Token", token)
 
215
 
 
216
        return req, nil
 
217
}
 
218
 
 
219
func executeRequestCheckStatusDecodeJSONResponse(client http.Client, req *http.Request, val interface{}) (err error) {
 
220
        resp, err := client.Do(req)
 
221
        if err != nil {
 
222
                return err
 
223
        }
 
224
 
 
225
        err = CheckHTTPResponseStatusCode(resp)
 
226
        if err != nil {
 
227
                return err
 
228
        }
 
229
 
 
230
        err = json.NewDecoder(resp.Body).Decode(&val)
 
231
        defer resp.Body.Close()
 
232
 
 
233
        return err
 
234
}