Changeset View
Changeset View
Standalone View
Standalone View
swh/graphql/resolvers/content.py
# Copyright (C) 2022 The Software Heritage developers | # Copyright (C) 2022 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
from typing import Union | from typing import Union | ||||
from swh.graphql.errors import InvalidInputError | |||||
from swh.model import hashutil | |||||
from .base_node import BaseSWHNode | from .base_node import BaseSWHNode | ||||
from .directory_entry import BaseDirectoryEntryNode | from .directory_entry import BaseDirectoryEntryNode | ||||
from .release import BaseReleaseNode | from .release import BaseReleaseNode | ||||
from .search import SearchResultNode | from .search import SearchResultNode | ||||
from .snapshot_branch import BaseSnapshotBranchNode | from .snapshot_branch import BaseSnapshotBranchNode | ||||
class BaseContentNode(BaseSWHNode): | class BaseContentNode(BaseSWHNode): | ||||
▲ Show 20 Lines • Show All 57 Lines • ▼ Show 20 Lines | |||||
class HashContentNode(BaseContentNode): | class HashContentNode(BaseContentNode): | ||||
""" | """ | ||||
Node resolver for a content requested with one or more hashes | Node resolver for a content requested with one or more hashes | ||||
""" | """ | ||||
def _get_node_data(self): | def _get_node_data(self): | ||||
hashes = dict(self.kwargs.get("hashes")) | try: | ||||
hashes = { | |||||
hash_type: hashutil.hash_to_bytes(hash_value) | |||||
for (hash_type, hash_value) in self.kwargs.items() | |||||
} | |||||
except ValueError as e: | |||||
# raise an input error in case of an invalid hash | |||||
raise InvalidInputError("Invalid content hash", e) | |||||
if not hashes: | |||||
raise InvalidInputError("At least one of the four hashes must be provided") | |||||
return self._get_content_by_hashes(hashes) | 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 | ||||
""" | """ | ||||
Show All 10 Lines |