Skip to content

Commit

Permalink
refactor: skip create node tree when getting documents
Browse files Browse the repository at this point in the history
  • Loading branch information
eoaksnes committed Jan 23, 2024
1 parent 0f96550 commit df39484
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 20 deletions.
20 changes: 16 additions & 4 deletions src/features/document/use_cases/get_document_use_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,22 @@

from authentication.models import User
from common.address import Address
from common.tree.tree_node import ListNode, Node
from common.tree.tree_node_serializer import tree_node_to_dict
from common.exceptions import BadRequestException
from services.document_service.document_service import DocumentService
from storage.internal.data_source_repository import get_data_source


def get_nested_dict_attribute(entity: dict | list, path_list: list[str]) -> dict | list:
try:
if isinstance(entity, list):
path_list[0] = int(path_list[0].strip("[]")) # type: ignore
if len(path_list) == 1:
return entity[path_list[0]] # type: ignore
return get_nested_dict_attribute(entity[path_list[0]], path_list[1:]) # type: ignore
except (KeyError, IndexError) as e:
raise BadRequestException(f"Attribute/Item '{path_list[0]}' does not exists in '{entity}'") from e


def get_document_use_case(
user: User,
address: str,
Expand All @@ -17,5 +27,7 @@ def get_document_use_case(
"""Get document by reference."""
document_service = DocumentService(repository_provider=repository_provider, user=user)
address_object = Address.from_absolute(address)
node: Node | ListNode = document_service.get_document(address_object, depth)
return tree_node_to_dict(node)
resolved_document, resolved_address = document_service.resolve_document(address_object, depth)
if len(resolved_address.attribute_path) > 0:
return get_nested_dict_attribute(resolved_document, resolved_address.attribute_path)
return resolved_document
36 changes: 20 additions & 16 deletions src/services/document_service/document_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def save(
return result
return ref_dict

def get_document(self, address: Address, depth: int = 0) -> Node | ListNode:
def resolve_document(self, address: Address, depth: int = 0) -> tuple[dict, ResolvedAddress]:
"""
Get document by address.
Expand Down Expand Up @@ -228,26 +228,30 @@ def get_document(self, address: Address, depth: int = 0) -> Node | ListNode:
depth=depth + len(list(filter(lambda x: x[0] != "[", resolved_address.attribute_path))),
depth_count=1,
)

node: Node = tree_node_from_dict(
resolved_document,
uid=resolved_address.document_id,
blueprint_provider=self.get_blueprint,
recipe_provider=self.get_storage_recipes,
data_source=resolved_address.data_source_id,
)

if resolved_address.attribute_path:
child = node.get_by_path(resolved_address.attribute_path)
if not child:
raise NotFoundException(f"Invalid path {resolved_address.attribute_path}")
return child
return node
except (NotFoundException, ApplicationException) as e:
e.data = e.dict()
e.debug = e.message
e.message = f"Failed to get document referenced with '{address}'"
raise e
return resolved_document, resolved_address

def get_document(self, address: Address, depth: int = 0) -> Node | ListNode:
resolved_document, resolved_address = self.resolve_document(address, depth)

node: Node = tree_node_from_dict(
resolved_document,
uid=resolved_address.document_id,
blueprint_provider=self.get_blueprint,
recipe_provider=self.get_storage_recipes,
data_source=resolved_address.data_source_id,
)

if resolved_address.attribute_path:
child = node.get_by_path(resolved_address.attribute_path)
if not child:
raise NotFoundException(f"Invalid path {resolved_address.attribute_path}")
return child
return node

def remove(self, address: Address) -> None:
node = self.get_document(address)
Expand Down

0 comments on commit df39484

Please sign in to comment.