Skip to content

Commit adce963

Browse files
committed
introduce _base.py and refactor
1 parent 870bc39 commit adce963

File tree

6 files changed

+81
-27
lines changed

6 files changed

+81
-27
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import urllib.parse
2+
from typing import Annotated
3+
4+
import packaging.version
5+
from models_library.utils.change_case import camel_to_snake
6+
from pydantic import BaseModel, ConfigDict, Field, HttpUrl, StringConstraints
7+
8+
from ...models._utils_pydantic import UriSchema
9+
from ..basic_types import VersionStr
10+
11+
12+
class ApiServerOutputSchema(BaseModel):
13+
model_config = ConfigDict(
14+
alias_generator=camel_to_snake,
15+
populate_by_name=True,
16+
extra="ignore", # Used to prune extra fields from internal data
17+
frozen=True,
18+
)
19+
20+
21+
class ApiServerInputSchema(BaseModel):
22+
model_config = ConfigDict(
23+
alias_generator=camel_to_snake,
24+
populate_by_name=True,
25+
extra="ignore", # Used to prune extra fields from internal data
26+
frozen=True,
27+
)
28+
29+
30+
class BaseService(BaseModel):
31+
id: Annotated[str, Field(..., description="Resource identifier")]
32+
version: Annotated[
33+
VersionStr, Field(..., description="Semantic version number of the resource")
34+
]
35+
title: Annotated[
36+
str,
37+
StringConstraints(max_length=100),
38+
Field(..., description="Human readable name"),
39+
]
40+
description: Annotated[
41+
str | None,
42+
StringConstraints(max_length=500),
43+
Field(default=None, description="Description of the resource"),
44+
]
45+
url: Annotated[
46+
HttpUrl | None, UriSchema(), Field(..., description="Link to get this resource")
47+
]
48+
49+
@property
50+
def pep404_version(self) -> packaging.version.Version:
51+
"""Rich version type that can be used e.g. to compare"""
52+
return packaging.version.parse(self.version)
53+
54+
@property
55+
def url_friendly_id(self) -> str:
56+
"""Use to pass id as parameter in URLs"""
57+
return urllib.parse.quote_plus(self.id)
58+
59+
@property
60+
def resource_name(self) -> str:
61+
"""Relative resource name"""
62+
return self.compose_resource_name(self.id, self.version)
63+
64+
@property
65+
def name(self) -> str:
66+
"""API standards notation (see api_resources.py)"""
67+
return self.resource_name
68+
69+
@classmethod
70+
def compose_resource_name(cls, key: str, version: str) -> str:
71+
raise NotImplementedError("Subclasses must implement this method")

services/api-server/src/simcore_service_api_server/models/schemas/_utils.py

Lines changed: 0 additions & 20 deletions
This file was deleted.

services/api-server/src/simcore_service_api_server/models/schemas/files.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from .._utils_pydantic import UriSchema
1919
from ..domain.files import File as DomainFile
2020
from ..domain.files import FileName
21-
from ._utils import ApiServerInputSchema, ApiServerOutputSchema
21+
from ._base import ApiServerInputSchema, ApiServerOutputSchema
2222

2323

2424
class UserFile(ApiServerInputSchema):

services/api-server/src/simcore_service_api_server/models/schemas/jobs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
from ..domain.files import File as DomainFile
4242
from ..domain.files import FileInProgramJobData
4343
from ..schemas.files import UserFile
44-
from ._utils import ApiServerInputSchema
44+
from ._base import ApiServerInputSchema
4545

4646
# JOB SUB-RESOURCES ----------
4747
#

services/api-server/src/simcore_service_api_server/models/schemas/programs.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
from models_library.services_regex import DYNAMIC_SERVICE_KEY_RE
77
from packaging.version import Version
88
from pydantic import BaseModel, ConfigDict, Field, HttpUrl, StringConstraints
9-
from simcore_service_api_server.models.schemas._utils import ApiServerOutputSchema
9+
from simcore_service_api_server.models.schemas._base import (
10+
ApiServerOutputSchema,
11+
BaseService,
12+
)
1013

1114
from ...models._utils_pydantic import UriSchema
1215
from ..api_resources import compose_resource_name
@@ -27,7 +30,7 @@
2730
]
2831

2932

30-
class BaseResource(BaseModel):
33+
class BaseService(BaseModel):
3134
id: Annotated[str, Field(..., description="Resource identifier")]
3235
version: Annotated[
3336
VersionStr, Field(..., description="Semantic version number of the resource")
@@ -71,7 +74,7 @@ def compose_resource_name(cls, key: str, version: str) -> str:
7174
raise NotImplementedError("Subclasses must implement this method")
7275

7376

74-
class Program(BaseResource, ApiServerOutputSchema):
77+
class Program(BaseService, ApiServerOutputSchema):
7578
"""A released program with a specific version"""
7679

7780
model_config = ConfigDict(

services/api-server/src/simcore_service_api_server/models/schemas/solvers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from models_library.services_regex import COMPUTATIONAL_SERVICE_KEY_RE
66
from pydantic import BaseModel, ConfigDict, Field, StringConstraints
77

8-
from ...models.schemas.programs import BaseResource
8+
from ...models.schemas._base import BaseService
99
from ..api_resources import compose_resource_name
1010

1111
# NOTE:
@@ -32,7 +32,7 @@
3232
]
3333

3434

35-
class Solver(BaseResource):
35+
class Solver(BaseService):
3636
"""A released solver with a specific version"""
3737

3838
maintainer: str = Field(..., description="Maintainer of the solver")

0 commit comments

Comments
 (0)