diff --git a/agentex/src/adapters/http/adapter_httpx.py b/agentex/src/adapters/http/adapter_httpx.py index 2276372..1fab0f0 100644 --- a/agentex/src/adapters/http/adapter_httpx.py +++ b/agentex/src/adapters/http/adapter_httpx.py @@ -79,8 +79,7 @@ def _get_streaming_client(cls) -> httpx.AsyncClient: @classmethod async def close_clients(cls) -> None: - # TODO: Call this method - """Close and cleanup the shared clients. Call this during app shutdown for proper cleanup.""" + """Close and cleanup the shared clients. Called during app shutdown for proper cleanup.""" if cls._regular_client: await cls._regular_client.aclose() cls._regular_client = None diff --git a/agentex/src/api/app.py b/agentex/src/api/app.py index d014215..3236cb5 100644 --- a/agentex/src/api/app.py +++ b/agentex/src/api/app.py @@ -10,6 +10,7 @@ from fastapi.staticfiles import StaticFiles from src.adapters.crud_store.exceptions import ItemDoesNotExist +from src.adapters.http.adapter_httpx import HttpxGateway from src.api.authentication_middleware import AgentexAuthMiddleware from src.api.health_interceptor import HealthCheckInterceptor from src.api.logged_api_route import LoggedAPIRoute @@ -68,6 +69,8 @@ async def lifespan(_: FastAPI): await dependencies.startup_global_dependencies() configure_statsd() yield + # Clean up HTTP clients before other shutdown tasks + await HttpxGateway.close_clients() await dependencies.async_shutdown() dependencies.shutdown() diff --git a/agentex/src/domain/repositories/agent_api_key_repository.py b/agentex/src/domain/repositories/agent_api_key_repository.py index 8522966..9ffd1a2 100644 --- a/agentex/src/domain/repositories/agent_api_key_repository.py +++ b/agentex/src/domain/repositories/agent_api_key_repository.py @@ -80,8 +80,8 @@ async def get_internal_api_key_by_agent_id( ) result = await session.execute(query) - agents = result.scalars().all() - return AgentAPIKeyEntity.model_validate(agents[0]) if agents else None + row = result.scalars().first() + return AgentAPIKeyEntity.model_validate(row) if row else None async def get_by_agent_id_and_name( self, agent_id: str, name: str, api_key_type: AgentAPIKeyType @@ -109,8 +109,8 @@ async def get_by_agent_id_and_name( ) result = await session.execute(query) - agents = result.scalars().all() - return AgentAPIKeyEntity.model_validate(agents[0]) if agents else None + row = result.scalars().first() + return AgentAPIKeyEntity.model_validate(row) if row else None async def get_by_agent_name_and_key_name( self, agent_name: str, key_name: str, api_key_type: AgentAPIKeyType @@ -138,8 +138,8 @@ async def get_by_agent_name_and_key_name( ) result = await session.execute(query) - agents = result.scalars().all() - return AgentAPIKeyEntity.model_validate(agents[0]) if agents else None + row = result.scalars().first() + return AgentAPIKeyEntity.model_validate(row) if row else None async def get_external_by_agent_id_and_key( self, agent_id: str, api_key: str @@ -167,8 +167,8 @@ async def get_external_by_agent_id_and_key( ) result = await session.execute(query) - agents = result.scalars().all() - return AgentAPIKeyEntity.model_validate(agents[0]) if agents else None + row = result.scalars().first() + return AgentAPIKeyEntity.model_validate(row) if row else None async def delete_by_agent_name_and_key_name( self, agent_name: str, key_name: str, api_key_type: AgentAPIKeyType