Changeset View
Changeset View
Standalone View
Standalone View
swh/deposit/client.py
# Copyright (C) 2017-2020 The Software Heritage developers | # Copyright (C) 2017-2020 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 | ||||
"""Module in charge of defining an swh-deposit client | """Module in charge of defining an swh-deposit client | ||||
""" | """ | ||||
import hashlib | import hashlib | ||||
import os | import os | ||||
import logging | |||||
import requests | import requests | ||||
import xmltodict | import xmltodict | ||||
import logging | |||||
from abc import ABCMeta, abstractmethod | from abc import ABCMeta, abstractmethod | ||||
from typing import Any, Dict | from typing import Any, Dict | ||||
from urllib.parse import urljoin | from urllib.parse import urljoin | ||||
from swh.core.config import SWHConfig | from swh.core.config import read_raw_config, config_basepath | ||||
logger = logging.getLogger(__name__) | logger = logging.getLogger(__name__) | ||||
class MaintenanceError(ValueError): | class MaintenanceError(ValueError): | ||||
"""Informational maintenance error exception | """Informational maintenance error exception | ||||
""" | """ | ||||
Show All 39 Lines | def _parse_with_filter(stream, encoding="utf-8", keys=[]): | ||||
""" | """ | ||||
data = _parse(stream, encoding=encoding) | data = _parse(stream, encoding=encoding) | ||||
m = {} | m = {} | ||||
for key in keys: | for key in keys: | ||||
m[key] = data.get(key) | m[key] = data.get(key) | ||||
return m | return m | ||||
class BaseApiDepositClient(SWHConfig): | class BaseApiDepositClient: | ||||
"""Deposit client base class | """Deposit client base class | ||||
""" | """ | ||||
CONFIG_BASE_FILENAME = "deposit/client" | |||||
DEFAULT_CONFIG = { | |||||
"url": ("str", "http://localhost:5006"), | |||||
"auth": ("dict", {}), # with optional 'username'/'password' keys | |||||
} | |||||
def __init__(self, config=None, _client=requests): | def __init__(self, config=None, _client=requests): | ||||
super().__init__() | |||||
if config is None: | if config is None: | ||||
self.config = super().parse_config_file() | config_file = os.environ["SWH_CONFIG_FILENAME"] | ||||
self.config: Dict[str, Any] = read_raw_config(config_basepath(config_file)) | |||||
else: | else: | ||||
self.config = config | self.config = config | ||||
self._client = _client | self._client = _client | ||||
self.base_url = self.config["url"].strip("/") + "/" | self.base_url = self.config["url"].strip("/") + "/" | ||||
auth = self.config["auth"] | auth = self.config["auth"] | ||||
if auth == {}: | if auth == {}: | ||||
self.auth = None | self.auth = None | ||||
▲ Show 20 Lines • Show All 559 Lines • Show Last 20 Lines |