-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvalue.go
61 lines (50 loc) · 1.23 KB
/
value.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
package main
import (
_ "embed"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
"time"
)
type value struct {
ID int `json:"id"`
Hidden bool `json:"hidden"`
Text string `json:"text"`
Background string `json:"background"`
}
func loadValues(hdgEndpoint string, timeout time.Duration, ids []int) (map[int]value, error) {
url := hdgEndpoint + "/ApiManager.php?action=dataRefresh"
hdgClient := http.Client{
Timeout: timeout,
}
reqBody := "nodes=" + strings.Trim(strings.Replace(fmt.Sprint(ids), " ", "-", -1), "[]")
req, err := http.NewRequest(http.MethodPost, url, strings.NewReader(reqBody))
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", "hdg-exporter")
req.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
res, reqErr := hdgClient.Do(req)
if reqErr != nil {
return nil, reqErr
}
if res.Body != nil {
defer res.Body.Close()
}
resBody, readErr := ioutil.ReadAll(res.Body)
if readErr != nil {
return nil, readErr
}
values := make([]value, 0)
jsonErr := json.Unmarshal(resBody, &values)
if jsonErr != nil {
return nil, jsonErr
}
result := make(map[int]value)
for _, v := range values {
result[v.ID] = v
}
return result, nil
}