-
Notifications
You must be signed in to change notification settings - Fork 2
/
amprapi.py
executable file
·100 lines (82 loc) · 2.91 KB
/
amprapi.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
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
#!/usr/bin/env python
#
# Copyright 2014 Tom Hayward <[email protected]>
#
# This file is part of python-amprapi.
#
# python-amprapi is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# python-amprapi is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with python-amprapi. If not, see <http://www.gnu.org/licenses/>.
from datetime import datetime
import json
import requests
import UserDict
import settings
class EncapEntry(UserDict.UserDict):
def __init__(self, initial_data):
UserDict.UserDict.__init__(self, initial_data)
self.data['updated'] = datetime.strptime(
self.data['updated'], "%Y-%m-%d %H:%M:%S")
def __hash__(self):
return id(self)
def network(self):
return "%(network)s/%(maskLength)s" % self.data
class AMPRAPI:
"""Python bindings for the AMPR Portal API.
Usage:
ampr = AMPRAPI()
result = ampr.endpoint
Example (with "encap" endpoint):
>>> import amprapi
>>> ampr = amprapi.AMPRAPI()
>>> for entry in ampr.encap:
... print "%(network)s/%(maskLength)s via %(gatewayIP)s" % entry
...
44.151.22.22/32 via 2.10.28.74
44.182.69.0/24 via 5.15.186.251
44.133.30.64/32 via 5.57.28.49
...
"""
_map = {
'encap': EncapEntry,
}
_api_version = 'v1'
_api_version_minor = "1.07"
def __init__(self, url=settings.API_URL, user=settings.API_USER,
api_key=settings.API_KEY):
self.url = url
self.user = user
self.api_key = api_key
if settings.CHECK_VERSION:
self.enforce_version()
def check_version(self):
return self._api_version_minor == self.get('version')['version']
def enforce_version(self):
if not self.check_version():
version = self.get('version')['version']
raise ValueError('Unknown API version: %s: %s' % (
version, self.get('changeLog')[version]))
def get(self, endpoint):
r = requests.get(self.url + self._api_version + '/' + endpoint,
auth=(self.user, self.api_key))
if r.status_code == 200:
return json.loads(r.text)
elif r.status_code == 404:
raise NotImplementedError(r.text)
else:
raise Exception(r.text)
def __getattr__(self, name):
return map(self._map.get(name), self.get(name))
if __name__ == "__main__":
ampr = AMPRAPI()
for entry in ampr.encap:
print "%(network)s/%(maskLength)s via %(gatewayIP)s" % entry