Skip to content

Commit

Permalink
refactoring cript.API._is_node_schema_valid
Browse files Browse the repository at this point in the history
taking the getting of node type from JSON and putting it into its own function that can be easily called and make the code cleaner
  • Loading branch information
nh916 committed Jul 25, 2023
1 parent 4f5f5a8 commit 0d94bc8
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 11 deletions.
14 changes: 3 additions & 11 deletions src/cript/api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
)
from cript.api.paginator import Paginator
from cript.api.utils.get_host_token import resolve_host_and_token
from cript.api.utils.helper_functions import _get_node_type_from_json
from cript.api.utils.save_helper import (
_fix_node_save,
_get_uuid_from_error_message,
Expand Down Expand Up @@ -477,18 +478,9 @@ def _is_node_schema_valid(self, node_json: str, is_patch: bool = False) -> bool:

db_schema = self._get_db_schema()

node_type: str = _get_node_type_from_json(node_json=node_json)

node_dict = json.loads(node_json)
try:
node_list = node_dict["node"]
except KeyError:
raise CRIPTJsonNodeError(node_list=node_dict["node"], json_str=json.dumps(node_dict))

# TODO should use the `_is_node_field_valid()` function from utils.py to keep the code DRY
# checking the node field "node": "Material"
if isinstance(node_list, list) and len(node_list) == 1 and isinstance(node_list[0], str):
node_type = node_list[0]
else:
raise CRIPTJsonNodeError(node_list, str(node_list))

# set the schema to test against http POST or PATCH of DB Schema
schema_http_method: str
Expand Down
1 change: 1 addition & 0 deletions src/cript/api/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# trunk-ignore-all(ruff/F401)

from .get_host_token import resolve_host_and_token
from .helper_functions import _get_node_type_from_json
44 changes: 44 additions & 0 deletions src/cript/api/utils/helper_functions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import json
from typing import Dict, List, Union

from cript.nodes.exceptions import CRIPTJsonNodeError
from cript.nodes.util import _is_node_field_valid


def _get_node_type_from_json(node_json: Union[Dict, str]) -> str:
"""
takes a node JSON and output the node_type `Project`, `Material`, etc.
1. convert node JSON dict or str to dict
1. do check the node list to be sure it only has a single type in it
1. get the node type and return it
Parameters
----------
node_json: [Dict, str]
Notes
-----
Takes a str or dict to be more versatile
Returns
-------
str:
node type
"""
# convert all JSON node strings to dict for easier handling
if isinstance(node_json, str):
node_json: Dict = json.loads(node_json)

try:
node_type_list: List[str] = node_json["node"]
except KeyError:
raise CRIPTJsonNodeError(node_list=node_json["node"], json_str=json.dumps(node_json))

# check to be sure the node list has a single type "node": ["Material"]
if _is_node_field_valid(node_type_list=node_type_list):
return node_type_list[0]

# if invalid then raise error
else:
raise CRIPTJsonNodeError(node_list=node_type_list, json_str=str(node_json))

0 comments on commit 0d94bc8

Please sign in to comment.