Repository navigation
Expand file tree
/
Copy pathapi.go
More file actions
180 lines (153 loc) · 4.62 KB
/
Copy pathapi.go
File metadata and controls
180 lines (153 loc) · 4.62 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
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
package peeringdb
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
)
const Version = "0.1.0"
const (
baseAPI = "https://www.peeringdb.com/api/"
facilityNamespace = "fac"
carrierNamespace = "carrier"
carrierFacilityNamespace = "carrierfac"
campusNamespace = "campus"
internetExchangeNamespace = "ix"
internetExchangeFacilityNamespace = "ixfac"
internetExchangeLANNamespace = "ixlan"
internetExchangePrefixNamespace = "ixpfx"
networkNamespace = "net"
networkFacilityNamespace = "netfac"
networkInternetExchangeLANNamespace = "netixlan"
organizationNamespace = "org"
networkContactNamespace = "poc"
)
var (
// ErrBuildingRequest is returned if the HTTP request to call the API
// cannot be built as expected.
ErrBuildingRequest = errors.New("error building request for peeringdb api")
// ErrQueryingAPI is returned if there is an issue while making the
// request to the API.
ErrQueryingAPI = errors.New("error querying peeringdb api")
// ErrRateLimitExceeded is returned if the API rate limit is exceeded.
ErrRateLimitExceeded = errors.New("rate limit exceeded")
// ErrInvalidID is returned when a resource ID is invalid (e.g. <= 0).
ErrInvalidID = errors.New("invalid resource ID")
)
// SocialMedia represents a social media link for a PeeringDB entity.
type SocialMedia struct {
Service string `json:"service"`
Identifier string `json:"identifier"`
}
// resource is a generic top-level structure for parsing JSON responses from
// the PeeringDB API.
type resource[T any] struct {
Meta struct {
Generated float64 `json:"generated,omitempty"`
} `json:"meta"`
Data []T `json:"data"`
}
// API is the structure used to interact with the PeeringDB API.
type API struct {
url string
apiKey string
client *http.Client
}
// Option configures an API instance.
type Option func(*API)
// WithURL sets a custom API endpoint URL.
func WithURL(u string) Option {
return func(api *API) {
api.url = u
}
}
// WithAPIKey sets the API key for authentication.
func WithAPIKey(apiKey string) Option {
return func(api *API) {
api.apiKey = apiKey
}
}
// WithHTTPClient sets a custom HTTP client.
func WithHTTPClient(client *http.Client) Option {
return func(api *API) {
api.client = client
}
}
// NewAPI returns a new API instance configured with the given options.
func NewAPI(opts ...Option) *API {
api := &API{
url: baseAPI,
client: &http.Client{},
}
for _, opt := range opts {
opt(api)
}
return api
}
// fetch queries the PeeringDB API for a given namespace and returns a slice
// of results. It uses generics to avoid per-entity boilerplate.
func fetch[T any](ctx context.Context, api *API, namespace string, search url.Values) ([]T, error) {
u := api.url + namespace + "?depth=1"
if len(search) > 0 {
u += "&" + search.Encode()
}
request, err := http.NewRequestWithContext(ctx, http.MethodGet, u, nil)
if err != nil {
return nil, fmt.Errorf("%w: %w", ErrBuildingRequest, err)
}
request.Header.Set("User-Agent", "go-peeringdb/"+Version)
if api.apiKey != "" {
request.Header.Set("Authorization", "Api-Key "+api.apiKey)
}
response, err := api.client.Do(request)
if err != nil {
return nil, fmt.Errorf("%w: %w", ErrQueryingAPI, err)
}
defer response.Body.Close()
if response.StatusCode == http.StatusTooManyRequests {
return nil, ErrRateLimitExceeded
}
if response.StatusCode != http.StatusOK {
body, _ := io.ReadAll(response.Body)
return nil, fmt.Errorf("%s: %s", response.Status, body)
}
var r resource[T]
if err := json.NewDecoder(response.Body).Decode(&r); err != nil {
return nil, err
}
return r.Data, nil
}
// fetchByID queries the PeeringDB API for a single resource by ID.
func fetchByID[T any](ctx context.Context, api *API, namespace string, id int) (*T, error) {
if id <= 0 {
return nil, ErrInvalidID
}
search := url.Values{}
search.Set("id", strconv.Itoa(id))
results, err := fetch[T](ctx, api, namespace, search)
if err != nil {
return nil, err
}
if len(results) == 0 {
return nil, nil
}
return &results[0], nil
}
// GetASN returns the Network matching the given AS number. If the AS number
// cannot be found, an error is returned.
func (api *API) GetASN(ctx context.Context, asn int) (*Network, error) {
search := url.Values{}
search.Set("asn", strconv.Itoa(asn))
networks, err := api.GetNetwork(ctx, search)
if err != nil {
return nil, err
}
if len(networks) == 0 {
return nil, fmt.Errorf("no network found for ASN %d", asn)
}
return &networks[0], nil
}