Skip to content

Commit dd1e115

Browse files
committed
Added route to request scope
1 parent e774597 commit dd1e115

5 files changed

Lines changed: 14 additions & 35 deletions

File tree

debug_toolbar/middleware.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from debug_toolbar.api import render_panel
1616
from debug_toolbar.settings import DebugToolbarSettings
1717
from debug_toolbar.toolbar import DebugToolbar
18-
from debug_toolbar.utils import import_string
18+
from debug_toolbar.utils import import_string, matched_route
1919

2020

2121
def show_toolbar(request: Request, settings: DebugToolbarSettings) -> bool:
@@ -60,8 +60,11 @@ async def dispatch(
6060
request: Request,
6161
call_next: RequestResponseEndpoint,
6262
) -> Response:
63+
request.scope["route"] = matched_route(request)
64+
6365
if (
64-
not self.show_toolbar(request, self.settings)
66+
not request.scope["route"]
67+
or not self.show_toolbar(request, self.settings)
6568
or self.settings.API_URL in request.url.path
6669
):
6770
return await call_next(request)

debug_toolbar/panels/logging.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from debug_toolbar.panels import Panel
99
from debug_toolbar.types import Stats
10-
from debug_toolbar.utils import is_coroutine, matched_endpoint, pluralize
10+
from debug_toolbar.utils import is_coroutine, pluralize
1111

1212
try:
1313
import threading
@@ -92,12 +92,7 @@ def nav_subtitle(self) -> str:
9292
return f"{record_count} message{pluralize(record_count)}"
9393

9494
async def process_request(self, request: Request) -> Response:
95-
endpoint = matched_endpoint(request)
96-
97-
if endpoint is None:
98-
return await super().process_request(request)
99-
100-
if is_coroutine(endpoint):
95+
if is_coroutine(request.scope["route"].endpoint):
10196
self.thread_id = threading.get_ident()
10297
else:
10398
self.thread_id = await run_in_threadpool(threading.get_ident)

debug_toolbar/panels/profiling.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from debug_toolbar.panels import Panel
88
from debug_toolbar.types import Stats
9-
from debug_toolbar.utils import is_coroutine, matched_endpoint
9+
from debug_toolbar.utils import is_coroutine
1010

1111

1212
class ProfilingPanel(Panel):
@@ -15,12 +15,7 @@ class ProfilingPanel(Panel):
1515

1616
async def process_request(self, request: Request) -> Response:
1717
self.profiler = Profiler(**self.toolbar.settings.PROFILER_OPTIONS)
18-
endpoint = matched_endpoint(request)
19-
20-
if endpoint is None:
21-
return await super().process_request(request)
22-
23-
is_async = is_coroutine(endpoint)
18+
is_async = is_coroutine(request.scope["route"].endpoint)
2419

2520
async def call(func: t.Callable) -> None:
2621
await run_in_threadpool(func) if not is_async else func()

debug_toolbar/panels/sqlalchemy.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,12 @@
44
from fastapi import Request, Response
55
from fastapi.concurrency import AsyncExitStack
66
from fastapi.dependencies.utils import solve_dependencies
7-
from fastapi.routing import APIRoute
87
from sqlalchemy import event
98
from sqlalchemy.engine import Connection, Engine
109
from sqlalchemy.engine.default import DefaultExecutionContext
1110
from sqlalchemy.orm import Session
1211

1312
from debug_toolbar.panels.sql import SQLPanel
14-
from debug_toolbar.utils import matched_route
1513

1614

1715
class SQLAlchemyPanel(SQLPanel):
@@ -55,11 +53,9 @@ def after_execute(
5553

5654
async def process_request(self, request: Request) -> Response:
5755
engines: t.Set[Engine] = set()
58-
route = matched_route(request)
56+
route = request.scope["route"]
5957

6058
if hasattr(route, "dependant"):
61-
route = t.cast(APIRoute, route)
62-
6359
if request.scope.get("fastapi_astack") is None:
6460
async with AsyncExitStack() as stack:
6561
request.scope["fastapi_astack"] = stack

debug_toolbar/utils.py

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from fastapi.routing import APIRoute
99
from pydantic.color import Color
1010
from starlette.routing import Match
11-
from starlette.staticfiles import StaticFiles
1211

1312

1413
def import_string(import_name: str) -> t.Any:
@@ -43,20 +42,11 @@ def get_name_from_obj(obj: t.Any) -> str:
4342
def matched_route(request: Request) -> t.Optional[APIRoute]:
4443
for route in request.app.routes:
4544
match, _ = route.matches(request.scope)
46-
if match == Match.FULL:
47-
return route
48-
return None
49-
5045

51-
def matched_endpoint(request: Request) -> t.Optional[t.Callable]:
52-
route = matched_route(request)
53-
if route is not None:
54-
endpoint = getattr(route, "endpoint", None)
55-
56-
if endpoint is not None:
57-
return endpoint
58-
if not isinstance(route.app, StaticFiles):
59-
return route.app
46+
if match == Match.FULL:
47+
if hasattr(route, "endpoint"):
48+
return route
49+
break
6050
return None
6151

6252

0 commit comments

Comments
 (0)