77from models_library .products import ProductName
88from models_library .users import UserID
99from simcore_postgres_database .models .api_keys import api_keys
10- from simcore_postgres_database .utils_repos import transaction_context
10+ from simcore_postgres_database .utils_repos import (
11+ pass_or_acquire_connection ,
12+ transaction_context ,
13+ )
1114from sqlalchemy .dialects .postgresql import insert as pg_insert
1215from sqlalchemy .ext .asyncio import AsyncConnection
1316
@@ -45,7 +48,7 @@ async def create_api_key(
4548 )
4649
4750 result = await conn .stream (stmt )
48- row = await result .first ()
51+ row = await result .one ()
4952
5053 return ApiKey (
5154 id = f"{ row .id } " , # NOTE See: https://github.com/ITISFoundation/osparc-simcore/issues/6919
@@ -111,7 +114,7 @@ async def list_api_keys(
111114 user_id : UserID ,
112115 product_name : ProductName ,
113116) -> list [ApiKey ]:
114- async with transaction_context (get_asyncpg_engine (app ), connection ) as conn :
117+ async with pass_or_acquire_connection (get_asyncpg_engine (app ), connection ) as conn :
115118 stmt = sa .select (api_keys .c .id , api_keys .c .display_name ).where (
116119 (api_keys .c .user_id == user_id ) & (api_keys .c .product_name == product_name )
117120 )
@@ -136,7 +139,7 @@ async def get_api_key(
136139 user_id : UserID ,
137140 product_name : ProductName ,
138141) -> ApiKey | None :
139- async with transaction_context (get_asyncpg_engine (app ), connection ) as conn :
142+ async with pass_or_acquire_connection (get_asyncpg_engine (app ), connection ) as conn :
140143 stmt = sa .select (api_keys ).where (
141144 (
142145 api_keys .c .id == int (api_key_id )
@@ -145,8 +148,8 @@ async def get_api_key(
145148 & (api_keys .c .product_name == product_name )
146149 )
147150
148- result = await conn .stream (stmt )
149- row = await result .first ()
151+ result = await conn .execute (stmt )
152+ row = result .one_or_none ()
150153
151154 return (
152155 ApiKey (
@@ -180,7 +183,7 @@ async def delete_api_key(
180183 await conn .execute (stmt )
181184
182185
183- async def prune_expired (
186+ async def delete_expired_api_keys (
184187 app : web .Application , connection : AsyncConnection | None = None
185188) -> list [str ]:
186189 async with transaction_context (get_asyncpg_engine (app ), connection ) as conn :
@@ -192,7 +195,6 @@ async def prune_expired(
192195 )
193196 .returning (api_keys .c .display_name )
194197 )
195- result = await conn .stream (stmt )
196-
197- rows = [row async for row in result ]
198- return [r .display_name for r in rows ]
198+ result = await conn .execute (stmt )
199+ rows = result .fetchall ()
200+ return [r .display_name for r in rows ]
0 commit comments