File size: 1,138 Bytes
926aeef d3e86e7 372d7a8 926aeef 372d7a8 5d52377 d3e86e7 926aeef d3e86e7 372d7a8 d3e86e7 372d7a8 926aeef d3e86e7 926aeef d3e86e7 926aeef d3e86e7 926aeef d3e86e7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
from flask import Flask, request, Response
import os
import requests
app = Flask(__name__)
# Load environment variables
API_KEY = os.getenv("TYPEGPT_API_KEY")
FAKE_KEY = os.getenv("FAKE_KEY", "fake-key") # Default to "fake-key" if not set
BASE_URL = "https://api.typegpt.net"
# Proxy endpoint to TypeGPT's API
@app.route("/<path:endpoint>", methods=["POST"])
def proxy(endpoint):
# Verify the client's fake API key
client_key = request.headers.get("Authorization")
if client_key != f"Bearer {FAKE_KEY}":
return Response("{\"error\": \"Unauthorized\"}", status=401, mimetype="application/json")
# Forward the request to TypeGPT
headers = {
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json"
}
data = request.get_data() # Get raw data from the client request
response = requests.post(f"{BASE_URL}/{endpoint}", headers=headers, data=data)
# Forward the raw response content and headers
return Response(response.content, status=response.status_code, headers=dict(response.headers))
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000)
|