forked from WSH032/fastapi-proxy-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodify-response.py
47 lines (34 loc) · 1.39 KB
/
modify-response.py
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
from collections.abc import AsyncIterator
from contextlib import asynccontextmanager
from fastapi import FastAPI
from fastapi_proxy_lib.core.http import ReverseHttpProxy
from starlette.requests import Request
from starlette.responses import AsyncContentStream, StreamingResponse
proxy = ReverseHttpProxy(base_url="http://www.example.com/")
@asynccontextmanager
async def close_proxy_event(_: FastAPI) -> AsyncIterator[None]:
"""Close proxy."""
yield
await proxy.aclose()
app = FastAPI(lifespan=close_proxy_event)
async def new_content(origin_content: AsyncContentStream) -> AsyncContentStream:
"""Fake content processing."""
async for chunk in origin_content:
# do some processing with chunk, e.g transcoding,
# here we just print and return it as an example.
print(chunk)
yield chunk
@app.get("/{path:path}")
async def _(request: Request, path: str = ""):
proxy_response = await proxy.proxy(request=request, path=path)
if isinstance(proxy_response, StreamingResponse):
# get the origin content stream
old_content = proxy_response.body_iterator
new_resp = StreamingResponse(
content=new_content(old_content),
status_code=proxy_response.status_code,
headers=proxy_response.headers,
media_type=proxy_response.media_type,
)
return new_resp
return proxy_response