11import types
2- from collections import deque
3- from osbot_utils .type_safe .primitives .domains .identifiers .Random_Guid import Random_Guid
2+ from collections import deque
3+ from fastapi import Request
4+ from starlette .responses import Response
45from osbot_utils .type_safe .Type_Safe import Type_Safe
56from osbot_utils .helpers .trace .Trace_Call__Config import Trace_Call__Config
7+ from osbot_fast_api .events .Fast_API__Http_Event import Fast_API__Http_Event
8+ from osbot_utils .type_safe .primitives .domains .identifiers .Random_Guid import Random_Guid
69
710
811HTTP_EVENTS__MAX_REQUESTS_LOGGED = 50
912
1013from typing import TYPE_CHECKING , Union
1114
12- # if TYPE_CHECKING:
13- # from fastapi import Request
14- # from starlette.responses import Response
15- from osbot_fast_api . api . events . Fast_API__Http_Event import Fast_API__Http_Event
15+
16+
17+
18+
1619
1720class Fast_API__Http_Events (Type_Safe ):
1821 #log_requests : bool = False # todo: change this to save on S3 and disk
@@ -31,14 +34,14 @@ def __init__(self,**kwargs):
3134 super ().__init__ (** kwargs )
3235 self .trace_call_config .ignore_start_with = ['osbot_fast_api.api.Fast_API__Http_Events' ] # so that we don't see traces from this
3336
34- def on_http_request (self , request : ' Request' ):
37+ def on_http_request (self , request : Request ):
3538 with self .request_data (request ) as _ :
3639 _ .on_request (request )
3740 self .request_trace_start (request )
3841 if self .callback_on_request :
3942 self .callback_on_request (_ )
4043
41- def on_http_response (self , request : ' Request' , response : ' Response' ):
44+ def on_http_response (self , request : Request , response : Response ):
4245 with self .request_data (request ) as _ :
4346 _ .on_response (response )
4447 # if StreamingResponse not in base_types(response): # handle the special case when the response is a StreamingResponse
@@ -47,7 +50,7 @@ def on_http_response(self, request: 'Request', response: 'Response'):
4750 if self .callback_on_response :
4851 self .callback_on_response (response , _ )
4952
50- def clean_request_data (self , request_data : ' Fast_API__Http_Event' ):
53+ def clean_request_data (self , request_data : Fast_API__Http_Event ):
5154 if self .clean_data :
5255 self .clean_request_data_field (request_data .http_event_request , 'headers' , 'cookie' )
5356 self .clean_request_data_field (request_data .http_event_response , 'headers' , 'cookie' )
@@ -72,16 +75,16 @@ def clean_request_data_field(self, request_data, variable_name, field_name):
7275 #print(f">>>>> on on_response_stream_end : {state}")
7376
7477 def create_request_data (self , request ):
75- from osbot_fast_api .api . events .Fast_API__Http_Event import Fast_API__Http_Event
76- from osbot_fast_api .api . events .Fast_API__Http_Event__Info import Fast_API__Http_Event__Info
78+ from osbot_fast_api .events .Fast_API__Http_Event import Fast_API__Http_Event
79+ from osbot_fast_api .events .schemas . Schema__Fast_API__Http_Event__Info import Schema__Fast_API__Http_Event__Info
7780
7881 if hasattr (request .state , 'request_id' ): # Use existing request_id if available (from Middleware__Request_ID)
7982 event_id = request .state .request_id
8083 else :
8184 event_id = Random_Guid () # Fallback if middleware not present
8285
8386 kwargs = dict (fast_api_name = self .fast_api_name )
84- http_event_info = Fast_API__Http_Event__Info (** kwargs )
87+ http_event_info = Schema__Fast_API__Http_Event__Info (** kwargs )
8588 http_event = Fast_API__Http_Event (http_event_info = http_event_info , event_id = event_id )
8689 event_id = http_event .event_id # get the random request_id/guid that was created in the ctor of Fast_API__Request_Data
8790 request .state .http_events = self # store a copy of this object in the request (so that it is available durant the request handling)
@@ -96,7 +99,7 @@ def create_request_data(self, request):
9699
97100 return http_event
98101
99- def request_data (self , request : ' Request' ): # todo: refactor all this request_data into a Request_Data class
102+ def request_data (self , request : Request ): # todo: refactor all this request_data into a Request_Data class
100103 if not hasattr (request .state , "request_data" ):
101104 request_data = self .create_request_data (request )
102105 else :
@@ -125,7 +128,7 @@ def request_trace_start(self, request):
125128 trace_call .start ()
126129 request .state .trace_call = trace_call
127130
128- def request_trace_stop (self , request : ' Request' ):
131+ def request_trace_stop (self , request : Request ):
129132 from osbot_utils .helpers .trace .Trace_Call import Trace_Call
130133 # pragma: no cover
131134 if self .trace_calls :
0 commit comments