Coverage for src / lexigram / contracts / web / guard.py: 100%

7 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-08-19 05:41 +0800

1"""Web guard protocols. 

2 

3Defines the core interface for guards that protect routes. 

4""" 

5 

6from __future__ import annotations 

7 

8from typing import TYPE_CHECKING, Any, Protocol, runtime_checkable 

9 

10if TYPE_CHECKING: 

11 from lexigram.contracts.web.execution_context import ExecutionContextProtocol 

12 

13 

14@runtime_checkable 

15class GuardProtocol(Protocol): 

16 """Protocol for web guards. 

17 

18 Guards determine whether a request should be handled by a specific route. 

19 They run before interceptors and handlers. 

20 """ 

21 

22 async def can_activate(self, context: ExecutionContextProtocol) -> bool: 

23 """Determine if the request can proceed. 

24 

25 Args: 

26 context: The execution context for the request. 

27 

28 Returns: 

29 True if the request is allowed, False otherwise. 

30 """ 

31 ... 

32 

33 async def handle_rejection(self, context: ExecutionContextProtocol) -> Any: 

34 """Handle guard rejection by returning a response. 

35 

36 Args: 

37 context: The execution context for the request. 

38 

39 Returns: 

40 A response object indicating the rejection. 

41 """ 

42 ... 

43 

44 

45__all__ = ["GuardProtocol"]