aboutsummaryrefslogtreecommitdiff
path: root/phpipam/backend.py
diff options
context:
space:
mode:
authorGravatar Jonas Gunz <himself@jonasgunz.de> 2021-01-13 21:31:30 +0100
committerGravatar Jonas Gunz <himself@jonasgunz.de> 2021-01-13 21:31:30 +0100
commit874f62e0ab04cdb72814b60cd20ab4459fa9b4a5 (patch)
treea1f368485d8ac425c574f85339b78254192781d1 /phpipam/backend.py
parent15ef268d0c588ab3b23e27c2e746e341f2a63ad1 (diff)
downloadpython-phpipam-874f62e0ab04cdb72814b60cd20ab4459fa9b4a5.tar.gz
rename package to phpipam-api
Diffstat (limited to 'phpipam/backend.py')
-rwxr-xr-xphpipam/backend.py61
1 files changed, 0 insertions, 61 deletions
diff --git a/phpipam/backend.py b/phpipam/backend.py
deleted file mode 100755
index 85079df..0000000
--- a/phpipam/backend.py
+++ /dev/null
@@ -1,61 +0,0 @@
-#
-# phpipam/backend.py
-# (c) 2021 Jonas Gunz <himself@jonasgunz.de>
-# License: MIT
-#
-import requests
-import json
-import datetime
-from dateutil.parser import parse as datetime_parse
-
-class ApiConnectionException(Exception):
- pass
-
-class ApiQueryException(Exception):
- pass
-
-class ApiObjectNotFoundException(Exception):
- pass
-
-class PhpipamBackend:
- def __init__(self, api_url, app_id, api_user, api_password):
- self.api_url = api_url.strip('/') + '/api/' + app_id
- self.api_user = api_user
- self.api_password = api_password
-
- # Check for static auth
- if len(self.api_user) == 0:
- self.api_token = self.api_password
- self.api_token_expires = ""
- else:
- self._getApiToken()
-
- def _getApiToken(self):
- data = requests.post(self.api_url + "/user", auth=(self.api_user,self.api_password)).json()
- if not data['success']:
- raise ApiConnectionException('Failed to authenticate: ' + str(data['code']) + ' ' + data['message'])
-
- self.api_token = data['data']['token']
- self.api_token_expires = data['data']['expires']
-
-
- def _isTokenExpired(self):
- # static auth does not expire
- if len(self.api_token_expires) == 0:
- return False
-
- expiration = datetime_parse(self.api_token_expires)
-
- return expiration < datetime.datetime.now()
-
- def request ( self, method, url, data = {} ):
- if self._isTokenExpired():
- self._getApiToken()
-
- data = requests.request(method, self.api_url + url, data=data, headers={'token':self.api_token}).json()
-
- if not 'success' in data or not data['success']:
- raise ApiQueryException("Query failed with code " + str(data['code']) + ": " + str(data['message']))
-
- return data['data']
-