-
Notifications
You must be signed in to change notification settings - Fork 9
Description
I would like to request a new feature for fastapi_proxy_lib, which is a library that allows fastapi users to easily create proxy endpoints. The feature is a ProxyResponse class that can be used as a return value for fastapi routes. The ProxyResponse class would take care of sending a request to a target URL, receiving the response, and forwarding it to the client. The ProxyResponse class would also allow the user to customize the request and response headers, as well as the response content, by passing optional arguments or a callback function.
Example
Here is an example of how the ProxyResponse class could be used in a fastapi app:
import fastapi
from fastapi_proxy_lib.fastapi import ProxyResponse
app=fastapi.FastAPI()
def my_respfun(headers,status_code,content):
# do something with headers and status_code, such as parsing, modifying, filtering, etc.
yield {
'headers':headers,
'status_code':status_code
}
yield from content
@app.get('/foo')
def foo():
return ProxyResponse(
url="http://www.example.com/foo/",
method='GET',
reqheaders={"User-Agent": "My Custom Agent"},
respheaders={"Content-Type": "text/plain"},
respfun=my_respfun
)
In this example, the /foo
endpoint would proxy the request to http://www.example.com/foo/
, using the GET
method and the custom User-Agent
header. The response would be forwarded to the client, with the Content-Type
header set to text/plain
. The response content would also be processed by the my_respfun
function, which could modify the headers, status code, or content as needed.
The ProxyResponse class would have the following constructor parameters:
url
: The target URL to proxy the request to. Required.method
: The HTTP method to use for the proxy request. Optional. Default: The same method as the original request.params
: The query parameters to use for the proxy request. Optional. Default: The same parameters as the original request.data
: The request body to use for the proxy request. Optional. Default: The same body as the original request.json
: The JSON data to use for the proxy request. Optional. Default: None. If provided, it will override thedata
parameter and set theContent-Type
header toapplication/json
.files
: The files to use for the proxy request. Optional. Default: None. If provided, it will override thedata
parameter and set theContent-Type
header tomultipart/form-data
.reqheaders
: A dictionary of custom headers to use for the proxy request. Optional. Default: The same headers as the original request, except for theHost
header, which will be set to the target URL's host.respheaders
: A dictionary of custom headers to use for the proxy response. Optional. Default: The same headers as the target response, except for theTransfer-Encoding
header, which will be removed if the response is not streaming.respfun
: A callback function to process the response headers, status code, and content. Optional. Default: None. If provided, it should take three arguments:headers
,status_code
, andcontent
, and return a generator that yields a dictionary with the keysheaders
andstatus_code
, followed by the modified content. Theheaders
andstatus_code
values will be used for the proxy response, and the content will be streamed to the client.