Web service initiated
This commit is contained in:
@@ -8,9 +8,9 @@ class Configs(BaseSettings):
|
||||
"""
|
||||
|
||||
PATH: str = ""
|
||||
HOST: str = ("",)
|
||||
PORT: int = (0,)
|
||||
LOG_LEVEL: str = ("info",)
|
||||
HOST: str = ""
|
||||
PORT: int = 0
|
||||
LOG_LEVEL: str = "info"
|
||||
RELOAD: int = 0
|
||||
ACCESS_TOKEN_TAG: str = ""
|
||||
|
||||
|
||||
@@ -10,6 +10,14 @@ from ApiServices.TemplateService.initializer.create_route import RouteRegisterCo
|
||||
from .config import api_config
|
||||
|
||||
|
||||
def create_events_if_any_cluster_set():
|
||||
import events
|
||||
|
||||
for event_str in events.__all__:
|
||||
if to_set_events := getattr(events, event_str, None):
|
||||
to_set_events.set_events_to_database()
|
||||
|
||||
|
||||
def create_app():
|
||||
|
||||
application = FastAPI(**api_config.api_info)
|
||||
@@ -36,6 +44,6 @@ def create_app():
|
||||
|
||||
route_register = RouteRegisterController(app=application, router_list=get_routes())
|
||||
application = route_register.register_routes()
|
||||
|
||||
create_events_if_any_cluster_set()
|
||||
application.openapi = lambda _=application: create_openapi_schema(_)
|
||||
return application
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
from fastapi import APIRouter, Request, Response
|
||||
|
||||
from ApiServices.TemplateService.events.events_setter import event_cluster
|
||||
from ApiServices.TemplateService.events.template.event import template_event_cluster
|
||||
|
||||
test_template_route = APIRouter(prefix="/test", tags=["Test"])
|
||||
|
||||
@@ -8,30 +8,23 @@ test_template_route = APIRouter(prefix="/test", tags=["Test"])
|
||||
@test_template_route.get(
|
||||
path="/template",
|
||||
description="Test Template Route",
|
||||
operation_id="bb20c8c6-a289-4cab-9da7-34ca8a36c8e5"
|
||||
operation_id="bb20c8c6-a289-4cab-9da7-34ca8a36c8e5",
|
||||
)
|
||||
def test_template(request: Request, response: Response):
|
||||
"""
|
||||
Test Template Route
|
||||
"""
|
||||
headers = dict(request.headers)
|
||||
event_cluster_matched = event_cluster.match_event(
|
||||
event_cluster_matched = template_event_cluster.match_event(
|
||||
event_keys=[
|
||||
"3f510dcf-9f84-4eb9-b919-f582f30adab1",
|
||||
"9f403034-deba-4e1f-b43e-b25d3c808d39",
|
||||
"b8ec6e64-286a-4f60-8554-7a3865454944"
|
||||
"b8ec6e64-286a-4f60-8554-7a3865454944",
|
||||
]
|
||||
)
|
||||
event_cluster_matched.example_callable()
|
||||
response.headers["X-Header"] = "Test Header GET"
|
||||
return {
|
||||
"completed": True,
|
||||
"message": "Test Template Route",
|
||||
"info": {
|
||||
"host": headers.get("host", "Not Found"),
|
||||
"user_agent": headers.get("user-agent", "Not Found"),
|
||||
},
|
||||
}
|
||||
if runner_callable := event_cluster_matched.example_callable():
|
||||
return runner_callable
|
||||
raise ValueError("Event key not found or multiple matches found")
|
||||
|
||||
|
||||
@test_template_route.post(
|
||||
@@ -42,13 +35,14 @@ def test_template_post(request: Request, response: Response):
|
||||
"""
|
||||
Test Template Route with Post Method
|
||||
"""
|
||||
headers = dict(request.headers)
|
||||
event_cluster_matched = template_event_cluster.match_event(
|
||||
event_keys=[
|
||||
"3f510dcf-9f84-4eb9-b919-f582f30adab1",
|
||||
"9f403034-deba-4e1f-b43e-b25d3c808d39",
|
||||
"b8ec6e64-286a-4f60-8554-7a3865454944",
|
||||
]
|
||||
)
|
||||
response.headers["X-Header"] = "Test Header POST"
|
||||
return {
|
||||
"completed": True,
|
||||
"message": "Test Template Route with Post Method",
|
||||
"info": {
|
||||
"host": headers.get("host", "Not Found"),
|
||||
"user_agent": headers.get("user-agent", "Not Found"),
|
||||
},
|
||||
}
|
||||
if runner_callable := event_cluster_matched.example_callable():
|
||||
return runner_callable
|
||||
raise ValueError("Event key not found or multiple matches found")
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
from .template.event import template_event_cluster
|
||||
|
||||
__all__ = [
|
||||
"template_event_cluster",
|
||||
]
|
||||
|
||||
@@ -9,18 +9,21 @@ single_event = Event(
|
||||
description="Example event description",
|
||||
)
|
||||
|
||||
|
||||
def example_callable():
|
||||
"""
|
||||
Example callable method
|
||||
"""
|
||||
return {
|
||||
"completed": True,
|
||||
"message": "Example callable method 2",
|
||||
"info": {
|
||||
"host": "example_host",
|
||||
"user_agent": "example_user_agent",
|
||||
},
|
||||
}
|
||||
"completed": True,
|
||||
"message": "Example callable method 2",
|
||||
"info": {
|
||||
"host": "example_host",
|
||||
"user_agent": "example_user_agent",
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
single_event.event_callable = example_callable
|
||||
|
||||
other_event = Event(
|
||||
@@ -30,28 +33,34 @@ other_event = Event(
|
||||
response_validator=None, # TODO: Add response validator
|
||||
description="Example event 2 description",
|
||||
)
|
||||
|
||||
|
||||
def example_callable_other():
|
||||
"""
|
||||
Example callable method
|
||||
"""
|
||||
return {
|
||||
"completed": True,
|
||||
"message": "Example callable method 1",
|
||||
"info": {
|
||||
"host": "example_host",
|
||||
"user_agent": "example_user_agent",
|
||||
},
|
||||
}
|
||||
"completed": True,
|
||||
"message": "Example callable method 1",
|
||||
"info": {
|
||||
"host": "example_host",
|
||||
"user_agent": "example_user_agent",
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
other_event.event_callable = example_callable_other
|
||||
|
||||
tokens_in_redis = [
|
||||
"3f510dcf-9f84-4eb9-b919-f582f30adab1",
|
||||
"9f403034-deba-4e1f-b43e-b25d3c808d39",
|
||||
"b8ec6e64-286a-4f60-8554-7a3865454944",
|
||||
"176b829c-7622-4cf2-b474-421e5acb637c",
|
||||
]
|
||||
template_event_cluster = EventCluster(endpoint_uu_id="bb20c8c6-a289-4cab-9da7-34ca8a36c8e5")
|
||||
"3f510dcf-9f84-4eb9-b919-f582f30adab1",
|
||||
"9f403034-deba-4e1f-b43e-b25d3c808d39",
|
||||
"b8ec6e64-286a-4f60-8554-7a3865454944",
|
||||
"176b829c-7622-4cf2-b474-421e5acb637c",
|
||||
]
|
||||
template_event_cluster = EventCluster(
|
||||
endpoint_uu_id="bb20c8c6-a289-4cab-9da7-34ca8a36c8e5"
|
||||
)
|
||||
template_event_cluster.add_event([single_event, other_event])
|
||||
matched_event = template_event_cluster.match_event(event_keys=tokens_in_redis)
|
||||
# matched_event = template_event_cluster.match_event(event_keys=tokens_in_redis)
|
||||
|
||||
print('event_callable', matched_event.event_callable())
|
||||
# print('event_callable', matched_event.event_callable())
|
||||
|
||||
@@ -9,30 +9,31 @@ class RouteRegisterController:
|
||||
self.app = app
|
||||
|
||||
@staticmethod
|
||||
def add_router_with_event_to_database(route: APIRouter):
|
||||
def add_router_with_event_to_database(router: 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 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")]:
|
||||
restriction = EndpointRestriction.find_or_create(
|
||||
**dict(
|
||||
for route_method in [
|
||||
method.lower() for method in getattr(route, "methods")
|
||||
]:
|
||||
restriction = EndpointRestriction.find_or_create(
|
||||
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
|
||||
operation_uu_id=operation_id, # UUID of the endpoint
|
||||
is_confirmed=True,
|
||||
db=db_session,
|
||||
)
|
||||
)
|
||||
if not restriction.meta_data.created:
|
||||
restriction.endpoint_code = f"AR{str(restriction.id).zfill(3)}"
|
||||
restriction.save(db=db_session)
|
||||
if restriction.meta_data.created:
|
||||
restriction.save(db=db_session)
|
||||
|
||||
def register_routes(self):
|
||||
for router in self.router_list:
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
|
||||
class EventCluster:
|
||||
|
||||
def __init__(self, endpoint_uu_id: str):
|
||||
@@ -25,33 +24,39 @@ class EventCluster:
|
||||
|
||||
def set_events_to_database(self):
|
||||
from Schemas import Events, EndpointRestriction
|
||||
with Events.new_session() as db_session:
|
||||
|
||||
with Events.new_session() as db_session:
|
||||
if to_save_endpoint := EndpointRestriction.filter_one(
|
||||
EndpointRestriction.uu_id == self.endpoint_uu_id,
|
||||
EndpointRestriction.operation_uu_id == self.endpoint_uu_id,
|
||||
db=db_session,
|
||||
).data:
|
||||
for event in self.events:
|
||||
event_obj = Events.find_or_create(
|
||||
event_to_save_database = Events.find_or_create(
|
||||
function_code=event.key,
|
||||
function_class=event.name,
|
||||
description=event.description,
|
||||
endpoint_code=self.endpoint_uu_id,
|
||||
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}')
|
||||
if event_to_save_database.meta_data.created:
|
||||
event_to_save_database.save(db=db_session)
|
||||
print(
|
||||
f"UUID: {event_to_save_database.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])
|
||||
# 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}"
|
||||
@@ -75,7 +80,6 @@ class Event:
|
||||
self.response_validator = response_validator
|
||||
self.description = description
|
||||
|
||||
|
||||
def event_callable(self):
|
||||
"""
|
||||
Example callable method
|
||||
|
||||
@@ -6,7 +6,7 @@ from ..config import api_config
|
||||
|
||||
async def token_middleware(request: Request, call_next):
|
||||
|
||||
base_url = "/".join(request.url.path.split("/")[:3])
|
||||
base_url = request.url.path
|
||||
safe_endpoints = [_[0] for _ in get_safe_endpoint_urls()]
|
||||
if base_url in safe_endpoints:
|
||||
return await call_next(request)
|
||||
|
||||
@@ -3,7 +3,7 @@ from fastapi import FastAPI
|
||||
from fastapi.routing import APIRoute
|
||||
from fastapi.openapi.utils import get_openapi
|
||||
|
||||
from ApiServices.TemplateService.config import template_api_config
|
||||
from .config import api_config as template_api_config
|
||||
from ApiServices.TemplateService.endpoints.routes import get_safe_endpoint_urls
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user