11import types
22from collections import deque
3- from osbot_utils .type_safe .Type_Safe import Type_Safe
4- from osbot_utils .helpers .trace .Trace_Call__Config import Trace_Call__Config
3+ from osbot_utils .type_safe .primitives .domains .identifiers .Random_Guid import Random_Guid
4+ from osbot_utils .type_safe .Type_Safe import Type_Safe
5+ from osbot_utils .helpers .trace .Trace_Call__Config import Trace_Call__Config
56
67
78HTTP_EVENTS__MAX_REQUESTS_LOGGED = 50
89
910from typing import TYPE_CHECKING , Union
1011
11- if TYPE_CHECKING :
12- from fastapi import Request
13- from starlette .responses import Response
14- from osbot_fast_api .api .events .Fast_API__Http_Event import Fast_API__Http_Event
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
1516
1617class Fast_API__Http_Events (Type_Safe ):
1718 #log_requests : bool = False # todo: change this to save on S3 and disk
@@ -25,7 +26,6 @@ class Fast_API__Http_Events(Type_Safe):
2526 requests_order : deque
2627 max_requests_logged : int = HTTP_EVENTS__MAX_REQUESTS_LOGGED
2728 fast_api_name : str
28- #add_header_request_id : bool = True
2929
3030 def __init__ (self ,** kwargs ):
3131 super ().__init__ (** kwargs )
@@ -75,9 +75,14 @@ def create_request_data(self, request):
7575 from osbot_fast_api .api .events .Fast_API__Http_Event import Fast_API__Http_Event
7676 from osbot_fast_api .api .events .Fast_API__Http_Event__Info import Fast_API__Http_Event__Info
7777
78+ if hasattr (request .state , 'request_id' ): # Use existing request_id if available (from Middleware__Request_ID)
79+ event_id = request .state .request_id
80+ else :
81+ event_id = Random_Guid () # Fallback if middleware not present
82+
7883 kwargs = dict (fast_api_name = self .fast_api_name )
7984 http_event_info = Fast_API__Http_Event__Info (** kwargs )
80- http_event = Fast_API__Http_Event (http_event_info = http_event_info )
85+ http_event = Fast_API__Http_Event (http_event_info = http_event_info , event_id = event_id )
8186 event_id = http_event .event_id # get the random request_id/guid that was created in the ctor of Fast_API__Request_Data
8287 request .state .http_events = self # store a copy of this object in the request (so that it is available durant the request handling)
8388 request .state .request_id = event_id # store request_id in request.state
@@ -103,8 +108,13 @@ def event_id(self, request):
103108 return self .request_data (request ).event_id
104109
105110 def request_messages (self , request ):
106- event_id = self .event_id (request )
107- return self .requests_data .get (event_id , {}).get ('messages' , [])
111+ event_id = self .event_id (request )
112+ http_event = self .requests_data .get (event_id )#.get('messages', [])
113+ if type (http_event ) is Fast_API__Http_Event :
114+ return http_event .messages ()
115+ if type (http_event ) is dict :
116+ return http_event .get ('messages' , [])
117+ return []
108118
109119 def request_trace_start (self , request ):
110120 from osbot_utils .helpers .trace .Trace_Call import Trace_Call
0 commit comments