From 2fd3f9fb2b6fcaee2a579722cee91d13d5e87ecd Mon Sep 17 00:00:00 2001 From: "daniel.x.olsson@ri.se" Date: Mon, 2 Jan 2023 15:30:55 +0100 Subject: Added SSL verify option and fixed edit function --- README.md | 2 +- phpipam_api/backend.py | 12 +++++++----- phpipam_api/resources.py | 2 +- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 59ba87c..e3c7c97 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ Functions shared by all controllers: * `get()` returns all obejcts in in controller * `byID(object_id=)` get specific obejct by ID * `create(data=)` -* `edit(data=)` +* `edit(object_id=, data=)` * `delete(object_id=)` ### sections diff --git a/phpipam_api/backend.py b/phpipam_api/backend.py index 85079df..b5ad180 100755 --- a/phpipam_api/backend.py +++ b/phpipam_api/backend.py @@ -18,10 +18,11 @@ class ApiObjectNotFoundException(Exception): pass class PhpipamBackend: - def __init__(self, api_url, app_id, api_user, api_password): + def __init__(self, api_url, app_id, api_user, api_password, verify=True): self.api_url = api_url.strip('/') + '/api/' + app_id self.api_user = api_user self.api_password = api_password + self.verify = verify # Check for static auth if len(self.api_user) == 0: @@ -31,7 +32,7 @@ class PhpipamBackend: self._getApiToken() def _getApiToken(self): - data = requests.post(self.api_url + "/user", auth=(self.api_user,self.api_password)).json() + data = requests.post(self.api_url + "/user", auth=(self.api_user,self.api_password), verify=self.verify).json() if not data['success']: raise ApiConnectionException('Failed to authenticate: ' + str(data['code']) + ' ' + data['message']) @@ -52,10 +53,11 @@ class PhpipamBackend: if self._isTokenExpired(): self._getApiToken() - data = requests.request(method, self.api_url + url, data=data, headers={'token':self.api_token}).json() + data = requests.request(method, self.api_url + url, data=data, headers={'token':self.api_token}, verify=self.verify).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'] + if 'data' in data: + return data['data'] + return data diff --git a/phpipam_api/resources.py b/phpipam_api/resources.py index 8f44c17..b8faef5 100755 --- a/phpipam_api/resources.py +++ b/phpipam_api/resources.py @@ -127,7 +127,7 @@ class PhpipamResource: def create(self, data): return self._backend.request('POST', f'/{self._type}/{object_id}', data=data) - def edit(self, data): + def edit(self, object_id, data): return self._backend.request('PATCH', f'/{self._type}/{object_id}', data=data) def delete(self, object_id): -- cgit v1.2.3