Update app.py
Browse files
app.py
CHANGED
@@ -61,7 +61,7 @@ def proxy(path):
|
|
61 |
random_key = random.choice(api_keys)
|
62 |
headers['Authorization'] = f"Bearer {random_key}"
|
63 |
|
64 |
-
# Handle streaming
|
65 |
if request.method == 'POST':
|
66 |
response = requests.post(
|
67 |
target_url,
|
@@ -70,31 +70,38 @@ def proxy(path):
|
|
70 |
params=request.args,
|
71 |
stream=True
|
72 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
elif request.method == 'GET':
|
74 |
response = requests.get(
|
75 |
target_url,
|
76 |
headers=headers,
|
77 |
-
params=request.args
|
78 |
-
stream=True
|
79 |
)
|
80 |
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
proxy_response = Response(
|
88 |
-
stream_with_context(generate()),
|
89 |
-
status=response.status_code
|
90 |
-
)
|
91 |
-
|
92 |
-
# Forward response headers
|
93 |
-
for key, value in response.headers.items():
|
94 |
-
if key.lower() not in ('content-length', 'transfer-encoding', 'connection'):
|
95 |
-
proxy_response.headers[key] = value
|
96 |
-
|
97 |
-
return proxy_response
|
98 |
|
99 |
@app.route('/', methods=['GET'])
|
100 |
def index():
|
|
|
61 |
random_key = random.choice(api_keys)
|
62 |
headers['Authorization'] = f"Bearer {random_key}"
|
63 |
|
64 |
+
# Handle POST requests with streaming
|
65 |
if request.method == 'POST':
|
66 |
response = requests.post(
|
67 |
target_url,
|
|
|
70 |
params=request.args,
|
71 |
stream=True
|
72 |
)
|
73 |
+
|
74 |
+
# Create a streaming response for POST
|
75 |
+
def generate():
|
76 |
+
for chunk in response.iter_content(chunk_size=8192):
|
77 |
+
yield chunk
|
78 |
+
|
79 |
+
# Create flask streaming response
|
80 |
+
proxy_response = Response(
|
81 |
+
stream_with_context(generate()),
|
82 |
+
status=response.status_code
|
83 |
+
)
|
84 |
+
# Forward response headers
|
85 |
+
for key, value in response.headers.items():
|
86 |
+
if key.lower() not in ('content-length', 'transfer-encoding', 'connection'):
|
87 |
+
proxy_response.headers[key] = value
|
88 |
+
|
89 |
+
return proxy_response
|
90 |
+
|
91 |
+
# Handle GET requests without streaming
|
92 |
elif request.method == 'GET':
|
93 |
response = requests.get(
|
94 |
target_url,
|
95 |
headers=headers,
|
96 |
+
params=request.args
|
|
|
97 |
)
|
98 |
|
99 |
+
# Create a regular response for GET
|
100 |
+
proxy_response = Response(
|
101 |
+
response.content,
|
102 |
+
status=response.status_code
|
103 |
+
)
|
104 |
+
return proxy_response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
105 |
|
106 |
@app.route('/', methods=['GET'])
|
107 |
def index():
|