AIMaster7 commited on
Commit
d56017b
·
verified ·
1 Parent(s): b4c4bc0

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +11 -6
main.py CHANGED
@@ -7,24 +7,27 @@ app = FastAPI()
7
  API_KEY = "sk-qO9N6kQEEULMWtF4YGVlTTSjIPllEm1h1wfEBzSmnSbxiXwe"
8
  BASE_URL = "https://fast.typegpt.net"
9
 
10
- # Custom public header
11
  PUBLIC_API_KEY = "TypeGPT-Free4ALL"
12
 
13
  @app.api_route("/{path:path}", methods=["GET", "POST"])
14
  async def proxy(request: Request, path: str, x_api_key: str = Header(None)):
 
15
  if x_api_key != PUBLIC_API_KEY:
16
- raise HTTPException(status_code=401, detail="Invalid API key")
17
 
18
- # Reconstruct full URL to target
19
  target_url = f"{BASE_URL}/{path}"
20
 
21
- # Prepare headers and body
22
  headers = dict(request.headers)
23
- headers["Authorization"] = f"Bearer {API_KEY}"
24
- headers.pop("host", None)
25
 
 
26
  body = await request.body()
27
 
 
28
  async with httpx.AsyncClient() as client:
29
  response = await client.request(
30
  request.method,
@@ -33,4 +36,6 @@ async def proxy(request: Request, path: str, x_api_key: str = Header(None)):
33
  headers=headers
34
  )
35
 
 
36
  return response.json()
 
 
7
  API_KEY = "sk-qO9N6kQEEULMWtF4YGVlTTSjIPllEm1h1wfEBzSmnSbxiXwe"
8
  BASE_URL = "https://fast.typegpt.net"
9
 
10
+ # Public API key for users to use
11
  PUBLIC_API_KEY = "TypeGPT-Free4ALL"
12
 
13
  @app.api_route("/{path:path}", methods=["GET", "POST"])
14
  async def proxy(request: Request, path: str, x_api_key: str = Header(None)):
15
+ # Check if the public API key matches the expected value
16
  if x_api_key != PUBLIC_API_KEY:
17
+ raise HTTPException(status_code=401, detail="Invalid API key. Please use 'TypeGPT-Free4ALL'.")
18
 
19
+ # Reconstruct full URL to target endpoint
20
  target_url = f"{BASE_URL}/{path}"
21
 
22
+ # Prepare headers from the incoming request
23
  headers = dict(request.headers)
24
+ headers["Authorization"] = f"Bearer {API_KEY}" # Add your secret API key here
25
+ headers.pop("host", None) # Remove host header to avoid conflicts
26
 
27
+ # Get the body of the request if present
28
  body = await request.body()
29
 
30
+ # Make the actual request to the backend API
31
  async with httpx.AsyncClient() as client:
32
  response = await client.request(
33
  request.method,
 
36
  headers=headers
37
  )
38
 
39
+ # Return the response back to the user
40
  return response.json()
41
+