updated events initializer

This commit is contained in:
2025-04-04 12:03:00 +03:00
parent f284d4c61b
commit b1c8203a33
24 changed files with 874 additions and 114 deletions

View File

@@ -0,0 +1,41 @@
from typing import List
from fastapi import APIRouter, FastAPI
class RouteRegisterController:
def __init__(self, app: FastAPI, router_list: List[APIRouter]):
self.router_list = router_list
self.app = app
@staticmethod
def add_router_with_event_to_database(route: APIRouter):
from Schemas import EndpointRestriction
with EndpointRestriction.new_session() as db_session:
route_path = str(getattr(route, "path"))
route_summary = str(getattr(route, "name")) or ""
operation_id = str(getattr(route, "operation_id")) or ""
if not operation_id:
return
for route_method in [method.lower() for method in getattr(route, "methods")]:
restriction = EndpointRestriction.find_or_create(
**dict(
endpoint_method=route_method,
endpoint_name=route_path,
endpoint_desc=route_summary.replace("_", " "),
endpoint_function=route_summary,
operation_uu_id=operation_id, # UUID of the endpoint
is_confirmed=True,
)
)
if not restriction.meta_data.created:
restriction.endpoint_code = f"AR{str(restriction.id).zfill(3)}"
restriction.save(db=db_session)
def register_routes(self):
for router in self.router_list:
self.app.include_router(router)
self.add_router_with_event_to_database(router)
return self.app

View File

@@ -0,0 +1,84 @@
class EventCluster:
def __init__(self, endpoint_uu_id: str):
self.endpoint_uu_id = endpoint_uu_id
self.events = []
def add_event(self, list_of_events: list["Event"]):
"""
Add an event to the cluster
"""
for event in list_of_events:
self.events.append(event)
self.events = list(set(self.events))
def get_event(self, event_key: str):
"""
Get an event by its key
"""
for event in self.events:
if event.key == event_key:
return event
return None
def set_events_to_database(self):
from Schemas import Events, EndpointRestriction
with Events.new_session() as db_session:
if to_save_endpoint := EndpointRestriction.filter_one(
EndpointRestriction.uu_id == self.endpoint_uu_id,
db=db_session,
).data:
for event in self.events:
event_obj = Events.find_or_create(
function_code=event.key,
function_class=event.name,
description=event.description,
endpoint_id=to_save_endpoint.id,
endpoint_uu_id=str(to_save_endpoint.uu_id),
is_confirmed=True,
active=True,
db=db_session,
)
event_obj.save()
print(f'UUID: {event_obj.uu_id} event is saved to {to_save_endpoint.uu_id}')
def match_event(self, event_keys: list[str]) -> "Event":
"""
Match an event by its key
"""
print('set(event_keys)', set(event_keys))
print('event.keys', set([event.key for event in self.events]))
intersection_of_key: set[str] = set(event_keys) & set([event.key for event in self.events])
if not len(intersection_of_key) == 1:
raise ValueError(
f"Event key not found or multiple matches found: {intersection_of_key}"
)
return self.get_event(event_key=list(intersection_of_key)[0])
class Event:
def __init__(
self,
name: str,
key: str,
request_validator: str = None,
response_validator: str = None,
description: str = "",
):
self.name = name
self.key = key
self.request_validator = request_validator
self.response_validator = response_validator
self.description = description
def event_callable(self):
"""
Example callable method
"""
print(self.name)
return {}