ka1kuk commited on
Commit
ac6ba74
1 Parent(s): 932907b

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +12 -13
main.py CHANGED
@@ -1,11 +1,10 @@
1
- from fastapi import FastAPI, HTTPException, Request
2
  from fastapi.middleware.cors import CORSMiddleware
3
- from fastapi.responses import Response
4
- import requests
5
 
6
  app = FastAPI()
7
 
8
- # Add CORS middleware to allow all origins. Adjust this in production!
9
  app.add_middleware(
10
  CORSMiddleware,
11
  allow_origins=["*"],
@@ -15,17 +14,17 @@ app.add_middleware(
15
  )
16
 
17
  @app.get("/")
18
- @app.post("/")
19
- async def proxy(request: Request, url: str):
20
  headers = {
21
- "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
22
  }
23
- if request.method == "GET":
24
- response = requests.get(url, headers=headers)
25
- elif request.method == "POST":
26
- data = await request.json() if request.headers.get("Content-Type") == "application/json" else None
27
- response = requests.post(url, json=data, headers=headers)
28
- return Response(content=response.content, status_code=response.status_code, headers=dict(response.headers))
 
29
 
30
  if __name__ == "__main__":
31
  import uvicorn
 
1
+ from fastapi import FastAPI, HTTPException, Query
2
  from fastapi.middleware.cors import CORSMiddleware
3
+ import httpx
 
4
 
5
  app = FastAPI()
6
 
7
+ # Add CORS middleware
8
  app.add_middleware(
9
  CORSMiddleware,
10
  allow_origins=["*"],
 
14
  )
15
 
16
  @app.get("/")
17
+ async def proxy(url: str = Query(..., description="URL to proxy")):
 
18
  headers = {
19
+ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
20
  }
21
+
22
+ async with httpx.AsyncClient() as client:
23
+ try:
24
+ response = await client.get(url, headers=headers)
25
+ return response.content
26
+ except httpx.RequestError as e:
27
+ raise HTTPException(status_code=500, detail="Unexpected error")
28
 
29
  if __name__ == "__main__":
30
  import uvicorn