-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
193 lines (150 loc) · 4.28 KB
/
client.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// Package bgpview contains an BGPView API client.
package bgpview
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"path"
"strconv"
"time"
)
const defaultBaseURL = "https://api.bgpview.io"
// Client a BGPView API client.
type Client struct {
baseURL *url.URL
HTTPClient *http.Client
}
// NewClient creates a new Client.
func NewClient() *Client {
baseURL, _ := url.Parse(defaultBaseURL)
return &Client{
baseURL: baseURL,
HTTPClient: &http.Client{Timeout: 5 * time.Second},
}
}
// GetASN gets ASN.
func (c Client) GetASN(ctx context.Context, asNumber int) (*ASNInfo, error) {
endpoint := c.baseURL.JoinPath("asn", strconv.Itoa(asNumber))
var apiResp ASNInfo
err := c.do(ctx, endpoint, &apiResp)
if err != nil {
return nil, err
}
return &apiResp, nil
}
// GetASNPrefixes gets ASN prefixes.
func (c Client) GetASNPrefixes(ctx context.Context, asNumber int) (*ASNPrefixesInfo, error) {
endpoint, err := c.baseURL.Parse(path.Join(c.baseURL.Path, "asn", strconv.Itoa(asNumber), "prefixes"))
if err != nil {
return nil, err
}
var apiResp ASNPrefixesInfo
err = c.do(ctx, endpoint, &apiResp)
if err != nil {
return nil, err
}
return &apiResp, nil
}
// GetASNPeers gets ASN peers.
func (c Client) GetASNPeers(ctx context.Context, asNumber int) (*ASNPeersInfo, error) {
endpoint := c.baseURL.JoinPath("asn", strconv.Itoa(asNumber), "peers")
var apiResp ASNPeersInfo
err := c.do(ctx, endpoint, &apiResp)
if err != nil {
return nil, err
}
return &apiResp, nil
}
// GetASNUpstreams gets ASN upstreams.
func (c Client) GetASNUpstreams(ctx context.Context, asNumber int) (*ASNUpstreamsInfo, error) {
endpoint := c.baseURL.JoinPath("asn", strconv.Itoa(asNumber), "upstreams")
var apiResp ASNUpstreamsInfo
err := c.do(ctx, endpoint, &apiResp)
if err != nil {
return nil, err
}
return &apiResp, nil
}
// GetASNDownstreams gets ASN downstreams.
func (c Client) GetASNDownstreams(ctx context.Context, asNumber int) (*ASNDownstreamsInfo, error) {
endpoint := c.baseURL.JoinPath("asn", strconv.Itoa(asNumber), "downstreams")
var apiResp ASNDownstreamsInfo
err := c.do(ctx, endpoint, &apiResp)
if err != nil {
return nil, err
}
return &apiResp, nil
}
// GetASNIxs gets ASN IXs.
func (c Client) GetASNIxs(ctx context.Context, asNumber int) (*ASNIxsInfo, error) {
endpoint := c.baseURL.JoinPath("asn", strconv.Itoa(asNumber), "ixs")
var apiResp ASNIxsInfo
err := c.do(ctx, endpoint, &apiResp)
if err != nil {
return nil, err
}
return &apiResp, nil
}
// GetPrefix gets Prefix.
func (c Client) GetPrefix(ctx context.Context, ipAddress string, cidr int) (*PrefixInfo, error) {
endpoint := c.baseURL.JoinPath("prefix", ipAddress, strconv.Itoa(cidr))
var apiResp PrefixInfo
err := c.do(ctx, endpoint, &apiResp)
if err != nil {
return nil, err
}
return &apiResp, nil
}
// GetIP gets IP.
func (c Client) GetIP(ctx context.Context, ipAddress string) (*IPInfo, error) {
endpoint := c.baseURL.JoinPath("ip", ipAddress)
var apiResp IPInfo
err := c.do(ctx, endpoint, &apiResp)
if err != nil {
return nil, err
}
return &apiResp, nil
}
// GetIX gets IX.
func (c Client) GetIX(ctx context.Context, ixID int) (*IXInfo, error) {
endpoint := c.baseURL.JoinPath("ix", strconv.Itoa(ixID))
var apiResp IXInfo
err := c.do(ctx, endpoint, &apiResp)
if err != nil {
return nil, err
}
return &apiResp, nil
}
// GetSearch searches resources by ASN, IP, Prefix, Name, Description.
func (c Client) GetSearch(ctx context.Context, term string) (*SearchInfo, error) {
endpoint := c.baseURL.JoinPath("search")
query := endpoint.Query()
query.Set("query_term", term)
endpoint.RawQuery = query.Encode()
var apiResp SearchInfo
err := c.do(ctx, endpoint, &apiResp)
if err != nil {
return nil, err
}
return &apiResp, nil
}
func (c Client) do(ctx context.Context, endpoint *url.URL, data interface{}) error {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint.String(), http.NoBody)
if err != nil {
return err
}
req.Header.Set("accept", "application/json")
resp, err := c.HTTPClient.Do(req)
if err != nil {
return err
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusOK {
data, _ := io.ReadAll(resp.Body)
return fmt.Errorf("%d: %s", resp.StatusCode, string(data))
}
return json.NewDecoder(resp.Body).Decode(data)
}