|
1 | 1 | # -*- coding: utf-8 -*- |
2 | 2 | # (c) Christian Meißner 2020 |
3 | 3 |
|
4 | | -# pylint: disable=raise-missing-from |
5 | | -# pylint: disable=super-with-arguments |
6 | | - |
| 4 | +import json |
7 | 5 | import requests |
8 | 6 |
|
9 | 7 | from requests.auth import HTTPBasicAuth |
10 | | -from pyhpipam.core.query import query |
| 8 | +from pyhpipam.core.exceptions import PyHPIPAMException |
11 | 9 |
|
12 | 10 | GET = requests.get |
13 | 11 | POST = requests.post |
@@ -48,11 +46,67 @@ def __init__( |
48 | 46 | if not self._api_encryption: |
49 | 47 | self._login() |
50 | 48 |
|
| 49 | + def _query(self, **kwargs): |
| 50 | + """ sends queries to phpIPAM API in a generalistic manner |
| 51 | +
|
| 52 | + :param path: Path to the controler and possibly to function to use. Default is ```user```. |
| 53 | + :param method: method to be used for the query (choice: ```GET```, ```POST```, ```PATCH```, ```OPTIONS```). |
| 54 | + :param headers: Headers for request. |
| 55 | + :param data: (optional) Dictionary Dictionary, list of tuples, bytes, or file-like object to send in the body of the :class:`Request`. |
| 56 | + :param params: (optional) Dictionary list of tuples or bytes to send in the query string for the :class:`Request`. |
| 57 | + :param auth: (optional) Auth tuple to enable Basic/Digest/Custom HTTP Auth. |
| 58 | + :param token: (optional) Api token get from last login. |
| 59 | + """ |
| 60 | + |
| 61 | + _api_path = kwargs.pop('path', 'user') |
| 62 | + _api_headers = kwargs.pop('headers', {}) |
| 63 | + _method = kwargs.pop('method', 'GET') |
| 64 | + _data = kwargs.pop('data', None) |
| 65 | + _params = kwargs.pop('params', {}) |
| 66 | + _auth = kwargs.pop('auth', None) |
| 67 | + |
| 68 | + if self._api_token: |
| 69 | + _api_headers['token'] = self._api_token |
| 70 | + |
| 71 | + _url = '{}/api/{}/{}'.format(self._api_url, self._api_appid, _api_path) |
| 72 | + |
| 73 | + if _data is not None: |
| 74 | + _data = json.dumps(_data) |
| 75 | + |
| 76 | + resp = _method( |
| 77 | + _url, |
| 78 | + params=_params, |
| 79 | + data=_data, |
| 80 | + headers=_api_headers, |
| 81 | + auth=_auth, |
| 82 | + verify=self._api_ssl_verify, |
| 83 | + timeout=self._api_timeout, |
| 84 | + ) |
| 85 | + |
| 86 | + result = resp.json() |
| 87 | + |
| 88 | + if result['code'] not in (200, 201) or not result['success']: |
| 89 | + raise PyHPIPAMException(code=result['code'], message=result['message']) |
| 90 | + else: |
| 91 | + if 'data' in result: |
| 92 | + return result['data'] |
| 93 | + |
51 | 94 | def _login(self): |
52 | 95 | _auth = HTTPBasicAuth(self._api_username, self._api_password) |
53 | | - resp = query(url=self._api_url, app_id=self._api_appid, method=POST, auth=_auth, verify=self._api_ssl_verify) |
| 96 | + resp = self._query(method=POST, auth=_auth) |
54 | 97 |
|
55 | | - self._token = resp['token'] |
| 98 | + self._api_token = resp['token'] |
56 | 99 |
|
57 | 100 | def get_token(self): |
58 | | - return self._token |
| 101 | + return self._api_token |
| 102 | + |
| 103 | + def get_entity(self, **kwargs): |
| 104 | + _controller = kwargs.pop('controller', None) |
| 105 | + _controller_path = kwargs.pop('controller_path', None) |
| 106 | + |
| 107 | + if _controller_path is None and _controller: |
| 108 | + _path = '{}'.format(_controller) |
| 109 | + elif _controller and _controller_path: |
| 110 | + _path = '{}/{}'.format(_controller, _controller_path) |
| 111 | + |
| 112 | + return self._query(token=self._api_token, method=GET, path=_path) |
0 commit comments