Skip to content

Commit fed33cd

Browse files
committed
changes in api-keys
1 parent bf2c61d commit fed33cd

File tree

3 files changed

+21
-24
lines changed

3 files changed

+21
-24
lines changed

services/web/server/src/simcore_service_webserver/api_keys/_repository.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
from models_library.products import ProductName
88
from models_library.users import UserID
99
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+
)
1114
from sqlalchemy.dialects.postgresql import insert as pg_insert
1215
from 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]

services/web/server/src/simcore_service_webserver/api_keys/_service.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ async def create_api_key(
3232
*,
3333
user_id: UserID,
3434
product_name: ProductName,
35-
display_name=str,
36-
expiration=dt.timedelta,
35+
display_name: str,
36+
expiration: dt.timedelta | None,
3737
) -> ApiKey:
3838
api_key, api_secret = _generate_api_key_and_secret(display_name)
3939

@@ -119,5 +119,5 @@ async def delete_api_key(
119119

120120

121121
async def prune_expired_api_keys(app: web.Application) -> list[str]:
122-
names: list[str] = await _repository.prune_expired(app)
122+
names: list[str] = await _repository.delete_expired_api_keys(app)
123123
return names

services/web/server/tests/unit/with_dbs/01/test_api_keys.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# pylint: disable=unused-variable
44
# pylint: disable=too-many-arguments
55

6+
import asyncio
67
from collections.abc import AsyncIterable
78
from datetime import timedelta
89
from http import HTTPStatus
@@ -27,7 +28,6 @@
2728
from tenacity import (
2829
retry_if_exception_type,
2930
stop_after_attempt,
30-
stop_after_delay,
3131
wait_fixed,
3232
)
3333

@@ -172,15 +172,10 @@ async def test_create_api_key_with_expiration(
172172
assert [d["displayName"] for d in data] == ["foo"]
173173

174174
# wait for api-key for it to expire and force-run scheduled task
175-
async for attempt in tenacity.AsyncRetrying(
176-
wait=wait_fixed(1),
177-
retry=retry_if_exception_type(AssertionError),
178-
stop=stop_after_delay(5 * expiration_interval.seconds),
179-
reraise=True,
180-
):
181-
with attempt:
182-
deleted = await api_keys_service.prune_expired_api_keys(client.app)
183-
assert deleted == ["foo"]
175+
await asyncio.sleep(1.1 * expiration_interval.seconds)
176+
177+
deleted = await api_keys_service.prune_expired_api_keys(client.app)
178+
assert deleted == ["foo"]
184179

185180
resp = await client.get("/v0/auth/api-keys")
186181
data, _ = await assert_status(resp, expected)

0 commit comments

Comments
 (0)