Changeset View
Changeset View
Standalone View
Standalone View
swh/core/api/__init__.py
Show First 20 Lines • Show All 78 Lines • ▼ Show 20 Lines | |||||
def remote_api_endpoint(path): | def remote_api_endpoint(path): | ||||
def dec(f): | def dec(f): | ||||
f._endpoint_path = path | f._endpoint_path = path | ||||
return f | return f | ||||
return dec | return dec | ||||
class APIError(Exception): | |||||
"""API Error""" | |||||
def __str__(self): | |||||
return ('An unexpected error occurred in the backend: {}' | |||||
.format(self.args)) | |||||
class MetaSWHRemoteAPI(type): | class MetaSWHRemoteAPI(type): | ||||
"""Metaclass for SWHRemoteAPI, which adds a method for each endpoint | """Metaclass for SWHRemoteAPI, which adds a method for each endpoint | ||||
of the database it is designed to access. | of the database it is designed to access. | ||||
See for example :class:`swh.indexer.storage.api.client.RemoteStorage`""" | See for example :class:`swh.indexer.storage.api.client.RemoteStorage`""" | ||||
def __new__(cls, name, bases, attributes): | def __new__(cls, name, bases, attributes): | ||||
# For each method wrapped with @remote_api_endpoint in an API backend | # For each method wrapped with @remote_api_endpoint in an API backend | ||||
# (eg. :class:`swh.indexer.storage.IndexerStorage`), add a new | # (eg. :class:`swh.indexer.storage.IndexerStorage`), add a new | ||||
▲ Show 20 Lines • Show All 41 Lines • ▼ Show 20 Lines | class SWHRemoteAPI(metaclass=MetaSWHRemoteAPI): | ||||
"""For each method of `backend_class` decorated with | """For each method of `backend_class` decorated with | ||||
:func:`remote_api_endpoint`, a method with the same prototype and | :func:`remote_api_endpoint`, a method with the same prototype and | ||||
docstring will be added to this class. Calls to this new method will | docstring will be added to this class. Calls to this new method will | ||||
be translated into HTTP requests to a remote server. | be translated into HTTP requests to a remote server. | ||||
This backend class will never be instantiated, it only serves as | This backend class will never be instantiated, it only serves as | ||||
a template.""" | a template.""" | ||||
def __init__(self, api_exception, url, | api_exception = APIError | ||||
"""The exception class to raise in case of communication error with | |||||
the server.""" | |||||
def __init__(self, url, api_exception=None, | |||||
timeout=None, chunk_size=4096, **kwargs): | timeout=None, chunk_size=4096, **kwargs): | ||||
if api_exception: | |||||
self.api_exception = api_exception | self.api_exception = api_exception | ||||
base_url = url if url.endswith('/') else url + '/' | base_url = url if url.endswith('/') else url + '/' | ||||
self.url = base_url | self.url = base_url | ||||
self.session = requests.Session() | self.session = requests.Session() | ||||
self.timeout = timeout | self.timeout = timeout | ||||
self.chunk_size = chunk_size | self.chunk_size = chunk_size | ||||
def _url(self, endpoint): | def _url(self, endpoint): | ||||
return '%s%s' % (self.url, endpoint) | return '%s%s' % (self.url, endpoint) | ||||
▲ Show 20 Lines • Show All 157 Lines • Show Last 20 Lines |