redis implemntations and api setup completed
This commit is contained in:
@@ -1,153 +1,8 @@
|
||||
from typing import Any, ClassVar, Dict, List, Optional
|
||||
from abc import abstractmethod
|
||||
from typing import Any, Dict, List, Optional, Callable
|
||||
from uuid import UUID
|
||||
|
||||
from .pageinfo import PageInfo
|
||||
|
||||
|
||||
class Event:
|
||||
|
||||
KEY_: str # static string uuid.uuid4().__str__()
|
||||
RESPONSE_VALIDATOR: ClassVar["PydanticModel"]
|
||||
REQUEST_VALIDATOR: ClassVar["PydanticModel"]
|
||||
DESCRIPTION: str
|
||||
EXTRA_OPTIONS: Optional[Dict[str, Any]] = None
|
||||
|
||||
def __init__(
|
||||
self, key: UUID, request_validator: "PydanticModel", response_validator: "PydanticModel",
|
||||
description: str, extra_options: Optional[Dict[str, Any]] = None
|
||||
) -> None:
|
||||
self.KEY_ = key
|
||||
self.REQUEST_VALIDATOR = request_validator
|
||||
self.RESPONSE_VALIDATOR = response_validator
|
||||
self.DESCRIPTION = description
|
||||
self.EXTRA_OPTIONS = extra_options
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
return f"This is an event of {self.__class__.__name__}. Description: {self.DESCRIPTION}"
|
||||
|
||||
@property
|
||||
def key(self):
|
||||
return str(self.KEY_)
|
||||
|
||||
@abstractmethod
|
||||
def endpoint_callable(request: "Request", data: Any):
|
||||
"""
|
||||
return cls.retrieve_event(event_function_code).retrieve_callable(token_dict, data)
|
||||
"""
|
||||
pass
|
||||
|
||||
|
||||
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)
|
||||
"""
|
||||
EVENTS: list[Event]
|
||||
HEADER_LANGUAGE_MODELS: list[Dict] # [Table.__language_model__ | Dict[__language_model__]]
|
||||
ERRORS_LANGUAGE_MODELS: Optional[list[Dict]] # [Dict[ErrorCode][lang]]
|
||||
|
||||
URL: str
|
||||
METHOD: str
|
||||
SUMMARY: str
|
||||
DESCRIPTION: str
|
||||
EXTRA_OPTIONS: Optional[Dict[str, Any]] = None
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
events: list[Event],
|
||||
headers: list[Dict],
|
||||
url: str,
|
||||
method: str,
|
||||
summary: str,
|
||||
description: str,
|
||||
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.DESCRIPTION = description
|
||||
self.HEADER_LANGUAGE_MODELS = headers
|
||||
self.ERRORS_LANGUAGE_MODELS = errors
|
||||
self.EXTRA_OPTIONS = extra_options
|
||||
|
||||
def retrieve_all_event_keys():
|
||||
"""
|
||||
self.EVENTS.iter()
|
||||
[FUNCTION_CODE]
|
||||
"""
|
||||
pass
|
||||
|
||||
def retrieve_event(event_function_code: str):
|
||||
if list_found := [event for event in self.EVENTS if str(event.key) == event_function_code]:
|
||||
return list_found[0]
|
||||
raise ValueError(f"Event with function code {event_function_code} not found")
|
||||
|
||||
def retrieve_redis_value() -> Dict:
|
||||
"""
|
||||
Key(f"METHOD_FUNCTION_CODES:{ClusterToMethod}:MethodEvent:Endpoint") : Value([FUNCTION_CODE, ...])
|
||||
"""
|
||||
|
||||
pass
|
||||
|
||||
|
||||
class CategoryCluster:
|
||||
|
||||
TAGS: list
|
||||
PREFIX: str
|
||||
PAGEINFO: PageInfo
|
||||
DESCRIPTION: str
|
||||
ENDPOINTS: list = [MethodToEvent]
|
||||
SUBCATEGORY: Optional[List["CategoryCluster"]] = []
|
||||
INCLUDE_IN_SCHEMA: Optional[bool] = True
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
tags: list,
|
||||
prefix: str,
|
||||
description: str,
|
||||
pageinfo: PageInfo,
|
||||
endpoints: list,
|
||||
sub_category: list = [],
|
||||
include_in_schema: Optional[bool] = True,
|
||||
):
|
||||
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
|
||||
|
||||
|
||||
def retrieve_all_function_codes():
|
||||
"""
|
||||
[FUNCTION_CODE, ...]
|
||||
self.ENDPOINTS -> iter()
|
||||
"""
|
||||
pass
|
||||
|
||||
def retrieve_page_info():
|
||||
"""
|
||||
PAGE_INFO:ClusterToMethod = {
|
||||
"PageInfo": {...}
|
||||
"subCategory": PAGE_INFO:ClusterToMethod
|
||||
}
|
||||
PAGE_INFO:ClusterToMethod = {
|
||||
"PageInfo": {...}
|
||||
"subCategory": PAGE_INFO:ClusterToMethod
|
||||
}
|
||||
"""
|
||||
pass
|
||||
|
||||
def retrieve_redis_value() -> Dict:
|
||||
"""
|
||||
Key(CLUSTER_FUNCTION_CODES:ClusterToMethod) : Value(PAGE_INFO, [FUNCTION_CODE, ...])
|
||||
"""
|
||||
pass
|
||||
from ApiLayers.AllConfigs.Redis.configs import RedisCategoryKeys
|
||||
|
||||
|
||||
class PageInfo:
|
||||
@@ -155,7 +10,7 @@ class PageInfo:
|
||||
NAME: str
|
||||
BUTTON_NAME: str
|
||||
PAGE_URL: str
|
||||
PAGEINFO: "PageInfo"
|
||||
PAGEINFO: Dict[str, Any]
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
@@ -173,4 +28,195 @@ class PageInfo:
|
||||
self.PARENT = parent
|
||||
|
||||
|
||||
class Event:
|
||||
|
||||
KEY_: str # static string uuid.uuid4().__str__()
|
||||
RESPONSE_VALIDATOR: Optional[Any]
|
||||
REQUEST_VALIDATOR: Optional[Any]
|
||||
DESCRIPTION: str
|
||||
EXTRA_OPTIONS: Optional[Dict[str, Any]] = None
|
||||
endpoint_callable: Any
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
name: str,
|
||||
key: str | UUID,
|
||||
description: str,
|
||||
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.DESCRIPTION = description
|
||||
self.EXTRA_OPTIONS = extra_options
|
||||
|
||||
@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):
|
||||
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_name: str) -> Dict:
|
||||
"""
|
||||
Key("METHOD_FUNCTION_CODES:{ClusterToMethod}:MethodEvent:Endpoint") : Value([FUNCTION_CODE, ...])
|
||||
"""
|
||||
redis_key = f"{RedisCategoryKeys.METHOD_FUNCTION_CODES}:{cluster_name}:{self.name}:{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: list[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: list[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]
|
||||
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:
|
||||
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"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user