Changeset View
Changeset View
Standalone View
Standalone View
swh/lister/core/lister_transports.py
# Copyright (C) 2017-2018 the Software Heritage developers | # Copyright (C) 2017-2018 the Software Heritage developers | ||||
# 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 abc | import abc | ||||
import random | import random | ||||
from datetime import datetime | from datetime import datetime | ||||
from email.utils import parsedate | from email.utils import parsedate | ||||
from pprint import pformat | from pprint import pformat | ||||
from xmlrpc import client | |||||
import requests | import requests | ||||
import xmltodict | import xmltodict | ||||
try: | try: | ||||
from swh.lister._version import __version__ | from swh.lister._version import __version__ | ||||
except ImportError: | except ImportError: | ||||
__version__ = 'devel' | __version__ = 'devel' | ||||
from .abstractattribute import AbstractAttribute | from .abstractattribute import AbstractAttribute | ||||
from .lister_base import FetchError | from .lister_base import FetchError | ||||
class ListerXMLRPCTransport(abc.ABC): | |||||
"""Use the xmlrpc library for making Lister endpoint requests. | |||||
To be used in conjunction with SWHListerBase or a subclass of it. | |||||
""" | |||||
SERVER = AbstractAttribute('string containing the server to contact for ' | |||||
'information') | |||||
def __init__(self): | |||||
self.lister_version = __version__ | |||||
def get_client(self, path): | |||||
"""Initialize client to query for result | |||||
""" | |||||
return client.ServerProxy(path) | |||||
def request_uri(self, _): | |||||
"""Same uri called once | |||||
""" | |||||
return self.SERVER | |||||
def request_params(self, identifier): | |||||
"""Cannot pass any parameters to query to the xmlrpc client so cannot | |||||
even pass our user-agent specifics. | |||||
""" | |||||
return {} | |||||
def transport_quota_check(self, response): | |||||
"""No rate limit dealing explained. | |||||
""" | |||||
return False, 0 | |||||
def transport_request(self, identifier): | |||||
"""Implements SWHListerBase.transport_request | |||||
""" | |||||
path = self.request_uri(identifier) | |||||
try: | |||||
return self.get_client(path) | |||||
except Exception as e: | |||||
raise FetchError(e) | |||||
def transport_response_to_string(self, response): | |||||
"""Implements SWHListerBase.transport_response_to_string for XMLRPC | |||||
given responses. | |||||
""" | |||||
s = pformat(self.SERVER) | |||||
s += '\n#\n' + pformat(response) # Note: will potentially be big | |||||
return s | |||||
class SWHListerHttpTransport(abc.ABC): | class SWHListerHttpTransport(abc.ABC): | ||||
"""Use the Requests library for making Lister endpoint requests. | """Use the Requests library for making Lister endpoint requests. | ||||
To be used in conjunction with SWHListerBase or a subclass of it. | To be used in conjunction with SWHListerBase or a subclass of it. | ||||
""" | """ | ||||
PATH_TEMPLATE = AbstractAttribute('string containing a python string' | PATH_TEMPLATE = AbstractAttribute('string containing a python string' | ||||
' format pattern that produces the API' | ' format pattern that produces the API' | ||||
▲ Show 20 Lines • Show All 145 Lines • Show Last 20 Lines |