Skip to content

Commit

Permalink
Merge pull request #311 from plangrid/jonls/async-handler
Browse files Browse the repository at this point in the history
Async handler support
  • Loading branch information
jonls authored Apr 8, 2024
2 parents 127f749 + 1d7de3b commit 6d9b178
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
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)

0 comments on commit 6d9b178

Please sign in to comment.