base#

class starlite.middleware.base.MiddlewareProtocol#

Bases: Protocol

Abstract middleware protocol.

async __call__(scope: Scope, receive: Receive, send: Send) None#

Execute the ASGI middleware.

Called by the previous middleware in the stack if a response is not awaited prior.

Upon completion, middleware should call the next ASGI handler and await it - or await a response created in its closure.

Parameters:
  • scope – The ASGI connection scope.

  • receive – The ASGI receive function.

  • send – The ASGI send function.

Returns:

None

__init__(*args, **kwargs)#
class starlite.middleware.base.DefineMiddleware#

Bases: object

Container enabling passing *args and **kwargs to Middleware class constructors and factory functions.

__init__(middleware: Callable[[...], ASGIApp], *args: Any, **kwargs: Any) None#

Initialize DefineMiddleware.

Parameters:
  • middleware – A callable that returns an ASGIApp.

  • *args – Positional arguments to pass to the callable.

  • **kwargs – Key word arguments to pass to the callable.

Notes

The callable will be passed a kwarg app, which is the next ASGI app to call in the middleware stack. It therefore must define such a kwarg.

__call__(app: ASGIApp) ASGIApp#

Call the middleware constructor or factory.

Parameters:

app – An ASGIApp, this value is the next ASGI handler to call in the middleware stack.

Returns:

Calls DefineMiddleware.middleware and returns the ASGIApp created.

class starlite.middleware.base.AbstractMiddleware#

Bases: object

Abstract middleware providing base functionality common to all middlewares, for dynamically engaging/bypassing the middleware based on paths, opt-keys and scope types.

When implementing new middleware, this class should be used as a base.

__init__(app: ASGIApp, exclude: Optional[Union[str, List[str]]] = None, exclude_opt_key: Optional[str] = None, scopes: Optional[Scopes] = None) None#

Initialize the middleware.

Parameters:
  • app – The next ASGI app to call.

  • exclude – A pattern or list of patterns to match against a request’s path. If a match is found, the middleware will be skipped.

  • exclude_opt_key – An identifier that is set in the route handler opt key which allows skipping the middleware.

  • scopes – ASGI scope types, should be a set including either or both ‘ScopeType.HTTP’ and ‘ScopeType.WEBSOCKET’.

abstract async __call__(scope: Scope, receive: Receive, send: Send) None#

Execute the ASGI middleware.

Called by the previous middleware in the stack if a response is not awaited prior.

Upon completion, middleware should call the next ASGI handler and await it - or await a response created in its closure.

Parameters:
  • scope – The ASGI connection scope.

  • receive – The ASGI receive function.

  • send – The ASGI send function.

Returns:

None