File tree Expand file tree Collapse file tree 4 files changed +28
-17
lines changed
packages/models-library/src/models_library/api_schemas_webserver
src/simcore_service_webserver/api_keys Expand file tree Collapse file tree 4 files changed +28
-17
lines changed Original file line number Diff line number Diff line change 11from datetime import timedelta
2- from typing import Any
2+ from typing import Annotated , Any
33
44from pydantic import BaseModel , ConfigDict , Field , SecretStr
55
@@ -78,15 +78,22 @@ class ApiKeyCreate(BaseModel):
7878
7979
8080class ApiKeyGet (BaseModel ):
81- display_name : str = Field (..., min_length = 3 )
81+ id_ : Annotated [int , Field (alias = "id" )]
82+ display_name : Annotated [str , Field (..., min_length = 3 )]
8283 api_key : str
8384 api_secret : str
8485
8586 model_config = ConfigDict (
87+ populate_by_name = True ,
8688 from_attributes = True ,
8789 json_schema_extra = {
8890 "examples" : [
89- {"display_name" : "myapi" , "api_key" : "key" , "api_secret" : "secret" },
91+ {
92+ "id" : 42 ,
93+ "display_name" : "myapi" ,
94+ "api_key" : "key" ,
95+ "api_secret" : "secret" ,
96+ },
9097 ]
9198 },
9299 )
Original file line number Diff line number Diff line change @@ -49,7 +49,7 @@ async def create_api_key(
4949 api_key , api_secret = _generate_api_key_and_secret (new .display_name )
5050
5151 # raises if name exists already!
52- await db .create (
52+ api_key_id = await db .create (
5353 app ,
5454 user_id = user_id ,
5555 product_name = product_name ,
@@ -60,6 +60,7 @@ async def create_api_key(
6060 )
6161
6262 return ApiKeyGet (
63+ id_ = api_key_id ,
6364 display_name = new .display_name ,
6465 api_key = api_key ,
6566 api_secret = api_secret ,
Original file line number Diff line number Diff line change 77from models_library .basic_types import IdInt
88from models_library .products import ProductName
99from models_library .users import UserID
10+ from pydantic import TypeAdapter
1011from simcore_postgres_database .models .api_keys import api_keys
1112from simcore_postgres_database .utils_repos import transaction_context
1213from sqlalchemy .dialects .postgresql import insert as pg_insert
@@ -44,7 +45,7 @@ async def create(
4445 expiration : timedelta | None ,
4546 api_key : str ,
4647 api_secret : str ,
47- ) -> list [ IdInt ] :
48+ ) -> IdInt :
4849 async with transaction_context (get_asyncpg_engine (app ), connection ) as conn :
4950 stmt = (
5051 api_keys .insert ()
@@ -60,8 +61,8 @@ async def create(
6061 )
6162
6263 result = await conn .stream (stmt )
63- rows = [ row async for row in result ]
64- return [ r . id for r in rows ]
64+ row = await result . first ()
65+ return TypeAdapter ( IdInt ). validate_python ( row . id )
6566
6667
6768async def get (
Original file line number Diff line number Diff line change @@ -30,19 +30,21 @@ async def fake_user_api_keys(
3030 logged_user : UserInfoDict ,
3131 osparc_product_name : ProductName ,
3232 faker : Faker ,
33- ) -> AsyncIterable [list [str ]]:
33+ ) -> AsyncIterable [list [int ]]:
3434 assert client .app
35- api_key_ids = []
35+ api_key_ids : list [ int ] = []
3636
3737 for _ in range (5 ):
38- api_key_ids += await db .create (
39- client .app ,
40- user_id = logged_user ["id" ],
41- product_name = osparc_product_name ,
42- display_name = faker .pystr (),
43- expiration = None ,
44- api_key = faker .pystr (),
45- api_secret = faker .pystr (),
38+ api_key_ids .append (
39+ await db .create (
40+ client .app ,
41+ user_id = logged_user ["id" ],
42+ product_name = osparc_product_name ,
43+ display_name = faker .pystr (),
44+ expiration = None ,
45+ api_key = faker .pystr (),
46+ api_secret = faker .pystr (),
47+ )
4648 )
4749
4850 yield api_key_ids
You can’t perform that action at this time.
0 commit comments