Skip to content

Commit 9af987e

Browse files
feat: add update functions endpoint
1 parent fa40d71 commit 9af987e

File tree

6 files changed

+42
-70
lines changed

6 files changed

+42
-70
lines changed

packages/models-library/src/models_library/api_schemas_webserver/functions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
FunctionOutputs,
2424
FunctionOutputSchema,
2525
FunctionSchemaClass,
26+
FunctionUpdate,
2627
JSONFunctionInputSchema,
2728
JSONFunctionOutputSchema,
2829
ProjectFunction,
@@ -131,3 +132,6 @@ class ProjectFunctionToRegister(ProjectFunction, InputSchema): ...
131132
RegisteredProjectFunctionGet | RegisteredSolverFunctionGet,
132133
Field(discriminator="function_class"),
133134
]
135+
136+
137+
class RegisteredFunctionUpdate(FunctionUpdate, InputSchema): ...

packages/models-library/src/models_library/functions.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ class RegisteredFunctionBase(FunctionBase):
101101
created_at: datetime.datetime
102102

103103

104+
class FunctionUpdate(BaseModel):
105+
title: str | None = None
106+
description: str | None = None
107+
108+
104109
class ProjectFunction(FunctionBase):
105110
function_class: Literal[FunctionClass.PROJECT] = FunctionClass.PROJECT
106111
project_id: ProjectID

services/web/server/src/simcore_service_webserver/functions/_controller/_functions_rest.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
FunctionToRegister,
55
RegisteredFunction,
66
RegisteredFunctionGet,
7+
RegisteredFunctionUpdate,
78
)
89
from models_library.api_schemas_webserver.users import MyFunctionPermissionsGet
910
from pydantic import TypeAdapter
@@ -82,7 +83,7 @@ async def get_function(request: web.Request) -> web.Response:
8283
)
8384

8485

85-
@routes.put(
86+
@routes.patch(
8687
"/{VTAG}/functions/{function_id}",
8788
name="update_function",
8889
)
@@ -93,9 +94,24 @@ async def update_function(request: web.Request) -> web.Response:
9394
path_params = parse_request_path_parameters_as(FunctionPathParams, request)
9495
function_id = path_params.function_id
9596

97+
function_update = TypeAdapter(RegisteredFunctionUpdate).validate_python(
98+
await request.json()
99+
)
96100
req_ctx = AuthenticatedRequestContext.model_validate(request)
97101

98-
raise NotImplementedError
102+
updated_function = await _functions_service.update_function(
103+
request.app,
104+
user_id=req_ctx.user_id,
105+
product_name=req_ctx.product_name,
106+
function_id=function_id,
107+
function=function_update,
108+
)
109+
110+
return envelope_json_response(
111+
TypeAdapter(RegisteredFunctionUpdate).validate_python(
112+
updated_function.model_dump(mode="json")
113+
)
114+
)
99115

100116

101117
@routes.delete(

services/web/server/src/simcore_service_webserver/functions/_controller/_functions_rpc.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
FunctionJobCollectionsListFilters,
1212
FunctionJobID,
1313
FunctionOutputSchema,
14+
FunctionUpdate,
1415
FunctionUserApiAccessRights,
1516
RegisteredFunction,
1617
RegisteredFunctionJob,
@@ -318,12 +319,12 @@ async def update_function_title(
318319
function_id: FunctionID,
319320
title: str,
320321
) -> RegisteredFunction:
321-
return await _functions_service.update_function_title(
322+
return await _functions_service.update_function(
322323
app=app,
323324
user_id=user_id,
324325
product_name=product_name,
325326
function_id=function_id,
326-
title=title,
327+
function=FunctionUpdate(title=title),
327328
)
328329

329330

@@ -342,12 +343,12 @@ async def update_function_description(
342343
function_id: FunctionID,
343344
description: str,
344345
) -> RegisteredFunction:
345-
return await _functions_service.update_function_description(
346+
return await _functions_service.update_function(
346347
app=app,
347348
user_id=user_id,
348349
product_name=product_name,
349350
function_id=function_id,
350-
description=description,
351+
function=FunctionUpdate(description=description),
351352
)
352353

353354

services/web/server/src/simcore_service_webserver/functions/_functions_repository.py

Lines changed: 5 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
FunctionOutputs,
2121
FunctionOutputSchema,
2222
FunctionsApiAccessRights,
23+
FunctionUpdate,
2324
FunctionUserApiAccessRights,
2425
RegisteredFunctionDB,
2526
RegisteredFunctionJobCollectionDB,
@@ -633,14 +634,14 @@ async def delete_function(
633634
)
634635

635636

636-
async def _update_function_attribute(
637+
async def update_function(
637638
app: web.Application,
638-
connection: AsyncConnection | None,
639+
connection: AsyncConnection | None = None,
639640
*,
640641
user_id: UserID,
641642
product_name: ProductName,
642643
function_id: FunctionID,
643-
**update_kwargs,
644+
function: FunctionUpdate,
644645
) -> RegisteredFunctionDB:
645646
async with transaction_context(get_asyncpg_engine(app), connection) as transaction:
646647
await check_user_api_access_rights(
@@ -667,7 +668,7 @@ async def _update_function_attribute(
667668
result = await transaction.execute(
668669
functions_table.update()
669670
.where(functions_table.c.uuid == function_id)
670-
.values(**update_kwargs)
671+
.values(**function.model_dump(exclude_none=True, exclude_unset=True))
671672
.returning(*_FUNCTIONS_TABLE_COLS)
672673
)
673674
row = result.one_or_none()
@@ -678,44 +679,6 @@ async def _update_function_attribute(
678679
return RegisteredFunctionDB.model_validate(row)
679680

680681

681-
async def update_function_title(
682-
app: web.Application,
683-
connection: AsyncConnection | None = None,
684-
*,
685-
user_id: UserID,
686-
product_name: ProductName,
687-
function_id: FunctionID,
688-
title: str,
689-
) -> RegisteredFunctionDB:
690-
return await _update_function_attribute(
691-
app,
692-
connection=connection,
693-
user_id=user_id,
694-
product_name=product_name,
695-
function_id=function_id,
696-
title=title,
697-
)
698-
699-
700-
async def update_function_description(
701-
app: web.Application,
702-
connection: AsyncConnection | None = None,
703-
*,
704-
user_id: UserID,
705-
product_name: ProductName,
706-
function_id: FunctionID,
707-
description: str,
708-
) -> RegisteredFunctionDB:
709-
return await _update_function_attribute(
710-
app,
711-
connection=connection,
712-
user_id=user_id,
713-
product_name=product_name,
714-
function_id=function_id,
715-
description=description,
716-
)
717-
718-
719682
async def get_function_job(
720683
app: web.Application,
721684
connection: AsyncConnection | None = None,

services/web/server/src/simcore_service_webserver/functions/_functions_service.py

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
FunctionJobDB,
1515
FunctionJobID,
1616
FunctionOutputSchema,
17+
FunctionUpdate,
1718
FunctionUserAccessRights,
1819
FunctionUserApiAccessRights,
1920
RegisteredFunction,
@@ -291,38 +292,20 @@ async def delete_function_job_collection(
291292
)
292293

293294

294-
async def update_function_title(
295+
async def update_function(
295296
app: web.Application,
296297
*,
297298
user_id: UserID,
298299
product_name: ProductName,
299300
function_id: FunctionID,
300-
title: str,
301+
function: FunctionUpdate,
301302
) -> RegisteredFunction:
302-
updated_function = await _functions_repository.update_function_title(
303+
updated_function = await _functions_repository.update_function(
303304
app=app,
304305
user_id=user_id,
305306
product_name=product_name,
306307
function_id=function_id,
307-
title=title,
308-
)
309-
return _decode_function(updated_function)
310-
311-
312-
async def update_function_description(
313-
app: web.Application,
314-
*,
315-
user_id: UserID,
316-
product_name: ProductName,
317-
function_id: FunctionID,
318-
description: str,
319-
) -> RegisteredFunction:
320-
updated_function = await _functions_repository.update_function_description(
321-
app=app,
322-
user_id=user_id,
323-
product_name=product_name,
324-
function_id=function_id,
325-
description=description,
308+
function=function,
326309
)
327310
return _decode_function(updated_function)
328311

0 commit comments

Comments
 (0)