Skip to content

Commit

Permalink
Update removal of decorators
Browse files Browse the repository at this point in the history
  • Loading branch information
Kludex committed Oct 12, 2022
1 parent a080271 commit 2e3540b
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
41 changes: 41 additions & 0 deletions starlette/applications.py
Expand Up @@ -2,6 +2,7 @@

from starlette.datastructures import State, URLPath
from starlette.middleware import Middleware
from starlette.middleware.base import BaseHTTPMiddleware
from starlette.middleware.errors import ServerErrorMiddleware
from starlette.middleware.exceptions import ExceptionMiddleware
from starlette.requests import Request
Expand Down Expand Up @@ -121,3 +122,43 @@ def url_path_for(self, name: str, **path_params: typing.Any) -> URLPath:
async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
scope["app"] = self
await self.middleware_stack(scope, receive, send)

def mount(
self, path: str, app: ASGIApp, name: typing.Optional[str] = None
) -> None: # pragma: nocover
self.router.mount(path, app=app, name=name)

def host(
self, host: str, app: ASGIApp, name: typing.Optional[str] = None
) -> None: # pragma: no cover
self.router.host(host, app=app, name=name)

def add_exception_handler(
self,
exc_class_or_status_code: typing.Union[int, typing.Type[Exception]],
handler: typing.Callable,
) -> None: # pragma: no cover
self.exception_handlers[exc_class_or_status_code] = handler
self.middleware_stack = self.build_middleware_stack()

def add_event_handler(
self, event_type: str, func: typing.Callable
) -> None: # pragma: no cover
self.router.add_event_handler(event_type, func)

def add_route(
self,
path: str,
route: typing.Callable,
methods: typing.Optional[typing.List[str]] = None,
name: typing.Optional[str] = None,
include_in_schema: bool = True,
) -> None: # pragma: no cover
self.router.add_route(
path, route, methods=methods, name=name, include_in_schema=include_in_schema
)

def add_websocket_route(
self, path: str, route: typing.Callable, name: typing.Optional[str] = None
) -> None: # pragma: no cover
self.router.add_websocket_route(path, route, name=name)
45 changes: 45 additions & 0 deletions starlette/routing.py
Expand Up @@ -721,3 +721,48 @@ async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:

def __eq__(self, other: typing.Any) -> bool:
return isinstance(other, Router) and self.routes == other.routes

def mount(
self, path: str, app: ASGIApp, name: typing.Optional[str] = None
) -> None: # pragma: nocover
route = Mount(path, app=app, name=name)
self.routes.append(route)

def host(
self, host: str, app: ASGIApp, name: typing.Optional[str] = None
) -> None: # pragma: no cover
route = Host(host, app=app, name=name)
self.routes.append(route)

def add_route(
self,
path: str,
endpoint: typing.Callable,
methods: typing.Optional[typing.List[str]] = None,
name: typing.Optional[str] = None,
include_in_schema: bool = True,
) -> None: # pragma: nocover
route = Route(
path,
endpoint=endpoint,
methods=methods,
name=name,
include_in_schema=include_in_schema,
)
self.routes.append(route)

def add_websocket_route(
self, path: str, endpoint: typing.Callable, name: typing.Optional[str] = None
) -> None: # pragma: no cover
route = WebSocketRoute(path, endpoint=endpoint, name=name)
self.routes.append(route)

def add_event_handler(
self, event_type: str, func: typing.Callable
) -> None: # pragma: no cover
assert event_type in ("startup", "shutdown")

if event_type == "startup":
self.on_startup.append(func)
else:
self.on_shutdown.append(func)

0 comments on commit 2e3540b

Please sign in to comment.