-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscaleway.go
174 lines (154 loc) · 4.8 KB
/
scaleway.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
package scaleway
import (
"bytes"
"encoding/json"
"io"
"net/http"
"net/url"
"time"
)
const (
libraryVersion = "0.1"
defaultAccountBaseURL = "https://account.scaleway.com/"
defaultComputeBaseURL = "https://api.scaleway.com/"
userAgent = "go-scaleway/" + libraryVersion
contentType = "application/json"
)
// A Client manages communcation with the Scaleway API.
type Client struct {
// HTTP client used to communicate with the API.
client *http.Client
// Base URL related to account actions.
AccountBaseURL *url.URL
// Base URL related to compute actions.
ComputeBaseURL *url.URL
// UserAgent used when communicating with the Scaleway API.
UserAgent string
// AuthToken used when communication with Scaleway API.
AuthToken string
// Services used for talking to Scaleway API.
Tokens *TokensService
Organizations *OrganizationsService
Users *UsersService
Volumes *VolumesService
Snapshots *SnapshotsService
Images *ImagesService
Servers *ServersService
Actions *ActionsService
IPs *IPsService
}
// timeLayout represents the time layout needed for parsing.
const timeLayout = "2006-01-02T15:04:05.999999Z07:00"
// Ntime represents custom time.
type Ntime time.Time
// UnmarshalJSON decodes JSON custom time.
// See: https://github.com/golang/go/issues/9037
func (t *Ntime) UnmarshalJSON(b []byte) error {
if string(b) == "null" {
return nil
}
tm, err := time.Parse(timeLayout, string(b[1:len(b)-1]))
if err != nil {
return err
}
*t = Ntime(tm)
return nil
}
// Response is a Scaleway API Response. This wrap the standard http.Response.
type Response struct {
*http.Response
}
// NewClient returns a new Scaleway API Client. If a nil httpClient
// provided, http.DefaultClient will be used.
func NewClient(httpClient *http.Client) *Client {
if httpClient == nil {
httpClient = http.DefaultClient
}
accountBaseURL, _ := url.Parse(defaultAccountBaseURL)
computeBaseURL, _ := url.Parse(defaultComputeBaseURL)
c := &Client{client: httpClient,
AccountBaseURL: accountBaseURL,
ComputeBaseURL: computeBaseURL,
UserAgent: userAgent}
c.Tokens = &TokensService{client: c}
c.Organizations = &OrganizationsService{client: c}
c.Users = &UsersService{client: c}
c.Volumes = &VolumesService{client: c}
c.Snapshots = &SnapshotsService{client: c}
c.Images = &ImagesService{client: c}
c.Servers = &ServersService{client: c}
c.Actions = &ActionsService{client: c}
c.IPs = &IPsService{client: c}
return c
}
// NewRequestAccount creates an API request. A relative URL can be provided in urlStr,
// in which case it is resolved relative to the BaseURL of the Client.
// Relative URLs should always be specified without a preceding slash. If
// specified, the value pointed to by body is JSON encoded and included as the
// request body.
func (c *Client) NewRequestAccount(method, urlStr string, body interface{}) (*http.Request, error) {
rel, err := url.Parse(urlStr)
if err != nil {
return nil, err
}
u := c.AccountBaseURL.ResolveReference(rel)
return c.newRequest(method, u.String(), body)
}
// NewRequestCompute creates an API request. A relative URL can be provided in urlStr,
// in which case it is resolved relative to the ComputeBaseURL of the Client.
// Relative URLs should always be specified without a preceding slash. If
// specified, the value pointed to by body is JSON encoded and included as the
// request body.
func (c *Client) NewRequestCompute(method, urlStr string, body interface{}) (*http.Request, error) {
rel, err := url.Parse(urlStr)
if err != nil {
return nil, err
}
u := c.ComputeBaseURL.ResolveReference(rel)
return c.newRequest(method, u.String(), body)
}
// newRequest creates http.Request.
func (c *Client) newRequest(method, u string, body interface{}) (*http.Request, error) {
var buf io.ReadWriter
if body != nil {
buf = new(bytes.Buffer)
err := json.NewEncoder(buf).Encode(body)
if err != nil {
return nil, err
}
}
req, err := http.NewRequest(method, u, buf)
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", contentType)
if c.UserAgent != "" {
req.Header.Add("User-Agent", c.UserAgent)
}
if c.AuthToken != "" {
req.Header.Add("X-Auth-Token", c.AuthToken)
}
return req, nil
}
// Do sends an API request and returns the API response.
func (c *Client) Do(req *http.Request, v interface{}) (*Response, error) {
resp, err := c.client.Do(req)
if err != nil {
return nil, err
}
defer func() {
resp.Body.Close()
}()
response := newResponse(resp)
if v != nil {
err = json.NewDecoder(resp.Body).Decode(v)
if err == io.EOF {
err = nil // ignore EOF errors caused by empty response body
}
}
return response, err
}
// newResponse creates a new Response for the provided http.Response.
func newResponse(r *http.Response) *Response {
return &Response{Response: r}
}