starlite.router#

class starlite.router.Router#

Bases: object

The 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 Request is 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 Request object and should not return any values.

  • before_request – A sync or async function called immediately before calling the route handler. Receives the starlite.connection.Request instance and any non-None return value is used for the response, bypassing the route handler.

  • cache_control – A cache-control header of type CacheControlHeader to add to route handlers of this router. Can be overridden by route handlers.

  • dependencies – A string keyed dictionary of dependency Provider instances.

  • etag – An etag header of type ETag to 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 Guard callables.

  • middleware – A list of Middleware.

  • opt – A string keyed dictionary of arbitrary values that can be accessed in Guards or wherever you have access to Request or ASGI Scope.

  • parameters – A mapping of Parameter definitions 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 ResponseHeader instances.

  • route_handlers – A required list of route handlers, which can include instances of Router, subclasses of Controller or 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.

Parameters:

value – a subclass or instance of Controller, an instance of Router or a function/method that has been decorated by any of the routing decorators, e.g. get, post.

Returns:

Collection of handlers added to the router.

property route_handler_method_map: Dict[str, Dict[Union[Literal['GET', 'POST', 'DELETE', 'PATCH', 'PUT', 'HEAD', 'TRACE', 'OPTIONS'], Literal['websocket'], Literal['asgi']], Any]]#

Map route paths to RouteHandlerMapItem

Returns:

A dictionary mapping paths to route handlers

classmethod get_route_handler_map(value: Union[Controller, Any, Router]) Dict[str, Dict[Union[Literal['GET', 'POST', 'DELETE', 'PATCH', 'PUT', 'HEAD', 'TRACE', 'OPTIONS'], Literal['websocket'], Literal['asgi']], Any]]#

Map route handlers to HTTP methods.