refactor(db)!: finalize pooled WNDB v2 migration
This commit is contained in:
+12
-12
@@ -11,6 +11,7 @@ from fastapi import APIRouter, Depends, Query
|
||||
from fastapi.encoders import jsonable_encoder
|
||||
from fastapi.routing import APIRoute
|
||||
from pydantic import BaseModel, JsonValue, create_model
|
||||
from starlette.concurrency import run_in_threadpool
|
||||
from starlette.responses import Response
|
||||
|
||||
from app.api.problem_details import ProblemDetails
|
||||
@@ -38,6 +39,14 @@ class Page(BaseModel, Generic[T]):
|
||||
offset: int
|
||||
|
||||
|
||||
async def _call_endpoint(endpoint, *args, **kwargs):
|
||||
"""Call async handlers directly and offload synchronous handlers."""
|
||||
if inspect.iscoroutinefunction(endpoint):
|
||||
return await endpoint(*args, **kwargs)
|
||||
result = await run_in_threadpool(endpoint, *args, **kwargs)
|
||||
return await result if inspect.isawaitable(result) else result
|
||||
|
||||
|
||||
_NAME_IS_NETWORK = {
|
||||
"pressure_sensor_placement_sensitivity_endpoint",
|
||||
"pressure_sensor_placement_kmeans_endpoint",
|
||||
@@ -59,7 +68,6 @@ _TIMESCALE_ROUTED_ENDPOINT_MODULES = {
|
||||
"app.api.v1.endpoints.leakage",
|
||||
"app.api.v1.endpoints.simulation",
|
||||
}
|
||||
_TIMESCALE_ROUTED_ENDPOINT_NAMES = {"open_project_endpoint"}
|
||||
|
||||
|
||||
def _clean_name(name: str) -> str:
|
||||
@@ -183,10 +191,7 @@ def _with_header_project_context(endpoint, route_name: str):
|
||||
if model_has_username:
|
||||
kwargs.pop(injected_user_name, None)
|
||||
with activate_project_routing(project_routing):
|
||||
result = endpoint(*args, **kwargs)
|
||||
if inspect.isawaitable(result):
|
||||
return await result
|
||||
return result
|
||||
return await _call_endpoint(endpoint, *args, **kwargs)
|
||||
|
||||
parameters = []
|
||||
for name, parameter in signature.parameters.items():
|
||||
@@ -206,7 +211,6 @@ def _with_header_project_context(endpoint, route_name: str):
|
||||
get_project_simulation_routing
|
||||
if (
|
||||
endpoint.__module__ in _TIMESCALE_ROUTED_ENDPOINT_MODULES
|
||||
or endpoint.__name__ in _TIMESCALE_ROUTED_ENDPOINT_NAMES
|
||||
)
|
||||
else get_project_business_routing
|
||||
)
|
||||
@@ -262,9 +266,7 @@ def _with_pagination(endpoint):
|
||||
else:
|
||||
limit = kwargs.pop("_rest_limit")
|
||||
offset = kwargs.pop("_rest_offset")
|
||||
result = endpoint(*args, **kwargs)
|
||||
if inspect.isawaitable(result):
|
||||
result = await result
|
||||
result = await _call_endpoint(endpoint, *args, **kwargs)
|
||||
if not isinstance(result, list):
|
||||
return result
|
||||
if handler_handles_pagination:
|
||||
@@ -313,9 +315,7 @@ def _with_jsonable_response(endpoint):
|
||||
|
||||
@wraps(endpoint)
|
||||
async def wrapper(*args, **kwargs):
|
||||
result = endpoint(*args, **kwargs)
|
||||
if inspect.isawaitable(result):
|
||||
result = await result
|
||||
result = await _call_endpoint(endpoint, *args, **kwargs)
|
||||
if isinstance(result, Response):
|
||||
return result
|
||||
return jsonable_encoder(result)
|
||||
|
||||
Reference in New Issue
Block a user