Skip to content

Commit 311ef77

Browse files
committed
refactored all api/events/* classes into events/*
1 parent 6b2a239 commit 311ef77

18 files changed

Lines changed: 71 additions & 74 deletions

osbot_fast_api/api/Fast_API.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from osbot_fast_api.api.middlewares.Middleware__Http_Request import Middleware__Http_Request
21
from osbot_fast_api.api.middlewares.Middleware__Request_ID import Middleware__Request_ID
32
from osbot_fast_api.schemas.Schema__Fast_API__Config import Schema__Fast_API__Config
43
from osbot_utils.type_safe.Type_Safe import Type_Safe

osbot_fast_api/api/events/Fast_API__Http_Event.py renamed to osbot_fast_api/events/Fast_API__Http_Event.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import logging
22
import time
3-
from decimal import Decimal
4-
from fastapi import Response, Request
5-
from osbot_fast_api.api.events.Fast_API__Http_Event__Info import Fast_API__Http_Event__Info
6-
from osbot_fast_api.api.events.Fast_API__Http_Event__Request import Fast_API__Http_Event__Request
7-
from osbot_fast_api.api.events.Fast_API__Http_Event__Response import Fast_API__Http_Event__Response
8-
from osbot_fast_api.api.events.Fast_API__Http_Event__Traces import Fast_API__Http_Event__Traces
9-
from osbot_utils.type_safe.Type_Safe import Type_Safe
10-
from osbot_utils.type_safe.primitives.domains.identifiers.Random_Guid import Random_Guid
11-
from osbot_utils.helpers.trace.Trace_Call import Trace_Call
12-
from osbot_utils.utils.Misc import timestamp_utc_now, current_thread_id, str_to_bytes
13-
from osbot_utils.utils.Objects import pickle_to_bytes
3+
from decimal import Decimal
4+
from fastapi import Response, Request
5+
from osbot_fast_api.events.schemas.Schema__Fast_API__Http_Event__Info import Schema__Fast_API__Http_Event__Info
6+
from osbot_fast_api.events.schemas.Schema__Fast_API__Http_Event__Request import Schema__Fast_API__Http_Event__Request
7+
from osbot_fast_api.events.schemas.Schema__Fast_API__Http_Event__Response import Schema__Fast_API__Http_Event__Response
8+
from osbot_fast_api.events.schemas.Schema__Fast_API__Http_Event__Traces import Schema__Fast_API__Http_Event__Traces
9+
from osbot_utils.type_safe.Type_Safe import Type_Safe
10+
from osbot_utils.type_safe.primitives.domains.identifiers.Random_Guid import Random_Guid
11+
from osbot_utils.helpers.trace.Trace_Call import Trace_Call
12+
from osbot_utils.utils.Misc import timestamp_utc_now, current_thread_id, str_to_bytes
13+
from osbot_utils.utils.Objects import pickle_to_bytes
1414

1515
HEADER_NAME__FAST_API_REQUEST_ID = 'fast-api-request-id'
1616
HEADER_NAME__CACHE_CONTROL = "cache-control"
@@ -22,10 +22,10 @@
2222

2323

2424
class Fast_API__Http_Event(Type_Safe):
25-
http_event_info : Fast_API__Http_Event__Info
26-
http_event_request : Fast_API__Http_Event__Request
27-
http_event_response : Fast_API__Http_Event__Response
28-
http_event_traces : Fast_API__Http_Event__Traces
25+
http_event_info : Schema__Fast_API__Http_Event__Info
26+
http_event_request : Schema__Fast_API__Http_Event__Request
27+
http_event_response : Schema__Fast_API__Http_Event__Response
28+
http_event_traces : Schema__Fast_API__Http_Event__Traces
2929
event_id : Random_Guid # todo: rename to http_event_id
3030

3131
def __init__(self, **kwargs):

osbot_fast_api/api/events/Fast_API__Http_Events.py renamed to osbot_fast_api/events/Fast_API__Http_Events.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
import 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
45
from osbot_utils.type_safe.Type_Safe import Type_Safe
56
from 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

811
HTTP_EVENTS__MAX_REQUESTS_LOGGED = 50
912

1013
from 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

1720
class 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:

osbot_fast_api/events/Fast_API__With_Events.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
from osbot_fast_api.api.Fast_API import Fast_API
2-
from osbot_fast_api.api.events.Fast_API__Http_Events import Fast_API__Http_Events
1+
from osbot_fast_api.api.Fast_API import Fast_API
2+
from osbot_fast_api.events.Fast_API__Http_Events import Fast_API__Http_Events
33

44

55
class Fast_API__With_Events(Fast_API):
@@ -16,7 +16,7 @@ def setup_middlewares(self): # Add
1616
return self
1717

1818
def setup_middleware__http_events(self): # Moved from base class
19-
from osbot_fast_api.api.middlewares.Middleware__Http_Request import Middleware__Http_Request
19+
from osbot_fast_api.events.middlewares.Middleware__Http_Request import Middleware__Http_Request
2020

2121
self.app().add_middleware(Middleware__Http_Request, http_events=self.http_events)
2222
return self

osbot_fast_api/api/middlewares/Middleware__Http_Request.py renamed to osbot_fast_api/events/middlewares/Middleware__Http_Request.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
from starlette.middleware.base import BaseHTTPMiddleware
2-
from osbot_fast_api.api.events.Fast_API__Http_Events import Fast_API__Http_Events
1+
from starlette.middleware.base import BaseHTTPMiddleware
2+
from osbot_fast_api.events.Fast_API__Http_Events import Fast_API__Http_Events
33

44
from typing import TYPE_CHECKING
55
if TYPE_CHECKING:

osbot_fast_api/api/middlewares/Middleware__Http_Request__Duration.py renamed to osbot_fast_api/events/middlewares/Middleware__Http_Request__Duration.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# from starlette.middleware.base import BaseHTTPMiddleware
55
# from starlette.responses import Response
66
#
7-
# from osbot_fast_api.api.events.Fast_API__Http_Events import Fast_API__Http_Events
7+
# from osbot_fast_api.events.Fast_API__Http_Events import Fast_API__Http_Events
88
#
99
#
1010
# class Middleware__Http_Request__Duration(BaseHTTPMiddleware):

osbot_fast_api/api/middlewares/Middleware__Http_Request__Trace_Calls.py renamed to osbot_fast_api/events/middlewares/Middleware__Http_Request__Trace_Calls.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# from fastapi import Request
33
# from starlette.responses import Response
44
#
5-
# from osbot_fast_api.api.events.Fast_API__Http_Events import Fast_API__Http_Events
5+
# from osbot_fast_api.events.Fast_API__Http_Events import Fast_API__Http_Events
66
# from osbot_utils.helpers.trace.Trace_Call import Trace_Call
77
# from osbot_utils.helpers.trace.Trace_Call__Config import Trace_Call__Config
88
#
File renamed without changes.

osbot_fast_api/api/events/Fast_API__Http_Event__Info.py renamed to osbot_fast_api/events/schemas/Schema__Fast_API__Http_Event__Info.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
from osbot_utils.type_safe.Type_Safe import Type_Safe
1+
from osbot_utils.type_safe.Type_Safe import Type_Safe
22
from osbot_utils.type_safe.primitives.domains.identifiers.Random_Guid import Random_Guid
33

44

5-
class Fast_API__Http_Event__Info(Type_Safe):
5+
class Schema__Fast_API__Http_Event__Info(Type_Safe):
66
fast_api_name : str = None
77
log_messages : list
88
client_city : str = None

osbot_fast_api/api/events/Fast_API__Http_Event__Request.py renamed to osbot_fast_api/events/schemas/Schema__Fast_API__Http_Event__Request.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
from decimal import Decimal
2-
from osbot_utils.type_safe.Type_Safe import Type_Safe
1+
from decimal import Decimal
2+
from osbot_utils.type_safe.Type_Safe import Type_Safe
33
from osbot_utils.type_safe.primitives.domains.identifiers.Random_Guid import Random_Guid
44

55

6-
class Fast_API__Http_Event__Request(Type_Safe):
6+
class Schema__Fast_API__Http_Event__Request(Type_Safe):
77
duration : Decimal = None
88
event_id : Random_Guid
99
host_name : str = None

0 commit comments

Comments
 (0)