Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async handler support #311

Merged
merged 1 commit into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/todo/todo/handlers/todo_handlers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from enum import Enum
import asyncio

from todo.database import todo_id_sequence, todo_database
from todo.app import rebar
Expand Down
2 changes: 1 addition & 1 deletion flask_rebar/rebar.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def wrapped(*args: P.args, **kwargs: P.kwargs) -> Union[T, Response]:
if headers_schema:
g.validated_headers = get_header_params_or_400(schema=headers_schema)

rv: Any = f(*args, **kwargs)
rv: Any = current_app.ensure_sync(f)(*args, **kwargs)

if not response_body_schema:
return rv
Expand Down
7 changes: 6 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"sphinx_rtd_theme==1.2.2",
"types-jsonschema==4.17.0.10",
"types-setuptools==68.0.0.3",
"flask[async]>=2,<4",
]

install_requires = [
Expand All @@ -42,7 +43,11 @@
packages=find_packages(exclude=("test*", "examples")),
package_data={"flask_rebar": ["py.typed"]},
include_package_data=True,
extras_require={"dev": development, "enum": ["marshmallow-enum~=1.5"]},
extras_require={
"dev": development,
"enum": ["marshmallow-enum~=1.5"],
"async": ["flask[async]>=2,<4"],
},
install_requires=install_requires,
url="https://github.com/plangrid/flask-rebar",
classifiers=[
Expand Down
18 changes: 18 additions & 0 deletions tests/test_rebar.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
:copyright: Copyright 2018 PlanGrid, Inc., see AUTHORS.
:license: MIT, see LICENSE for details.
"""
import asyncio
import json
import unittest

Expand Down Expand Up @@ -864,3 +865,20 @@ def post_foo(*args, **kwargs):
self.assertIsNotNone(swagger) # really only care that it didn't barf
swagger = SwaggerV3Generator().generate(registry)
self.assertIsNotNone(swagger)

def test_async_handler(self):
rebar = Rebar()
registry = rebar.create_handler_registry()

@registry.handles(
rule="/async",
method="GET",
response_body_schema=DefaultResponseSchema(),
)
async def get_response():
await asyncio.sleep(0)
return DEFAULT_RESPONSE

app = create_rebar_app(rebar)
resp = app.test_client().get(path="/async")
self.assertEqual(resp.status_code, 200)
Loading