-
Notifications
You must be signed in to change notification settings - Fork 1
/
device.go
149 lines (125 loc) · 4.14 KB
/
device.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
package main
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"io/ioutil"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)
const DEVICE_API_URL = "https://wdm-a.wbx2.com/wdm/api/v1/devices"
type Device struct {
Token string
WebSocketUrl string
Url string
UserID string
Services map[string]string
logger *log.Entry
config *Config
}
func NewDevice(config *Config) (*Device, error) {
logger := log.WithField("type", "Device")
token := config.GetString("auth-token")
for token == "" {
fmt.Println("Please provide a valid auth-token associated to your Webex Teams account. To obtain one, you can go to https://developer.webex.com/login, Documentation, Api Reference, choose any API endpoint and you will be able to copy the Authorization token on the right.")
fmt.Print("token> ")
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
token = scanner.Text()
}
var response *http.Response
if deviceUrl := config.GetString("device-url"); deviceUrl != "" {
// Try to fetch current device
request, err := http.NewRequest("GET", deviceUrl, nil)
if err != nil {
return nil, errors.Wrap(err, "failed to create http request")
}
request.Header.Set("Authorization", "Bearer "+token)
request.Header.Set("Content-Type", "application/json")
logger.Trace("Fetch device")
response, err = http.DefaultClient.Do(request)
if err != nil {
return nil, errors.Wrap(err, "failed to create device")
}
defer response.Body.Close()
}
if response == nil || (response.StatusCode != http.StatusOK && response.StatusCode != http.StatusUnauthorized) {
// No device-url or previous call failed, try to create a new device
deviceRegisterRequest, err := json.Marshal(struct {
DeviceName string `json:"deviceName"`
DeviceType string `json:"deviceType"`
LocalizedModel string `json:"localizedModel"`
Model string `json:"model"`
Name string `json:"name"`
SystemName string `json:"systemName"`
SystemVersion string `json:"systemVersion"`
}{
"webinc",
"DESKTOP",
"webinc",
"webinc",
"webinc",
"webinc",
buildVersion,
})
if err != nil {
return nil, errors.Wrap(err, "failed to marshal deviceRegisterRequest")
}
request, err := http.NewRequest("POST", DEVICE_API_URL, bytes.NewBuffer(deviceRegisterRequest))
if err != nil {
return nil, errors.Wrap(err, "failed to create http request")
}
request.Header.Set("Authorization", "Bearer "+token)
request.Header.Set("Content-Type", "application/json")
logger.Trace("Create device")
response, err = http.DefaultClient.Do(request)
if err != nil {
return nil, errors.Wrap(err, "failed to create device")
}
defer response.Body.Close()
}
if response.StatusCode == http.StatusUnauthorized {
// Token is incorrect
config.SetString("auth-token", "")
config.Save()
return nil, errors.New("token is invalid, please retry")
}
if response.StatusCode != http.StatusOK {
responseError, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, errors.Wrap(err, "failed to read error response")
}
return nil, errors.Errorf("failed to register device: %s", responseError)
}
var device Device
err := json.NewDecoder(response.Body).Decode(&device)
if err != nil {
return nil, errors.Wrap(err, "failed to unmarshal device info")
}
logger = logger.WithField("device", device)
logger.Trace("Device created")
// Keep values
device.Token = token
device.logger = logger
device.config = config
// Store in config
config.SetString("auth-token", token)
config.SetString("device-url", device.Url)
if err := config.Save(); err != nil {
device.logger.WithError(err).Error("Failed to save config")
}
return &device, nil
}
func (d *Device) RequestService(method string, service string, url string, data io.Reader) (*http.Response, error) {
httpRequest, err := http.NewRequest(method, d.Services[service]+url, data)
if err != nil {
return nil, errors.Wrap(err, "failed to create kms http request")
}
httpRequest.Header.Set("Authorization", "Bearer "+d.Token)
httpRequest.Header.Set("Content-type", "application/json")
return http.DefaultClient.Do(httpRequest)
}