import sqlmodel import sqlalchemy.engine.base from .validators import validator from .validators import validator_result class application_part: """ This class is parent for parts of the applications. It define responses and method for access to database. """ def __init__(self, database: sqlalchemy.engine.base.Engine) -> None: """ This initialize part of application, database connection is required to have access to it in the application parts. Parameters: database (Engine): Database connection """ self.__connector = database @property def _connector(self) -> sqlalchemy.engine.base.Engine: """ It return connection to database. """ return self.__connector def _apikey_response(self, apikey: str) -> dict: """ It return response with apikey. """ return self._success_response(apikey = apikey) def _validation(self, name: str, target: validator) -> dict | None: """ This help validating. It require name of the validation, and validator. Then validate, and if validator is fine, return None or when something is bad, then return response. Parameters: name (str): Name of the validation target (validator): Validator to check Returns: (dict | None): Response with error or None on success """ result = target.result if result == validator_result.valid: return None return self._fail_response( code = int(result), description = validator_result.name(result), validating = name ) def _success_response(self, **kwargs) -> dict: """ It returns success response, with additional params. """ base = dict() base.update(kwargs) base["status"] = "success" return base def _fail_no_apikey(self) -> dict: """ This return error response for not founded ApiKey. """ return self._fail_response(cause = "ApiKey not exists.") def _fail_bad_password(self) -> dict: """ This return error response to use when bad password provided. """ return self._fail_response(cause = "Password validation incorrect.") def _fail_response(self, **kwargs) -> dict: """ It return fail response, with additions content. """ base = dict() base.update(kwargs) base["status"] = "fail" return base