Changeset View
Changeset View
Standalone View
Standalone View
swh/graphql/resolvers/content.py
Show All 11 Lines | |||||
from .snapshot_branch import BaseSnapshotBranchNode | from .snapshot_branch import BaseSnapshotBranchNode | ||||
class BaseContentNode(BaseSWHNode): | class BaseContentNode(BaseSWHNode): | ||||
""" | """ | ||||
Base resolver for all the content nodes | Base resolver for all the content nodes | ||||
""" | """ | ||||
def _get_content_by_hash(self, checksums: dict): | def _get_content_by_hashes(self, hashes: dict): | ||||
content = self.archive.get_contents(checksums) | content = self.archive.get_contents(hashes) | ||||
# in case of a conflict, return the first element | # in case of a conflict, return the first element | ||||
return content[0] if content else None | return content[0] if content else None | ||||
@property | @property | ||||
def checksum(self): | def hashes(self): | ||||
# FIXME, use a Node instead | # FIXME, use a Node instead | ||||
return {k: v.hex() for (k, v) in self._node.hashes().items()} | return {k: v.hex() for (k, v) in self._node.hashes().items()} | ||||
@property | @property | ||||
def id(self): | def id(self): | ||||
return self._node.sha1_git | return self._node.sha1_git | ||||
@property | @property | ||||
Show All 28 Lines | |||||
class ContentNode(BaseContentNode): | class ContentNode(BaseContentNode): | ||||
""" | """ | ||||
Node resolver for a content requested directly with its SWHID | Node resolver for a content requested directly with its SWHID | ||||
""" | """ | ||||
def _get_node_data(self): | def _get_node_data(self): | ||||
checksums = {"sha1_git": self.kwargs.get("swhid").object_id} | hashes = {"sha1_git": self.kwargs.get("swhid").object_id} | ||||
return self._get_content_by_hash(checksums) | return self._get_content_by_hashes(hashes) | ||||
class HashContentNode(BaseContentNode): | class HashContentNode(BaseContentNode): | ||||
""" | """ | ||||
Node resolver for a content requested with one or more checksums | Node resolver for a content requested with one or more hashes | ||||
""" | """ | ||||
def _get_node_data(self): | def _get_node_data(self): | ||||
checksums = dict(self.kwargs.get("checksums")) | hashes = dict(self.kwargs.get("hashes")) | ||||
return self._get_content_by_hash(checksums) | return self._get_content_by_hashes(hashes) | ||||
class TargetContentNode(BaseContentNode): | class TargetContentNode(BaseContentNode): | ||||
""" | """ | ||||
Node resolver for a content requested as a target | Node resolver for a content requested as a target | ||||
""" | """ | ||||
_can_be_null = True | _can_be_null = True | ||||
obj: Union[ | obj: Union[ | ||||
SearchResultNode, | SearchResultNode, | ||||
BaseDirectoryEntryNode, | BaseDirectoryEntryNode, | ||||
BaseReleaseNode, | BaseReleaseNode, | ||||
BaseSnapshotBranchNode, | BaseSnapshotBranchNode, | ||||
] | ] | ||||
def _get_node_data(self): | def _get_node_data(self): | ||||
return self._get_content_by_hash(checksums={"sha1_git": self.obj.target_hash}) | return self._get_content_by_hashes(hashes={"sha1_git": self.obj.target_hash}) |