-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclient.go
221 lines (191 loc) · 5 KB
/
client.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// Package cloudability provides a client for the cloudability api.
package cloudability
import (
"bytes"
"encoding/json"
"errors"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"path"
"time"
)
const (
apiV1URL = "https://app.cloudability.com/api/1"
apiV3URL = "https://api.cloudability.com/v3"
)
// Client - Cloudability client.
type Client struct {
*http.Client
V1BaseURL *url.URL
V3BaseURL *url.URL
UserAgent string
apikey string
}
// NewClient - This constructor creates a Cloudability client.
func NewClient(apikey string) *Client {
c := &Client{
Client: &http.Client{Timeout: 30 * time.Second},
UserAgent: "cloudability-sdk-go",
apikey: apikey,
}
c.V1BaseURL, _ = url.Parse(apiV1URL)
c.V3BaseURL, _ = url.Parse(apiV3URL)
return c
}
// APIError - Cloudability Error
type APIError struct {
Error errorDetail `json:"error"`
}
type errorDetail struct {
Status float64 `json:"status"`
Code []string `json:"code"`
Messages []string `json:"messages"`
}
type endpointI interface {
buildURL(endpoint string) *url.URL
newRequest(method string, u *url.URL, body interface{}) (*http.Request, error)
doRequest(req *http.Request, result interface{}) (*http.Response, error)
}
type endpoint struct {
*Client
BaseURL *url.URL
EndpointPath string
}
type v1Endpoint struct {
*endpoint
}
type v3Endpoint struct {
*endpoint
}
func (c *Client) SetTimeout(d time.Duration) {
c.Timeout = d
}
func newEndpoint(c *Client, baseURL *url.URL, endpointPath string) *endpoint {
e := &endpoint{
Client: c,
BaseURL: baseURL,
EndpointPath: endpointPath,
}
return e
}
func newV1Endpoint(c *Client, endpointPath string) *v1Endpoint {
return &v1Endpoint{newEndpoint(c, c.V1BaseURL, endpointPath)}
}
func newV3Endpoint(c *Client, endpointPath string) *v3Endpoint {
return &v3Endpoint{newEndpoint(c, c.V3BaseURL, endpointPath)}
}
func (e *endpoint) buildURL(endpointPath string) *url.URL {
u, err := url.Parse(endpointPath)
if err != nil {
log.Fatal(err)
}
u.Path = path.Join(e.EndpointPath, u.Path)
u.Path = path.Join(e.BaseURL.Path, u.Path)
return e.BaseURL.ResolveReference(u)
}
type v3Result[T any] struct {
Result T `json:"result"`
}
func (c *Client) get(e endpointI, endpoint string, result interface{}) error {
return c.do(e, "GET", endpoint, nil, result)
}
func (c *Client) post(e endpointI, endpoint string, body interface{}, result interface{}) error {
return c.do(e, "POST", endpoint, body, result)
}
func (c *Client) put(e endpointI, endpoint string, body interface{}) error {
return c.do(e, "PUT", endpoint, body, nil)
}
func (c *Client) delete(e endpointI, endpoint string) error {
return c.do(e, "DELETE", endpoint, nil, nil)
}
func (c *Client) do(e endpointI, method string, path string, body interface{}, result interface{}) error {
u := e.buildURL(path)
req, err := e.newRequest(method, u, body)
if err != nil {
return err
}
resp, err := e.doRequest(req, result)
if err == nil {
defer resp.Body.Close()
}
return err
}
func (c *Client) doRequest(req *http.Request, result interface{}) (*http.Response, error) {
resp, err := c.Do(req)
if err != nil {
return nil, err
}
if !(resp.StatusCode == http.StatusOK || resp.StatusCode == http.StatusCreated) {
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return nil, errors.New(string(bodyBytes))
}
if result != nil {
err = json.NewDecoder(resp.Body).Decode(result)
if err != nil {
log.Fatal(err)
}
}
return resp, err
}
func (c *Client) doRaw(e endpointI, method string, path string) (string, error) {
u := e.buildURL(path)
req, err := e.newRequest(method, u, nil)
if err != nil {
return "", err
}
resp, err := c.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
if !(resp.StatusCode == http.StatusOK || resp.StatusCode == http.StatusCreated) {
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
return "", errors.New(string(bodyBytes))
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(body), nil
}
func (c *Client) newRequest(method string, u *url.URL, 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.String(), buf)
if err != nil {
return nil, err
}
if body != nil {
req.Header.Set("Content-Type", "application/json")
}
req.Header.Set("Accept", "application/json")
req.Header.Set("User-Agent", c.UserAgent)
return req, nil
}
func (e *v1Endpoint) newRequest(method string, u *url.URL, body interface{}) (*http.Request, error) {
req, err := e.Client.newRequest(method, u, body)
q := req.URL.Query()
q.Add("auth_token", e.apikey)
req.URL.RawQuery = q.Encode()
return req, err
}
func (e *v3Endpoint) newRequest(method string, u *url.URL, body interface{}) (*http.Request, error) {
req, err := e.Client.newRequest(method, u, body)
req.SetBasicAuth(e.apikey, "")
return req, err
}