Skip to content

Commit 32d3480

Browse files
authored
Feature/request parameters (#16)
* Make tests more detailed on task to test We need more detailed tests for different tasks so we split `api_connection` from `ensure_section` to test both tasks separatly. * Support request parameters In former versions we don't support request parameters, so we add these feature now. It's now possible to add a dictionary with parameters to each method (`get_entity`, `create_entity`, `update_entity` and `delete_entity`).
1 parent 97d9173 commit 32d3480

File tree

4 files changed

+90
-29
lines changed

4 files changed

+90
-29
lines changed

phpypam/core/api.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ def _query(self, **kwargs):
7171

7272
_url = '{}/api/{}/{}'.format(self._api_url, self._api_appid, _api_path)
7373

74+
if _params and not _url.endswith('/'):
75+
_url = _url + '/'
76+
7477
resp = _method(
7578
_url,
7679
params=_params,
@@ -101,34 +104,38 @@ def get_token(self):
101104
def get_entity(self, controller, **kwargs):
102105
_path = controller
103106
_controller_path = kwargs.pop('controller_path', None)
107+
_params = kwargs.pop('params', None)
104108

105109
if _controller_path:
106110
_path = '{}/{}'.format(_path, _controller_path)
107111

108-
return self._query(token=self._api_token, method=GET, path=_path)
112+
return self._query(token=self._api_token, method=GET, path=_path, params=_params)
109113

110114
def create_entity(self, controller, data, **kwargs):
111115
_path = controller
112116
_controller_path = kwargs.pop('controller_path', None)
117+
_params = kwargs.pop('params', None)
113118

114119
if _controller_path:
115120
_path = '{}/{}'.format(_path, _controller_path)
116121

117-
return self._query(token=self._api_token, method=POST, path=_path, data=data)
122+
return self._query(token=self._api_token, method=POST, path=_path, data=data, params=_params)
118123

119124
def delete_entity(self, controller, controller_path, **kwargs):
120125
_path = '{}/{}'.format(controller, controller_path)
126+
_params = kwargs.pop('params', None)
121127

122-
return self._query(token=self._api_token, method=DELETE, path=_path)
128+
return self._query(token=self._api_token, method=DELETE, path=_path, params=_params)
123129

124130
def update_entity(self, controller, data, **kwargs):
125131
_path = controller
126132
_controller_path = kwargs.pop('controller_path', None)
133+
_params = kwargs.pop('params', None)
127134

128135
if _controller_path:
129136
_path = '{}/{}'.format(_path, _controller_path)
130137

131-
return self._query(token=self._api_token, method=PATCH, path=_path, data=data)
138+
return self._query(token=self._api_token, method=PATCH, path=_path, data=data, params=_params)
132139

133140
def controllers(self):
134141
result = self._query(token=self._api_token, method=OPTIONS, path='/')

tests/api_connection.py

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,4 @@
1919
ssl_verify=True
2020
)
2121

22-
my_section = dict(
23-
name='foobar',
24-
description='new section',
25-
permissions='{"3":"1","2":"2"}'
26-
)
27-
try:
28-
entity = pi.get_entity(controller='sections', controller_path=my_section['name'])
29-
except PHPyPAMEntityNotFoundException:
30-
print('create entity')
31-
entity = pi.create_entity(controller='sections', data=my_section)
32-
entity = pi.get_entity(controller='sections', controller_path=my_section['name'])
33-
34-
entity = pi.get_entity(controller='sections', controller_path=my_section['name'])
35-
print(json.dumps(entity, indent=4, sort_keys=True))
36-
37-
my_section['description'] = 'new description'
38-
39-
print('update entity')
40-
pi.update_entity(controller='sections', controller_path=entity['id'], data=my_section)
41-
42-
entity = pi.get_entity(controller='sections', controller_path=my_section['name'])
43-
print(json.dumps(entity, indent=4, sort_keys=True))
44-
45-
print('delete entity')
46-
pi.delete_entity(controller='sections', controller_path=entity['id'])
22+
print(pi.get_token())

tests/ensure_section.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/usr/bin/env python
2+
3+
import phpypam
4+
import json
5+
import yaml
6+
7+
with open('tests/vars/server.yml') as c:
8+
server = yaml.safe_load(c)
9+
10+
from phpypam import PHPyPAMEntityNotFoundException
11+
12+
13+
if __name__ == '__main__':
14+
pi = phpypam.api(
15+
url=server['url'],
16+
app_id=server['app_id'],
17+
username=server['username'],
18+
password=server['password'],
19+
ssl_verify=True
20+
)
21+
22+
my_section = dict(
23+
name='foobar',
24+
description='new section',
25+
permissions='{"3":"1","2":"2"}'
26+
)
27+
try:
28+
entity = pi.get_entity(controller='sections', controller_path=my_section['name'])
29+
except PHPyPAMEntityNotFoundException:
30+
print('create entity')
31+
entity = pi.create_entity(controller='sections', data=my_section)
32+
entity = pi.get_entity(controller='sections', controller_path=my_section['name'])
33+
34+
entity = pi.get_entity(controller='sections', controller_path=my_section['name'])
35+
print(json.dumps(entity, indent=4, sort_keys=True))
36+
37+
my_section['description'] = 'new description'
38+
39+
print('update entity')
40+
pi.update_entity(controller='sections', controller_path=entity['id'], data=my_section)
41+
42+
entity = pi.get_entity(controller='sections', controller_path=my_section['name'])
43+
print(json.dumps(entity, indent=4, sort_keys=True))
44+
45+
print('delete entity')
46+
pi.delete_entity(controller='sections', controller_path=entity['id'])

tests/search_section_by_name.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env python
2+
3+
import phpypam
4+
import json
5+
import yaml
6+
7+
with open('tests/vars/server.yml') as c:
8+
server = yaml.safe_load(c)
9+
10+
from phpypam import PHPyPAMEntityNotFoundException
11+
12+
13+
if __name__ == '__main__':
14+
pi = phpypam.api(
15+
url=server['url'],
16+
app_id=server['app_id'],
17+
username=server['username'],
18+
password=server['password'],
19+
ssl_verify=True
20+
)
21+
22+
search_params = [
23+
{'filter_by': 'name', 'filter_value': 'non_existing_section'},
24+
{'filter_by': 'name', 'filter_value': 'IPv6', 'filter_match': 'full'},
25+
]
26+
27+
for search in search_params:
28+
try:
29+
entity = pi.get_entity(controller='sections', params=search)
30+
print("""Entity with name '{0}' found:\nResult:\n{1}""".format(search['filter_value'], json.dumps(entity, indent=2, sort_keys=True)))
31+
except PHPyPAMEntityNotFoundException:
32+
print("Entity with name '{0}' not found.".format(search['filter_value']))

0 commit comments

Comments
 (0)