-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.go
182 lines (172 loc) · 4.47 KB
/
api.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package vultr
import (
"fmt"
"io/ioutil"
"net/http"
"net/url"
"encoding/json"
)
import . "github.com/visionmedia/go-debug"
var debug = Debug("Api")
type Client struct {
APIKey string
URL string
Http *http.Client
Params Parameters
}
var returncodes = map[int]string {
200 : "OK",
400 : "Invalid location (URL)",
403 : "Invalid or missing API key",
405 : "Invalid HTTP Method",
500 : "Internal server error",
412 : "ERROR",
}
func MakeClient(apikey string) (*Client, error) {
// If it exists, grab teh token from the environment
if apikey == "" {
return nil,fmt.Errorf("no API key provided to vultr API")
}
client := Client{
APIKey: apikey,
Http: http.DefaultClient,
URL: "https://api.vultr.com/v1",
}
return &client,nil
}
func NewClient(apikey string) (*Client, error) {
client,err := MakeClient(apikey)
if err != nil {
return nil,err
}
err = client.initParams()
if err != nil {
return nil,err
}
return client,nil
}
func (c *Client) initParams() (error) {
params,err := NewParameters(c)
if err != nil {
return err
}
c.Params = params
return nil
}
// send a request including api key
func (c *Client) RequestByte(params map[string]string, action string,method string) ([]byte, error) {
p := url.Values{}
p.Add("api_key",c.APIKey)
// even on post we need api_key in the url
url := ""
if method == "POST" {
url = c.URL + action + "?" + p.Encode()
}
// Build up our request parameters
for k, v := range params {
p.Add(k, v)
}
// Add the params to our URL
if method == "GET" {
url = c.URL + action + "?" + p.Encode()
}
var body []byte
var err error
debug("will hit url %s\n",url)
if method == "GET" {
body, err = c.doReq(c.Http.Get(url))
} else {
body, err = c.doReq(c.Http.PostForm(url,p))
}
if err != nil {
return nil,err
}
return body, nil
}
// create a new request and returns body as string
func (c *Client) RequestStr(params map[string]string, action string,method string) (string, error) {
data,err := c.RequestByte(params,action,method)
return string(data),err
}
func (c *Client) RequestInterface(params map[string]string, action string,method string,out interface{}) (error) {
data,err := c.RequestByte(params,action,method)
if err != nil {
return err
}
err = json.Unmarshal(data, out)
if err != nil {
panic(fmt.Sprintf("Error reading json: %s", err))
}
return nil
}
// Create a new request and decodes to Jaons
func (c *Client) RequestMap(params map[string]string, action string,method string) (map[string]interface{}, error) {
var f interface{}
err := c.RequestInterface(params,action,method,&f)
if err != nil {
return nil,err
}
switch test := f.(type) {
case []interface{}:
// got array which means empty
return nil,nil
case map[string]interface{}:
return test,nil
default:
// unknown type
panic(fmt.Sprintf("error parsing json"))
}
}
// Create a new request and decodes to Jaons
func (c *Client) RequestArr(params map[string]string, action string,method string) ([]interface{}, error) {
var f interface{}
err := c.RequestInterface(params,action,method,&f)
if err != nil {
return nil,err
}
switch test := f.(type) {
case []interface{}:
// got array
return test,nil
case map[string]interface{}:
panic(fmt.Sprintf("Got map while expecting array in JSON response"))
default:
// unknown type
panic(fmt.Sprintf("error parsing json"))
}
}
// checkResp wraps http.Client.Do() and verifies that the
// request was successful. A non-200 request returns an error
// as per the vultr api documentation
func (c *Client) doReq(resp *http.Response,err error) ([]byte,error) {
// If the err is already there, there was an error higher
// up the chain, so just return that
//resp,err := c.Http.Do(req)
if err != nil {
return nil,fmt.Errorf("Error making request: %s",err)
}
switch i := returncodes[resp.StatusCode]; {
case i == "OK":
body, err := c.getBody(resp)
if err != nil {
return nil,fmt.Errorf("Error reading body %s:", err)
}
return body,nil
case i == "ERROR":
body,err := c.getBody(resp)
if err != nil {
return nil,fmt.Errorf("API Error which could not be read")
}
return nil,fmt.Errorf(string(body))
default:
return nil, fmt.Errorf("API Error: %s", i)
}
}
func (c *Client) getBody(resp *http.Response) ([]byte, error) {
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil,err
}
return body,nil
}