Skip to content

Commit

Permalink
Add version check.
Browse files Browse the repository at this point in the history
Check the API version before every call so we don't break something if a
backward-incompatible change is introduced.
  • Loading branch information
kd7lxl committed Nov 26, 2014
1 parent 7175255 commit 024628d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
13 changes: 13 additions & 0 deletions amprapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,26 @@ class AMPRAPI:
'encap': EncapObject,
}
_api_version = 'v1'
_api_version_minor = "1.04"

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))
Expand Down
1 change: 1 addition & 0 deletions settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
API_URL = 'https://portal.ampr.org/api/'
API_USER = ''
API_KEY = ''
CHECK_VERSION = True

0 comments on commit 024628d

Please sign in to comment.