| 1 |
/* |
| 2 |
Copyright 2013-2014 Canonical Ltd. |
| 3 |
|
| 4 |
This program is free software: you can redistribute it and/or modify it |
| 5 |
under the terms of the GNU General Public License version 3, as published |
| 6 |
by the Free Software Foundation. |
| 7 |
|
| 8 |
This program is distributed in the hope that it will be useful, but |
| 9 |
WITHOUT ANY WARRANTY; without even the implied warranties of |
| 10 |
MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR |
| 11 |
PURPOSE. See the GNU General Public License for more details. |
| 12 |
|
| 13 |
You should have received a copy of the GNU General Public License along |
| 14 |
with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
*/ |
| 16 |
|
| 17 |
// Package kit contains reusable building blocks for acceptance. |
| 18 |
package kit |
| 19 |
|
| 20 |
import ( |
| 21 |
"bytes" |
| 22 |
_ "crypto/sha512" // support sha384/512 certs |
| 23 |
"crypto/tls" |
| 24 |
"encoding/json" |
| 25 |
"errors" |
| 26 |
"io" |
| 27 |
"io/ioutil" |
| 28 |
"net/http" |
| 29 |
"net/url" |
| 30 |
) |
| 31 |
|
| 32 |
// APIClient helps making api requests. |
| 33 |
type APIClient struct { |
| 34 |
ServerAPIURL string |
| 35 |
// hook to adjust requests |
| 36 |
MassageRequest func(req *http.Request, message interface{}) *http.Request |
| 37 |
// other state |
| 38 |
httpClient *http.Client |
| 39 |
} |
| 40 |
|
| 41 |
type APIError struct { |
| 42 |
Msg string |
| 43 |
Body []byte |
| 44 |
} |
| 45 |
|
| 46 |
func (e *APIError) Error() string { |
| 47 |
return e.Msg |
| 48 |
} |
| 49 |
|
| 50 |
// SetupClient sets up the http client to make requests. |
| 51 |
func (api *APIClient) SetupClient(tlsConfig *tls.Config, disableKeepAlives bool, maxIdleConnsPerHost int) { |
| 52 |
api.httpClient = &http.Client{ |
| 53 |
Transport: &http.Transport{TLSClientConfig: tlsConfig, |
| 54 |
DisableKeepAlives: disableKeepAlives, |
| 55 |
MaxIdleConnsPerHost: maxIdleConnsPerHost}, |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
var ErrNOk = errors.New("not ok") |
| 60 |
|
| 61 |
func readBody(respBody io.ReadCloser) (map[string]interface{}, error) { |
| 62 |
defer respBody.Close() |
| 63 |
body, err := ioutil.ReadAll(respBody) |
| 64 |
if err != nil { |
| 65 |
return nil, err |
| 66 |
} |
| 67 |
var res map[string]interface{} |
| 68 |
err = json.Unmarshal(body, &res) |
| 69 |
if err != nil { |
| 70 |
return nil, &APIError{err.Error(), body} |
| 71 |
} |
| 72 |
return res, nil |
| 73 |
} |
| 74 |
|
| 75 |
// Post a API request. |
| 76 |
func (api *APIClient) PostRequest(path string, message interface{}) (map[string]interface{}, error) { |
| 77 |
packedMessage, err := json.Marshal(message) |
| 78 |
if err != nil { |
| 79 |
panic(err) |
| 80 |
} |
| 81 |
reader := bytes.NewReader(packedMessage) |
| 82 |
|
| 83 |
url := api.ServerAPIURL + path |
| 84 |
request, _ := http.NewRequest("POST", url, reader) |
| 85 |
request.ContentLength = int64(reader.Len()) |
| 86 |
request.Header.Set("Content-Type", "application/json") |
| 87 |
|
| 88 |
if api.MassageRequest != nil { |
| 89 |
request = api.MassageRequest(request, message) |
| 90 |
} |
| 91 |
|
| 92 |
resp, err := api.httpClient.Do(request) |
| 93 |
if err != nil { |
| 94 |
return nil, err |
| 95 |
} |
| 96 |
res, err := readBody(resp.Body) |
| 97 |
if err != nil { |
| 98 |
return nil, err |
| 99 |
} |
| 100 |
|
| 101 |
if ok, _ := res["ok"].(bool); !ok { |
| 102 |
return res, ErrNOk |
| 103 |
} |
| 104 |
return res, nil |
| 105 |
} |
| 106 |
|
| 107 |
// Get resource from API endpoint. |
| 108 |
func (api *APIClient) GetRequest(path string, params map[string]string) (map[string]interface{}, error) { |
| 109 |
apiURL := api.ServerAPIURL + path |
| 110 |
if len(params) != 0 { |
| 111 |
vals := url.Values{} |
| 112 |
for k, v := range params { |
| 113 |
vals.Set(k, v) |
| 114 |
} |
| 115 |
apiURL += "?" + vals.Encode() |
| 116 |
} |
| 117 |
request, _ := http.NewRequest("GET", apiURL, nil) |
| 118 |
|
| 119 |
resp, err := api.httpClient.Do(request) |
| 120 |
if err != nil { |
| 121 |
return nil, err |
| 122 |
} |
| 123 |
res, err := readBody(resp.Body) |
| 124 |
if err != nil { |
| 125 |
return nil, err |
| 126 |
} |
| 127 |
return res, nil |
| 128 |
} |