-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmistDeviceQuery.go
More file actions
61 lines (54 loc) · 1.3 KB
/
mistDeviceQuery.go
File metadata and controls
61 lines (54 loc) · 1.3 KB
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
package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
)
// DeviceInfo - expected return from API
type DeviceInfo struct {
Manufacturer string `json:"manufacture"`
HostName string `json:"hostname"`
UpTime int `json:"uptime"`
}
func doRequest(req *http.Request, apiToken string) ([]byte, error) {
token := fmt.Sprintf("Token " + apiToken)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", token)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if 404 == resp.StatusCode {
return nil, errors.New("Device not found")
}
if 200 != resp.StatusCode {
return nil, fmt.Errorf("%s", body)
}
return body, nil
}
// GetDeviceInfo - query the REST API
func GetDeviceInfo(cfg Config) (*DeviceInfo, error) {
url := fmt.Sprintf("https://api.mistsys.com/api/v1/sites/%s/stats/clients/%s", cfg.SiteID, cfg.DeviceMac)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
bytes, err := doRequest(req, cfg.MistAPIToken)
if err != nil {
return nil, err
}
var data DeviceInfo
err = json.Unmarshal(bytes, &data)
if err != nil {
return nil, err
}
return &data, nil
}