-
Notifications
You must be signed in to change notification settings - Fork 4
/
client.go
485 lines (371 loc) · 12.1 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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
package coreclient
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"time"
"github.com/datarhei/core-client-go/v16/api"
"github.com/Masterminds/semver/v3"
)
const (
coreapp = "datarhei-core"
coremajor = 16
coreversion = "^16.7.2" // first public release
)
type HTTPClient interface {
Do(req *http.Request) (*http.Response, error)
}
type RestClient interface {
// String returns a string representation of the connection
String() string
// ID returns the ID of the connected datarhei Core
ID() string
// Tokens returns the access and refresh token of the current session
Tokens() (string, string)
// Address returns the address of the connected datarhei Core
Address() string
About() api.About // GET /
Config() (int64, api.Config, error) // GET /config
ConfigSet(config interface{}) error // POST /config
ConfigReload() error // GET /config/reload
Graph(query api.GraphQuery) (api.GraphResponse, error) // POST /graph
DiskFSList(sort, order string) ([]api.FileInfo, error) // GET /v3/fs/disk
DiskFSHasFile(path string) bool // HEAD /v3/fs/disk/{path}
DiskFSGetFile(path string) (io.ReadCloser, error) // GET /v3/fs/disk/{path}
DiskFSDeleteFile(path string) error // DELETE /v3/fs/disk/{path}
DiskFSAddFile(path string, data io.Reader) error // PUT /v3/fs/disk/{path}
MemFSList(sort, order string) ([]api.FileInfo, error) // GET /v3/fs/mem
MemFSHasFile(path string) bool // HEAD /v3/fs/mem/{path}
MemFSGetFile(path string) (io.ReadCloser, error) // GET /v3/fs/mem/{path}
MemFSDeleteFile(path string) error // DELETE /v3/fs/mem/{path}
MemFSAddFile(path string, data io.Reader) error // PUT /v3/fs/mem/{path}
FilesystemList(name, pattern, sort, order string) ([]api.FileInfo, error) // GET /v3/fs/{name}
FilesystemHasFile(name, path string) bool // HEAD /v3/fs/{name}/{path}
FilesystemGetFile(name, path string) (io.ReadCloser, error) // GET /v3/fs/{name}/{path}
FilesystemDeleteFile(name, path string) error // DELETE /v3/fs/{name}/{path}
FilesystemAddFile(name, path string, data io.Reader) error // PUT /v3/fs/{name}/{path}
Log() ([]api.LogEvent, error) // GET /log
Metadata(key string) (api.Metadata, error) // GET /v3/metadata/{key}
MetadataSet(key string, metadata api.Metadata) error // PUT /v3/metadata/{key}
MetricsList() ([]api.MetricsDescription, error) // GET /v3/metrics
Metrics(query api.MetricsQuery) (api.MetricsResponse, error) // POST /v3/metrics
ProcessList(opts ProcessListOptions) ([]api.Process, error) // GET /v3/process
ProcessAdd(p api.ProcessConfig) error // POST /v3/process
Process(id string, filter []string) (api.Process, error) // GET /v3/process/{id}
ProcessUpdate(id string, p api.ProcessConfig) error // PUT /v3/process/{id}
ProcessDelete(id string) error // DELETE /v3/process/{id}
ProcessCommand(id, command string) error // PUT /v3/process/{id}/command
ProcessProbe(id string) (api.Probe, error) // GET /v3/process/{id}/probe
ProcessConfig(id string) (api.ProcessConfig, error) // GET /v3/process/{id}/config
ProcessReport(id string) (api.ProcessReport, error) // GET /v3/process/{id}/report
ProcessState(id string) (api.ProcessState, error) // GET /v3/process/{id}/state
ProcessMetadata(id, key string) (api.Metadata, error) // GET /v3/process/{id}/metadata/{key}
ProcessMetadataSet(id, key string, metadata api.Metadata) error // PUT /v3/process/{id}/metadata/{key}
RTMPChannels() ([]api.RTMPChannel, error) // GET /v3/rtmp
SRTChannels() (api.SRTChannels, error) // GET /v3/srt
Sessions(collectors []string) (api.SessionsSummary, error) // GET /v3/session
SessionsActive(collectors []string) (api.SessionsActive, error) // GET /v3/session/active
Skills() (api.Skills, error) // GET /v3/skills
SkillsReload() error // GET /v3/skills/reload
WidgetProcess(id string) (api.WidgetProcess, error) // GET /v3/widget/process/{id}
}
// Config is the configuration for a new REST API client.
type Config struct {
// Address is the address of the datarhei Core to connect to.
Address string
// Username and password are credentials to authorize access to the API.
Username string
Password string
// Access and refresh token from an existing session to authorize access to the API.
AccessToken string
RefreshToken string
// Auth0Token is a valid Auth0 token to authorize access to the API.
Auth0Token string
// Client is a HTTPClient that will be used for the API calls. Optional.
Client HTTPClient
}
// restclient implements the RestClient interface.
type restclient struct {
address string
prefix string
accessToken string
refreshToken string
username string
password string
auth0Token string
client HTTPClient
about api.About
version struct {
connectedCore *semver.Version
methods map[string]*semver.Constraints
}
}
// New returns a new REST API client for the given config. The error is non-nil
// in case of an error.
func New(config Config) (RestClient, error) {
r := &restclient{
address: config.Address,
prefix: "/api",
username: config.Username,
password: config.Password,
accessToken: config.AccessToken,
refreshToken: config.RefreshToken,
auth0Token: config.Auth0Token,
client: config.Client,
}
if r.client == nil {
r.client = &http.Client{
Timeout: 15 * time.Second,
}
}
about, err := r.info()
if err != nil {
return nil, err
}
r.about = about
if r.about.App != coreapp {
return nil, fmt.Errorf("didn't receive the expected API response (got: %s, want: %s)", r.about.Name, coreapp)
}
mustNewConstraint := func(constraint string) *semver.Constraints {
v, _ := semver.NewConstraint(constraint)
return v
}
r.version.methods = map[string]*semver.Constraints{
"GET/api/v3/srt": mustNewConstraint("^16.9.0"),
"GET/api/v3/metrics": mustNewConstraint("^16.10.0"),
}
if len(r.about.ID) != 0 {
c, _ := semver.NewConstraint(coreversion)
v, err := semver.NewVersion(r.about.Version.Number)
if err != nil {
return nil, err
}
if !c.Check(v) {
return nil, fmt.Errorf("the core version (%s) is not supported, because a version %s is required", r.about.Version.Number, coreversion)
}
r.version.connectedCore = v
} else {
v, err := semver.NewVersion(r.about.Version.Number)
if err != nil {
return nil, err
}
if coremajor != v.Major() {
return nil, fmt.Errorf("the core major version (%d) is not supported, because %d is required", v.Major(), coremajor)
}
if err := r.login(); err != nil {
return nil, err
}
}
return r, nil
}
func (r restclient) String() string {
return fmt.Sprintf("%s %s (%s) %s @ %s", r.about.Name, r.about.Version.Number, r.about.Version.Arch, r.about.ID, r.address)
}
func (r *restclient) ID() string {
return r.about.ID
}
func (r *restclient) Tokens() (string, string) {
return r.accessToken, r.refreshToken
}
func (r *restclient) Address() string {
return r.address
}
func (r *restclient) About() api.About {
return r.about
}
func (r *restclient) login() error {
login := api.Login{}
hasLocalJWT := false
useLocalJWT := false
hasAuth0 := false
useAuth0 := false
for _, auths := range r.about.Auths {
if auths == "localjwt" {
hasLocalJWT = true
break
} else if strings.HasPrefix(auths, "auth0 ") {
hasAuth0 = true
break
}
}
if !hasLocalJWT && !hasAuth0 {
return fmt.Errorf("the API doesn't provide any supported auth method")
}
if len(r.auth0Token) != 0 && hasAuth0 {
useAuth0 = true
}
if !useAuth0 {
if (len(r.username) != 0 || len(r.password) != 0) && hasLocalJWT {
useLocalJWT = true
}
}
if !useAuth0 && !useLocalJWT {
return fmt.Errorf("none of the provided auth credentials can be used")
}
if useLocalJWT {
login.Username = r.username
login.Password = r.password
}
var buf bytes.Buffer
e := json.NewEncoder(&buf)
e.Encode(login)
req, err := http.NewRequest("POST", r.address+r.prefix+"/login", &buf)
if err != nil {
return err
}
req.Header.Add("Content-Type", "application/json")
if useAuth0 {
req.Header.Add("Authorization", "Bearer "+r.auth0Token)
}
status, body, err := r.request(req)
if err != nil {
return err
}
if status != 200 {
return fmt.Errorf("wrong username and/or password")
}
data, _ := io.ReadAll(body)
jwt := api.JWT{}
json.Unmarshal(data, &jwt)
r.accessToken = jwt.AccessToken
r.refreshToken = jwt.RefreshToken
about, err := r.info()
if err != nil {
return err
}
if len(about.ID) == 0 {
return fmt.Errorf("login to the API failed")
}
c, _ := semver.NewConstraint(coreversion)
v, err := semver.NewVersion(about.Version.Number)
if err != nil {
return err
}
if !c.Check(v) {
return fmt.Errorf("the core version (%s) is not supported, because a version %s is required", about.Version.Number, coreversion)
}
r.version.connectedCore = v
r.about = about
return nil
}
func (r *restclient) checkVersion(method, path string) error {
c := r.version.methods[method+path]
if c == nil {
return nil
}
if !c.Check(r.version.connectedCore) {
return fmt.Errorf("this method is only available in version %s of the core", c.String())
}
return nil
}
func (r *restclient) refresh() error {
req, err := http.NewRequest("GET", r.address+r.prefix+"/login/refresh", nil)
if err != nil {
return err
}
req.Header.Add("Authorization", "Bearer "+r.refreshToken)
status, body, err := r.request(req)
if err != nil {
return err
}
if err != nil {
return err
}
if status != 200 {
return fmt.Errorf("invalid refresh token")
}
data, _ := io.ReadAll(body)
jwt := api.JWTRefresh{}
json.Unmarshal(data, &jwt)
r.accessToken = jwt.AccessToken
return nil
}
func (r *restclient) info() (api.About, error) {
req, err := http.NewRequest("GET", r.address+r.prefix, nil)
if err != nil {
return api.About{}, err
}
if len(r.accessToken) != 0 {
req.Header.Add("Authorization", "Bearer "+r.accessToken)
}
status, body, err := r.request(req)
if err != nil {
return api.About{}, err
}
if status != 200 {
return api.About{}, fmt.Errorf("access to API failed (%d)", status)
}
data, _ := io.ReadAll(body)
about := api.About{}
json.Unmarshal(data, &about)
return about, nil
}
func (r *restclient) request(req *http.Request) (int, io.ReadCloser, error) {
resp, err := r.client.Do(req)
if err != nil {
return -1, nil, err
}
return resp.StatusCode, resp.Body, nil
}
func (r *restclient) stream(method, path, contentType string, data io.Reader) (io.ReadCloser, error) {
if err := r.checkVersion(method, r.prefix+path); err != nil {
return nil, err
}
req, err := http.NewRequest(method, r.address+r.prefix+path, data)
if err != nil {
return nil, err
}
if method == "POST" || method == "PUT" {
req.Header.Add("Content-Type", contentType)
}
if len(r.accessToken) != 0 {
req.Header.Add("Authorization", "Bearer "+r.accessToken)
}
status, body, err := r.request(req)
if status == http.StatusUnauthorized {
if err := r.refresh(); err != nil {
if err := r.login(); err != nil {
return nil, err
}
}
req.Header.Set("Authorization", "Bearer "+r.accessToken)
status, body, err = r.request(req)
}
if err != nil {
return nil, err
}
if status < 200 || status >= 300 {
e := api.Error{
Code: status,
}
defer body.Close()
data, err := io.ReadAll(body)
if err != nil {
return nil, e
}
e.Body = data
err = json.Unmarshal(data, &e)
if err != nil {
return nil, e
}
// In case it's not an api.Error, reconstruct the return code. With this
// and the body, the caller can reconstruct the correct error.
if e.Code == 0 {
e.Code = status
}
return nil, e
}
return body, nil
}
func (r *restclient) call(method, path, contentType string, data io.Reader) ([]byte, error) {
body, err := r.stream(method, path, contentType, data)
if err != nil {
return nil, err
}
defer body.Close()
x, err := io.ReadAll(body)
return x, err
}