= FastAPI() app.include_router( proxy.router, prefix='/redirect', tags=['redirect']) router = APIRouter() class Data(): data: Any = None async def _get_redirect(session, alias, url): async with session.get(url) as r: response_json = await r.json() return {alias: {'response': response_json}} @router.get('/{redirect_path:path}') async def get_redirect(redirect_path): async with aiohttp.ClientSession() as session: tasks = [asyncio.ensure_future( _get_redirect( session, alias, helpers.redirect_builder( alias,url,redirect_path,redirect_map)) ) for alias, url in urls.items()] return await asyncio.gather(*tasks) async def _post_redirect( session, data, alias, url): async with session.post(url, data) as r: response_json = await r.json() return {alias: {'response': response_json}} @router.post('/{redirect_path:path}') async def post_redirect(redirect_path, data): data.data['job_id'] = get_job_id() async with aiohttp.ClientSession() as session: tasks = [asyncio.ensure_future( _post_redirect( session, data.data, alias, helpers.redirect_builder( alias,url,redirect_path,redirect_map)) ) for alias, url in urls.items()] return await asyncio.gather(*tasks) main.py proxy.py proxy.py 転送先は環境変数で 定義してdictに変換。 aiohttpでasyncリクエスト。