Skip to content

Commit

Permalink
fix: response dir
Browse files Browse the repository at this point in the history
  • Loading branch information
nazarfil committed Dec 13, 2024
1 parent 31e06a4 commit 2069839
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
17 changes: 14 additions & 3 deletions hexa/connections/dhis2/DHIS2Client.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from logging import getLogger

import requests
from requests.adapters import HTTPAdapter
from urllib3 import Retry

from config import logging
logger = getLogger(__name__)


class DHIS2ClientException(Exception):
Expand All @@ -15,7 +17,7 @@ def __str__(self):
return self.message

def log_error(self):
logging.error(f"DHIS2 Cient Error : {self.message}")
logger.error(f"DHIS2 Cient Error : {self.message}")


class DHIS2Client(requests.Session):
Expand All @@ -26,6 +28,7 @@ def __init__(self, url: str, username: str, password: str):
self.password = password

self.authenticate()
self.status = self.ping()

@staticmethod
def parse_api_url(url: str) -> str:
Expand Down Expand Up @@ -54,7 +57,7 @@ def request(self, method: str, url: str, *args, **kwargs) -> requests.Response:
self.raise_if_error(resp)
return resp
except requests.RequestException as exc:
logging.exception(exc)
logger.exception(exc)
raise

def raise_if_error(self, r: requests.Response) -> None:
Expand All @@ -66,3 +69,11 @@ def raise_if_error(self, r: requests.Response) -> None:
)

r.raise_for_status()

def ping(self) -> requests.Response:
response = self.get(f"{self.url}/system/ping")
if response.status_code in [200, 406]:
logger.info(f"Logged in to '{self.url}' as '{self.username}'")
else:
self.raise_if_error(response)
return response
Empty file.
5 changes: 2 additions & 3 deletions hexa/connections/dhis2/test/test_dhis2client.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ def test_successful_authentication(self):

for version in VERSIONS:
with self.subTest(version=version):
client = DHIS2Client("https://127.0.0.1:8080/", "admin", "district")
path_to_version = Path(response_dir, version, "api_authenticate.yaml")
responses._add_from_file(path_to_version)
session = client.authenticate()
assert session.status_code == 200
client = DHIS2Client("http://localhost:8080/", "admin", "district")
assert client.status.status_code == 200

0 comments on commit 2069839

Please sign in to comment.