Changeset View
Changeset View
Standalone View
Standalone View
swh/objstorage/api/client.py
# Copyright (C) 2015-2022 The Software Heritage developers | # Copyright (C) 2015-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 Iterator, Optional | from typing import Any, Dict, Iterator, Optional | ||||
import msgpack | import msgpack | ||||
from swh.core.api import RPCClient | from swh.core.api import RPCClient | ||||
from swh.model import hashutil | |||||
from swh.objstorage.constants import DEFAULT_LIMIT | from swh.objstorage.constants import DEFAULT_LIMIT | ||||
from swh.objstorage.exc import Error, ObjNotFoundError, ObjStorageAPIError | from swh.objstorage.exc import Error, ObjNotFoundError, ObjStorageAPIError | ||||
from swh.objstorage.interface import CompositeObjId, ObjId, ObjStorageInterface | from swh.objstorage.interface import CompositeObjId, ObjId, ObjStorageInterface | ||||
from swh.objstorage.objstorage import objid_to_default_hex | |||||
class RemoteObjStorage(RPCClient): | class RemoteObjStorage(RPCClient): | ||||
"""Proxy to a remote object storage. | """Proxy to a remote object storage. | ||||
This class allows to connect to an object storage server via | This class allows to connect to an object storage server via | ||||
http protocol. | http protocol. | ||||
Show All 14 Lines | class RemoteObjStorage(RPCClient): | ||||
def __iter__(self) -> Iterator[CompositeObjId]: | def __iter__(self) -> Iterator[CompositeObjId]: | ||||
yield from self.list_content() | yield from self.list_content() | ||||
def list_content( | def list_content( | ||||
self, | self, | ||||
last_obj_id: Optional[ObjId] = None, | last_obj_id: Optional[ObjId] = None, | ||||
limit: int = DEFAULT_LIMIT, | limit: int = DEFAULT_LIMIT, | ||||
) -> Iterator[CompositeObjId]: | ) -> Iterator[CompositeObjId]: | ||||
params = {"limit": limit} | params: Dict[str, Any] = {"limit": limit} | ||||
if last_obj_id: | if last_obj_id: | ||||
params["last_obj_id"] = hashutil.hash_to_hex(last_obj_id) | params["last_obj_id"] = objid_to_default_hex(last_obj_id) | ||||
response = self.raw_verb( | response = self.raw_verb( | ||||
"get", | "get", | ||||
"content", | "content", | ||||
headers={"accept": "application/x-msgpack"}, | headers={"accept": "application/x-msgpack"}, | ||||
params=params, | params=params, | ||||
stream=True, | stream=True, | ||||
) | ) | ||||
yield from msgpack.Unpacker(response.raw, raw=True) | yield from msgpack.Unpacker(response.raw, raw=False) |