Skip to content

Commit 3bd7812

Browse files
Rename repository module as services
1 parent 0662c07 commit 3bd7812

File tree

16 files changed

+219
-199
lines changed

16 files changed

+219
-199
lines changed

aiida_restapi/main.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@
1111

1212

1313
def create_app() -> FastAPI:
14-
"""Create the FastAPI application and include the routers."""
14+
"""Create the FastAPI application and include the routers.
15+
16+
:return: The FastAPI application.
17+
:rtype: FastAPI
18+
"""
1519

1620
read_only = os.getenv('AIIDA_RESTAPI_READ_ONLY') == '1'
1721

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
"""REST API models for ORM nodes."""
2-
31
from __future__ import annotations
42

53
import typing as t
@@ -10,6 +8,80 @@
108
from importlib_metadata import EntryPoint
119

1210

11+
class NodeStatistics(pdt.BaseModel):
12+
"""Pydantic model representing node statistics."""
13+
14+
total: int = pdt.Field(
15+
description='Total number of nodes.',
16+
examples=[47],
17+
)
18+
types: dict[str, int] = pdt.Field(
19+
description='Number of nodes by type.',
20+
examples=[
21+
{
22+
'data.core.int.Int.': 42,
23+
'data.core.singlefile.SinglefileData.': 5,
24+
}
25+
],
26+
)
27+
ctime_by_day: dict[str, int] = pdt.Field(
28+
description='Number of nodes created per day (YYYY-MM-DD).',
29+
examples=[
30+
{
31+
'2012-01-01': 10,
32+
'2012-01-02': 15,
33+
}
34+
],
35+
)
36+
37+
38+
class NodeType(pdt.BaseModel):
39+
"""Pydantic model representing a node type."""
40+
41+
label: str = pdt.Field(description='The class name of the node type.')
42+
node_type: str = pdt.Field(description='The AiiDA node type string.')
43+
nodes: str = pdt.Field(description='The URL to access nodes of this type.')
44+
projections: str = pdt.Field(description='The URL to access projectable properties of this node type.')
45+
node_schema: str = pdt.Field(description='The URL to access the schema of this node type.')
46+
47+
48+
class RepoFileMetadata(pdt.BaseModel):
49+
"""Pydantic model representing the metadata of a file in the AiiDA repository."""
50+
51+
type: t.Literal['FILE'] = pdt.Field(
52+
description='The type of the repository object.',
53+
)
54+
binary: bool = pdt.Field(
55+
False,
56+
description='Whether the file is binary.',
57+
)
58+
size: int = pdt.Field(
59+
description='The size of the file in bytes.',
60+
)
61+
download: str = pdt.Field(
62+
description='The URL to download the file.',
63+
)
64+
65+
66+
class RepoDirMetadata(pdt.BaseModel):
67+
"""Pydantic model representing the metadata of a directory in the AiiDA repository."""
68+
69+
type: t.Literal['DIRECTORY'] = pdt.Field(
70+
description='The type of the repository object.',
71+
)
72+
objects: dict[str, t.Union[RepoFileMetadata, 'RepoDirMetadata']] = pdt.Field(
73+
description='A dictionary with the metadata of the objects in the directory.',
74+
)
75+
76+
77+
MetadataType = t.Union[RepoFileMetadata, RepoDirMetadata]
78+
79+
80+
class NodeLink(Node.Model):
81+
link_label: str = pdt.Field(description='The label of the link to the node.')
82+
link_type: str = pdt.Field(description='The type of the link to the node.')
83+
84+
1385
class NodeModelRegistry:
1486
"""Registry for AiiDA REST API node models.
1587

aiida_restapi/models/node/__init__.py

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

aiida_restapi/models/node/models.py

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

aiida_restapi/routers/computers.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111

1212
from aiida_restapi.common.pagination import PaginatedResults
1313
from aiida_restapi.common.query import QueryParams, query_params
14-
from aiida_restapi.repository.entity import EntityRepository
14+
from aiida_restapi.services.entity import EntityService
1515

1616
from .auth import UserInDB, get_current_active_user
1717

1818
read_router = APIRouter(prefix='/computers')
1919
write_router = APIRouter(prefix='/computers')
2020

21-
repository = EntityRepository[orm.Computer, orm.Computer.Model](orm.Computer)
21+
service = EntityService[orm.Computer, orm.Computer.Model](orm.Computer)
2222

2323

2424
@read_router.get(
@@ -39,23 +39,23 @@ async def get_computers_schema(
3939
500 for any other failures.
4040
"""
4141
try:
42-
return repository.get_entity_schema(which=which)
42+
return service.get_schema(which=which)
4343
except ValueError as exception:
4444
raise HTTPException(status_code=422, detail=str(exception)) from exception
4545
except Exception as exception:
4646
raise HTTPException(status_code=500, detail=str(exception)) from exception
4747

4848

4949
@read_router.get(
50-
'/projectable_properties',
50+
'/projections',
5151
response_model=list[str],
5252
)
53-
async def get_computer_projectable_properties() -> list[str]:
54-
"""Get projectable properties for AiiDA computers.
53+
async def get_computer_projections() -> list[str]:
54+
"""Get queryable projections for AiiDA computers.
5555
56-
:return: The list of projectable properties for AiiDA computers.
56+
:return: The list of queryable projections for AiiDA computers.
5757
"""
58-
return repository.get_projectable_properties()
58+
return service.get_projections()
5959

6060

6161
@read_router.get(
@@ -73,7 +73,7 @@ async def get_computers(
7373
:param queries: The query parameters, including filters, order_by, page_size, and page.
7474
:return: The paginated results, including total count, current page, page size, and list of computer models.
7575
"""
76-
return repository.get_entities(queries)
76+
return service.get_many(queries)
7777

7878

7979
@read_router.get(
@@ -92,7 +92,7 @@ async def get_computer(pk: str) -> orm.Computer.Model:
9292
500 for any other failures.
9393
"""
9494
try:
95-
return repository.get_entity_by_id(pk)
95+
return service.get_one(pk)
9696
except NotExistent as exception:
9797
raise HTTPException(status_code=404, detail=str(exception)) from exception
9898
except Exception as exception:
@@ -113,8 +113,7 @@ async def get_computer_metadata(pk: str) -> dict[str, t.Any]:
113113
500 for any other failures.
114114
"""
115115
try:
116-
computer = repository.get_entity_by_id(pk)
117-
return computer.metadata
116+
return service.get_field(pk, 'metadata')
118117
except NotExistent as exception:
119118
raise HTTPException(status_code=404, detail=str(exception)) from exception
120119
except Exception as exception:
@@ -140,6 +139,6 @@ async def create_computer(
140139
:raises HTTPException: 500 for any failures during computer creation.
141140
"""
142141
try:
143-
return repository.create_entity(computer_model)
142+
return service.add_one(computer_model)
144143
except Exception as exception:
145144
raise HTTPException(status_code=500, detail=str(exception))

aiida_restapi/routers/groups.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111

1212
from aiida_restapi.common.pagination import PaginatedResults
1313
from aiida_restapi.common.query import QueryParams, query_params
14-
from aiida_restapi.repository.entity import EntityRepository
14+
from aiida_restapi.services.entity import EntityService
1515

1616
from .auth import UserInDB, get_current_active_user
1717

1818
read_router = APIRouter(prefix='/groups')
1919
write_router = APIRouter(prefix='/groups')
2020

21-
repository = EntityRepository[orm.Group, orm.Group.Model](orm.Group)
21+
service = EntityService[orm.Group, orm.Group.Model](orm.Group)
2222

2323

2424
@read_router.get(
@@ -39,23 +39,23 @@ async def get_groups_schema(
3939
500 for any other failures.
4040
"""
4141
try:
42-
return repository.get_entity_schema(which=which)
42+
return service.get_schema(which=which)
4343
except ValueError as exception:
4444
raise HTTPException(status_code=422, detail=str(exception)) from exception
4545
except Exception as exception:
4646
raise HTTPException(status_code=500, detail=str(exception)) from exception
4747

4848

4949
@read_router.get(
50-
'/projectable_properties',
50+
'/projections',
5151
response_model=list[str],
5252
)
53-
async def get_group_projectable_properties() -> list[str]:
54-
"""Get projectable properties for AiiDA groups.
53+
async def get_group_projections() -> list[str]:
54+
"""Get queryable projections for AiiDA groups.
5555
56-
:return: The list of projectable properties for AiiDA groups.
56+
:return: The list of queryable projections for AiiDA groups.
5757
"""
58-
return repository.get_projectable_properties()
58+
return service.get_projections()
5959

6060

6161
@read_router.get(
@@ -73,7 +73,7 @@ async def get_groups(
7373
:param queries: The query parameters, including filters, order_by, page_size, and page.
7474
:return: The paginated results, including total count, current page, page size, and list of group models.
7575
"""
76-
return repository.get_entities(queries)
76+
return service.get_many(queries)
7777

7878

7979
@read_router.get(
@@ -92,7 +92,7 @@ async def get_group(uuid: str) -> orm.Group.Model:
9292
500 for any other server error.
9393
"""
9494
try:
95-
return repository.get_entity_by_id(uuid)
95+
return service.get_one(uuid)
9696
except NotExistent as exception:
9797
raise HTTPException(status_code=404, detail=str(exception)) from exception
9898
except Exception as exception:
@@ -113,7 +113,7 @@ async def get_group_extras(uuid: str) -> dict[str, t.Any]:
113113
500 for other failures during retrieval.
114114
"""
115115
try:
116-
return repository.get_entity_extras(uuid)
116+
return service.get_field(uuid, 'extras')
117117
except NotExistent as exception:
118118
raise HTTPException(status_code=404, detail=str(exception)) from exception
119119
except Exception as exception:
@@ -139,6 +139,6 @@ async def create_group(
139139
:raises HTTPException: 500 for any failures during group creation.
140140
"""
141141
try:
142-
return repository.create_entity(group_model)
142+
return service.add_one(group_model)
143143
except Exception as exception:
144144
raise HTTPException(status_code=500, detail=str(exception)) from exception

0 commit comments

Comments
 (0)