Skip to content

Commit

Permalink
Add a proxy endpoint to proxy any requests
Browse files Browse the repository at this point in the history
  • Loading branch information
ragmehos authored and test committed Sep 16, 2024
1 parent d87305b commit a2da346
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
21 changes: 21 additions & 0 deletions mediaflow_proxy/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,3 +343,24 @@ async def get_public_ip():
"""
ip_address_data = await request_with_retry("GET", "https://api.ipify.org?format=json", {})
return ip_address_data.json()


async def proxy_endpoint(request: Request, target_url: str, headers: dict):
# Extract request details
method = request.method
body = await request.body()
headers["content-type"]=request.headers["content-type"]
# Create a httpx client and forward the request
async with httpx.AsyncClient() as client:
response = await client.request(
method,
target_url,
headers=headers,
content=body,
)
# Return the response from the target URL
return Response(
content=response.content,
status_code=response.status_code,
headers=dict(response.headers)
)
18 changes: 17 additions & 1 deletion mediaflow_proxy/routes.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from fastapi import Request, Depends, APIRouter
from pydantic import HttpUrl

from .handlers import handle_hls_stream_proxy, proxy_stream, get_manifest, get_playlist, get_segment, get_public_ip
from .handlers import handle_hls_stream_proxy, proxy_stream, get_manifest, get_playlist, get_segment, get_public_ip, proxy_endpoint
from .utils.http_utils import get_proxy_headers

proxy_router = APIRouter()
Expand Down Expand Up @@ -145,3 +145,19 @@ async def get_mediaflow_proxy_public_ip():
Response: The HTTP response with the public IP address in the form of a JSON object. {"ip": "xxx.xxx.xxx.xxx"}
"""
return await get_public_ip()


@proxy_router.api_route("/endpoint", methods=["GET", "POST", "PUT", "DELETE", "PATCH"])
async def proxy(request: Request, d: HttpUrl, headers: dict = Depends(get_proxy_headers)):
"""
Proxies any URL to the other URL sent by d.
Args:
request (Request): The incoming HTTP request.
d (HttpUrl): The URL of the destination to proxy.
headers (dict): The headers to include in the request.
Returns:
Response: The HTTP response with the streamed content.
"""
return await proxy_endpoint(request, str(d), headers)

0 comments on commit a2da346

Please sign in to comment.