Spaces:
Paused
Paused
Commit ·
3283add
1
Parent(s): bfb3375
Update repo
Browse files
main.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
from fastapi import FastAPI, Depends, HTTPException, Request, Response
|
| 2 |
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
|
| 3 |
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
-
from httpx import AsyncClient
|
| 5 |
import os
|
| 6 |
|
| 7 |
app = FastAPI()
|
|
@@ -16,12 +16,19 @@ app.add_middleware(
|
|
| 16 |
allow_headers=["*"],
|
| 17 |
)
|
| 18 |
|
|
|
|
| 19 |
# Security scheme for Bearer token
|
| 20 |
security = HTTPBearer()
|
| 21 |
|
| 22 |
# Replace with the URL of your target API endpoint
|
| 23 |
TARGET_API_URL = os.environ.get("TARGET_API_URL", "https://httpbin.org/anything")
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
async def verify_token(credentials: HTTPAuthorizationCredentials = Depends(security)):
|
| 27 |
"""
|
|
@@ -33,17 +40,15 @@ async def verify_token(credentials: HTTPAuthorizationCredentials = Depends(secur
|
|
| 33 |
raise HTTPException(status_code=401, detail="Invalid token")
|
| 34 |
|
| 35 |
|
| 36 |
-
async def proxy_api(request: Request
|
| 37 |
"""
|
| 38 |
-
Proxies the request to the target API endpoint
|
| 39 |
changes the original Authorization Bearer header to x-api-key,
|
| 40 |
and adds a new Authorization Bearer header to the outgoing request.
|
| 41 |
"""
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
async with AsyncClient() as client:
|
| 47 |
target_url = TARGET_API_URL + request.url.path # Construct target URL
|
| 48 |
query_params = request.query_params
|
| 49 |
request_body = await request.body()
|
|
@@ -72,7 +77,9 @@ async def proxy_api(request: Request, token_valid: bool = Depends(verify_token))
|
|
| 72 |
pass # Or you could raise an HTTPException if you want to reject such requests
|
| 73 |
|
| 74 |
# Add the new Authorization Bearer header for the target API request
|
| 75 |
-
|
|
|
|
|
|
|
| 76 |
|
| 77 |
try:
|
| 78 |
response = await client.request(
|
|
@@ -94,11 +101,23 @@ async def proxy_api(request: Request, token_valid: bool = Depends(verify_token))
|
|
| 94 |
)
|
| 95 |
|
| 96 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
# Define a catch-all path operation to proxy all requests
|
| 98 |
@app.api_route(
|
| 99 |
"/{path:path}", methods=["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS", "HEAD"]
|
| 100 |
)
|
| 101 |
-
async def proxy_endpoint(
|
|
|
|
|
|
|
| 102 |
return await proxy_api(request) # Call proxy_api and return its Response
|
| 103 |
|
| 104 |
|
|
|
|
| 1 |
from fastapi import FastAPI, Depends, HTTPException, Request, Response
|
| 2 |
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
|
| 3 |
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
+
from httpx import AsyncClient, Timeout
|
| 5 |
import os
|
| 6 |
|
| 7 |
app = FastAPI()
|
|
|
|
| 16 |
allow_headers=["*"],
|
| 17 |
)
|
| 18 |
|
| 19 |
+
|
| 20 |
# Security scheme for Bearer token
|
| 21 |
security = HTTPBearer()
|
| 22 |
|
| 23 |
# Replace with the URL of your target API endpoint
|
| 24 |
TARGET_API_URL = os.environ.get("TARGET_API_URL", "https://httpbin.org/anything")
|
| 25 |
|
| 26 |
+
# Configure request timeout from environment variable or default
|
| 27 |
+
REQUEST_TIMEOUT_SECONDS = float(
|
| 28 |
+
os.getenv("REQUEST_TIMEOUT_SECONDS", 60.0)
|
| 29 |
+
) # Default to 60 seconds
|
| 30 |
+
request_timeout = Timeout(REQUEST_TIMEOUT_SECONDS)
|
| 31 |
+
|
| 32 |
|
| 33 |
async def verify_token(credentials: HTTPAuthorizationCredentials = Depends(security)):
|
| 34 |
"""
|
|
|
|
| 40 |
raise HTTPException(status_code=401, detail="Invalid token")
|
| 41 |
|
| 42 |
|
| 43 |
+
async def proxy_api(request: Request):
|
| 44 |
"""
|
| 45 |
+
Proxies the request to the target API endpoint,
|
| 46 |
changes the original Authorization Bearer header to x-api-key,
|
| 47 |
and adds a new Authorization Bearer header to the outgoing request.
|
| 48 |
"""
|
| 49 |
+
async with AsyncClient(
|
| 50 |
+
timeout=request_timeout
|
| 51 |
+
) as client: # Set timeout for AsyncClient
|
|
|
|
|
|
|
| 52 |
target_url = TARGET_API_URL + request.url.path # Construct target URL
|
| 53 |
query_params = request.query_params
|
| 54 |
request_body = await request.body()
|
|
|
|
| 77 |
pass # Or you could raise an HTTPException if you want to reject such requests
|
| 78 |
|
| 79 |
# Add the new Authorization Bearer header for the target API request
|
| 80 |
+
target_api_bearer_token = os.getenv("TARGET_API_BEARER_TOKEN")
|
| 81 |
+
if target_api_bearer_token: # check if env var is set before adding header
|
| 82 |
+
headers["Authorization"] = f"Bearer {target_api_bearer_token}"
|
| 83 |
|
| 84 |
try:
|
| 85 |
response = await client.request(
|
|
|
|
| 101 |
)
|
| 102 |
|
| 103 |
|
| 104 |
+
@app.get("/health")
|
| 105 |
+
async def health_check(request: Request):
|
| 106 |
+
return await proxy_api(request)
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
@app.get("/", tags=["Home"])
|
| 110 |
+
async def api_home(request: Request):
|
| 111 |
+
return await proxy_api(request)
|
| 112 |
+
|
| 113 |
+
|
| 114 |
# Define a catch-all path operation to proxy all requests
|
| 115 |
@app.api_route(
|
| 116 |
"/{path:path}", methods=["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS", "HEAD"]
|
| 117 |
)
|
| 118 |
+
async def proxy_endpoint(
|
| 119 |
+
path: str, request: Request, token_valid: bool = Depends(verify_token)
|
| 120 |
+
):
|
| 121 |
return await proxy_api(request) # Call proxy_api and return its Response
|
| 122 |
|
| 123 |
|