"""Requests to ows-abacus-state.""" from typing import Dict, List import urllib.parse from config import app_logger as logger from src import constants from src.connectors.exceptions import BaseOwsServiceException from src.connectors.requests import get, post, put from src.models import AbacusState, AbacusStateBulkQueryResponse SERVICE_NAME = 'ows-abacus-state' class OwsStateException(BaseOwsServiceException): """ows-abacus-state exception.""" SERVICE = SERVICE_NAME class StateStatus: COMPLETE = 'complete' ERROR = 'error' class ErrorMsg: GET_REQUEST = 'ERROR in GET {}' PUT_REQUEST = 'ERROR in PUT {}' NO_STATE_MSG = '{} state not found for ID: {}' UPDATE_STATE = 'Failed to update abacus state: {} to status: {}' class LoggerMsg: GET_STATE = 'Getting abacus states from table {} using id {}' NO_STATE_MSG = 'No state found with name {}' UPDATE_STATE_FAIL = ErrorMsg.UPDATE_STATE def get_abacus_states( parent_table_name: str, parent_table_id: int ) -> List[AbacusState]: """Get abacus states for specified parent_table and id.""" path = f'/abacus-state/{parent_table_name}/{parent_table_id}' response = get(SERVICE_NAME, path) if response.status_code != 200: raise OwsStateException(ErrorMsg.GET_REQUEST.format(path)) return [AbacusState.model_validate(elem) for elem in response.json()] def update_abacus_state_by_id(abacus_state_id: int, body: Dict[str, str]) -> None: """Update single abacus state.""" logger.info(constants.UPDATE_STATE_MSG.format(abacus_state_id)) path = f'/abacus-state/{abacus_state_id}' response = put(SERVICE_NAME, path, body) if response.status_code != 200: raise OwsStateException(ErrorMsg.PUT_REQUEST.format(path)) def get_state( parent_table_name: str, target_id: int, action_name: str = constants.ACTION_NAME ) -> AbacusState: """Get calculate_payments state using parent_table_name and target_id.""" if action_name == constants.ACTION_NAME: logger.info(LoggerMsg.GET_STATE.format(parent_table_name, target_id)) states = get_abacus_states(parent_table_name, target_id) if not states: logger.error(LoggerMsg.NO_STATE_MSG.format(action_name)) raise OwsStateException( ErrorMsg.NO_STATE_MSG.format(parent_table_name, target_id) ) current_state = next( (state for state in states if state.action_name == action_name), None ) if not current_state: logger.error(LoggerMsg.NO_STATE_MSG.format(action_name)) raise OwsStateException( ErrorMsg.NO_STATE_MSG.format(parent_table_name, target_id) ) return current_state def bulk_query_abacus_states( action_name: str, parent_table_name: str, parent_table_ids: List[int], limit: int = 100, offset: int = 0, ) -> AbacusStateBulkQueryResponse: """Bulk query abacus states with filters.""" params = { 'limit': limit, 'offset': offset, } query_string = urllib.parse.urlencode(params) path = f'/abacus-states/query?{query_string}' body = { 'action_name': action_name, 'parent_table_name': parent_table_name, 'parent_table_ids': parent_table_ids, } response = post(SERVICE_NAME, path, body) if response.status_code != 200: raise OwsStateException(f'ERROR in POST {path} with body {body}') return AbacusStateBulkQueryResponse.model_validate(response.json())