rr1 commited on
Commit
301d461
·
verified ·
1 Parent(s): b9059b6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -20
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 response
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
- # Create a response with the same status code, headers, and streaming content
82
- def generate():
83
- for chunk in response.iter_content(chunk_size=8192):
84
- yield chunk
85
-
86
- # Create flask response
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():