-
Notifications
You must be signed in to change notification settings - Fork 4
/
api.go
806 lines (704 loc) · 18.6 KB
/
api.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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
// go-api - Client for the Cacophony API server.
// Copyright (C) 2018, The Cacophony Project
//
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.
package api
import (
"bytes"
"crypto/sha1"
"encoding/json"
"errors"
"fmt"
"io"
"mime/multipart"
"net"
"net/http"
"net/url"
"os"
"path"
"strconv"
"time"
goconfig "github.com/TheCacophonyProject/go-config"
)
const (
httpTimeout = 60 * time.Second
timeout = 30 * time.Second
apiBasePath = "/api/v1"
regURL = "/devices"
authURL = "/authenticate_device"
)
type CacophonyDevice struct {
group string
name string
password string
id int
saltId int
}
func (d *CacophonyDevice) hostname() string {
return safeName(d.name) + "-" + safeName(d.group)
}
type CacophonyAPI struct {
device *CacophonyDevice
httpClient *http.Client
serverURL string
token string
}
// joinURL creates an absolute url with supplied baseURL, and all paths
func joinURL(baseURL string, paths ...string) string {
u, err := url.Parse(baseURL)
if err != nil {
return ""
}
url := path.Join(paths...)
u.Path = path.Join(u.Path, url)
return u.String()
}
func (api *CacophonyAPI) getAuthURL() string {
return joinURL(api.serverURL, authURL)
}
func (api *CacophonyAPI) getRegURL() string {
return joinURL(api.serverURL, apiBasePath, regURL)
}
func (api *CacophonyAPI) Password() string {
return api.device.password
}
func (api *CacophonyAPI) DeviceID() int {
return api.device.id
}
func (api *CacophonyAPI) DeviceName() string {
return api.device.name
}
func (api *CacophonyAPI) GroupName() string {
return api.device.group
}
// apiFromConfig creates a CacophonyAPI from the config files. The API will need
// to be registered or be authenticated before used.
func apiFromConfig() (*CacophonyAPI, error) {
conf, err := NewConfig(goconfig.DefaultConfigDir)
if err != nil {
return nil, err
}
if err := conf.read(); err != nil {
return nil, err
}
device := &CacophonyDevice{
group: conf.Group,
name: conf.DeviceName,
id: conf.DeviceID,
password: conf.DevicePassword,
}
api := &CacophonyAPI{
serverURL: conf.ServerURL,
device: device,
httpClient: newHTTPClient(),
}
return api, err
}
// New will get an API from the config files and authenticate. Will return an
// error if the device has not been registered yet.
func New() (*CacophonyAPI, error) {
api, err := apiFromConfig()
if err != nil {
return nil, err
}
if err := api.authenticate(); err != nil {
return nil, err
}
return api, nil
}
// Register will check that there is not already device config files, will then
// register with the given parameters and then save them in new config files.
func Register(devicename, password, group, apiURL string, saltId int) (*CacophonyAPI, error) {
url, err := url.Parse(apiURL)
if err != nil {
return nil, err
}
conf, err := NewConfig(goconfig.DefaultConfigDir)
if err != nil {
return nil, err
}
if err := conf.read(); err != nil {
return nil, err
}
if conf.Registered() {
return nil, errors.New("device is already registered")
}
regData := map[string]interface{}{
"group": group,
"devicename": devicename,
"password": password,
}
if saltId != 0 {
regData["saltId"] = saltId
}
payload, err := json.Marshal(regData)
if err != nil {
return nil, err
}
api := &CacophonyAPI{
serverURL: url.String(),
httpClient: newHTTPClient(),
}
postResp, err := api.httpClient.Post(
api.getRegURL(),
"application/json",
bytes.NewReader(payload),
)
if err != nil {
return nil, err
}
defer postResp.Body.Close()
if err := handleHTTPResponse(postResp); err != nil {
return nil, err
}
var respData tokenResponse
d := json.NewDecoder(postResp.Body)
if err := d.Decode(&respData); err != nil {
return nil, fmt.Errorf("decode: %v", err)
}
api.device = &CacophonyDevice{
id: respData.ID,
group: group,
name: devicename,
password: password,
saltId: respData.SaltId,
}
api.token = respData.Token
conf.DeviceID = respData.ID
conf.DeviceName = devicename
conf.DevicePassword = password
conf.Group = group
conf.ServerURL = url.String()
if err := conf.write(); err != nil {
return nil, err
}
if err := updateHostnameAndSaltGrains(api.device); err != nil {
return nil, err
}
return api, nil
}
// authenticate a device with Cacophony API and retrieves the token
func (api *CacophonyAPI) authenticate() error {
if api.device.password == "" {
return errNotRegistered
}
data := map[string]interface{}{
"password": api.device.password,
}
if api.device.id > 0 {
data["deviceID"] = api.device.id
} else {
data["devicename"] = api.device.name
data["groupname"] = api.device.group
}
payload, err := json.Marshal(data)
if err != nil {
return err
}
postResp, err := api.httpClient.Post(
api.getAuthURL(),
"application/json",
bytes.NewReader(payload),
)
if err != nil {
return err
}
defer postResp.Body.Close()
if err := handleHTTPResponse(postResp); err != nil {
return err
}
var resp tokenResponse
d := json.NewDecoder(postResp.Body)
if err := d.Decode(&resp); err != nil {
return fmt.Errorf("decode: %v", err)
}
api.device.id = resp.ID
api.token = resp.Token
return nil
}
// newHTTPClient initializes and returns a http.Client with default settings
func newHTTPClient() *http.Client {
return &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: timeout, // connection timeout
KeepAlive: 30 * time.Second,
DualStack: true,
}).DialContext,
TLSHandshakeTimeout: timeout,
ResponseHeaderTimeout: timeout,
ExpectContinueTimeout: 1 * time.Second,
MaxIdleConns: 5,
IdleConnTimeout: 90 * time.Second,
},
}
}
func shaHash(r io.Reader) (string, error) {
h := sha1.New()
if _, err := io.Copy(h, r); err != nil {
return "", err
}
hashString := fmt.Sprintf("%x", h.Sum(nil))
return hashString, nil
}
// UploadVideo uploads the file to Cacophony API as a multipartmessage
func (api *CacophonyAPI) UploadVideo(r io.Reader, data map[string]interface{}) (int, error) {
buf := new(bytes.Buffer)
w := multipart.NewWriter(buf)
// This will write to fileBytes as it reads r to get the sha hash
var fileBytes bytes.Buffer
tee := io.TeeReader(r, &fileBytes)
hash, err := shaHash(tee)
if err != nil {
return 0, err
}
if data == nil {
data = make(map[string]interface{})
}
if _, ok := data["type"]; !ok {
data["type"] = "thermalRaw"
}
data["fileHash"] = hash
// JSON encoded "data" parameter.
dataBuf, err := json.Marshal(data)
if err != nil {
return 0, err
}
if err := w.WriteField("data", string(dataBuf)); err != nil {
return 0, err
}
// Add the file as a new MIME part.
fw, err := w.CreateFormFile("file", "file")
if err != nil {
return 0, err
}
io.Copy(fw, &fileBytes)
w.Close()
req, err := http.NewRequest("POST", joinURL(api.serverURL, apiBasePath, "/recordings"), buf)
if err != nil {
return 0, err
}
req.Header.Set("Content-Type", w.FormDataContentType())
req.Header.Set("Authorization", api.token)
resp, err := api.httpClient.Do(req)
if err != nil {
return 0, err
}
defer resp.Body.Close()
if err := handleHTTPResponse(resp); err != nil {
return 0, err
}
var fr fileUploadResponse
d := json.NewDecoder(resp.Body)
if err := d.Decode(&fr); err != nil {
return 0, err
}
return fr.RecordingID, nil
}
type tokenResponse struct {
Messages []string
Token string
ID int
SaltId int
}
type fileUploadResponse struct {
RecordingID int
StatusCode int
Messages []string
}
// getFileFromJWT downloads a file from the Cacophony API using supplied JWT
// and saves it to the supplied path
func (api *CacophonyAPI) getFileFromJWT(jwt, filePath string) error {
// Get the data
u, err := url.Parse(api.serverURL)
if err != nil {
return err
}
u.Path = path.Join(apiBasePath, "/signedUrl")
params := url.Values{}
params.Add("jwt", jwt)
u.RawQuery = params.Encode()
resp, err := http.Get(u.String())
if err != nil {
return err
}
defer resp.Body.Close()
// Check server response
if err := handleHTTPResponse(resp); err != nil {
return err
}
// Writer the body to file
out, err := os.Create(filePath)
if err != nil {
return err
}
defer out.Close()
_, err = io.Copy(out, resp.Body)
if err != nil {
os.Remove(filePath)
return err
}
return nil
}
type FileResponse struct {
File FileInfo
Jwt string
FileSize int
}
type FileInfo struct {
Details FileDetails
Type string
}
type FileDetails struct {
Name string
OriginalName string
}
// GetFileDetails of the supplied fileID from the Cacophony API and return FileResponse info.
// This can then be parsed into DownloadFile to download the file
func (api *CacophonyAPI) GetFileDetails(fileID int) (*FileResponse, error) {
buf := new(bytes.Buffer)
req, err := http.NewRequest("GET", joinURL(api.serverURL, apiBasePath, "/files/"+strconv.Itoa(fileID)), buf)
if err != nil {
return nil, err
}
req.Header.Set("Authorization", api.token)
resp, err := api.httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var fr FileResponse
d := json.NewDecoder(resp.Body)
if err := d.Decode(&fr); err != nil {
return &fr, err
}
return &fr, nil
}
// DownloadFile specified by fileResponse and save it to filePath
func (api *CacophonyAPI) DownloadFile(fileResponse *FileResponse, filePath string) error {
if _, err := os.Stat(filePath); err == nil {
return err
}
return api.getFileFromJWT(fileResponse.Jwt, filePath)
}
// ReportEvent described by jsonDetails and timestamps to the Cacophony API
func (api *CacophonyAPI) ReportEvent(jsonDetails []byte, times []time.Time) error {
// Deserialise the JSON event details into a map.
var details map[string]interface{}
err := json.Unmarshal(jsonDetails, &details)
if err != nil {
return err
}
// Convert the event times for sending and add to the map to send.
dateTimes := make([]string, 0, len(times))
for _, t := range times {
dateTimes = append(dateTimes, formatTimestamp(t))
}
details["dateTimes"] = dateTimes
// Serialise the map back to JSON for sending.
jsonAll, err := json.Marshal(details)
if err != nil {
return err
}
// Prepare request.
req, err := http.NewRequest("POST", joinURL(api.serverURL, apiBasePath, "/events"), bytes.NewReader(jsonAll))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", api.token)
resp, err := api.httpClient.Do(req)
if err != nil {
return temporaryError(err)
}
defer resp.Body.Close()
if err := handleHTTPResponse(resp); err != nil {
return err
}
return nil
}
// handleHTTPResponse checks StatusCode of a response for success and returns an http error
// described in error.go
func handleHTTPResponse(resp *http.Response) error {
if !(isHTTPSuccess(resp.StatusCode)) {
body, err := io.ReadAll(resp.Body)
if err != nil {
return temporaryError(fmt.Errorf("request failed (%d) and body read failed: %v", resp.StatusCode, err))
}
return &Error{
message: fmt.Sprintf("HTTP request failed (%d): %s", resp.StatusCode, body),
permanent: isHTTPClientError(resp.StatusCode),
}
}
return nil
}
// formatTimestamp to time.RFC3339Nano format
func formatTimestamp(t time.Time) string {
return t.UTC().Format(time.RFC3339Nano)
}
func isHTTPSuccess(code int) bool {
return code >= 200 && code < 300
}
func isHTTPClientError(code int) bool {
return code >= 400 && code < 500
}
// GetSchedule will get the audio schedule
func (api *CacophonyAPI) GetSchedule() ([]byte, error) {
req, err := http.NewRequest("GET", joinURL(api.serverURL, apiBasePath, "schedules"), nil)
if err != nil {
return nil, err
}
req.Header.Set("Authorization", api.token)
// client := new(http.Client)
resp, err := api.httpClient.Do(req)
if err != nil {
return []byte{}, err
}
defer resp.Body.Close()
return io.ReadAll(resp.Body)
}
// This allows the device to be registered even
func (api *CacophonyAPI) ReRegisterByAuthorized(newName, newGroup, newPassword, authToken string) error {
data := map[string]string{
"newName": newName,
"newGroup": newGroup,
"newPassword": newPassword,
"authorizedToken": authToken,
}
jsonAll, err := json.Marshal(data)
if err != nil {
return err
}
url := joinURL(api.serverURL, apiBasePath, "devices/reregister-authorized")
req, err := http.NewRequest("POST", url, bytes.NewReader(jsonAll))
if err != nil {
return err
}
req.Header.Set("Authorization", api.token)
req.Header.Set("Content-Type", "application/json")
resp, err := api.httpClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if err := handleHTTPResponse(resp); err != nil {
return err
}
var respData tokenResponse
d := json.NewDecoder(resp.Body)
if err := d.Decode(&respData); err != nil {
return fmt.Errorf("decode: %v", err)
}
api.device = &CacophonyDevice{
id: respData.ID,
group: newGroup,
name: newName,
password: newPassword,
}
api.token = respData.Token
api.device.password = newPassword
conf, err := NewConfig(goconfig.DefaultConfigDir)
if err != nil {
return err
}
conf.DeviceName = newName
conf.Group = newGroup
conf.ServerURL = api.serverURL
conf.DevicePassword = newPassword
conf.DeviceID = respData.ID
if err := conf.write(); err != nil {
return err
}
return updateHostnameAndSaltGrains(api.device)
}
// Reregister will register getting a new name and/or group
func (api *CacophonyAPI) Reregister(newName, newGroup, newPassword string) error {
data := map[string]string{
"newName": newName,
"newGroup": newGroup,
"newPassword": newPassword,
}
jsonAll, err := json.Marshal(data)
if err != nil {
return err
}
url := joinURL(api.serverURL, apiBasePath, "devices/reregister")
req, err := http.NewRequest("POST", url, bytes.NewReader(jsonAll))
if err != nil {
return err
}
req.Header.Set("Authorization", api.token)
req.Header.Set("Content-Type", "application/json")
resp, err := api.httpClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if err := handleHTTPResponse(resp); err != nil {
return err
}
var respData tokenResponse
d := json.NewDecoder(resp.Body)
if err := d.Decode(&respData); err != nil {
return fmt.Errorf("decode: %v", err)
}
api.device = &CacophonyDevice{
id: respData.ID,
group: newGroup,
name: newName,
password: newPassword,
}
api.token = respData.Token
api.device.password = newPassword
conf, err := NewConfig(goconfig.DefaultConfigDir)
if err != nil {
return err
}
conf.DeviceName = newName
conf.Group = newGroup
conf.ServerURL = api.serverURL
conf.DevicePassword = newPassword
conf.DeviceID = respData.ID
if err := conf.write(); err != nil {
return err
}
return updateHostnameAndSaltGrains(api.device)
}
var errNotRegistered = errors.New("device is not registered")
func IsNotRegisteredError(err error) bool {
return err == errNotRegistered
}
// Send heart beat from device with expected next heart beat time
func (api *CacophonyAPI) Heartbeat(nextHeartBeat time.Time) ([]byte, error) {
url := joinURL(api.serverURL, apiBasePath, "devices/heartbeat")
data := map[string]string{
"nextHeartbeat": nextHeartBeat.Format(time.RFC3339),
}
payload, err := json.Marshal(data)
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", url, bytes.NewReader(payload))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", api.token)
if err != nil {
return nil, err
}
resp, err := api.httpClient.Do(req)
if err != nil {
return []byte{}, err
}
defer resp.Body.Close()
return io.ReadAll(resp.Body)
}
// Ensure names match the API
type Settings struct {
ReferenceImagePOV string
ReferenceImagePOVFileSize int
ReferenceImageInSitu string
ReferenceImageInSituFileSize int
Warp Warp
MaskRegions []Region
RatThresh interface{}
Success bool
Messages []string
}
type Warp struct {
Dimensions Dimensions
Origin Point
TopLeft Point
TopRight Point
BottomLeft Point
BottomRight Point
}
type Dimensions struct {
Width int
Height int
}
type Point struct {
X int
Y int
}
type Region struct {
RegionData []Point `json:"regionData"`
}
func (api *CacophonyAPI) GetDeviceSettings() (map[string]interface{}, error) {
url := joinURL(api.serverURL, apiBasePath, "devices/"+strconv.Itoa(api.device.id)+"/settings")
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.Header.Set("Authorization", api.token)
req.Header.Set("Content-Type", "application/json")
resp, err := api.httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if err := handleHTTPResponse(resp); err != nil {
return nil, err
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var response struct {
Settings map[string]interface{} `json:"settings"`
Success bool `json:"success"`
Messages []string `json:"messages"`
}
err = json.Unmarshal(body, &response)
if err != nil {
return nil, err
}
return response.Settings, nil
}
// UpdateDeviceSettings updates the device settings on the API and returns the updated settings
func (api *CacophonyAPI) UpdateDeviceSettings(settings map[string]interface{}) (map[string]interface{}, error) {
url := joinURL(api.serverURL, apiBasePath, "devices/"+strconv.Itoa(api.device.id)+"/settings")
payload, err := json.Marshal(map[string]interface{}{
"settings": settings,
})
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", url, bytes.NewReader(payload))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", api.token)
resp, err := api.httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if err := handleHTTPResponse(resp); err != nil {
return nil, err
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var response struct {
Settings map[string]interface{} `json:"settings"`
Success bool `json:"success"`
Messages []string `json:"messages"`
}
err = json.Unmarshal(body, &response)
if err != nil {
return nil, err
}
return response.Settings, nil
}