AEUPH commited on
Commit
7607e24
·
verified ·
1 Parent(s): d9d875f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -9
app.py CHANGED
@@ -1,4 +1,4 @@
1
- from fastapi import FastAPI, Request
2
  import httpx
3
  from starlette.responses import RedirectResponse
4
 
@@ -14,23 +14,38 @@ async def forward(request: Request):
14
 
15
  # Check if the forward URL is present
16
  if not forward_url:
17
- return {"error": "param1 (forward URL) is required"}
 
 
 
 
18
 
19
  # Prepare the new URL with remaining query parameters
20
- # This assumes that the forward URL does not already have query parameters.
21
  if query_params: # If there are other parameters to forward
22
  params_as_str = "&".join([f"{key}={value}" for key, value in query_params.items()])
23
  forward_url_with_params = f"{forward_url}?{params_as_str}"
24
  else:
25
  forward_url_with_params = forward_url
26
 
27
- # Forward the request to the new URL using httpx for asynchronous HTTP requests
28
- async with httpx.AsyncClient() as client:
29
- response = await client.get(forward_url_with_params)
 
 
 
 
 
 
 
 
 
30
 
31
- # For simplicity, we're directly returning the response from the forwarded URL
32
- # In a real application, you might want to handle different response scenarios
33
- return response.json()
 
 
 
34
 
35
  # To run the server:
36
  # uvicorn app:app --reload
 
1
+ from fastapi import FastAPI, Request, HTTPException
2
  import httpx
3
  from starlette.responses import RedirectResponse
4
 
 
14
 
15
  # Check if the forward URL is present
16
  if not forward_url:
17
+ raise HTTPException(status_code=400, detail="param1 (forward URL) is required")
18
+
19
+ # Validate the forward URL (basic validation)
20
+ if not (forward_url.startswith('http://') or forward_url.startswith('https://')):
21
+ raise HTTPException(status_code=400, detail="Invalid URL. URL must start with http:// or https://")
22
 
23
  # Prepare the new URL with remaining query parameters
 
24
  if query_params: # If there are other parameters to forward
25
  params_as_str = "&".join([f"{key}={value}" for key, value in query_params.items()])
26
  forward_url_with_params = f"{forward_url}?{params_as_str}"
27
  else:
28
  forward_url_with_params = forward_url
29
 
30
+ try:
31
+ # Forward the request to the new URL using httpx for asynchronous HTTP requests
32
+ async with httpx.AsyncClient() as client:
33
+ response = await client.get(forward_url_with_params)
34
+ response.raise_for_status() # Raises an exception for 4XX/5XX responses
35
+
36
+ except httpx.RequestError as e:
37
+ # Handle request errors (e.g., network issues)
38
+ raise HTTPException(status_code=500, detail=f"Request error: {str(e)}")
39
+ except httpx.HTTPStatusError as e:
40
+ # Handle responses with error status codes
41
+ raise HTTPException(status_code=e.response.status_code, detail=f"Error response from forwarded URL: {str(e)}")
42
 
43
+ try:
44
+ # Attempt to return the JSON response
45
+ return response.json()
46
+ except ValueError:
47
+ # If the response is not JSON, return as plain text
48
+ return Response(content=response.text, media_type="text/plain")
49
 
50
  # To run the server:
51
  # uvicorn app:app --reload