50 lines
2.1 KiB
Python
50 lines
2.1 KiB
Python
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(router: APIRouter):
|
|
from Schemas import EndpointRestriction
|
|
|
|
with EndpointRestriction.new_session() as db_session:
|
|
for route in router.routes:
|
|
route_path = str(getattr(route, "path"))
|
|
route_summary = str(getattr(route, "name"))
|
|
operation_id = getattr(route, "operation_id", None)
|
|
if not operation_id:
|
|
continue
|
|
|
|
for route_method in [
|
|
method.lower() for method in getattr(route, "methods")
|
|
]:
|
|
add_or_update_dict = dict(
|
|
endpoint_method=route_method,
|
|
endpoint_name=route_path,
|
|
endpoint_desc=route_summary.replace("_", " "),
|
|
endpoint_function=route_summary,
|
|
operation_uu_id=operation_id,
|
|
is_confirmed=True,
|
|
)
|
|
endpoint_restriction_found = EndpointRestriction.filter_one_system(
|
|
EndpointRestriction.operation_uu_id == operation_id, db=db_session,
|
|
).data
|
|
if endpoint_restriction_found:
|
|
endpoint_restriction_found.update(**add_or_update_dict, db=db_session)
|
|
endpoint_restriction_found.save(db=db_session)
|
|
else:
|
|
restriction = EndpointRestriction.find_or_create(**add_or_update_dict, db=db_session)
|
|
if restriction.meta_data.created:
|
|
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
|