Coverage for src / lexigram / contracts / web / middleware / registry_protocol.py: 0%

14 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-08-15 18:57 +0800

1"""Web middleware registry protocol for cross-package middleware registration.""" 

2 

3from __future__ import annotations 

4 

5from collections.abc import Callable 

6from typing import Any, Protocol 

7 

8if True: 

9 Scope = dict[str, Any] 

10 Receive = Any 

11 Send = Any 

12 ASGIApp = Callable[[Scope, Receive, Send], Any] 

13 MiddlewareFactory = Callable[[ASGIApp], ASGIApp] 

14 

15 

16class MiddlewareRegistryProtocol(Protocol): 

17 """Protocol for registering ASGI middleware. 

18 

19 This protocol defines the interface for middleware registration 

20 used by cross-package integrations like tenant context bridging. 

21 

22 Supports two registration patterns: 

23 - Class-based: register_middleware(MyMiddlewareClass) 

24 - Factory-based: register_middleware_factory(lambda app: MyMiddleware(app)) 

25 """ 

26 

27 def register_middleware( 

28 self, 

29 middleware_class: type[Any], 

30 *, 

31 priority: int = 0, 

32 **options: Any, 

33 ) -> None: 

34 """Register an ASGI middleware class. 

35 

36 Args: 

37 middleware_class: ASGI middleware class to register. 

38 priority: Middleware priority (higher runs earlier). 

39 **options: Additional middleware options. 

40 """ 

41 ... 

42 

43 def register_middleware_factory(self, factory: MiddlewareFactory) -> None: 

44 """Register an ASGI middleware factory function. 

45 

46 This is useful for cross-package integrations that need to 

47 wrap an app with middleware at registration time. 

48 

49 Args: 

50 factory: A callable that takes an ASGI app and returns 

51 a wrapped ASGI app with middleware applied. 

52 """ 

53 ... 

54 

55 def get_middleware_stack(self) -> list[Callable[..., Any]]: 

56 """Get the registered middleware in composition order. 

57 

58 Returns: 

59 List of middleware classes/factories. 

60 """ 

61 ... 

62 

63 

64__all__ = ["MiddlewareRegistryProtocol"]