12
12
13
13
@dataclass
14
14
class InterceptorRequest :
15
- """Container for request data that can be modified by interceptors """
15
+ """Request data container for interceptor processing """
16
16
method : str
17
17
url : str
18
18
headers : Dict [str , str ]
@@ -21,84 +21,49 @@ class InterceptorRequest:
21
21
22
22
@dataclass
23
23
class InterceptorResponse (Generic [T ]):
24
- """Container for response data that can be processed by interceptors """
24
+ """Response data container for interceptor processing """
25
25
status_code : int
26
26
headers : Dict [str , str ]
27
27
body : T
28
28
request : InterceptorRequest
29
29
raw_response : httpx .Response
30
30
31
31
class Interceptor (ABC ):
32
- """Base class for implementing request/response interceptors"""
32
+ """Base class for request/response interceptors"""
33
33
34
34
@abstractmethod
35
35
def before_request (self , request : InterceptorRequest ) -> InterceptorRequest :
36
- """Process and optionally modify the request before it is sent.
37
-
38
- Args:
39
- request: The request to process
40
-
41
- Returns:
42
- The processed request
43
- """
36
+ """Process request before sending"""
44
37
pass
45
38
46
39
@abstractmethod
47
40
def after_response (self , response : InterceptorResponse [T ]) -> InterceptorResponse [T ]:
48
- """Process and optionally modify the response after it is received.
49
-
50
- Args:
51
- response: The response to process
52
-
53
- Returns:
54
- The processed response
55
- """
41
+ """Process response after receiving"""
56
42
pass
57
43
58
44
class InterceptorChain :
59
- """Manages a chain of interceptors that process requests/responses in sequence """
45
+ """Chain of interceptors for sequential request/response processing """
60
46
61
47
def __init__ (self , interceptors : Optional [list [Interceptor ]] = None ):
62
48
self ._interceptors = interceptors or []
63
49
64
50
def add_interceptor (self , interceptor : Interceptor ) -> None :
65
- """Add an interceptor to the chain"""
66
51
self ._interceptors .append (interceptor )
67
52
68
53
def execute_before_request (self , request : InterceptorRequest ) -> InterceptorRequest :
69
- """Execute all interceptors' before_request methods in sequence"""
70
- print ("\n === Intercepted Request ===" )
71
- print (f"Method: { request .method } " )
72
- print (f"URL: { request .url } " )
73
- print (f"Headers: { request .headers } " )
74
- if request .params :
75
- print (f"Query Params: { request .params } " )
76
- if request .body :
77
- print (f"Request Body: { request .body } " )
78
- print ("========================\n " )
79
-
80
54
current_request = request
81
55
for interceptor in self ._interceptors :
82
56
try :
83
57
current_request = interceptor .before_request (current_request )
84
58
except Exception as e :
85
- # Log error but continue processing
86
- print (f"Error in interceptor { interceptor .__class__ .__name__ } : { e } " )
59
+ continue
87
60
return current_request
88
61
89
62
def execute_after_response (self , response : InterceptorResponse [T ]) -> InterceptorResponse [T ]:
90
- """Execute all interceptors' after_response methods in sequence"""
91
- print ("\n === Intercepted Response ===" )
92
- print (f"Status Code: { response .status_code } " )
93
- print (f"Headers: { response .headers } " )
94
- print (f"Response Body: { response .body } " )
95
- print ("=========================\n " )
96
-
97
63
current_response = response
98
64
for interceptor in self ._interceptors :
99
65
try :
100
66
current_response = interceptor .after_response (current_response )
101
67
except Exception as e :
102
- # Log error but continue processing
103
- print (f"Error in interceptor { interceptor .__class__ .__name__ } : { e } " )
68
+ continue
104
69
return current_response
0 commit comments