Changeset View
Changeset View
Standalone View
Standalone View
swh/deposit/client/__init__.py
# Copyright (C) 2017-2018 The Software Heritage developers | # Copyright (C) 2017-2018 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 requests | import requests | ||||
import xmltodict | import xmltodict | ||||
import logging | import logging | ||||
from abc import ABCMeta, abstractmethod | from abc import ABCMeta, abstractmethod | ||||
from urllib.parse import urljoin | |||||
from swh.core.config import SWHConfig | from swh.core.config import SWHConfig | ||||
logger = logging.getLogger(__name__) | logger = logging.getLogger(__name__) | ||||
def _parse(stream, encoding='utf-8'): | def _parse(stream, encoding='utf-8'): | ||||
▲ Show 20 Lines • Show All 52 Lines • ▼ Show 20 Lines | class BaseApiDepositClient(SWHConfig): | ||||
def __init__(self, config=None, _client=requests): | def __init__(self, config=None, _client=requests): | ||||
super().__init__() | super().__init__() | ||||
if config is None: | if config is None: | ||||
self.config = super().parse_config_file() | self.config = super().parse_config_file() | ||||
else: | else: | ||||
self.config = config | self.config = config | ||||
self._client = _client | self._client = _client | ||||
self.base_url = self.config['url'] | 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 | ||||
else: | else: | ||||
self.auth = (auth['username'], auth['password']) | self.auth = (auth['username'], auth['password']) | ||||
def do(self, method, url, *args, **kwargs): | def do(self, method, url, *args, **kwargs): | ||||
"""Internal method to deal with requests, possibly with basic http | """Internal method to deal with requests, possibly with basic http | ||||
Show All 10 Lines | def do(self, method, url, *args, **kwargs): | ||||
method_fn = getattr(self._client, method) | method_fn = getattr(self._client, method) | ||||
else: | else: | ||||
raise ValueError('Development error, unsupported method %s' % ( | raise ValueError('Development error, unsupported method %s' % ( | ||||
method)) | method)) | ||||
if self.auth: | if self.auth: | ||||
kwargs['auth'] = self.auth | kwargs['auth'] = self.auth | ||||
full_url = '%s%s' % (self.base_url.rstrip('/'), url) | full_url = urljoin(self.base_url, url.lstrip('/')) | ||||
ardumont: heads up, that's the code giving you and me the hardtime when we discuss it in [1]
[1]… | |||||
return method_fn(full_url, *args, **kwargs) | return method_fn(full_url, *args, **kwargs) | ||||
class PrivateApiDepositClient(BaseApiDepositClient): | class PrivateApiDepositClient(BaseApiDepositClient): | ||||
"""Private API deposit client to: | """Private API deposit client to: | ||||
- read a given deposit's archive(s) | - read a given deposit's archive(s) | ||||
- read a given deposit's metadata | - read a given deposit's metadata | ||||
▲ Show 20 Lines • Show All 460 Lines • Show Last 20 Lines |
heads up, that's the code giving you and me the hardtime when we discuss it in [1]
[1] D2427#inline-16447