"""Interface to the ows-accounting microservice.""" from owsrequest import request from flows import config from .exceptions import OWSError ACCOUNTING_PERIOD_ENDPOINT = '/accounting_period' ACCOUNTING_SERVICE = 'ows-accounting' def get_accounting_period(correlation_id=None): """Get active accounting period ID (APID). Fetch APID with a GET call to ows-accounting (see: GET /accounting_period endpoint). Args: correlation_id (str): correlation id. Raises: OWSError: if an error occurs during the OWS call. Returns: str: ID of active accounting period. """ domain = ACCOUNTING_SERVICE resource = ACCOUNTING_PERIOD_ENDPOINT ows_response = request.process( application=config.SERVICE_NAME, environment=config.ENVIRONMENT, method='GET', service_name=domain, path=resource, correlation_id=correlation_id) if ows_response.status_code is 200: return ows_response.json()['accounting_period'] else: raise OWSError( """An error occurred while requesting accounting period from {ows_service}""" '@ {url}: {status_code} {reason}.'.format( ows_service=ACCOUNTING_SERVICE, url=ows_response.url, status_code=ows_response.status_code, reason=ows_response.reason))