Skip to content

Commit 8ba004b

Browse files
committed
Revert partial usage and add chema bool
1 parent dee0d94 commit 8ba004b

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

starlette_plus/core.py

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@
1919
import inspect
2020
import logging
2121
from collections.abc import Callable, Coroutine, Iterator, Sequence
22-
from functools import partial
2322
from typing import TYPE_CHECKING, Any, ClassVar, Self, TypeAlias, TypedDict, Unpack
2423

2524
from starlette.applications import Starlette
2625
from starlette.middleware import Middleware
26+
from starlette.requests import Request
27+
from starlette.responses import Response
2728
from starlette.routing import Mount, Route, WebSocketRoute
2829
from starlette.types import Receive, Scope, Send
2930

@@ -90,6 +91,16 @@ def __init__(self, **kwargs: Unpack[RouteOptions]) -> None:
9091
self._limits: list[RateLimitData] = kwargs.get("limits", [])
9192
self._is_websocket: bool = kwargs.get("websocket", False)
9293
self._view: View | None = None
94+
self._include_in_schema: bool = kwargs["include_in_schema"]
95+
96+
async def __call__(self, scope: Scope, receive: Receive, send: Send) -> Any:
97+
request: Request = Request(scope, receive, send)
98+
response: Response | None = await self._coro(self._view, request)
99+
100+
if not response:
101+
return Response(status_code=204)
102+
103+
await response(scope, receive, send)
93104

94105

95106
LimitDecorator: TypeAlias = Callable[..., RouteCoro] | _Route
@@ -103,6 +114,7 @@ def route(
103114
methods: Methods = ["GET"],
104115
prefix: bool = True,
105116
websocket: bool = False,
117+
include_in_schema: bool = True,
106118
) -> Callable[..., _Route]:
107119
def decorator(coro: Callable[..., RouteCoro]) -> _Route:
108120
if not asyncio.iscoroutinefunction(coro):
@@ -113,7 +125,15 @@ def decorator(coro: Callable[..., RouteCoro]) -> _Route:
113125
raise ValueError(f"Route callback function must not be named any: {', '.join(disallowed)}")
114126

115127
limits: list[RateLimitData] = getattr(coro, "__limits__", [])
116-
return _Route(path=path, coro=coro, methods=methods, prefix=prefix, limits=limits, websocket=websocket)
128+
return _Route(
129+
path=path,
130+
coro=coro,
131+
methods=methods,
132+
prefix=prefix,
133+
limits=limits,
134+
websocket=websocket,
135+
include_in_schema=include_in_schema,
136+
)
117137

118138
return decorator
119139

@@ -186,14 +206,13 @@ def __new__(cls, *args: Any, **kwargs: Any) -> Self:
186206
setattr(member, method, member._coro)
187207

188208
new: WebSocketRoute | Route
189-
endpoint: partial[RouteCoro] = partial(member._coro, self)
190209

191210
if member._is_websocket:
192-
new = WebSocketRoute(path=path, endpoint=endpoint, name=f"{name}.{member._coro.__name__}")
211+
new = WebSocketRoute(path=path, endpoint=member, name=f"{name}.{member._coro.__name__}")
193212
else:
194213
new = Route(
195214
path=path,
196-
endpoint=endpoint,
215+
endpoint=member,
197216
methods=member._methods,
198217
name=f"{name}.{member._coro.__name__}",
199218
)
@@ -262,14 +281,13 @@ def __new__(cls, *args: Any, **kwargs: Any) -> Self:
262281
setattr(member, method, member._coro)
263282

264283
new: WebSocketRoute | Route
265-
endpoint: partial[RouteCoro] = partial(member._coro, self)
266284

267285
if member._is_websocket:
268-
new = WebSocketRoute(path=path, endpoint=endpoint, name=f"{name}.{member._coro.__name__}")
286+
new = WebSocketRoute(path=path, endpoint=member, name=f"{name}.{member._coro.__name__}")
269287
else:
270288
new = Route(
271289
path=path,
272-
endpoint=endpoint,
290+
endpoint=member,
273291
methods=member._methods,
274292
name=f"{name}.{member._coro.__name__}",
275293
)

starlette_plus/types_/core.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,4 @@ class RouteOptions(TypedDict):
3232
prefix: bool
3333
websocket: bool
3434
limits: list[RateLimitData]
35+
include_in_schema: bool

0 commit comments

Comments
 (0)