Skip to content

Commit 3f69457

Browse files
committed
Interceptor-Clean up
1 parent c37965e commit 3f69457

File tree

1 file changed

+8
-43
lines changed

1 file changed

+8
-43
lines changed

src/openai/_interceptor.py

+8-43
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
@dataclass
1414
class InterceptorRequest:
15-
"""Container for request data that can be modified by interceptors"""
15+
"""Request data container for interceptor processing"""
1616
method: str
1717
url: str
1818
headers: Dict[str, str]
@@ -21,84 +21,49 @@ class InterceptorRequest:
2121

2222
@dataclass
2323
class InterceptorResponse(Generic[T]):
24-
"""Container for response data that can be processed by interceptors"""
24+
"""Response data container for interceptor processing"""
2525
status_code: int
2626
headers: Dict[str, str]
2727
body: T
2828
request: InterceptorRequest
2929
raw_response: httpx.Response
3030

3131
class Interceptor(ABC):
32-
"""Base class for implementing request/response interceptors"""
32+
"""Base class for request/response interceptors"""
3333

3434
@abstractmethod
3535
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"""
4437
pass
4538

4639
@abstractmethod
4740
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"""
5642
pass
5743

5844
class InterceptorChain:
59-
"""Manages a chain of interceptors that process requests/responses in sequence"""
45+
"""Chain of interceptors for sequential request/response processing"""
6046

6147
def __init__(self, interceptors: Optional[list[Interceptor]] = None):
6248
self._interceptors = interceptors or []
6349

6450
def add_interceptor(self, interceptor: Interceptor) -> None:
65-
"""Add an interceptor to the chain"""
6651
self._interceptors.append(interceptor)
6752

6853
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-
8054
current_request = request
8155
for interceptor in self._interceptors:
8256
try:
8357
current_request = interceptor.before_request(current_request)
8458
except Exception as e:
85-
# Log error but continue processing
86-
print(f"Error in interceptor {interceptor.__class__.__name__}: {e}")
59+
continue
8760
return current_request
8861

8962
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-
9763
current_response = response
9864
for interceptor in self._interceptors:
9965
try:
10066
current_response = interceptor.after_response(current_response)
10167
except Exception as e:
102-
# Log error but continue processing
103-
print(f"Error in interceptor {interceptor.__class__.__name__}: {e}")
68+
continue
10469
return current_response

0 commit comments

Comments
 (0)