Changeset View
Changeset View
Standalone View
Standalone View
swh/objstorage/backends/http.py
# Copyright (C) 2021 The Software Heritage developers | # Copyright (C) 2021 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 | ||||
import logging | import logging | ||||
from typing import Iterator, Optional | from typing import Iterator, Optional | ||||
from urllib.parse import urljoin | from urllib.parse import urljoin | ||||
import requests | import requests | ||||
from swh.model import hashutil | from swh.model import hashutil | ||||
from swh.objstorage import exc | from swh.objstorage import exc | ||||
from swh.objstorage.interface import ObjId | from swh.objstorage.interface import CompositeObjId, ObjId | ||||
from swh.objstorage.objstorage import ( | from swh.objstorage.objstorage import ( | ||||
DEFAULT_LIMIT, | DEFAULT_LIMIT, | ||||
ObjStorage, | ObjStorage, | ||||
compute_hash, | compute_hash, | ||||
decompressors, | decompressors, | ||||
) | ) | ||||
LOGGER = logging.getLogger(__name__) | LOGGER = logging.getLogger(__name__) | ||||
Show All 17 Lines | def __init__(self, url=None, compression=None, **kwargs): | ||||
if not self.root_path.endswith("/"): | if not self.root_path.endswith("/"): | ||||
self.root_path += "/" | self.root_path += "/" | ||||
self.compression = compression | self.compression = compression | ||||
def check_config(self, *, check_write): | def check_config(self, *, check_write): | ||||
"""Check the configuration for this object storage""" | """Check the configuration for this object storage""" | ||||
return True | return True | ||||
def __contains__(self, obj_id): | def __contains__(self, obj_id: ObjId) -> bool: | ||||
resp = self.session.head(self._path(obj_id)) | resp = self.session.head(self._path(obj_id)) | ||||
return resp.status_code == 200 | return resp.status_code == 200 | ||||
def __iter__(self): | def __iter__(self) -> Iterator[CompositeObjId]: | ||||
raise exc.NonIterableObjStorage("__iter__") | raise exc.NonIterableObjStorage("__iter__") | ||||
def __len__(self): | def __len__(self): | ||||
raise exc.NonIterableObjStorage("__len__") | raise exc.NonIterableObjStorage("__len__") | ||||
def add(self, content: bytes, obj_id: ObjId, check_presence: bool = True) -> None: | def add(self, content: bytes, obj_id: ObjId, check_presence: bool = True) -> None: | ||||
raise exc.ReadOnlyObjStorage("add") | raise exc.ReadOnlyObjStorage("add") | ||||
def delete(self, obj_id: ObjId): | def delete(self, obj_id: ObjId): | ||||
raise exc.ReadOnlyObjStorage("delete") | raise exc.ReadOnlyObjStorage("delete") | ||||
def restore(self, content: bytes, obj_id: ObjId) -> None: | def restore(self, content: bytes, obj_id: ObjId) -> None: | ||||
raise exc.ReadOnlyObjStorage("restore") | raise exc.ReadOnlyObjStorage("restore") | ||||
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[ObjId]: | ) -> Iterator[CompositeObjId]: | ||||
raise exc.NonIterableObjStorage("__len__") | raise exc.NonIterableObjStorage("__len__") | ||||
def get(self, obj_id: ObjId) -> bytes: | def get(self, obj_id: ObjId) -> bytes: | ||||
try: | try: | ||||
resp = self.session.get(self._path(obj_id)) | resp = self.session.get(self._path(obj_id)) | ||||
resp.raise_for_status() | resp.raise_for_status() | ||||
except Exception: | except Exception: | ||||
raise exc.ObjNotFoundError(obj_id) | raise exc.ObjNotFoundError(obj_id) | ||||
Show All 19 Lines |