-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
171 lines (151 loc) · 3.83 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
package dogo
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"os"
)
// Params are to be used in conjuction with POST or PUT.
type Params map[string]interface{}
// Client is a client to the DigitalOcean API service
type Client struct {
// DO Access Token
Token string
// Base DO API URL
URL string
}
// NewClient creates a new Client.
func NewClient(token string) (*Client, error) {
if token == "" {
token = os.Getenv("DIGITALOCEAN_TOKEN")
}
if token == "" {
return nil, ErrEnv
}
cl := &Client{
Token: token,
URL: "https://api.digitalocean.com/v2",
}
return cl, nil
}
func (c *Client) get(endpoint string, v interface{}) error {
endpoint = fmt.Sprintf("%s/%s", c.URL, endpoint)
req, err := http.NewRequest("GET", endpoint, nil)
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
err = c.DoRequest(req, v)
if err != nil {
return err
}
return nil
}
func (c *Client) delete(endpoint string) error {
endpoint = fmt.Sprintf("%s/%s", c.URL, endpoint)
req, err := http.NewRequest("DELETE", endpoint, nil)
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
err = c.DoRequest(req, nil)
if err != nil {
return err
}
return nil
}
func (c *Client) post(endpoint string, opts interface{}, v interface{}) error {
endpoint = fmt.Sprintf("%s/%s", c.URL, endpoint)
payload, err := json.Marshal(opts)
if err != nil {
return err
}
req, err := http.NewRequest("POST", endpoint, bytes.NewReader(payload))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
c.DoRequest(req, v)
if err != nil {
return err
}
return nil
}
func (c *Client) put(endpoint string, opts interface{}, v interface{}) error {
endpoint = fmt.Sprintf("%s/%s", c.URL, endpoint)
payload, err := json.Marshal(opts)
if err != nil {
return err
}
req, err := http.NewRequest("PUT", endpoint, bytes.NewReader(payload))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
c.DoRequest(req, v)
if err != nil {
return err
}
return nil
}
// DoRequest sends the request over to the DigitalOcean API servers.
func (c *Client) DoRequest(req *http.Request, v interface{}) error {
cl := &http.Client{}
req.Header.Set("Authorization", "Bearer "+c.Token)
resp, err := cl.Do(req)
if err != nil {
return fmt.Errorf("dogo: Error attemping request: %s", err)
}
err = decode(resp, v)
if err != nil {
return err
}
return nil
}
// Decode parses the response.
func decode(resp *http.Response, v interface{}) error {
// create error
if resp.StatusCode < 200 || resp.StatusCode >= 400 {
apiErr := &APIError{
StatusCode: resp.StatusCode,
}
err := json.NewDecoder(resp.Body).Decode(apiErr)
if err != nil {
return fmt.Errorf("dogo: Error UnMarshaling JSON Response into error: %s", err)
}
return apiErr
}
if v != nil {
err := json.NewDecoder(resp.Body).Decode(&v)
if err != nil {
return fmt.Errorf("dogo: Error UnMarshaling JSON Response into struct: %s", err)
}
}
return nil
}
// DoAction performs an action on an endpoint resource with the passed id,
// type of action and its required params are described in the DigitalOcean API.
//
// For example in this case the image resource.
//
// https://developers.digitalocean.com/v2/#image-actions
//
// An example of some params:
// params := digitalocean.Params{
// "type": "transfer",
// "region": "nyc2",
// }
//
// The above example specifies the type of action, in this case resizing and
// the additional param in this case the size to resize to "1024mb".
//
// Params will sometimes only require the type of action and no additional params.
func (c *Client) DoAction(endpoint string, id int, params Params) error {
u := fmt.Sprintf("%s/%d/actions", endpoint, id)
err := c.post(u, params, nil)
if err != nil {
return err
}
return nil
}