33
44import sqlalchemy as sa
55from aiohttp import web
6- from asyncpg .exceptions import UniqueViolationError
76from models_library .products import ProductName
87from models_library .users import UserID
98from simcore_postgres_database .models .api_keys import api_keys
1514from sqlalchemy .ext .asyncio import AsyncConnection
1615
1716from ..db .plugin import get_asyncpg_engine
18- from .errors import ApiKeyDuplicatedDisplayNameError
1917from .models import ApiKey
2018
2119_logger = logging .getLogger (__name__ )
@@ -36,30 +34,6 @@ async def create_api_key(
3634 api_key : str ,
3735 api_secret : str ,
3836) -> ApiKey :
39- async with transaction_context (get_asyncpg_engine (app ), connection ) as conn :
40- stmt = pg_insert (api_keys ).values (
41- display_name = display_name ,
42- user_id = user_id ,
43- product_name = product_name ,
44- api_key = api_key ,
45- api_secret = _hash_secret (api_secret ),
46- expires_at = (sa .func .now () + expiration ) if expiration else None ,
47- )
48-
49- try :
50- result = await conn .stream (stmt .returning (api_keys ))
51- row = await result .one ()
52- except UniqueViolationError as exc :
53- raise ApiKeyDuplicatedDisplayNameError (display_name = display_name ) from exc
54-
55- return ApiKey (
56- id = f"{ row .id } " , # NOTE See: https://github.com/ITISFoundation/osparc-simcore/issues/6919
57- display_name = display_name ,
58- expiration = expiration ,
59- api_key = api_key ,
60- api_secret = api_secret ,
61- )
62-
6337 async with transaction_context (get_asyncpg_engine (app ), connection ) as conn :
6438 stmt = (
6539 pg_insert (api_keys )
0 commit comments