-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add `/nodes` endpoint supporting GET and POST methods for creating and querying nodes of the AiiDA graph. Some node types, such as `SinglefileData` require a file to be passed to the node constructor. In order to avoid complications with multiple requests and be able to send both the JSON metadata and the file in the same request, a `/nodes/singlefile` endpoint is added that accepts content type `multipart/form-data`. This introduces a bit of inconsistency in the use of the API (which is otherwise `application/json`) but is is a practical workaround for the time being until a better solution is identified.
- Loading branch information
Showing
12 changed files
with
580 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
# -*- coding: utf-8 -*- | ||
"""Declaration of FastAPI application.""" | ||
from typing import List, Optional | ||
|
||
from aiida import orm | ||
from aiida.cmdline.utils.decorators import with_dbenv | ||
from fastapi import APIRouter, Depends, File, HTTPException | ||
from importlib_metadata import entry_points | ||
|
||
from aiida_restapi import models | ||
|
||
from .auth import get_current_active_user | ||
|
||
router = APIRouter() | ||
|
||
ENTRY_POINTS = entry_points() | ||
|
||
|
||
@router.get("/nodes", response_model=List[models.Node]) | ||
@with_dbenv() | ||
async def read_nodes() -> List[models.Node]: | ||
"""Get list of all nodes""" | ||
return models.Node.get_entities() | ||
|
||
|
||
@router.get("/nodes/projectable_properties", response_model=List[str]) | ||
async def get_nodes_projectable_properties() -> List[str]: | ||
"""Get projectable properties for nodes endpoint""" | ||
|
||
return models.Node.get_projectable_properties() | ||
|
||
|
||
@router.get("/nodes/{nodes_id}", response_model=models.Node) | ||
@with_dbenv() | ||
async def read_node(nodes_id: int) -> Optional[models.Node]: | ||
"""Get nodes by id.""" | ||
qbobj = orm.QueryBuilder() | ||
|
||
qbobj.append(orm.Node, filters={"id": nodes_id}, project=["**"], tag="node").limit( | ||
1 | ||
) | ||
return qbobj.dict()[0]["node"] | ||
|
||
|
||
@router.post("/nodes", response_model=models.Node) | ||
@with_dbenv() | ||
async def create_node( | ||
node: models.Node_Post, | ||
current_user: models.User = Depends( | ||
get_current_active_user | ||
), # pylint: disable=unused-argument | ||
) -> models.Node: | ||
"""Create new AiiDA node.""" | ||
|
||
node_dict = node.dict(exclude_unset=True) | ||
node_type = node_dict.pop("node_type", None) | ||
|
||
try: | ||
(entry_point_node,) = ENTRY_POINTS.select( | ||
group="aiida.rest.post", name=node_type | ||
) | ||
except ValueError as exc: | ||
raise HTTPException( | ||
status_code=404, detail="Entry point '{}' not recognized.".format(node_type) | ||
) from exc | ||
|
||
try: | ||
orm_object = entry_point_node.load().create_new_node(node_type, node_dict) | ||
except (TypeError, ValueError, KeyError) as err: | ||
raise HTTPException(status_code=400, detail="Error: {0}".format(err)) from err | ||
|
||
return models.Node.from_orm(orm_object) | ||
|
||
|
||
@router.post("/nodes/singlefile", response_model=models.Node) | ||
@with_dbenv() | ||
async def create_upload_file( | ||
upload_file: bytes = File(...), | ||
params: models.Node_Post = Depends(models.Node_Post.as_form), # type: ignore # pylint: disable=maybe-no-member | ||
current_user: models.User = Depends( | ||
get_current_active_user | ||
), # pylint: disable=unused-argument | ||
) -> models.Node: | ||
"""Endpoint for uploading file data""" | ||
node_dict = params.dict(exclude_unset=True, exclude_none=True) | ||
node_type = node_dict.pop("node_type", None) | ||
|
||
try: | ||
(entry_point_node,) = entry_points(group="aiida.rest.post", name=node_type) | ||
except KeyError as exc: | ||
raise KeyError("Entry point '{}' not recognized.".format(node_type)) from exc | ||
|
||
orm_object = entry_point_node.load().create_new_node_with_file( | ||
node_type, node_dict, upload_file | ||
) | ||
|
||
return models.Node.from_orm(orm_object) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
data: | ||
aiidaVersion: 1.6.4 | ||
aiidaVersion: 1.6.5 | ||
node: | ||
label: node 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
- hostname: localhost_1 | ||
- description: '' | ||
hostname: localhost_1 | ||
id: int | ||
metadata: {} | ||
name: test_comp_1 | ||
|
Oops, something went wrong.