Middleware object that is used to encapsulate middleware functions. This should generally not be instantiated directly, but rather through the `sanic.Sanic.middleware` decorator and its variants. Args: func (MiddlewareType): The middleware function to be called. locatio
| 14 | |
| 15 | |
| 16 | class Middleware: |
| 17 | """Middleware object that is used to encapsulate middleware functions. |
| 18 | |
| 19 | This should generally not be instantiated directly, but rather through |
| 20 | the `sanic.Sanic.middleware` decorator and its variants. |
| 21 | |
| 22 | Args: |
| 23 | func (MiddlewareType): The middleware function to be called. |
| 24 | location (MiddlewareLocation): The location of the middleware. |
| 25 | priority (int): The priority of the middleware. |
| 26 | """ |
| 27 | |
| 28 | _counter = count() |
| 29 | count: int |
| 30 | |
| 31 | __slots__ = ("func", "priority", "location", "definition") |
| 32 | |
| 33 | def __init__( |
| 34 | self, |
| 35 | func: MiddlewareType, |
| 36 | location: MiddlewareLocation, |
| 37 | priority: int = 0, |
| 38 | ) -> None: |
| 39 | self.func = func |
| 40 | self.priority = priority |
| 41 | self.location = location |
| 42 | self.definition = next(Middleware._counter) |
| 43 | |
| 44 | def __call__(self, *args, **kwargs): |
| 45 | return self.func(*args, **kwargs) |
| 46 | |
| 47 | def __hash__(self) -> int: |
| 48 | return hash(self.func) |
| 49 | |
| 50 | def __repr__(self) -> str: |
| 51 | return ( |
| 52 | f"{self.__class__.__name__}(" |
| 53 | f"func=<function {self.func.__name__}>, " |
| 54 | f"priority={self.priority}, " |
| 55 | f"location={self.location.name})" |
| 56 | ) |
| 57 | |
| 58 | @property |
| 59 | def order(self) -> tuple[int, int]: |
| 60 | """Return a tuple of the priority and definition order. |
| 61 | |
| 62 | This is used to sort the middleware. |
| 63 | |
| 64 | Returns: |
| 65 | tuple[int, int]: The priority and definition order. |
| 66 | """ |
| 67 | return (self.priority, -self.definition) |
| 68 | |
| 69 | @classmethod |
| 70 | def convert( |
| 71 | cls, |
| 72 | *middleware_collections: Sequence[Middleware | MiddlewareType], |
| 73 | location: MiddlewareLocation, |
no outgoing calls
searching dependent graphs…