starlite.router#
- class starlite.router.Router#
Bases:
objectThe Starlite Router class.
A Router instance is used to group controller, routers and route handler functions under a shared path fragment
- __init__(path: str, *, after_request: Optional[Union[Callable[[Callable[[Union[HTTPScope, WebSocketScope], Callable[[...], Awaitable[Union[HTTPRequestEvent, HTTPDisconnectEvent, WebSocketConnectEvent, WebSocketReceiveEvent, WebSocketDisconnectEvent]]], Callable[[Union[HTTPResponseStartEvent, HTTPResponseBodyEvent, HTTPServerPushEvent, HTTPDisconnectEvent, WebSocketAcceptEvent, WebSocketSendEvent, WebSocketResponseStartEvent, WebSocketResponseBodyEvent, WebSocketCloseEvent]], Awaitable[None]]], Awaitable[None]]], Union[Callable[[Union[HTTPScope, WebSocketScope], Callable[[...], Awaitable[Union[HTTPRequestEvent, HTTPDisconnectEvent, WebSocketConnectEvent, WebSocketReceiveEvent, WebSocketDisconnectEvent]]], Callable[[Union[HTTPResponseStartEvent, HTTPResponseBodyEvent, HTTPServerPushEvent, HTTPDisconnectEvent, WebSocketAcceptEvent, WebSocketSendEvent, WebSocketResponseStartEvent, WebSocketResponseBodyEvent, WebSocketCloseEvent]], Awaitable[None]]], Awaitable[None]], Awaitable[Callable[[Union[HTTPScope, WebSocketScope], Callable[[...], Awaitable[Union[HTTPRequestEvent, HTTPDisconnectEvent, WebSocketConnectEvent, WebSocketReceiveEvent, WebSocketDisconnectEvent]]], Callable[[Union[HTTPResponseStartEvent, HTTPResponseBodyEvent, HTTPServerPushEvent, HTTPDisconnectEvent, WebSocketAcceptEvent, WebSocketSendEvent, WebSocketResponseStartEvent, WebSocketResponseBodyEvent, WebSocketCloseEvent]], Awaitable[None]]], Awaitable[None]]]]], Callable[[Any], Union[Any, Awaitable[Any]]]]] = None, after_response: Optional[Callable[[Any], Union[None, Awaitable[None]]]] = None, before_request: Optional[Callable[[Any], Union[Any, Awaitable[Any]]]] = None, cache_control: Optional[CacheControlHeader] = None, dependencies: Optional[Dict[str, Provide]] = None, etag: Optional[ETag] = None, exception_handlers: Optional[Dict[Union[int, Type[Exception]], Callable[[Any, _ExceptionT], Any]]] = None, guards: Optional[List[Callable[[Any, Any], Union[None, Awaitable[None]]]]] = None, middleware: Optional[List[Union[Callable[[...], Callable[[Union[HTTPScope, WebSocketScope], Callable[[...], Awaitable[Union[HTTPRequestEvent, HTTPDisconnectEvent, WebSocketConnectEvent, WebSocketReceiveEvent, WebSocketDisconnectEvent]]], Callable[[Union[HTTPResponseStartEvent, HTTPResponseBodyEvent, HTTPServerPushEvent, HTTPDisconnectEvent, WebSocketAcceptEvent, WebSocketSendEvent, WebSocketResponseStartEvent, WebSocketResponseBodyEvent, WebSocketCloseEvent]], Awaitable[None]]], Awaitable[None]]], Any, Iterator[Tuple[Callable[[Union[HTTPScope, WebSocketScope], Callable[[...], Awaitable[Union[HTTPRequestEvent, HTTPDisconnectEvent, WebSocketConnectEvent, WebSocketReceiveEvent, WebSocketDisconnectEvent]]], Callable[[Union[HTTPResponseStartEvent, HTTPResponseBodyEvent, HTTPServerPushEvent, HTTPDisconnectEvent, WebSocketAcceptEvent, WebSocketSendEvent, WebSocketResponseStartEvent, WebSocketResponseBodyEvent, WebSocketCloseEvent]], Awaitable[None]]], Awaitable[None]], Dict[str, Any]]], Type[Any]]]] = None, opt: Optional[Dict[str, Any]] = None, parameters: Optional[Dict[str, Any]] = None, response_class: Optional[Type[Any]] = None, response_cookies: Optional[List[Any]] = None, response_headers: Optional[Dict[str, Any]] = None, route_handlers: List[Union[Type[Any], Any, Callable[[...], Any]]], security: Optional[List[Dict[str, List[str]]]] = None, tags: Optional[List[str]] = None, type_encoders: Optional[Dict[Any, Callable[[Any], Any]]] = None) None#
Initialize a
Router.- Parameters:
after_request – A sync or async function executed before a
Requestis passed to any route handler. If this function returns a value, the request will not reach the route handler, and instead this value will be used.after_response – A sync or async function called after the response has been awaited. It receives the
Requestobject and should not return any values.before_request – A sync or async function called immediately before calling the route handler. Receives the
starlite.connection.Requestinstance and any non-Nonereturn value is used for the response, bypassing the route handler.cache_control – A
cache-controlheader of typeCacheControlHeaderto add to route handlers of this router. Can be overridden by route handlers.dependencies – A string keyed dictionary of dependency
Providerinstances.etag – An
etagheader of typeETagto add to route handlers of this router. Can be overridden by route handlers.exception_handlers – A dictionary that maps handler functions to status codes and/or exception types.
guards – A list of
Guardcallables.middleware – A list of
Middleware.opt – A string keyed dictionary of arbitrary values that can be accessed in
Guardsor wherever you have access toRequestorASGI Scope.parameters – A mapping of
Parameterdefinitions available to all application paths.path – A path fragment that is prefixed to all route handlers, controllers and other routers associated with the router instance.
response_class – A custom subclass of [starlite.response.Response] to be used as the default for all route handlers, controllers and other routers associated with the router instance.
response_cookies – A list of [Cookie](starlite.datastructures.Cookie] instances.
response_headers – A string keyed dictionary mapping
ResponseHeaderinstances.route_handlers – A required list of route handlers, which can include instances of
Router, subclasses ofControlleror any function decorated by the route handler decorators.security – A list of dictionaries that will be added to the schema of all route handlers under the router.
tags – A list of string tags that will be appended to the schema of all route handlers under the router.
type_encoders – A mapping of types to callables that transform them into types supported for serialization.
- register(value: Union[Type[Any], Any, Callable[[...], Any]]) List[BaseRoute]#
Register a Controller, Route instance or RouteHandler on the router.