7
7
from models_library .products import ProductName
8
8
from models_library .users import UserID
9
9
from 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
+ )
11
14
from sqlalchemy .dialects .postgresql import insert as pg_insert
12
15
from sqlalchemy .ext .asyncio import AsyncConnection
13
16
@@ -45,7 +48,7 @@ async def create_api_key(
45
48
)
46
49
47
50
result = await conn .stream (stmt )
48
- row = await result .first ()
51
+ row = await result .one ()
49
52
50
53
return ApiKey (
51
54
id = f"{ row .id } " , # NOTE See: https://github.com/ITISFoundation/osparc-simcore/issues/6919
@@ -111,7 +114,7 @@ async def list_api_keys(
111
114
user_id : UserID ,
112
115
product_name : ProductName ,
113
116
) -> 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 :
115
118
stmt = sa .select (api_keys .c .id , api_keys .c .display_name ).where (
116
119
(api_keys .c .user_id == user_id ) & (api_keys .c .product_name == product_name )
117
120
)
@@ -136,7 +139,7 @@ async def get_api_key(
136
139
user_id : UserID ,
137
140
product_name : ProductName ,
138
141
) -> 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 :
140
143
stmt = sa .select (api_keys ).where (
141
144
(
142
145
api_keys .c .id == int (api_key_id )
@@ -145,8 +148,8 @@ async def get_api_key(
145
148
& (api_keys .c .product_name == product_name )
146
149
)
147
150
148
- result = await conn .stream (stmt )
149
- row = await result .first ()
151
+ result = await conn .execute (stmt )
152
+ row = result .one_or_none ()
150
153
151
154
return (
152
155
ApiKey (
@@ -180,7 +183,7 @@ async def delete_api_key(
180
183
await conn .execute (stmt )
181
184
182
185
183
- async def prune_expired (
186
+ async def delete_expired_api_keys (
184
187
app : web .Application , connection : AsyncConnection | None = None
185
188
) -> list [str ]:
186
189
async with transaction_context (get_asyncpg_engine (app ), connection ) as conn :
@@ -192,7 +195,6 @@ async def prune_expired(
192
195
)
193
196
.returning (api_keys .c .display_name )
194
197
)
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