-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnodes.go
More file actions
68 lines (56 loc) · 1.33 KB
/
nodes.go
File metadata and controls
68 lines (56 loc) · 1.33 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
62
63
64
65
66
67
68
package checkhost
import (
"encoding/json"
"fmt"
"io"
"net/http"
)
type NodesIPsResult struct {
Nodes []string `json:"nodes"`
}
func (c *Client) GetNodesIPs() ([]string, error) {
req, _ := http.NewRequest(
http.MethodGet,
fmt.Sprintf("%s/nodes/ips", c.baseURL),
nil,
)
req.Header.Set("Accept", "application/json")
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
var result NodesIPsResult
if err := json.Unmarshal(body, &result); err != nil {
return nil, fmt.Errorf("invalid JSON: %v\nBody: %s", err, string(body))
}
return result.Nodes, nil
}
type Node struct {
ASN string `json:"asn"`
IP string `json:"ip"`
Location []string `json:"location"`
}
type NodesResult struct {
Nodes map[string]Node `json:"nodes"`
}
func (c *Client) GetNodes() (map[string]Node, error) {
req, _ := http.NewRequest(
http.MethodGet,
fmt.Sprintf("%s/nodes/hosts", c.baseURL),
nil,
)
req.Header.Set("Accept", "application/json")
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
var result NodesResult
if err := json.Unmarshal(body, &result); err != nil {
return nil, fmt.Errorf("invalid JSON: %v\nBody: %s", err, string(body))
}
return result.Nodes, nil
}