Skip to content
This repository has been archived by the owner on Jul 3, 2023. It is now read-only.

Commit

Permalink
Makes the fastapi async example more robust
Browse files Browse the repository at this point in the history
When you use `/docs` to create the request, it does
not pass in an empty dict, so making sure the path
does not break.
  • Loading branch information
skrawcz committed Oct 17, 2022
1 parent 5a9de3f commit 755d41d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
10 changes: 9 additions & 1 deletion examples/async/async_module.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
7 changes: 7 additions & 0 deletions examples/async/fastapi_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 755d41d

Please sign in to comment.