diff --git a/swh/core/api.py b/swh/core/api.py --- a/swh/core/api.py +++ b/swh/core/api.py @@ -4,6 +4,8 @@ # See top-level LICENSE file for more information import collections +import functools +import inspect import json import logging import pickle @@ -19,6 +21,13 @@ pass +def remote_api_endpoint(path): + def dec(f): + f._endpoint_path = path + return f + return dec + + class SWHRemoteAPI: """Proxy to an internal SWH API @@ -104,6 +113,42 @@ return decode_response(response) +def autogen_client_api(db_class): + """Decorator for SWHRemoteAPI, which adds a method for each endpoint + of the database it is designed to access. + + See for example :class:`swh.indexer.storage.api.client.RemoteStorage`""" + def decorator(client_api_class): + # For each method wrapped with @remote_api_endpoint in an API backend + # (eg. :class:`swh.indexer.storage.IndexerStorage`), add a new + # method in RemoteStorage, with the same documentation. + # + # Note that, despite the usage of decorator magic (eg. functools.wrap), + # this never actually calls an IndexerStorage method. + for (name, meth) in db_class.__dict__.items(): + if hasattr(meth, '_endpoint_path'): + def _closure(name, meth): + wrapped_meth = inspect.unwrap(meth) + + @functools.wraps(meth) # Copy signature and doc + def meth_(*args, **kwargs): + # Match arguments and parameters + post_data = inspect.getcallargs( + wrapped_meth, *args, **kwargs) + + # Remove arguments that should not be passed + self = post_data.pop('self') + post_data.pop('cur', None) + post_data.pop('db', None) + + # Send the request. + return self.post(meth._endpoint_path, post_data) + setattr(client_api_class, name, meth_) + _closure(name, meth) + return client_api_class + return decorator + + class BytesRequest(Request): """Request with proper escaping of arbitrary byte sequences.""" encoding = 'utf-8' @@ -143,3 +188,33 @@ class SWHServerAPIApp(Flask): request_class = BytesRequest + + +def register_db_endpoints(app, backend_class, backend_factory): + """For each endpoint of the given database, calls app.route to call + a function that decodes the request and sends it to the backend object + provided by the factory. + + :param SWHServerAPIApp app: The flask application endpoints will be + registered to + :param Any backend_class: The class of the backend, which will be + analyzed to look for API endpoints. + :param Callable[[], backend_class] backend_factory: A function with no + argument that returns + an instance of + `backend_class`.""" + from flask import request + + # See autogen_client_api to understand this magic + for (name, meth) in backend_class.__dict__.items(): + if hasattr(meth, '_endpoint_path'): + def closure(name, meth): + @app.route('/'+meth._endpoint_path, methods=['POST']) + @functools.wraps(meth) # Copy signature and doc + def _f(): + # Call the actual code + obj_meth = getattr(backend_factory(), name) + return encode_data_server( + obj_meth(**decode_request(request))) + globals()[name] = _f + closure(name, meth) diff --git a/swh/core/tests/test_api.py b/swh/core/tests/test_api.py new file mode 100644 --- /dev/null +++ b/swh/core/tests/test_api.py @@ -0,0 +1,83 @@ +# Copyright (C) 2018 The Software Heritage developers +# See the AUTHORS file at the top-level directory of this distribution +# License: GNU General Public License version 3, or any later version +# See top-level LICENSE file for more information + +import unittest +from nose.tools import istest + +import requests_mock +from werkzeug.wrappers import BaseResponse +from werkzeug.test import Client as WerkzeugTestClient + +from swh.core.api import ( + register_db_endpoints, error_handler, encode_data_server, + remote_api_endpoint, SWHRemoteAPI, + autogen_client_api, SWHServerAPIApp) + + +class ApiTest(unittest.TestCase): + @istest + def test_server(self): + testcase = self + nb_endpoint_calls = 0 + + class TestStorage: + @remote_api_endpoint('test_endpoint_url') + def test_endpoint(self, test_data, db=None, cur=None): + nonlocal nb_endpoint_calls + nb_endpoint_calls += 1 + + testcase.assertEqual(test_data, 'spam') + return 'egg' + + app = SWHServerAPIApp('testapp') + register_db_endpoints(app, TestStorage, lambda: TestStorage()) + + @app.errorhandler(Exception) + def my_error_handler(exception): + return error_handler(exception, encode_data_server) + + client = WerkzeugTestClient(app, BaseResponse) + res = client.post('/test_endpoint_url', + headers={'Content-Type': 'application/x-msgpack'}, + data=b'\x81\xa9test_data\xa4spam') + + self.assertEqual(nb_endpoint_calls, 1) + self.assertEqual(b''.join(res.response), b'\xa3egg') + + @istest + def test_client(self): + class TestStorage: + @remote_api_endpoint('test_endpoint_url') + def test_endpoint(self, test_data, db=None, cur=None): + pass + + nb_http_calls = 0 + + def callback(request, context): + nonlocal nb_http_calls + nb_http_calls += 1 + self.assertEqual(request.headers['Content-Type'], + 'application/x-msgpack') + self.assertEqual(request.body, b'\x81\xa9test_data\xa4spam') + context.headers['Content-Type'] = 'application/x-msgpack' + context.content = b'\xa3egg' + return b'\xa3egg' + + adapter = requests_mock.Adapter() + adapter.register_uri('POST', + 'mock://example.com/test_endpoint_url', + content=callback) + + @autogen_client_api(TestStorage) + class Testclient(SWHRemoteAPI): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.session.mount('mock', adapter) + + c = Testclient('foo', 'mock://example.com/') + res = c.test_endpoint('spam') + + self.assertEqual(nb_http_calls, 1) + self.assertEqual(res, 'egg')