diff --git a/examples/async/async_module.py b/examples/async/async_module.py index 4544d6e5..33432b8c 100644 --- a/examples/async/async_module.py +++ b/examples/async/async_module.py @@ -1,11 +1,19 @@ import asyncio +import json +import logging import aiohttp import fastapi +logger = logging.getLogger(__name__) + async def request_raw(request: fastapi.Request) -> dict: - return await request.json() + try: + return await request.json() + except json.JSONDecodeError as e: + logger.warning(f"Unable to get JSON from request. Error is:\n{e}") + return {} def foo(request_raw: dict) -> str: diff --git a/examples/async/fastapi_example.py b/examples/async/fastapi_example.py index 384007de..44257e80 100644 --- a/examples/async/fastapi_example.py +++ b/examples/async/fastapi_example.py @@ -12,3 +12,10 @@ async def call(request: fastapi.Request) -> dict: dr = h_async.AsyncDriver({}, async_module) input_data = {"request": request} return await dr.raw_execute(["pipeline"], inputs=input_data) + + +if __name__ == "__main__": + # If you run this as a script, then the app will be started on localhost:8000 + import uvicorn + + uvicorn.run(app, host="0.0.0.0", port=8000)