Replace deprecated ASGIRoute import

This commit is contained in:
latinogino
2025-12-31 14:11:06 +01:00
parent 29ea1c7695
commit c9e482ca2f

View File

@@ -20,7 +20,8 @@ from .dolibarr_client import DolibarrClient, DolibarrAPIError
# HTTP transport imports # HTTP transport imports
from starlette.applications import Starlette from starlette.applications import Starlette
from starlette.responses import Response from starlette.responses import Response
from starlette.routing import ASGIRoute, Route from starlette.routing import Route
from starlette.types import Receive, Scope, Send
import uvicorn import uvicorn
@@ -1447,6 +1448,15 @@ async def _run_stdio_server(_config: Config) -> None:
def _build_http_app(session_manager: StreamableHTTPSessionManager) -> Starlette: def _build_http_app(session_manager: StreamableHTTPSessionManager) -> Starlette:
"""Create Starlette app that forwards to the StreamableHTTP session manager.""" """Create Starlette app that forwards to the StreamableHTTP session manager."""
class ASGIEndpoint:
"""Lightweight adapter so Route treats our handler as an ASGI app."""
def __init__(self, handler):
self.handler = handler
async def __call__(self, scope: Scope, receive: Receive, send: Send):
await self.handler(scope, receive, send)
async def options_handler(request): async def options_handler(request):
"""Lightweight CORS-friendly response for preflight requests.""" """Lightweight CORS-friendly response for preflight requests."""
return Response(status_code=204) return Response(status_code=204)
@@ -1459,12 +1469,14 @@ def _build_http_app(session_manager: StreamableHTTPSessionManager) -> Starlette:
"""Adapter to call the StreamableHTTPSessionManager with ASGI signature.""" """Adapter to call the StreamableHTTPSessionManager with ASGI signature."""
await session_manager.handle_request(scope, receive, send) await session_manager.handle_request(scope, receive, send)
asgi_endpoint = ASGIEndpoint(asgi_handler)
return Starlette( return Starlette(
routes=[ routes=[
ASGIRoute("/", asgi_handler, methods=["GET", "POST", "DELETE"]), Route("/", asgi_endpoint, methods=["GET", "POST", "DELETE"]),
ASGIRoute("/{path:path}", asgi_handler, methods=["GET", "POST", "DELETE"]), Route("/{path:path}", asgi_endpoint, methods=["GET", "POST", "DELETE"]),
ASGIRoute("/", options_handler, methods=["OPTIONS"]), Route("/", options_handler, methods=["OPTIONS"]),
ASGIRoute("/{path:path}", options_handler, methods=["OPTIONS"]), Route("/{path:path}", options_handler, methods=["OPTIONS"]),
], ],
lifespan=lifespan, lifespan=lifespan,
) )