-
Notifications
You must be signed in to change notification settings - Fork 14
/
client.go
175 lines (145 loc) · 4.07 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
package glesys
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strings"
)
const version = "8.4.0"
type httpClientInterface interface {
Do(*http.Request) (*http.Response, error)
}
type clientInterface interface {
get(ctx context.Context, path string, v interface{}) error
post(ctx context.Context, path string, v interface{}, params interface{}) error
}
// Client is used to interact with the GleSYS API
type Client struct {
apiKey string
BaseURL *url.URL
httpClient httpClientInterface
project string
userAgent string
DNSDomains *DNSDomainService
EmailDomains *EmailDomainService
IPs *IPService
LoadBalancers *LoadBalancerService
ObjectStorages *ObjectStorageService
PrivateNetworks *PrivateNetworkService
Servers *ServerService
ServerDisks *ServerDisksService
Networks *NetworkService
NetworkAdapters *NetworkAdapterService
NetworkCircuits *NetworkCircuitService
}
// NewClient creates a new Client for interacting with the GleSYS API. This is
// the main entrypoint for API interactions.
func NewClient(project, apiKey, userAgent string) *Client {
BaseURL, _ := url.Parse("https://api.glesys.com")
c := &Client{
apiKey: apiKey,
BaseURL: BaseURL,
httpClient: http.DefaultClient,
project: project,
userAgent: userAgent,
}
c.DNSDomains = &DNSDomainService{client: c}
c.EmailDomains = &EmailDomainService{client: c}
c.IPs = &IPService{client: c}
c.LoadBalancers = &LoadBalancerService{client: c}
c.ObjectStorages = &ObjectStorageService{client: c}
c.PrivateNetworks = &PrivateNetworkService{client: c}
c.Servers = &ServerService{client: c}
c.ServerDisks = &ServerDisksService{client: c}
c.Networks = &NetworkService{client: c}
c.NetworkAdapters = &NetworkAdapterService{client: c}
c.NetworkCircuits = &NetworkCircuitService{client: c}
return c
}
// SetBaseURL can be used to set a custom BaseURL
func (c *Client) SetBaseURL(bu string) error {
url, err := url.Parse(bu)
if err != nil {
return err
}
c.BaseURL = url
return nil
}
func (c *Client) get(ctx context.Context, path string, v interface{}) error {
request, err := c.newRequest(ctx, "GET", path, nil)
if err != nil {
return err
}
return c.do(request, v)
}
func (c *Client) post(ctx context.Context, path string, v interface{}, params interface{}) error {
request, err := c.newRequest(ctx, "POST", path, params)
if err != nil {
return err
}
return c.do(request, v)
}
func (c *Client) newRequest(ctx context.Context, method, path string, params interface{}) (*http.Request, error) {
u, err := url.Parse(path)
if err != nil {
return nil, err
}
if c.BaseURL != nil {
u = c.BaseURL.ResolveReference(u)
}
buffer := new(bytes.Buffer)
if params != nil {
err = json.NewEncoder(buffer).Encode(params)
if err != nil {
return nil, err
}
}
request, err := http.NewRequestWithContext(ctx, method, u.String(), buffer)
if err != nil {
return nil, err
}
userAgent := strings.TrimSpace(fmt.Sprintf("%s glesys-go/%s", c.userAgent, version))
request.Header.Set("Content-Type", "application/json")
request.Header.Set("User-Agent", userAgent)
request.SetBasicAuth(c.project, c.apiKey)
return request, nil
}
func (c *Client) do(request *http.Request, v interface{}) error {
response, err := c.httpClient.Do(request)
if err != nil {
return err
}
if response.StatusCode != http.StatusOK {
return handleResponseError(response)
}
return parseResponseBody(response, v)
}
func handleResponseError(response *http.Response) error {
data := struct {
Response struct {
Status struct {
Text string `json:"text"`
} `json:"status"`
} `json:"response"`
}{}
err := parseResponseBody(response, &data)
if err != nil {
return err
}
return fmt.Errorf("Request failed with HTTP error: %v (%v)", response.StatusCode, strings.TrimSpace(data.Response.Status.Text))
}
func parseResponseBody(response *http.Response, v interface{}) error {
if v == nil {
return nil
}
defer response.Body.Close()
body, err := io.ReadAll(response.Body)
if err != nil {
return err
}
return json.Unmarshal(body, v)
}