Skip to content

Commit 75a1aed

Browse files
committed
@bisgaard-itis review: validations
1 parent 66c283e commit 75a1aed

File tree

4 files changed

+48
-25
lines changed

4 files changed

+48
-25
lines changed

services/api-server/src/simcore_service_api_server/_service_jobs.py

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
from pydantic import HttpUrl
1717
from servicelib.logging_utils import log_context
1818

19-
from .exceptions.custom_errors import ServiceConfigurationError
2019
from .models.schemas.jobs import Job, JobInputs
2120
from .models.schemas.programs import Program
2221
from .models.schemas.solvers import Solver
@@ -142,21 +141,3 @@ async def create_job(
142141
job_id=job.id,
143142
)
144143
return job, new_project
145-
146-
147-
def check_user_product_consistency(
148-
service_cls_name: str,
149-
job_service: JobService,
150-
user_id: UserID,
151-
product_name: ProductName,
152-
) -> None:
153-
if user_id != job_service.user_id:
154-
msg = f"User ID {user_id} does not match job service user ID {job_service.user_id}"
155-
raise ServiceConfigurationError(
156-
service_cls_name=service_cls_name, detail_msg=msg
157-
)
158-
if product_name != job_service.product_name:
159-
msg = f"Product name {product_name} does not match job service product name {job_service.product_name}"
160-
raise ServiceConfigurationError(
161-
service_cls_name=service_cls_name, detail_msg=msg
162-
)

services/api-server/src/simcore_service_api_server/_service_solvers.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
from models_library.users import UserID
1414
from pydantic import NonNegativeInt, PositiveInt
1515

16-
from ._service_jobs import JobService, check_user_product_consistency
16+
from ._service_jobs import JobService
17+
from ._service_utils import check_user_product_consistency
1718
from .exceptions.backend_errors import (
1819
ProgramOrSolverOrStudyNotFoundError,
1920
)
@@ -36,12 +37,18 @@ class SolverService:
3637
product_name: ProductName
3738

3839
def __post_init__(self):
39-
# Context check
4040
check_user_product_consistency(
4141
service_cls_name=self.__class__.__name__,
42+
service_provider=self.catalog_service,
43+
user_id=self.user_id,
44+
product_name=self.product_name,
45+
)
46+
47+
check_user_product_consistency(
48+
service_cls_name=self.__class__.__name__,
49+
service_provider=self.job_service,
4250
user_id=self.user_id,
4351
product_name=self.product_name,
44-
job_service=self.job_service,
4552
)
4653

4754
async def get_solver(

services/api-server/src/simcore_service_api_server/_service_studies.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
from models_library.rpc_pagination import PageLimitInt
1010
from models_library.users import UserID
1111

12-
from ._service_jobs import JobService, check_user_product_consistency
12+
from ._service_jobs import JobService
13+
from ._service_utils import check_user_product_consistency
1314
from .models.api_resources import compose_resource_name
1415
from .models.schemas.jobs import Job
1516
from .models.schemas.studies import StudyID
@@ -24,12 +25,11 @@ class StudyService:
2425
product_name: ProductName
2526

2627
def __post_init__(self):
27-
# Context check
2828
check_user_product_consistency(
2929
service_cls_name=self.__class__.__name__,
30+
service_provider=self.job_service,
3031
user_id=self.user_id,
3132
product_name=self.product_name,
32-
job_service=self.job_service,
3333
)
3434

3535
async def list_jobs(
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from typing import Protocol
2+
3+
from models_library.products import ProductName
4+
from models_library.users import UserID
5+
6+
from .exceptions.custom_errors import ServiceConfigurationError
7+
8+
9+
class UserProductProvider(Protocol):
10+
"""Protocol for classes that provide user_id and product_name properties."""
11+
12+
@property
13+
def user_id(self) -> UserID: ...
14+
15+
@property
16+
def product_name(self) -> ProductName: ...
17+
18+
19+
def check_user_product_consistency(
20+
service_cls_name: str,
21+
service_provider: UserProductProvider,
22+
user_id: UserID,
23+
product_name: ProductName,
24+
) -> None:
25+
26+
if user_id != service_provider.user_id:
27+
msg = f"User ID {user_id} does not match {service_provider.__class__.__name__} user ID {service_provider.user_id}"
28+
raise ServiceConfigurationError(
29+
service_cls_name=service_cls_name, detail_msg=msg
30+
)
31+
if product_name != service_provider.product_name:
32+
msg = f"Product name {product_name} does not match {service_provider.__class__.__name__}product name {service_provider.product_name}"
33+
raise ServiceConfigurationError(
34+
service_cls_name=service_cls_name, detail_msg=msg
35+
)

0 commit comments

Comments
 (0)