Skip to content

Commit

Permalink
redid exception handling for new endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewelamb committed Oct 25, 2023
1 parent eb947e2 commit 37cf270
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand All @@ -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(
Expand Down Expand Up @@ -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).
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
"""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:
schema_url (str): The URL to the schema file
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)
Expand All @@ -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:
Expand All @@ -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
42 changes: 0 additions & 42 deletions apps/schematic/api/schematic_api/controllers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 37cf270

Please sign in to comment.