forked from docker/machine
/
request.go
65 lines (53 loc) · 1.28 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
package egoscale
import (
"crypto/hmac"
"crypto/sha1"
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"net/url"
"strings"
)
func rawValue(b json.RawMessage) (json.RawMessage, error) {
var m map[string]json.RawMessage
if err := json.Unmarshal(b, &m); err != nil {
return nil, err
}
for _, v := range m {
return v, nil
}
return nil, fmt.Errorf("Unable to extract raw value from:\n\n%s\n\n", string(b))
}
func (exo *Client) Request(command string, params url.Values) (json.RawMessage, error) {
mac := hmac.New(sha1.New, []byte(exo.apiSecret))
params.Set("apikey", exo.apiKey)
params.Set("command", command)
params.Set("response", "json")
s := strings.Replace(strings.ToLower(params.Encode()), "+", "%20", -1)
mac.Write([]byte(s))
signature := url.QueryEscape(base64.StdEncoding.EncodeToString(mac.Sum(nil)))
s = params.Encode()
url := exo.endpoint + "?" + s + "&signature=" + signature
resp, err := exo.client.Get(url)
if err != nil {
return nil, err
}
b, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
return nil, err
}
b, err = rawValue(b)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
var e Error
if err := json.Unmarshal(b, &e); err != nil {
return nil, err
}
return nil, e.Error()
}
return b, nil
}