Skip to content
This repository was archived by the owner on Apr 24, 2024. It is now read-only.

Commit e5191e0

Browse files
author
David Huser
authored
Merge pull request #24 from davidhuser/dev
version 2.2.0
2 parents 8348fbf + f371940 commit e5191e0

File tree

6 files changed

+47
-18
lines changed

6 files changed

+47
-18
lines changed

CHANGELOG.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
CHANGELOG
33
=========
44

5+
2.2.0
6+
-----
7+
- allow a `timeout` parameter to prevent requests from waiting indefinitely on a response (see `here <https://docs.python-requests.org/en/master/user/quickstart/#timeouts>`_)
8+
=======
9+
510
2.1.2
611
-----
712
- Bugfix: set `python_requires` correctly in setup.py

README.rst

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,17 @@ system info (this is persisted across the session):
166166
Getting things
167167
--------------
168168

169-
Normal method: ``api.get()``
169+
Normal method: ``api.get()``, e.g.
170+
171+
.. code:: python
172+
173+
r = api.get('organisationUnits/Rp268JB6Ne4', params={'fields': 'id,name'})
174+
data = r.json()
175+
176+
Parameters:
177+
178+
- `timeout`: to override the timeout value (default: 5 seconds) in order to prevent the client to wait indefinitely on a server response.
179+
170180

171181
Paging
172182
^^^^^^

dhis2/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
__title__ = "dhis2.py"
22
__description__ = "Python wrapper for DHIS2"
33
__url__ = "https://github.com/davidhuser/dhis2.py"
4-
__version__ = "2.1.2"
4+
__version__ = "2.2.0"
55
__author__ = "David Huser"
66
__author_email__ = "[email protected]"
77
__license__ = "MIT"

dhis2/api.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -258,25 +258,27 @@ def _make_request(
258258
params = kwargs.get("params")
259259

260260
data = kwargs.get("data", kwargs.get("json", None))
261+
timeout = kwargs.get("timeout")
262+
261263
url = "{}/{}".format(self.api_url, endpoint)
262264
self._validate_request(endpoint, file_type, data, params)
263265

264266
if method == "get":
265267
stream = kwargs.get("stream", False)
266268
url = "{}.{}".format(url, file_type)
267-
r = self.session.get(url, params=params, stream=stream)
269+
r = self.session.get(url, params=params, stream=stream, timeout=timeout)
268270

269271
elif method == "post":
270-
r = self.session.post(url=url, json=data, params=params)
272+
r = self.session.post(url=url, json=data, params=params, timeout=timeout)
271273

272274
elif method == "put":
273-
r = self.session.put(url=url, json=data, params=params)
275+
r = self.session.put(url=url, json=data, params=params, timeout=timeout)
274276

275277
elif method == "patch":
276-
r = self.session.patch(url=url, json=data, params=params)
278+
r = self.session.patch(url=url, json=data, params=params, timeout=timeout)
277279

278280
elif method == "delete":
279-
r = self.session.delete(url=url, params=params)
281+
r = self.session.delete(url=url, params=params, timeout=timeout)
280282

281283
else:
282284
raise ClientException("Non-supported HTTP method: {}".format(method))
@@ -289,17 +291,19 @@ def get(
289291
file_type: str = "json",
290292
params: Union[dict, List[tuple]] = None,
291293
stream: bool = False,
294+
timeout: int = 5
292295
) -> requests.Response:
293296
"""
294297
GET from DHIS2
295298
:param endpoint: DHIS2 API endpoint
296299
:param file_type: DHIS2 API File Type (json, xml, csv), defaults to JSON
297300
:param params: HTTP parameters
298301
:param stream: use requests' stream parameter
302+
:param timeout: request timeout in seconds
299303
:return: requests.Response object
300304
"""
301305
return self._make_request(
302-
"get", endpoint, params=params, file_type=file_type, stream=stream
306+
"get", endpoint, params=params, file_type=file_type, stream=stream, timeout=timeout
303307
)
304308

305309
def post(

tests/test_api.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,3 +262,12 @@ def test_json_arg_valid(api):
262262
responses.add(responses.POST, url, json=data, status=204)
263263
api.post(endpoint, data=data)
264264
api.post(endpoint, json=data)
265+
266+
267+
@responses.activate
268+
def test_kwargs(api):
269+
endpoint = "dataElements"
270+
url = "{}/{}.json".format(API_URL, endpoint)
271+
responses.add(responses.GET, url, status=200)
272+
api.get(endpoint, timeout=1)
273+
assert len(responses.calls) == 1

tests/test_version.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@
55
__version__,
66
__author__,
77
__author_email__,
8-
__license__,
8+
__license__
99
)
1010

11-
12-
def test_version_attributes():
13-
assert __title__
14-
assert __description__
15-
assert __url__
16-
assert __version__
17-
assert __author__
18-
assert __author_email__
19-
assert __license__
11+
def test_version():
12+
assert all([
13+
__title__,
14+
__description__,
15+
__url__,
16+
__version__,
17+
__author__,
18+
__author_email__,
19+
__license__
20+
])

0 commit comments

Comments
 (0)