Changeset View
Changeset View
Standalone View
Standalone View
swh/core/api/__init__.py
# Copyright (C) 2015-2017 The Software Heritage developers | # Copyright (C) 2015-2017 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 | ||||
import collections | import collections | ||||
import functools | import functools | ||||
import inspect | import inspect | ||||
import json | import json | ||||
import logging | import logging | ||||
import pickle | import pickle | ||||
import requests | import requests | ||||
import datetime | import datetime | ||||
from typing import ClassVar, Optional, Type | |||||
from deprecated import deprecated | from deprecated import deprecated | ||||
from flask import Flask, Request, Response, request, abort | from flask import Flask, Request, Response, request, abort | ||||
from .serializers import (decode_response, | from .serializers import (decode_response, | ||||
encode_data_client as encode_data, | encode_data_client as encode_data, | ||||
msgpack_dumps, msgpack_loads, SWHJSONDecoder) | msgpack_dumps, msgpack_loads, SWHJSONDecoder) | ||||
from .negotiation import (Formatter as FormatterBase, | from .negotiation import (Formatter as FormatterBase, | ||||
Negotiator as NegotiatorBase, | Negotiator as NegotiatorBase, | ||||
▲ Show 20 Lines • Show All 112 Lines • ▼ Show 20 Lines | def __add_endpoint(meth_name, meth, attributes): | ||||
attributes[meth_name] = meth_ | attributes[meth_name] = meth_ | ||||
class RPCClient(metaclass=MetaRPCClient): | class RPCClient(metaclass=MetaRPCClient): | ||||
"""Proxy to an internal SWH RPC | """Proxy to an internal SWH RPC | ||||
""" | """ | ||||
backend_class = None | backend_class = None # type: ClassVar[Optional[type]] | ||||
"""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.""" | ||||
api_exception = APIError | api_exception = APIError # type: ClassVar[Type[Exception]] | ||||
"""The exception class to raise in case of communication error with | """The exception class to raise in case of communication error with | ||||
the server.""" | the server.""" | ||||
def __init__(self, url, api_exception=None, | def __init__(self, url, api_exception=None, | ||||
timeout=None, chunk_size=4096, **kwargs): | timeout=None, chunk_size=4096, **kwargs): | ||||
if api_exception: | 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 + '/' | ||||
▲ Show 20 Lines • Show All 187 Lines • Show Last 20 Lines |