Repository navigation
Expand file tree
/
Copy pathfastAPI.py
More file actions
64 lines (44 loc) · 2.1 KB
/
Copy pathfastAPI.py
File metadata and controls
64 lines (44 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import asyncio
from collections.abc import AsyncIterator
from contextlib import asynccontextmanager
from socket import AF_INET
from typing import Annotated, Any, TypedDict
import aiohttp
from fastapi import FastAPI, Query, Request, Response
from fastapi.logger import logger
from fastAPI_aiohttp.http_client import UPSTREAM_BASE_URL, UPSTREAM_PATH, HTTPClient, fetch_json
from fastAPI_aiohttp.services import Report
SIZE_POOL_AIOHTTP = 100
class State(TypedDict):
http_client: aiohttp.ClientSession
@asynccontextmanager
async def lifespan(_app: FastAPI) -> AsyncIterator[State]:
"""Open one shared ClientSession at startup and close it at shutdown."""
timeout = aiohttp.ClientTimeout(total=2)
connector = aiohttp.TCPConnector(family=AF_INET, limit_per_host=SIZE_POOL_AIOHTTP)
async with aiohttp.ClientSession(UPSTREAM_BASE_URL, timeout=timeout, connector=connector) as client:
logger.info("HTTP client opened")
yield {"http_client": client}
logger.info("HTTP client closed")
app = FastAPI(docs_url="/", lifespan=lifespan)
@app.get("/endpoint")
async def endpoint(client: HTTPClient) -> dict[str, Any]:
return await fetch_json(client, UPSTREAM_PATH)
@app.get("/endpoint_multi")
async def endpoint_multi(client: HTTPClient) -> dict[str, int]:
# both upstream calls run concurrently
first, second = await asyncio.gather(fetch_json(client, UPSTREAM_PATH), fetch_json(client, UPSTREAM_PATH))
return {"success": first["success"] + second["success"]}
@app.get("/endpoint_complex_codebase")
async def endpoint_complex_codebase(report: Report, calls: Annotated[int, Query(ge=1, le=10)] = 3) -> dict[str, int]:
# the endpoint asks for a service, not for the HTTP client (see services.py)
return await report.build_report(calls)
@app.post("/endpoint_stream")
async def endpoint_stream(request: Request) -> Response:
body = bytearray(b"RST")
async for chunk in request.stream():
body += chunk
return Response(bytes(body), media_type="text/plain")
if __name__ == "__main__": # local dev
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)