swh.core.api package

Module contents

class swh.core.api.Negotiator(func: Callable[[], Any])[source]

Bases: swh.core.api.negotiation.Negotiator

best_mimetype()[source]
swh.core.api.negotiate(formatter_cls, *args, **kwargs)[source]
class swh.core.api.Formatter(request_mimetype: Optional[str] = None)[source]

Bases: swh.core.api.negotiation.Formatter

configure(extra_encoders=None)[source]
class swh.core.api.JSONFormatter(request_mimetype: Optional[str] = None)[source]

Bases: swh.core.api.Formatter

format: Optional[str] = 'json'
mimetypes: List[str] = ['application/json']
render(obj)[source]
class swh.core.api.MsgpackFormatter(request_mimetype: Optional[str] = None)[source]

Bases: swh.core.api.Formatter

format: Optional[str] = 'msgpack'
mimetypes: List[str] = ['application/x-msgpack']
render(obj)[source]
exception swh.core.api.RemoteException(payload: Optional[Any] = None, response: Optional[requests.models.Response] = None)[source]

Bases: Exception

raised when remote returned an out-of-band failure notification, e.g., as a HTTP status code or serialized exception

response

HTTP response corresponding to the failure

swh.core.api.remote_api_endpoint(path) → Callable[[F], F][source]
exception swh.core.api.APIError[source]

Bases: Exception

API Error

class swh.core.api.MetaRPCClient(name, bases, attributes)[source]

Bases: type

Metaclass for RPCClient, which adds a method for each endpoint of the database it is designed to access.

See for example swh.indexer.storage.api.client.RemoteStorage

class swh.core.api.RPCClient(url, api_exception=None, timeout=None, chunk_size=4096, reraise_exceptions=None, **kwargs)[source]

Bases: object

Proxy to an internal SWH RPC

backend_class = None

For each method of backend_class decorated with remote_api_endpoint(), a method with the same prototype and docstring will be added to this class. Calls to this new method will be translated into HTTP requests to a remote server.

This backend class will never be instantiated, it only serves as a template.

extra_type_encoders: List[Tuple[type, str, Callable]] = []

Value of extra_encoders passed to json_dumps or msgpack_dumps to be able to serialize more object types.

extra_type_decoders: Dict[str, Callable] = {}

Value of extra_decoders passed to json_loads or msgpack_loads to be able to deserialize more object types.

api_exception

The exception class to raise in case of communication error with the server.

alias of APIError

reraise_exceptions: ClassVar[List[Type[Exception]]] = []

On server errors, if any of the exception classes in this list has the same name as the error name, then the exception will be instantiated and raised instead of a generic RemoteException.

raw_verb(verb, endpoint, **opts)[source]
post(endpoint, data, **opts)[source]
post_stream(endpoint, data, **opts)
get(endpoint, **opts)[source]
get_stream(endpoint, **opts)[source]
raise_for_status(response) → None[source]

check response HTTP status code and raise an exception if it denotes an error; do nothing otherwise

class swh.core.api.BytesRequest(environ, populate_request=True, shallow=False)[source]

Bases: flask.wrappers.Request

Request with proper escaping of arbitrary byte sequences.

encoding = 'utf-8'
encoding_errors = 'surrogateescape'
swh.core.api.encode_data_server(data, content_type='application/x-msgpack', extra_type_encoders=None)[source]
swh.core.api.decode_request(request, extra_decoders=None)[source]
swh.core.api.error_handler(exception, encoder, status_code=500)[source]
class swh.core.api.RPCServerApp(*args, backend_class=None, backend_factory=None, **kwargs)[source]

Bases: flask.app.Flask

For each endpoint of the given backend_class, tells app.route to call a function that decodes the request and sends it to the backend object provided by the factory.

Parameters
  • backend_class (Any) – The class of the backend, which will be analyzed to look for API endpoints.

  • backend_class]] backend_factory (Optional[Callable[[],) – A function with no argument that returns an instance of backend_class. If unset, defaults to calling backend_class constructor directly.

request_class

alias of BytesRequest

extra_type_encoders: List[Tuple[type, str, Callable]] = []

Value of extra_encoders passed to json_dumps or msgpack_dumps to be able to serialize more object types.

extra_type_decoders: Dict[str, Callable] = {}

Value of extra_decoders passed to json_loads or msgpack_loads to be able to deserialize more object types.