diff --git a/apps/schematic/api/schematic_api/controllers/schema_controller_impl.py b/apps/schematic/api/schematic_api/controllers/schema_controller_impl.py index 068bada78a..3a29fea04f 100644 --- a/apps/schematic/api/schematic_api/controllers/schema_controller_impl.py +++ b/apps/schematic/api/schematic_api/controllers/schema_controller_impl.py @@ -15,15 +15,14 @@ from schematic_api.models.connected_nodes import ConnectedNodes from schematic_api.controllers.utils import ( handle_exceptions, - handle_endpoint_status, download_schema_file_as_jsonld, ) -@handle_endpoint_status +@handle_exceptions def get_component( component_label: str, schema_url: str, include_index: bool = False -) -> str: +) -> tuple[Union[str, BasicError], int]: """ Get all the attributes associated with a specific data model component formatted as a dataframe (stored as a JSON String). @@ -34,13 +33,17 @@ def get_component( include_index (bool): Whether to include the indexes of the dataframe Returns: - str: The component + tuple[Union[str, BasicError], int]: A tuple + The first item is either the component or an error object + The second item is the response status """ schema_path = download_schema_file_as_jsonld(schema_url) explorer = AttributesExplorer(schema_path) - return explorer.parse_component_attributes( + result: Union[str, BasicError] = explorer.parse_component_attributes( component=component_label, save_file=False, include_index=include_index ) + status = 200 + return result, status def get_connected_nodes_from_schematic( @@ -190,8 +193,8 @@ def get_property_label( return result, status -@handle_endpoint_status -def get_schema_attributes(schema_url: str) -> str: +@handle_exceptions +def get_schema_attributes(schema_url: str) -> tuple[Union[str, BasicError], int]: """ Get all the attributes associated with a data model formatted as a dataframe (stored as a JSON String). @@ -200,11 +203,15 @@ def get_schema_attributes(schema_url: str) -> str: schema_url (str): The URL of the schema in json form Returns: - str: The schema + tuple[Union[str, BasicError], int]: A tuple + The first item is either the sttraibutes or an error object + The second item is the response status """ schema_path = download_schema_file_as_jsonld(schema_url) explorer = AttributesExplorer(schema_path) - return explorer.parse_attributes(save_file=False) + result: Union[str, BasicError] = explorer.parse_attributes(save_file=False) + status = 200 + return result, status def get_node_properties_from_schematic( diff --git a/apps/schematic/api/schematic_api/controllers/tangled_tree_controller_impl.py b/apps/schematic/api/schematic_api/controllers/tangled_tree_controller_impl.py index 8bff4c4470..986fc2287d 100644 --- a/apps/schematic/api/schematic_api/controllers/tangled_tree_controller_impl.py +++ b/apps/schematic/api/schematic_api/controllers/tangled_tree_controller_impl.py @@ -1,19 +1,20 @@ """Tangled tree controllers""" -from typing import Literal +from typing import Literal, Union from schematic.visualization.tangled_tree import TangledTree # type: ignore +from schematic_api.models.basic_error import BasicError from schematic_api.controllers.utils import ( - handle_endpoint_status, + handle_exceptions, download_schema_file_as_jsonld, ) -@handle_endpoint_status +@handle_exceptions def get_tangled_tree_layers( schema_url: str, figure_type: Literal["component", "dependency"] = "component" -) -> str: +) -> tuple[Union[str, BasicError], int]: """Gets layers for a tangled tree visualization. Args: @@ -21,8 +22,9 @@ def get_tangled_tree_layers( figure_type (Literal["component", "dependency"]): Figure type to generate. Returns: - str: A json in string form that represents the layers for a single - tangled tree + tuple[Union[str, BasicError], int]: A tuple + The first item is either the layers or an error object + The second item is the response status """ schema_path = download_schema_file_as_jsonld(schema_url) tangled_tree = TangledTree(schema_path, figure_type) @@ -34,15 +36,18 @@ def get_tangled_tree_layers( layers_list: list[str] = tangled_tree.get_tangled_tree_layers(save_file=False) if len(layers_list) == 0: raise ValueError("TangledTree.get_tangled_tree_layers() returned an empty list") - return layers_list[0] + result: Union[str, BasicError] = layers_list[0] + status = 200 + return result, status -@handle_endpoint_status + +@handle_exceptions def get_tangled_tree_text( schema_url: str, figure_type: Literal["component", "dependency"] = "component", text_format: Literal["plain", "highlighted"] = "plain", -) -> str: +) -> tuple[Union[str, BasicError], int]: """Gets text for a tangled tree visualization. Args: @@ -52,8 +57,15 @@ def get_tangled_tree_text( rendering to return Returns: - str: A csv in string form + tuple[Union[str, BasicError], int]: A tuple + The first item is either the text or an error object + The second item is the response status """ schema_path = download_schema_file_as_jsonld(schema_url) tangled_tree = TangledTree(schema_path, figure_type) - return tangled_tree.get_text_for_tangled_tree(text_format, save_file=False) + result: Union[str, BasicError] = tangled_tree.get_text_for_tangled_tree( + text_format, save_file=False + ) + status = 200 + + return result, status diff --git a/apps/schematic/api/schematic_api/controllers/utils.py b/apps/schematic/api/schematic_api/controllers/utils.py index d2b23152bc..921f47b5eb 100644 --- a/apps/schematic/api/schematic_api/controllers/utils.py +++ b/apps/schematic/api/schematic_api/controllers/utils.py @@ -66,48 +66,6 @@ def func(*args: Any, **kwargs: Any) -> tuple[Union[Any, BasicError], int]: return func -def handle_endpoint_status(endpoint_function: Callable) -> Callable: - """ - This is designed to be used as a decorator for endpoint functions. - The endpoint function is called in a try block, and then various - Synapse and Schematic exceptions are handled and returned as the - BasicError object. - - Args: - f (Callable): A function that calls the input function - """ - - def func(*args: Any, **kwargs: Any) -> tuple[Union[Any, BasicError], int]: - try: - result: Union[Any, BasicError] = endpoint_function(*args, **kwargs) - status = 200 - return result, status - - except SynapseNoCredentialsError as error: - status = 401 - result = BasicError( - "Missing or invalid Synapse credentials error", status, str(error) - ) - return result, status - - except SynapseAuthenticationError as error: - status = 401 - result = BasicError("Forbidden Synapse access error", status, str(error)) - return result, status - - except AccessCredentialsError as error: - status = 403 - result = BasicError("Synapse entity access error", status, str(error)) - return result, status - - except Exception as error: # pylint: disable=broad-exception-caught - status = 500 - result = BasicError("Internal error", status, str(error)) - return result, status - - return func - - def download_schema_file_as_jsonld(schema_url: str) -> str: """Downloads a schema and saves it as temp file