@@ -39,7 +39,7 @@ async def get_users(db: AsyncSession = Depends(get_db)):
3939from app .utils .auth import auth_utils
4040from app .utils .communication .notifications import NotificationManager , NotificationTool
4141from app .utils .state import (
42- LifespanState ,
42+ GlobalState ,
4343 RuntimeLifespanState ,
4444 disconnect_redis_client ,
4545 disconnect_scheduler ,
@@ -61,28 +61,31 @@ async def get_users(db: AsyncSession = Depends(get_db)):
6161hyperion_access_logger = logging .getLogger ("hyperion.access" )
6262hyperion_error_logger = logging .getLogger ("hyperion.error" )
6363
64+ GLOBAL_STATE : GlobalState
6465
65- async def init_app_state (
66+
67+ async def init_state (
6668 app : FastAPI ,
6769 settings : Settings ,
6870 hyperion_error_logger : logging .Logger ,
69- ) -> LifespanState :
71+ ) -> None :
7072 """
71- Initialize the state of the application . This dependency should be used at the start of the application lifespan.
73+ Initialize the global state for the project . This dependency should be called at the start of the application lifespan.
7274
7375 This methode should be called as a dependency, and test may override it to provide their own state.
7476 ```python
75- state = app.dependency_overrides.get(
76- init_app_state ,
77- init_app_state ,
77+ app.dependency_overrides.get(
78+ init_state ,
79+ init_state ,
7880 )(
7981 app=app,
8082 settings=settings,
8183 hyperion_error_logger=hyperion_error_logger,
8284 )
83- state = cast("LifespanState", state)
8485 ```
8586 """
87+ global GLOBAL_STATE
88+
8689 engine = init_engine (settings = settings )
8790
8891 SessionLocal = init_SessionLocal (engine )
@@ -94,7 +97,7 @@ async def init_app_state(
9497
9598 scheduler = await init_scheduler (
9699 settings = settings ,
97- app = app ,
100+ _dependency_overrides = app . dependency_overrides ,
98101 )
99102
100103 ws_manager = await init_websocket_connection_manager (
@@ -112,7 +115,7 @@ async def init_app_state(
112115
113116 mail_templates = init_mail_templates (settings = settings )
114117
115- return LifespanState (
118+ GLOBAL_STATE = GlobalState (
116119 engine = engine ,
117120 SessionLocal = SessionLocal ,
118121 redis_client = redis_client ,
@@ -126,17 +129,17 @@ async def init_app_state(
126129
127130
128131async def disconnect_state (
129- state : LifespanState ,
130132 hyperion_error_logger : logging .Logger ,
131133) -> None :
132134 """
133135 Disconnect items requiring it. This dependency should be used at the end of the application lifespan.
134136
135- This methode should be called as a dependency as test may need to run additional steps
137+ This methode should be called as a dependency as tests may need to run additional steps
136138 """
137- disconnect_redis_client (state ["redis_client" ])
138- await disconnect_scheduler (state ["scheduler" ])
139- await disconnect_websocket_connection_manager (state ["ws_manager" ])
139+
140+ disconnect_redis_client (GLOBAL_STATE ["redis_client" ])
141+ await disconnect_scheduler (GLOBAL_STATE ["scheduler" ])
142+ await disconnect_websocket_connection_manager (GLOBAL_STATE ["ws_manager" ])
140143
141144 hyperion_error_logger .info ("Application state disconnected successfully." )
142145
@@ -179,7 +182,7 @@ def get_settings() -> Settings:
179182 return construct_prod_settings ()
180183
181184
182- async def get_db (state : AppState ) -> AsyncGenerator [AsyncSession , None ]:
185+ async def get_db () -> AsyncGenerator [AsyncSession , None ]:
183186 """
184187 Return a database session that will be automatically committed and closed after usage.
185188
@@ -202,7 +205,7 @@ async def get_db(state: AppState) -> AsyncGenerator[AsyncSession, None]:
202205 # Add objects that may be rolled back in case of an error here
203206 ```
204207 """
205- async with state ["SessionLocal" ]() as db :
208+ async with GLOBAL_STATE ["SessionLocal" ]() as db :
206209 try :
207210 yield db
208211 except HTTPException :
@@ -217,42 +220,42 @@ async def get_db(state: AppState) -> AsyncGenerator[AsyncSession, None]:
217220 await db .close ()
218221
219222
220- async def get_unsafe_db (state : AppState ) -> AsyncGenerator [AsyncSession , None ]:
223+ async def get_unsafe_db () -> AsyncGenerator [AsyncSession , None ]:
221224 """
222225 Return a database session but don't close it automatically
223226
224227 It should only be used for really specific cases where `get_db` will not work
225228 """
226229
227- async with state ["SessionLocal" ]() as db :
230+ async with GLOBAL_STATE ["SessionLocal" ]() as db :
228231 yield db
229232
230233
231- def get_redis_client (state : AppState ) -> redis .Redis | None :
234+ def get_redis_client () -> redis .Redis | None :
232235 """
233236 Dependency that returns the redis client
234237
235238 If the redis client is not available, it will return None.
236239 """
237- return state ["redis_client" ]
240+ return GLOBAL_STATE ["redis_client" ]
238241
239242
240- def get_scheduler (state : AppState ) -> Scheduler :
241- return state ["scheduler" ]
243+ def get_scheduler () -> Scheduler :
244+ return GLOBAL_STATE ["scheduler" ]
242245
243246
244- def get_websocket_connection_manager (state : AppState ) -> WebsocketConnectionManager :
245- return state ["ws_manager" ]
247+ def get_websocket_connection_manager () -> WebsocketConnectionManager :
248+ return GLOBAL_STATE ["ws_manager" ]
246249
247250
248- def get_notification_manager (state : AppState ) -> NotificationManager :
251+ def get_notification_manager () -> NotificationManager :
249252 """
250253 Dependency that returns the notification manager.
251254 This dependency provide a low level tool allowing to use notification manager internal methods.
252255
253256 If you want to send a notification, prefer `get_notification_tool` dependency.
254257 """
255- return state ["notification_manager" ]
258+ return GLOBAL_STATE ["notification_manager" ]
256259
257260
258261def get_notification_tool (
@@ -271,22 +274,20 @@ def get_notification_tool(
271274 )
272275
273276
274- def get_drive_file_manager (state : AppState ) -> DriveFileManager :
277+ def get_drive_file_manager () -> DriveFileManager :
275278 """
276279 Dependency that returns the drive file manager.
277280 """
278281
279- return state ["drive_file_manager" ]
282+ return GLOBAL_STATE ["drive_file_manager" ]
280283
281284
282285@lru_cache
283286def get_payment_tool (
284287 name : HelloAssoConfigName ,
285- ) -> Callable [[AppState ], PaymentTool ]:
286- def get_payment_tool (
287- state : AppState ,
288- ) -> PaymentTool :
289- payment_tools = state ["payment_tools" ]
288+ ) -> Callable [[], PaymentTool ]:
289+ def get_payment_tool () -> PaymentTool :
290+ payment_tools = GLOBAL_STATE ["payment_tools" ]
290291 if name not in payment_tools :
291292 hyperion_error_logger .warning (
292293 f"HelloAsso API credentials are not set for { name .value } , payment won't be available" ,
@@ -298,14 +299,12 @@ def get_payment_tool(
298299 return get_payment_tool
299300
300301
301- def get_mail_templates (
302- state : AppState ,
303- ) -> calypsso .MailTemplates :
302+ def get_mail_templates () -> calypsso .MailTemplates :
304303 """
305304 Dependency that returns the mail templates manager.
306305 """
307306
308- return state ["mail_templates" ]
307+ return GLOBAL_STATE ["mail_templates" ]
309308
310309
311310def get_token_data (
0 commit comments