"""Interface for the ows-royalties microservice.""" from marshmallow import ValidationError from owsrequest import request as requests from owsresponse import response from account.constants import error, service from account.utils.exception import ExceptionDictOwsResponse from account.validation.schemas.statement_period import StatementPeriodDetailsSchema def get_statement_period(statement_period: int) -> dict: """Get a statement_period details for the given period_id. Args: statement_period (int): id of the statement period. Returns: dict: Validated statement period details or error response. """ path = f'/statement-period/{statement_period}' result = requests.get(service.OWS_ROYALTIES, path) if result.status_code != 200: return response.create_error_response( status=result.status_code, code=error.ERROR_CODE_OWS_ROYALTIES_REQUEST, message=result.text, ) data = result.json() schema = StatementPeriodDetailsSchema() try: validated_data = schema.load(data) return schema.dump(validated_data) except ValidationError as err: raise ExceptionDictOwsResponse( status=500, code=error.ERROR_CODE_OWS_ROYALTIES_REQUEST, message=f'Invalid response schema: {err}', )