From 41c17027909bd275aad6877ef361e3390c10c93a Mon Sep 17 00:00:00 2001 From: Yann Forget Date: Tue, 22 Aug 2023 15:08:40 +0200 Subject: [PATCH] feat(dhis2): support post requests --- openhexa/toolbox/dhis2/api.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/openhexa/toolbox/dhis2/api.py b/openhexa/toolbox/dhis2/api.py index daf7975..1e569da 100644 --- a/openhexa/toolbox/dhis2/api.py +++ b/openhexa/toolbox/dhis2/api.py @@ -52,7 +52,7 @@ def raise_if_error(response: requests.Response): if response.status_code != 200 and "json" in response.headers["content-type"]: msg = response.json() if msg.get("status") == "ERROR": - raise DHIS2Error(f"{msg.get('status')} {msg.get('httpStatusCode')}:" f" {msg.get('message')}") + raise DHIS2Error(f"{msg.get('status')} {msg.get('httpStatusCode')}: {msg.get('message')}") # raise with requests if no error message provided response.raise_for_status() @@ -111,3 +111,8 @@ def merge_pages(pages: Sequence[requests.Response]) -> dict: for page in pages: merged_response[key] += page.json()[key] return merged_response + + def post(self, endpoint: str, json: dict = None, params: dict = None) -> requests.Response: + r = self.session.post(f"{self.url}/{endpoint}", json=json, params=params) + self.raise_if_error(r) + return r