Commit 38a41f43 authored by Felix Delattre's avatar Felix Delattre
Browse files

Switched to asynchronous quart framework

parent 6fe7cee6
Pipeline #25885 passed with stage
in 1 minute and 17 seconds
...@@ -30,6 +30,6 @@ tests: ...@@ -30,6 +30,6 @@ tests:
stage: test stage: test
interruptible: true interruptible: true
script: script:
- export FLASK_APP=obmdataapi - export QUART_APP=obmdataapi
- nohup flask run > log.txt 2>&1 & - nohup quart run > log.txt 2>&1 &
- pytest tests - pytest tests
...@@ -16,7 +16,7 @@ Requirements ...@@ -16,7 +16,7 @@ Requirements
------------ ------------
* python >= 3.7 * python >= 3.7
* Flask >= 2.0.1 * quart >= 0.15.1
Installation Installation
------------ ------------
......
...@@ -19,45 +19,26 @@ ...@@ -19,45 +19,26 @@
import logging import logging
import sys import sys
from flask import Flask, request, Response from quart import Quart
from jsonrpcserver import method, dispatch from .jsonrpcquart import JSONRPCQuart
logger = logging.getLogger() logger = logging.getLogger()
logger.setLevel(logging.DEBUG) logger.setLevel(logging.DEBUG)
logger.addHandler(logging.StreamHandler(sys.stdout)) logger.addHandler(logging.StreamHandler(sys.stdout))
logger.info("obmapidata has started")
def create_app(test_config=None): app = Quart(__name__)
"""Create and configure an instance of the Flask application.""" app.api = JSONRPCQuart()
logger.info("obmapidata has started")
app = Flask(__name__, instance_relative_config=True) app.route(
app.config.from_mapping( "/",
# a default secret that should be overridden by instance config methods=[
SECRET_KEY="dev" "POST",
) ],
)(app.api.handler)
if test_config is None:
# load the instance config, if it exists, when not testing
app.config.from_pyfile("config.py", silent=True)
else:
# load the test config if passed in
app.config.update(test_config)
@method @app.api.add_function
def ping(): async def ping():
return "pong" return "pong"
@app.route("/", methods=["POST"])
def index():
req = request.get_data().decode()
response = dispatch(req)
return Response(str(response), response.http_status, mimetype="application/json")
return app
if __name__ == "__main__":
create_app().run()
#!/usr/bin/env python3
# Copyright (C) 2021:
# Helmholtz-Zentrum Potsdam Deutsches GeoForschungsZentrum GFZ
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
# General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see http://www.gnu.org/licenses/.
import json
from quart import Response, request
from ajsonrpc.backend.common import CommonBackend
class JSONRPCQuart(CommonBackend):
@property
def handler(self):
"""Get Quart Handler"""
async def handle():
request_body = await request.body
response = await self.manager.get_response_for_payload(request_body)
return Response(json.dumps(response.body), mimetype="application/json")
return handle
#!/usr/bin/env python3 #!/usr/bin/env python3
# Copyright (c) 2020-2021 # Copyright (C) 2021
# #
# * Helmholtz-Zentrum Potsdam Deutsches GeoForschungsZentrum GFZ # * Helmholtz-Zentrum Potsdam Deutsches GeoForschungsZentrum GFZ
# #
...@@ -20,9 +20,9 @@ ...@@ -20,9 +20,9 @@
from setuptools import setup, find_packages from setuptools import setup, find_packages
tests_require = [ tests_require = [
"Flask>=2.0.1",
"jsonrpcclient[requests]", "jsonrpcclient[requests]",
"pytest", "pytest",
"quart",
] ]
linters_require = ["pre-commit", "pylint"] linters_require = ["pre-commit", "pylint"]
...@@ -36,7 +36,7 @@ setup( ...@@ -36,7 +36,7 @@ setup(
license="AGPLv3+", license="AGPLv3+",
keywords="API", keywords="API",
author="Helmholtz-Zentrum Potsdam Deutsches GeoForschungsZentrum GFZ", author="Helmholtz-Zentrum Potsdam Deutsches GeoForschungsZentrum GFZ",
install_requires=["Flask>=2.0.1", "jsonrpcserver"], install_requires=["ajsonrpc", "quart"],
tests_require=tests_require, tests_require=tests_require,
extras_require={ extras_require={
"tests": tests_require, "tests": tests_require,
......
...@@ -17,16 +17,12 @@ ...@@ -17,16 +17,12 @@
# along with this program. If not, see http://www.gnu.org/licenses/. # along with this program. If not, see http://www.gnu.org/licenses/.
import pytest import pytest
import obmdataapi
from obmdataapi import create_app
@pytest.fixture @pytest.fixture
def app(): def app():
# create the app with common test config yield obmdataapi.app
app = create_app({"TESTING": True})
yield app
@pytest.fixture @pytest.fixture
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment