@@ -71,8 +71,29 @@ async def initialize(
7171
7272 except Exception as e :
7373 logger .error (f'❌ Failed to initialize admin OBP client: { e } ' )
74+ # Clean up any partially created resources
75+ await self ._cleanup_partial_init ()
7476 raise ValueError (f'Admin client initialization failed: { e } ' ) from e
7577
78+ async def _cleanup_partial_init (self ) -> None :
79+ """Clean up resources from a failed initialization."""
80+ if self ._client :
81+ try :
82+ await self ._client .close ()
83+ except Exception as cleanup_error :
84+ logger .debug (f'Error during cleanup: { cleanup_error } ' )
85+
86+ if self ._auth and hasattr (self ._auth , 'async_requests_client' ):
87+ if self ._auth .async_requests_client :
88+ try :
89+ await self ._auth .async_requests_client .close ()
90+ except Exception as cleanup_error :
91+ logger .debug (f'Error during cleanup: { cleanup_error } ' )
92+
93+ self ._client = None
94+ self ._auth = None
95+ self ._initialized = False
96+
7697 def get_client (self ) -> OBPClient :
7798 """
7899 Get the singleton admin OBP client instance.
@@ -112,10 +133,16 @@ def is_initialized(self) -> bool:
112133
113134 async def close (self ) -> None :
114135 """Clean up resources during app shutdown."""
136+ # Close OBP client session
137+ if self ._client :
138+ await self ._client .close ()
139+ logger .info ('🔌 Admin OBP client session closed' )
140+
141+ # Close auth session
115142 if self ._auth and hasattr (self ._auth , 'async_requests_client' ):
116143 if self ._auth .async_requests_client :
117144 await self ._auth .async_requests_client .close ()
118- logger .info ('🔌 Admin client HTTP session closed' )
145+ logger .info ('🔌 Admin auth HTTP session closed' )
119146
120147 self ._initialized = False
121148 self ._client = None
@@ -160,7 +187,7 @@ def get_admin_client() -> OBPClient:
160187 >>>
161188 >>> # Later, anywhere in the app
162189 >>> admin_client = get_admin_client()
163- >>> response = await admin_client.get("/obp/v6.0.0/banks")
190+ >>> banks = await admin_client.get("/obp/v6.0.0/banks")
164191 """
165192 return _admin_manager .get_client ()
166193
0 commit comments