Spaces:
Running
Running
Create proxy.py
Browse files
proxy.py
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import APIRouter, HTTPException, Depends, Request
|
2 |
+
from sqlalchemy.orm import Session
|
3 |
+
from app.database import SessionLocal
|
4 |
+
from app.models import APIKey
|
5 |
+
import requests
|
6 |
+
|
7 |
+
router = APIRouter()
|
8 |
+
TYPEGPT_API_URL = "https://api.typegpt.net/v1/chat/completions"
|
9 |
+
TYPEGPT_API_KEY = "sk-ciH62IRAfuzg288E12QFVwuND0Og1GCZFc0fiJ2Sk37AElEA"
|
10 |
+
|
11 |
+
def get_db():
|
12 |
+
db = SessionLocal()
|
13 |
+
try:
|
14 |
+
yield db
|
15 |
+
finally:
|
16 |
+
db.close()
|
17 |
+
|
18 |
+
@router.post("/proxy-request")
|
19 |
+
def proxy_request(request: Request, db: Session = Depends(get_db)):
|
20 |
+
user_key = request.headers.get("X-API-KEY")
|
21 |
+
if not user_key:
|
22 |
+
raise HTTPException(status_code=401, detail="API key required")
|
23 |
+
|
24 |
+
api_key = db.query(APIKey).filter(APIKey.key == user_key).first()
|
25 |
+
if not api_key:
|
26 |
+
raise HTTPException(status_code=403, detail="Invalid API key")
|
27 |
+
|
28 |
+
response = requests.post(
|
29 |
+
TYPEGPT_API_URL, headers={"Authorization": f"Bearer {TYPEGPT_API_KEY}"},
|
30 |
+
json=request.json()
|
31 |
+
)
|
32 |
+
return response.json()
|