sanic.middleware.Middleware#

Middleware object that is used to encapsulate middleware functions.

class Middleware(func: MiddlewareType, location: MiddlewareLocation, priority: int = 0): -> None

This should generally not be instantiated directly, but rather through the sanic.Sanic.middleware decorator and its variants.

Parameters
func
MiddlewareType

The middleware function to be called.

location
MiddlewareLocation

The location of the middleware.

priority
int

The priority of the middleware.

convert#

Convert middleware collections to a deque of Middleware objects.

@classmethod
def convert(middleware_collections: Sequence[Union[Middleware, MiddlewareType]], location: MiddlewareLocation): -> Deque[Middleware]

Parameters
*middleware_collections
Sequence[Union[Middleware, MiddlewareType]]

The middleware collections to convert.

location
MiddlewareLocation

The location of the middleware.

Return
Deque[Middleware]

The converted middleware.

order#

Return a tuple of the priority and definition order.

@property
def order(self): -> tuple[int, int]

This is used to sort the middleware.

Return
tuple[int, int]

The priority and definition order.

reset_count#

Reset the counter for the middleware definition order.

@classmethod
def reset_count(): -> None

This is used for testing.

sanic.middleware.MiddlewareLocation#

Enum where members are also (and must be) ints

Inherits from: IntEnum, int, ReprEnum, Enum

class MiddlewareLocation(value, names = None, module = None, qualname = None, type = None, start = 1, boundary = None)