forked from einride/balena-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsupervisorv1.go
69 lines (64 loc) · 1.35 KB
/
supervisorv1.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
package balena
import (
"context"
"fmt"
"net/http"
)
const supervisorv1BasePath = "v1"
// SupervisorV1Service handles communication with the supervisor v1 related methods of the
// Balena API.
type SupervisorV1Service struct {
service
apiKey string
deviceUUID string
appID string
local bool
}
func (s *SupervisorV1Service) Reboot(ctx context.Context, force bool) error {
type request struct {
Force bool `json:"force"`
}
req, err := s.newRequest(
ctx,
http.MethodPost,
supervisorv1BasePath+"/reboot",
&request{Force: force},
)
if err != nil {
return fmt.Errorf("unable to create restart service request: %v", err)
}
var resp struct {
Data string `json:"Data"`
Error string `json:"Error"`
}
err = s.client.Do(req, &resp)
if err != nil {
return fmt.Errorf("unable to reboot device: %v", err)
}
if resp.Data != "OK" {
return fmt.Errorf("unable to reboot device: %v", resp.Error)
}
return nil
}
func (s *SupervisorV1Service) newRequest(
ctx context.Context,
method string,
urlStr string,
body interface{},
) (*http.Request, error) {
u := urlStr
m := method
q := fmt.Sprintf("apikey=%s", s.apiKey)
b := body
if !s.local {
u = "supervisor/" + u
m = http.MethodPost
q = ""
b = &CloudRequest{
UUID: s.deviceUUID,
Method: method,
Data: body,
}
}
return s.client.NewRequest(ctx, m, u, q, b)
}