wag-managment-api-service-v.../Events/Engine/abstract_class.py

251 lines
7.6 KiB
Python

from abc import abstractmethod
from typing import Any, Dict, List, Optional, Callable
from uuid import UUID
from ApiLayers.AllConfigs.Redis.configs import RedisCategoryKeys
class PageInfo:
NAME: str
BUTTON_NAME: str
PAGE_URL: str
PAGEINFO: Dict[str, Any]
def __init__(
self,
name: str,
title: Dict[str, Any],
description: Dict[str, Any],
icon: str,
parent: str
):
self.NAME = name
self.TITLE = title
self.DESCRIPTION = description
self.ICON = icon
self.PARENT = parent
class Event:
KEY_: str # static string uuid.uuid4().__str__()
RESPONSE_VALIDATOR: Optional[Any]
REQUEST_VALIDATOR: Optional[Any]
DESCRIPTION: str
LANGUAGE_MODELS: list
RESPONSE_VALIDATOR_STATIC: str
EXTRA_OPTIONS: Optional[Dict[str, Any]] = None
endpoint_callable: Any
def __init__(
self,
name: str,
key: str | UUID,
description: str,
language_models: list[Dict[str, Dict]],
response_validation_static: str = None,
request_validator: Optional[Any] = None,
response_validator: Optional[Any] = None,
extra_options: Optional[Dict[str, Any]] = None,
) -> None:
self.NAME = name
self.KEY_ = key
self.REQUEST_VALIDATOR = request_validator
self.RESPONSE_VALIDATOR = response_validator
self.RESPONSE_VALIDATOR_STATIC = response_validation_static
self.LANGUAGE_MODELS = language_models
self.DESCRIPTION = description
self.EXTRA_OPTIONS = extra_options
@property
def is_static_response(self):
return bool(self.RESPONSE_VALIDATOR_STATIC)
@property
def static_response(self):
from Services.Redis.Actions.actions import RedisActions
from ApiLayers.AllConfigs.Redis.configs import RedisValidationKeysAction
if self.is_static_response:
static_response = RedisActions.get_json(
list_keys=[
f"{RedisValidationKeysAction.static_response_key}:{self.RESPONSE_VALIDATOR_STATIC}"
]
)
if static_response.status:
return static_response.first
return None
@property
def description(self):
return f"This is an event of {self.name}. Description: {self.DESCRIPTION}"
@property
def name(self):
return self.NAME
@property
def key(self) -> str:
return str(self.KEY_)
@abstractmethod
def endpoint_callable(self, **kwargs) -> Any:
"""
Retrieves the endpoint function based on the event key.
"""
return self.endpoint_callable(**kwargs)
class MethodToEvent:
"""
for all endpoint callable
def endpoint_callable(request: Request, data: PydanticModel):
return cls.retrieve_event(event_function_code).retrieve_callable(token_dict, data)
[Table.__language_model__ | Dict[__language_model__]]
[Dict[ErrorCode][lang]]
"""
EVENTS: dict[str, Event]
HEADER_LANGUAGE_MODELS: list[Dict]
ERRORS_LANGUAGE_MODELS: Optional[list[Dict]]
URL: str
METHOD: str
SUMMARY: str
DESCRIPTION: str
DECORATORS_LIST: Optional[Callable] = []
EXTRA_OPTIONS: Optional[Dict[str, Any]] = None
def __init__(
self,
name: str,
events: dict[str, Event],
headers: list[Dict],
url: str,
method: str,
summary: str,
description: str,
decorators_list: Optional[List[Callable]] = None,
errors: Optional[list[Dict]] = None,
extra_options: Optional[Dict[str, Any]] = None,
):
self.EVENTS = events
self.URL = url
self.METHOD = method
self.SUMMARY = summary
self.NAME = name
self.DESCRIPTION = description
self.DECORATORS_LIST = decorators_list
self.HEADER_LANGUAGE_MODELS = headers
self.ERRORS_LANGUAGE_MODELS = errors
self.EXTRA_OPTIONS = extra_options
@property
def name(self):
return self.NAME
def retrieve_all_event_keys(self):
"""
Retrieves all event keys from the events list.
"""
return [str(event_key) for event_key in self.EVENTS.keys()]
def retrieve_event(self, event_function_code: str) -> Event:
"""
Retrieves the event object from the events list based on the event function code.
"""
if found_event := self.EVENTS.get(event_function_code, None):
return found_event
raise ValueError(f"Event with function code {event_function_code} not found")
def retrieve_redis_value(self, cluster: "CategoryCluster") -> Dict:
"""
Key("METHOD_FUNCTION_CODES:{ClusterToMethod}:MethodEvent:Endpoint") : Value([FUNCTION_CODE, ...])
"""
redis_key = f"{RedisCategoryKeys.METHOD_FUNCTION_CODES}:{cluster.name}:{self.name}:{f"{cluster.PREFIX}{self.URL}"}"
return {redis_key: self.retrieve_all_event_keys()}
@staticmethod
def endpoint_callable(**kwargs):
"""
return cls.retrieve_event(event_function_code).retrieve_callable(token_dict, data)
"""
raise NotImplementedError("Endpoint callable method is not implemented")
class CategoryCluster:
TAGS: list
PREFIX: str
PAGEINFO: PageInfo
DESCRIPTION: str
ENDPOINTS: dict[str, MethodToEvent] # {"MethodToEvent": MethodToEvent, ...}
SUBCATEGORY: Optional[List["CategoryCluster"]] # [CategoryCluster, ...]
INCLUDE_IN_SCHEMA: Optional[bool] = True
def __init__(
self,
name: str,
tags: list,
prefix: str,
description: str,
pageinfo: PageInfo,
endpoints: dict[str, MethodToEvent],
sub_category: list,
include_in_schema: Optional[bool] = True,
):
self.NAME = name
self.TAGS = tags
self.PREFIX = prefix
self.PAGEINFO = pageinfo
self.DESCRIPTION = description
self.ENDPOINTS = endpoints or {}
self.SUBCATEGORY = sub_category or []
self.INCLUDE_IN_SCHEMA = include_in_schema
@property
def name(self):
return self.NAME
def get_redis_cluster_index_value(self):
"""
RedisCategoryKeys.CLUSTER_2_METHOD_EVENT
Returns the class name and function codes for the class.
"""
dict_cluster_2_method, list_endpoints = {}, [
i.name for i in self.ENDPOINTS.values()
]
for endpoint_name in list_endpoints:
dict_cluster_2_method[endpoint_name] = self.name
dict_cluster_2_method[self.name] = list_endpoints
return dict_cluster_2_method
def retrieve_all_function_codes(self):
"""
Retrieves all function codes by iterating over the events list.
"""
all_function_codes = []
for event_method in self.ENDPOINTS.values():
all_function_codes.extend(
[str(event_key) for event_key in event_method.EVENTS.keys()]
)
return all_function_codes
def retrieve_redis_value(self) -> Dict:
"""
Create Redis Key and Value from function codes
Key(CLUSTER_FUNCTION_CODES:ClusterToMethod) : Value(PAGE_INFO, [FUNCTION_CODE, ...])
"""
return {
f"{RedisCategoryKeys.CLUSTER_FUNCTION_CODES}:{self.name}": self.retrieve_all_function_codes()
}
def retrieve_page_info(self):
"""
PAGE_INFO:ClusterToMethod = {"PageInfo": {...}, "subCategory": PAGE_INFO:ClusterToMethod}
"""
raise NotImplementedError(
"CategoryCluster retrieve_page_info() method is not implemented"
)