-
Notifications
You must be signed in to change notification settings - Fork 40
/
registry.py
51 lines (37 loc) · 1.19 KB
/
registry.py
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
#
#
#
from requests import Session
class RegistryNotFound(Exception):
def __init__(self):
super().__init__('Object Not Found')
class Registry(object):
BASE = 'https://explorer.dn42.dev/api/registry'
def __init__(self):
self._session = Session()
def _request(self, method, path, params=None, data=None):
url = f'{self.BASE}{path}'
resp = self._session.request(method, url, params=params, json=data)
if resp.status_code == 404:
raise RegistryNotFound()
resp.raise_for_status()
return resp
def _transform_response(self, resp):
'''
Transform response from a list of lists
to a dictionary
[['as-name', 'ABC-AS'], ['descr', 'ABCs AS']]
to
{'as-name': 'ABC-AS', 'descr': 'ABCs AS'}
'''
data = {}
for elem in resp:
data[elem[0]] = elem[1]
return data
def asns(self):
path = '/aut-num'
return self._request('GET', path).json()['aut-num']
def asn(self, asn):
path = f'/aut-num/AS{asn}?raw'
resp = self._request('GET', path).json()[f'aut-num/AS{asn}']
return self._transform_response(resp)