def handle_error( err: Exception, dhis2_server: ConnectionSettings, print_notifications: bool, ): message = f'Importing ONSE ISS facility cases from DHIS2 failed: {err}' if print_notifications: print(message, file=sys.stderr) else: dhis2_server.get_requests().notify_exception(message) raise err
def handle_success( dhis2_server: ConnectionSettings, print_notifications: bool, ): message = 'Successfully imported ONSE ISS facility cases from DHIS2' if print_notifications: print(message, file=sys.stderr) else: # For most things we pass silently. But we can repurpose # `notify_error()` to tell admins that the import went through, # because it only happens once a quarter. dhis2_server.get_requests().notify_error(message)
def fetch_data_set( dhis2_server: ConnectionSettings, data_set_id: str, org_unit_id: str, period: str, ) -> Optional[List[dict]]: """ Returns a list of `DHIS2 data values`_, or ``None`` if the the given org unit has no data collected for the last quarter. Raises exceptions on connection timeout or non-200 response status. .. _DHIS2 data values: https://docs.dhis2.org/master/en/developer/html/webapi_data_values.html """ max_attempts = 3 backoff_seconds = 3 * 60 requests = dhis2_server.get_requests() endpoint = '/dataValueSets' if DROP_API_PREFIX else '/api/dataValueSets' params = { 'period': period, 'dataSet': data_set_id, 'orgUnit': org_unit_id, } attempt = 0 while True: attempt += 1 try: response = requests.get(endpoint, params, raise_for_status=True) except (RequestException, HTTPError): if attempt < max_attempts: sleep(backoff_seconds * attempt) else: raise else: break return response.json().get('dataValues', None)
def fetch_data_set( dhis2_server: ConnectionSettings, data_set_id: str, org_unit_id: str, ) -> Optional[List[dict]]: """ Returns a list of `DHIS2 data values`_, or ``None`` if the the given org unit has no data collected for the last quarter. Raises exceptions on connection timeout or non-200 response status. .. _DHIS2 data values: https://docs.dhis2.org/master/en/developer/html/webapi_data_values.html """ requests = dhis2_server.get_requests() endpoint = '/dataValueSets' if DROP_API_PREFIX else '/api/dataValueSets' params = { 'period': get_last_quarter(), 'dataSet': data_set_id, 'orgUnit': org_unit_id, } response = requests.get(endpoint, params, raise_for_status=True) return response.json().get('dataValues', None)