-
Notifications
You must be signed in to change notification settings - Fork 3
/
client.go
278 lines (228 loc) · 7.46 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package client
import (
"bytes"
"encoding/json"
"io/ioutil"
"net"
"net/url"
"strings"
"github.com/apex/log"
dpcontext "github.com/deviceplane/controller/pkg/context"
dphttp "github.com/deviceplane/controller/pkg/http"
"github.com/deviceplane/controller/pkg/models"
dpwebsocket "github.com/deviceplane/controller/pkg/websocket"
"github.com/function61/holepunch-server/pkg/wsconnadapter"
)
const (
bundleURL = "bundle"
)
type Client struct {
url *url.URL
projectID string
httpClient *dphttp.Client
deviceID string
accessKey string
}
func NewClient(url *url.URL, projectID string, httpClient *dphttp.Client) *Client {
if httpClient == nil {
httpClient = dphttp.DefaultClient
}
return &Client{
url: url,
projectID: projectID,
httpClient: httpClient,
}
}
func (c *Client) SetDeviceID(deviceID string) {
c.deviceID = deviceID
}
func (c *Client) SetAccessKey(accessKey string) {
c.accessKey = accessKey
}
func (c *Client) RegisterDevice(ctx *dpcontext.Context, registrationToken string) (*models.RegisterDeviceResponse, error) {
req := models.RegisterDeviceRequest{
DeviceRegistrationTokenID: registrationToken,
}
var registerDeviceResponse models.RegisterDeviceResponse
err := c.post(ctx, req, ®isterDeviceResponse, "projects", c.projectID, "devices", "register")
if err != nil {
return nil, err
}
return ®isterDeviceResponse, nil
}
func (c *Client) GetBundleBytes(ctx *dpcontext.Context) ([]byte, error) {
return c.getB(ctx, "projects", c.projectID, "devices", c.deviceID, "bundle")
}
func (c *Client) SetDeviceInfo(ctx *dpcontext.Context, req models.SetDeviceInfoRequest) error {
return c.post(ctx, req, nil, "projects", c.projectID, "devices", c.deviceID, "info")
}
func (c *Client) SendDeviceMetrics(ctx *dpcontext.Context, req models.DatadogPostMetricsRequest) error {
return c.post(ctx, req, nil, "projects", c.projectID, "devices", c.deviceID, "forwardmetrics", "device")
}
func (c *Client) SendServiceMetrics(ctx *dpcontext.Context, req models.IntermediateServiceMetricsRequest) error {
return c.post(ctx, req, nil, "projects", c.projectID, "devices", c.deviceID, "forwardmetrics", "service")
}
func (c *Client) SetDeviceApplicationStatus(ctx *dpcontext.Context, applicationID string, req models.SetDeviceApplicationStatusRequest) error {
return c.post(ctx, req, nil, "projects", c.projectID, "devices", c.deviceID, "applications", applicationID, "deviceapplicationstatuses")
}
func (c *Client) DeleteDeviceApplicationStatus(ctx *dpcontext.Context, applicationID string) error {
return c.delete(ctx, nil, "projects", c.projectID, "devices", c.deviceID, "applications", applicationID, "deviceapplicationstatuses")
}
func (c *Client) SetDeviceServiceStatus(ctx *dpcontext.Context, applicationID, service string, req models.SetDeviceServiceStatusRequest) error {
return c.post(ctx, req, nil, "projects", c.projectID, "devices", c.deviceID, "applications", applicationID, "services", service, "deviceservicestatuses")
}
func (c *Client) DeleteDeviceServiceStatus(ctx *dpcontext.Context, applicationID, service string) error {
return c.delete(ctx, nil, "projects", c.projectID, "devices", c.deviceID, "applications", applicationID, "services", service, "deviceservicestatuses")
}
func (c *Client) SetDeviceServiceState(ctx *dpcontext.Context, applicationID, service string, req models.SetDeviceServiceStateRequest) error {
return c.post(ctx, req, nil, "projects", c.projectID, "devices", c.deviceID, "applications", applicationID, "services", service, "deviceservicestates")
}
func (c *Client) DeleteDeviceServiceState(ctx *dpcontext.Context, applicationID, service string) error {
return c.delete(ctx, nil, "projects", c.projectID, "devices", c.deviceID, "applications", applicationID, "services", service, "deviceservicestates")
}
func (c *Client) InitiateDeviceConnection(ctx *dpcontext.Context) (net.Conn, error) {
req, err := dphttp.NewRequest(ctx, "", "", nil)
if err != nil {
return nil, err
}
req.SetBasicAuth(c.accessKey, "")
wsConn, _, err := dpwebsocket.DefaultDialer.Dial(
ctx,
getWebsocketURL(c.url, "projects", c.projectID, "devices", c.deviceID, "connection"),
req.Header,
)
if err != nil {
return nil, err
}
return wsconnadapter.New(wsConn.Conn), nil
}
func (c *Client) Revdial(ctx *dpcontext.Context, path string) (*dpwebsocket.Conn, *dphttp.Response, error) {
return dpwebsocket.DefaultDialer.Dial(
ctx,
getWebsocketURL(c.url, strings.TrimPrefix(path, "/")),
nil,
)
}
func (c *Client) get(ctx *dpcontext.Context, out interface{}, s ...string) error {
bytes, err := c.getB(ctx, s...)
if err != nil {
return err
}
if len(bytes) == 0 {
return nil
}
return json.Unmarshal(bytes, &out)
}
func (c *Client) getB(ctx *dpcontext.Context, s ...string) ([]byte, error) {
req, err := dphttp.NewRequest(ctx, "GET", getURL(c.url, s...), nil)
if err != nil {
return nil, err
}
req.SetBasicAuth(c.accessKey, "")
resp, err := c.httpClient.Do(req)
if err != nil {
log.WithFields(log.Fields{
"error": err.Error(),
}).Debug("GET response")
return nil, err
}
defer resp.Body.Close()
bytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
log.WithFields(log.Fields{
"status": resp.Status,
"code": resp.StatusCode,
"body": string(bytes),
}).Debug("GET response")
return bytes, nil
}
func (c *Client) post(ctx *dpcontext.Context, in, out interface{}, s ...string) error {
bytes, err := c.postB(ctx, in, s...)
if err != nil {
return err
}
if len(bytes) == 0 {
return nil
}
return json.Unmarshal(bytes, &out)
}
func (c *Client) postB(ctx *dpcontext.Context, in interface{}, s ...string) ([]byte, error) {
reqBytes, err := json.Marshal(in)
if err != nil {
return nil, err
}
reader := bytes.NewReader(reqBytes)
req, err := dphttp.NewRequest(ctx, "POST", getURL(c.url, s...), reader)
if err != nil {
return nil, err
}
req.SetBasicAuth(c.accessKey, "")
resp, err := c.httpClient.Do(req)
if err != nil {
log.WithFields(log.Fields{
"error": err.Error(),
}).Debug("POST response")
return nil, err
}
defer resp.Body.Close()
bytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
log.WithFields(log.Fields{
"status": resp.Status,
"code": resp.StatusCode,
"body": string(bytes),
}).Debug("POST response")
return bytes, nil
}
func (c *Client) delete(ctx *dpcontext.Context, out interface{}, s ...string) error {
bytes, err := c.deleteB(ctx, s...)
if err != nil {
return err
}
if len(bytes) == 0 {
return nil
}
return json.Unmarshal(bytes, &out)
}
func (c *Client) deleteB(ctx *dpcontext.Context, s ...string) ([]byte, error) {
req, err := dphttp.NewRequest(ctx, "DELETE", getURL(c.url, s...), nil)
if err != nil {
return nil, err
}
req.SetBasicAuth(c.accessKey, "")
resp, err := c.httpClient.Do(req)
if err != nil {
log.WithFields(log.Fields{
"error": err.Error(),
}).Debug("DELETE response")
return nil, err
}
defer resp.Body.Close()
bytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
log.WithFields(log.Fields{
"status": resp.Status,
"code": resp.StatusCode,
"body": string(bytes),
}).Debug("DELETE response")
return bytes, nil
}
func getURL(url *url.URL, s ...string) string {
return strings.Join(append([]string{url.String()}, s...), "/")
}
func getWebsocketURL(u *url.URL, s ...string) string {
uCopy, _ := url.Parse(u.String())
switch uCopy.Scheme {
case "http":
uCopy.Scheme = "ws"
default:
uCopy.Scheme = "wss"
}
return strings.Join(append([]string{uCopy.String()}, s...), "/")
}