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

Commit

Permalink
Merge pull request #24 from davidhuser/dev
Browse files Browse the repository at this point in the history
version 2.2.0
  • Loading branch information
David Huser authored Aug 9, 2021
2 parents 8348fbf + f371940 commit e5191e0
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 18 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
CHANGELOG
=========

2.2.0
-----
- 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>`_)
=======

2.1.2
-----
- Bugfix: set `python_requires` correctly in setup.py
Expand Down
12 changes: 11 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,17 @@ system info (this is persisted across the session):
Getting things
--------------

Normal method: ``api.get()``
Normal method: ``api.get()``, e.g.

.. code:: python
r = api.get('organisationUnits/Rp268JB6Ne4', params={'fields': 'id,name'})
data = r.json()
Parameters:

- `timeout`: to override the timeout value (default: 5 seconds) in order to prevent the client to wait indefinitely on a server response.


Paging
^^^^^^
Expand Down
2 changes: 1 addition & 1 deletion dhis2/__version__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
__title__ = "dhis2.py"
__description__ = "Python wrapper for DHIS2"
__url__ = "https://github.com/davidhuser/dhis2.py"
__version__ = "2.1.2"
__version__ = "2.2.0"
__author__ = "David Huser"
__author_email__ = "[email protected]"
__license__ = "MIT"
16 changes: 10 additions & 6 deletions dhis2/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,25 +258,27 @@ def _make_request(
params = kwargs.get("params")

data = kwargs.get("data", kwargs.get("json", None))
timeout = kwargs.get("timeout")

url = "{}/{}".format(self.api_url, endpoint)
self._validate_request(endpoint, file_type, data, params)

if method == "get":
stream = kwargs.get("stream", False)
url = "{}.{}".format(url, file_type)
r = self.session.get(url, params=params, stream=stream)
r = self.session.get(url, params=params, stream=stream, timeout=timeout)

elif method == "post":
r = self.session.post(url=url, json=data, params=params)
r = self.session.post(url=url, json=data, params=params, timeout=timeout)

elif method == "put":
r = self.session.put(url=url, json=data, params=params)
r = self.session.put(url=url, json=data, params=params, timeout=timeout)

elif method == "patch":
r = self.session.patch(url=url, json=data, params=params)
r = self.session.patch(url=url, json=data, params=params, timeout=timeout)

elif method == "delete":
r = self.session.delete(url=url, params=params)
r = self.session.delete(url=url, params=params, timeout=timeout)

else:
raise ClientException("Non-supported HTTP method: {}".format(method))
Expand All @@ -289,17 +291,19 @@ def get(
file_type: str = "json",
params: Union[dict, List[tuple]] = None,
stream: bool = False,
timeout: int = 5
) -> requests.Response:
"""
GET from DHIS2
:param endpoint: DHIS2 API endpoint
:param file_type: DHIS2 API File Type (json, xml, csv), defaults to JSON
:param params: HTTP parameters
:param stream: use requests' stream parameter
:param timeout: request timeout in seconds
:return: requests.Response object
"""
return self._make_request(
"get", endpoint, params=params, file_type=file_type, stream=stream
"get", endpoint, params=params, file_type=file_type, stream=stream, timeout=timeout
)

def post(
Expand Down
9 changes: 9 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,3 +262,12 @@ def test_json_arg_valid(api):
responses.add(responses.POST, url, json=data, status=204)
api.post(endpoint, data=data)
api.post(endpoint, json=data)


@responses.activate
def test_kwargs(api):
endpoint = "dataElements"
url = "{}/{}.json".format(API_URL, endpoint)
responses.add(responses.GET, url, status=200)
api.get(endpoint, timeout=1)
assert len(responses.calls) == 1
21 changes: 11 additions & 10 deletions tests/test_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@
__version__,
__author__,
__author_email__,
__license__,
__license__
)


def test_version_attributes():
assert __title__
assert __description__
assert __url__
assert __version__
assert __author__
assert __author_email__
assert __license__
def test_version():
assert all([
__title__,
__description__,
__url__,
__version__,
__author__,
__author_email__,
__license__
])

0 comments on commit e5191e0

Please sign in to comment.