nya-proxy 0.2.5__tar.gz → 0.3.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {nya_proxy-0.2.5 → nya_proxy-0.3.1}/PKG-INFO +57 -25
- {nya_proxy-0.2.5 → nya_proxy-0.3.1}/README.md +56 -24
- {nya_proxy-0.2.5 → nya_proxy-0.3.1}/nya/__init__.py +2 -10
- nya_proxy-0.3.1/nya/_version.py +1 -0
- {nya_proxy-0.2.5 → nya_proxy-0.3.1}/nya/common/exceptions.py +103 -9
- {nya_proxy-0.2.5 → nya_proxy-0.3.1}/nya/common/models.py +21 -32
- {nya_proxy-0.2.5 → nya_proxy-0.3.1}/nya/config/manager.py +89 -315
- {nya_proxy-0.2.5 → nya_proxy-0.3.1}/nya/config.yaml +4 -3
- {nya_proxy-0.2.5 → nya_proxy-0.3.1}/nya/core/handler.py +97 -94
- nya_proxy-0.3.1/nya/core/proxy.py +172 -0
- nya_proxy-0.3.1/nya/core/queue.py +289 -0
- nya_proxy-0.3.1/nya/core/request.py +221 -0
- nya_proxy-0.3.1/nya/core/streaming.py +124 -0
- {nya_proxy-0.2.5 → nya_proxy-0.3.1}/nya/dashboard/api.py +5 -37
- {nya_proxy-0.2.5 → nya_proxy-0.3.1}/nya/schema.json +5 -10
- {nya_proxy-0.2.5 → nya_proxy-0.3.1}/nya/server/app.py +16 -15
- nya_proxy-0.3.1/nya/services/control.py +136 -0
- nya_proxy-0.3.1/nya/services/lb.py +158 -0
- nya_proxy-0.3.1/nya/services/limit.py +125 -0
- {nya_proxy-0.2.5 → nya_proxy-0.3.1}/nya/services/metrics.py +8 -19
- {nya_proxy-0.2.5 → nya_proxy-0.3.1}/nya/utils/header.py +9 -7
- {nya_proxy-0.2.5 → nya_proxy-0.3.1}/nya/utils/helper.py +6 -3
- {nya_proxy-0.2.5 → nya_proxy-0.3.1}/nya_proxy.egg-info/PKG-INFO +57 -25
- {nya_proxy-0.2.5 → nya_proxy-0.3.1}/nya_proxy.egg-info/SOURCES.txt +4 -8
- {nya_proxy-0.2.5 → nya_proxy-0.3.1}/pyproject.toml +1 -1
- nya_proxy-0.2.5/nya/_version.py +0 -1
- nya_proxy-0.2.5/nya/core/factory.py +0 -231
- nya_proxy-0.2.5/nya/core/proxy.py +0 -330
- nya_proxy-0.2.5/nya/core/request.py +0 -543
- nya_proxy-0.2.5/nya/core/response.py +0 -187
- nya_proxy-0.2.5/nya/core/streaming.py +0 -123
- nya_proxy-0.2.5/nya/services/key.py +0 -279
- nya_proxy-0.2.5/nya/services/lb.py +0 -218
- nya_proxy-0.2.5/nya/services/limit.py +0 -213
- nya_proxy-0.2.5/nya/services/queue.py +0 -453
- {nya_proxy-0.2.5 → nya_proxy-0.3.1}/LICENSE +0 -0
- {nya_proxy-0.2.5 → nya_proxy-0.3.1}/nya/common/__init__.py +0 -0
- {nya_proxy-0.2.5 → nya_proxy-0.3.1}/nya/common/constants.py +0 -0
- {nya_proxy-0.2.5 → nya_proxy-0.3.1}/nya/config/__init__.py +0 -0
- {nya_proxy-0.2.5 → nya_proxy-0.3.1}/nya/core/__init__.py +0 -0
- {nya_proxy-0.2.5 → nya_proxy-0.3.1}/nya/dashboard/__init__.py +0 -0
- {nya_proxy-0.2.5 → nya_proxy-0.3.1}/nya/html/favicon.ico +0 -0
- {nya_proxy-0.2.5 → nya_proxy-0.3.1}/nya/html/index.html +0 -0
- {nya_proxy-0.2.5 → nya_proxy-0.3.1}/nya/html/login.html +0 -0
- {nya_proxy-0.2.5 → nya_proxy-0.3.1}/nya/html/static/css/styles.css +0 -0
- {nya_proxy-0.2.5 → nya_proxy-0.3.1}/nya/html/static/js/dashboard.js +0 -0
- {nya_proxy-0.2.5 → nya_proxy-0.3.1}/nya/html/static/logo.svg +0 -0
- {nya_proxy-0.2.5 → nya_proxy-0.3.1}/nya/server/__init__.py +0 -0
- {nya_proxy-0.2.5 → nya_proxy-0.3.1}/nya/server/auth.py +0 -0
- {nya_proxy-0.2.5 → nya_proxy-0.3.1}/nya/services/__init__.py +0 -0
- {nya_proxy-0.2.5 → nya_proxy-0.3.1}/nya/utils/__init__.py +0 -0
- {nya_proxy-0.2.5 → nya_proxy-0.3.1}/nya_proxy.egg-info/dependency_links.txt +0 -0
- {nya_proxy-0.2.5 → nya_proxy-0.3.1}/nya_proxy.egg-info/entry_points.txt +0 -0
- {nya_proxy-0.2.5 → nya_proxy-0.3.1}/nya_proxy.egg-info/requires.txt +0 -0
- {nya_proxy-0.2.5 → nya_proxy-0.3.1}/nya_proxy.egg-info/top_level.txt +0 -0
- {nya_proxy-0.2.5 → nya_proxy-0.3.1}/setup.cfg +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: nya-proxy
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.3.1
|
|
4
4
|
Summary: A lightweight, flexible API proxy with dynamic token rotation, load balancing, and rate limiting capabilities.
|
|
5
5
|
Author-email: "Nya Foundation Team (k3scat)" <k3scat@gmail.com>
|
|
6
6
|
License-Expression: MIT
|
|
@@ -185,14 +185,14 @@ docker run -d \
|
|
|
185
185
|
Configuration reference can be found under [Configs folder](configs/) folder
|
|
186
186
|
|
|
187
187
|
```yaml
|
|
188
|
-
# Basic config.yaml example for
|
|
188
|
+
# Basic config.yaml example for OpenAI API Compatible Endpoints
|
|
189
189
|
server:
|
|
190
190
|
host: 0.0.0.0
|
|
191
191
|
port: 8080
|
|
192
192
|
api_key:
|
|
193
193
|
logging:
|
|
194
194
|
enabled: true
|
|
195
|
-
level:
|
|
195
|
+
level: debug
|
|
196
196
|
log_file: app.log
|
|
197
197
|
proxy:
|
|
198
198
|
enabled: false
|
|
@@ -200,7 +200,6 @@ server:
|
|
|
200
200
|
dashboard:
|
|
201
201
|
enabled: true
|
|
202
202
|
queue:
|
|
203
|
-
enabled: true
|
|
204
203
|
max_size: 200
|
|
205
204
|
expiry_seconds: 300
|
|
206
205
|
cors:
|
|
@@ -214,8 +213,9 @@ default_settings:
|
|
|
214
213
|
key_variable: keys
|
|
215
214
|
load_balancing_strategy: round_robin
|
|
216
215
|
rate_limit:
|
|
217
|
-
|
|
218
|
-
|
|
216
|
+
enabled: true
|
|
217
|
+
endpoint_rate_limit: 10/s # Default endpoint rate limit, can be overridden by each API
|
|
218
|
+
key_rate_limit: 10/m # Default key rate limit, can be overridden by each API
|
|
219
219
|
rate_limit_paths:
|
|
220
220
|
- "*"
|
|
221
221
|
retry:
|
|
@@ -227,11 +227,36 @@ default_settings:
|
|
|
227
227
|
retry_status_codes: [ 429, 500, 502, 503, 504 ]
|
|
228
228
|
timeouts:
|
|
229
229
|
request_timeout_seconds: 300
|
|
230
|
+
|
|
231
|
+
# Request body substitution settings
|
|
232
|
+
request_body_substitution:
|
|
233
|
+
enabled: false
|
|
234
|
+
# Substitution rules for request body with JMEPath
|
|
235
|
+
rules:
|
|
236
|
+
- name: "Remove frequency_penalty"
|
|
237
|
+
operation: remove
|
|
238
|
+
path: "frequency_penalty"
|
|
239
|
+
conditions:
|
|
240
|
+
- field: "frequency_penalty"
|
|
241
|
+
operator: "exists"
|
|
242
|
+
- name: "Remove presence_penalty"
|
|
243
|
+
operation: remove
|
|
244
|
+
path: "presence_penalty"
|
|
245
|
+
conditions:
|
|
246
|
+
- field: "presence_penalty"
|
|
247
|
+
operator: "exists"
|
|
230
248
|
|
|
231
|
-
# API configurations, each API can have its own settings, but will inherit from default_settings if not specified
|
|
232
249
|
apis:
|
|
233
|
-
gemini:
|
|
250
|
+
gemini:
|
|
251
|
+
# Any OpenAI-Compatible API
|
|
234
252
|
name: Google Gemini API
|
|
253
|
+
# Gemini: https://generativelanguage.googleapis.com/v1beta/openai
|
|
254
|
+
# OpenAI: https://api.openai.com/v1
|
|
255
|
+
# Anthropic: https://api.anthropic.com/v1
|
|
256
|
+
# DeepSeek: https://api.deepseek.com/v1
|
|
257
|
+
# Mistral: https://api.mistral.ai/v1
|
|
258
|
+
# OpenRouter: https://api.openrouter.ai/v1
|
|
259
|
+
# Ollama: http://localhost:11434/v1
|
|
235
260
|
endpoint: https://generativelanguage.googleapis.com/v1beta/openai
|
|
236
261
|
aliases:
|
|
237
262
|
- /gemini
|
|
@@ -245,6 +270,7 @@ apis:
|
|
|
245
270
|
- your_gemini_key_3
|
|
246
271
|
load_balancing_strategy: least_requests
|
|
247
272
|
rate_limit:
|
|
273
|
+
enabled: true
|
|
248
274
|
# For Gemini, the rate limits (gemini-2.5-pro-exp-03-25) for each key are 5 RPM and 25 RPD
|
|
249
275
|
# Ideally, the endpoint rate limit should be n x Per-Key-RPD, where n is the number of keys
|
|
250
276
|
endpoint_rate_limit: 75/d
|
|
@@ -254,23 +280,29 @@ apis:
|
|
|
254
280
|
- "/chat/*"
|
|
255
281
|
- "/images/*"
|
|
256
282
|
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
283
|
+
test:
|
|
284
|
+
name: Test API
|
|
285
|
+
endpoint: http://127.0.0.1:8082
|
|
286
|
+
key_variable: keys
|
|
287
|
+
headers:
|
|
288
|
+
Authorization: 'Bearer ${{keys}}'
|
|
289
|
+
User-Agent: ${{agents}}
|
|
290
|
+
variables:
|
|
291
|
+
keys:
|
|
292
|
+
- your_test_key_1
|
|
293
|
+
- your_test_key_2
|
|
294
|
+
- your_test_key_3
|
|
295
|
+
agents:
|
|
296
|
+
- test_agent_1
|
|
297
|
+
- test_agent_2
|
|
298
|
+
- test_agent_3
|
|
299
|
+
load_balancing_strategy: least_requests
|
|
300
|
+
rate_limit:
|
|
301
|
+
enabled: true
|
|
302
|
+
endpoint_rate_limit: 20/m
|
|
303
|
+
key_rate_limit: 1/10s
|
|
304
|
+
rate_limit_paths:
|
|
305
|
+
- "/v1/*"
|
|
274
306
|
|
|
275
307
|
# feel free to add more APIs here, just follow the same structure as above
|
|
276
308
|
```
|
|
@@ -143,14 +143,14 @@ docker run -d \
|
|
|
143
143
|
Configuration reference can be found under [Configs folder](configs/) folder
|
|
144
144
|
|
|
145
145
|
```yaml
|
|
146
|
-
# Basic config.yaml example for
|
|
146
|
+
# Basic config.yaml example for OpenAI API Compatible Endpoints
|
|
147
147
|
server:
|
|
148
148
|
host: 0.0.0.0
|
|
149
149
|
port: 8080
|
|
150
150
|
api_key:
|
|
151
151
|
logging:
|
|
152
152
|
enabled: true
|
|
153
|
-
level:
|
|
153
|
+
level: debug
|
|
154
154
|
log_file: app.log
|
|
155
155
|
proxy:
|
|
156
156
|
enabled: false
|
|
@@ -158,7 +158,6 @@ server:
|
|
|
158
158
|
dashboard:
|
|
159
159
|
enabled: true
|
|
160
160
|
queue:
|
|
161
|
-
enabled: true
|
|
162
161
|
max_size: 200
|
|
163
162
|
expiry_seconds: 300
|
|
164
163
|
cors:
|
|
@@ -172,8 +171,9 @@ default_settings:
|
|
|
172
171
|
key_variable: keys
|
|
173
172
|
load_balancing_strategy: round_robin
|
|
174
173
|
rate_limit:
|
|
175
|
-
|
|
176
|
-
|
|
174
|
+
enabled: true
|
|
175
|
+
endpoint_rate_limit: 10/s # Default endpoint rate limit, can be overridden by each API
|
|
176
|
+
key_rate_limit: 10/m # Default key rate limit, can be overridden by each API
|
|
177
177
|
rate_limit_paths:
|
|
178
178
|
- "*"
|
|
179
179
|
retry:
|
|
@@ -185,11 +185,36 @@ default_settings:
|
|
|
185
185
|
retry_status_codes: [ 429, 500, 502, 503, 504 ]
|
|
186
186
|
timeouts:
|
|
187
187
|
request_timeout_seconds: 300
|
|
188
|
+
|
|
189
|
+
# Request body substitution settings
|
|
190
|
+
request_body_substitution:
|
|
191
|
+
enabled: false
|
|
192
|
+
# Substitution rules for request body with JMEPath
|
|
193
|
+
rules:
|
|
194
|
+
- name: "Remove frequency_penalty"
|
|
195
|
+
operation: remove
|
|
196
|
+
path: "frequency_penalty"
|
|
197
|
+
conditions:
|
|
198
|
+
- field: "frequency_penalty"
|
|
199
|
+
operator: "exists"
|
|
200
|
+
- name: "Remove presence_penalty"
|
|
201
|
+
operation: remove
|
|
202
|
+
path: "presence_penalty"
|
|
203
|
+
conditions:
|
|
204
|
+
- field: "presence_penalty"
|
|
205
|
+
operator: "exists"
|
|
188
206
|
|
|
189
|
-
# API configurations, each API can have its own settings, but will inherit from default_settings if not specified
|
|
190
207
|
apis:
|
|
191
|
-
gemini:
|
|
208
|
+
gemini:
|
|
209
|
+
# Any OpenAI-Compatible API
|
|
192
210
|
name: Google Gemini API
|
|
211
|
+
# Gemini: https://generativelanguage.googleapis.com/v1beta/openai
|
|
212
|
+
# OpenAI: https://api.openai.com/v1
|
|
213
|
+
# Anthropic: https://api.anthropic.com/v1
|
|
214
|
+
# DeepSeek: https://api.deepseek.com/v1
|
|
215
|
+
# Mistral: https://api.mistral.ai/v1
|
|
216
|
+
# OpenRouter: https://api.openrouter.ai/v1
|
|
217
|
+
# Ollama: http://localhost:11434/v1
|
|
193
218
|
endpoint: https://generativelanguage.googleapis.com/v1beta/openai
|
|
194
219
|
aliases:
|
|
195
220
|
- /gemini
|
|
@@ -203,6 +228,7 @@ apis:
|
|
|
203
228
|
- your_gemini_key_3
|
|
204
229
|
load_balancing_strategy: least_requests
|
|
205
230
|
rate_limit:
|
|
231
|
+
enabled: true
|
|
206
232
|
# For Gemini, the rate limits (gemini-2.5-pro-exp-03-25) for each key are 5 RPM and 25 RPD
|
|
207
233
|
# Ideally, the endpoint rate limit should be n x Per-Key-RPD, where n is the number of keys
|
|
208
234
|
endpoint_rate_limit: 75/d
|
|
@@ -212,23 +238,29 @@ apis:
|
|
|
212
238
|
- "/chat/*"
|
|
213
239
|
- "/images/*"
|
|
214
240
|
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
241
|
+
test:
|
|
242
|
+
name: Test API
|
|
243
|
+
endpoint: http://127.0.0.1:8082
|
|
244
|
+
key_variable: keys
|
|
245
|
+
headers:
|
|
246
|
+
Authorization: 'Bearer ${{keys}}'
|
|
247
|
+
User-Agent: ${{agents}}
|
|
248
|
+
variables:
|
|
249
|
+
keys:
|
|
250
|
+
- your_test_key_1
|
|
251
|
+
- your_test_key_2
|
|
252
|
+
- your_test_key_3
|
|
253
|
+
agents:
|
|
254
|
+
- test_agent_1
|
|
255
|
+
- test_agent_2
|
|
256
|
+
- test_agent_3
|
|
257
|
+
load_balancing_strategy: least_requests
|
|
258
|
+
rate_limit:
|
|
259
|
+
enabled: true
|
|
260
|
+
endpoint_rate_limit: 20/m
|
|
261
|
+
key_rate_limit: 1/10s
|
|
262
|
+
rate_limit_paths:
|
|
263
|
+
- "/v1/*"
|
|
232
264
|
|
|
233
265
|
# feel free to add more APIs here, just follow the same structure as above
|
|
234
266
|
```
|
|
@@ -5,10 +5,10 @@ NyaProxy - A cute and simple low-level API proxy with dynamic token rotation.
|
|
|
5
5
|
from ._version import __version__
|
|
6
6
|
from .common.exceptions import (
|
|
7
7
|
APIConfigError,
|
|
8
|
-
APIKeyExhaustedError,
|
|
9
8
|
ConfigurationError,
|
|
10
9
|
ConnectionError,
|
|
11
10
|
EndpointRateLimitExceededError,
|
|
11
|
+
NoAvailableAPIKeyError,
|
|
12
12
|
NyaProxyError,
|
|
13
13
|
QueueFullError,
|
|
14
14
|
RequestExpiredError,
|
|
@@ -21,14 +21,10 @@ from .common.models import ProxyRequest
|
|
|
21
21
|
# Import key components for easier access
|
|
22
22
|
from .config.manager import ConfigManager
|
|
23
23
|
from .core.proxy import NyaProxyCore
|
|
24
|
-
from .core.request import RequestExecutor
|
|
25
|
-
from .core.response import ResponseProcessor
|
|
26
24
|
from .dashboard.api import DashboardAPI
|
|
27
|
-
from .services.key import KeyManager
|
|
28
25
|
from .services.lb import LoadBalancer
|
|
29
26
|
from .services.limit import RateLimiter
|
|
30
27
|
from .services.metrics import MetricsCollector
|
|
31
|
-
from .services.queue import RequestQueue
|
|
32
28
|
from .utils.header import HeaderUtils
|
|
33
29
|
from .utils.helper import format_elapsed_time
|
|
34
30
|
|
|
@@ -38,15 +34,11 @@ __all__ = [
|
|
|
38
34
|
"ConfigManager",
|
|
39
35
|
"DashboardAPI",
|
|
40
36
|
"HeaderUtils",
|
|
41
|
-
"KeyManager",
|
|
42
37
|
"LoadBalancer",
|
|
43
38
|
"MetricsCollector",
|
|
44
39
|
"ProxyRequest",
|
|
45
40
|
"NyaProxyCore",
|
|
46
41
|
"RateLimiter",
|
|
47
|
-
"RequestExecutor",
|
|
48
|
-
"RequestQueue",
|
|
49
|
-
"ResponseProcessor",
|
|
50
42
|
# Utilities
|
|
51
43
|
"format_elapsed_time",
|
|
52
44
|
# Exceptions
|
|
@@ -56,7 +48,7 @@ __all__ = [
|
|
|
56
48
|
"EndpointRateLimitExceededError",
|
|
57
49
|
"QueueFullError",
|
|
58
50
|
"RequestExpiredError",
|
|
59
|
-
"
|
|
51
|
+
"NoAvailableAPIKeyError",
|
|
60
52
|
"APIConfigError",
|
|
61
53
|
"UnknownAPIError",
|
|
62
54
|
"ConnectionError",
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.3.1"
|
|
@@ -6,12 +6,32 @@ Improved custom exceptions for NyaProxy.
|
|
|
6
6
|
class NyaProxyError(Exception):
|
|
7
7
|
"""Base exception class for all NyaProxy errors."""
|
|
8
8
|
|
|
9
|
+
def __init__(self, message: str = None):
|
|
10
|
+
"""
|
|
11
|
+
Initialize NyaProxy error.
|
|
12
|
+
|
|
13
|
+
Args:
|
|
14
|
+
message: Error message
|
|
15
|
+
"""
|
|
16
|
+
super().__init__()
|
|
17
|
+
self.message = message or "An error occurred in NyaProxy"
|
|
18
|
+
|
|
9
19
|
pass
|
|
10
20
|
|
|
11
21
|
|
|
12
22
|
class ConfigurationError(NyaProxyError):
|
|
13
23
|
"""Exception raised for configuration errors."""
|
|
14
24
|
|
|
25
|
+
def __init__(self, message: str):
|
|
26
|
+
"""
|
|
27
|
+
Initialize configuration error.
|
|
28
|
+
|
|
29
|
+
Args:
|
|
30
|
+
message: Error message
|
|
31
|
+
"""
|
|
32
|
+
super().__init__(f"NyaProxy configuration error: {message}")
|
|
33
|
+
self.message = message
|
|
34
|
+
|
|
15
35
|
pass
|
|
16
36
|
|
|
17
37
|
|
|
@@ -25,7 +45,7 @@ class VariablesConfigurationError(ConfigurationError):
|
|
|
25
45
|
Args:
|
|
26
46
|
message: Error message
|
|
27
47
|
"""
|
|
28
|
-
super().__init__(f"
|
|
48
|
+
super().__init__(f"NyaProxy variables configuration error: {message}")
|
|
29
49
|
self.message = message
|
|
30
50
|
|
|
31
51
|
|
|
@@ -45,7 +65,9 @@ class EndpointRateLimitExceededError(NyaProxyError):
|
|
|
45
65
|
"""
|
|
46
66
|
self.api_name = api_name
|
|
47
67
|
self.reset_in_seconds = reset_in_seconds
|
|
48
|
-
super().__init__(
|
|
68
|
+
super().__init__(
|
|
69
|
+
message or f"NyaProxy: Rate limit exceeded for {api_name} endpoint"
|
|
70
|
+
)
|
|
49
71
|
|
|
50
72
|
|
|
51
73
|
class QueueFullError(NyaProxyError):
|
|
@@ -61,7 +83,9 @@ class QueueFullError(NyaProxyError):
|
|
|
61
83
|
"""
|
|
62
84
|
self.api_name = api_name
|
|
63
85
|
self.max_size = max_size
|
|
64
|
-
super().__init__(
|
|
86
|
+
super().__init__(
|
|
87
|
+
f"NyaProxy: Request queue for {api_name} is full (capacity: {max_size})"
|
|
88
|
+
)
|
|
65
89
|
|
|
66
90
|
|
|
67
91
|
class RequestExpiredError(NyaProxyError):
|
|
@@ -78,11 +102,11 @@ class RequestExpiredError(NyaProxyError):
|
|
|
78
102
|
self.api_name = api_name
|
|
79
103
|
self.wait_time = wait_time
|
|
80
104
|
super().__init__(
|
|
81
|
-
f"Request for {api_name} expired after waiting {wait_time:.1f}s"
|
|
105
|
+
f"NyaProxy: Request for {api_name} expired after waiting {wait_time:.1f}s in queue"
|
|
82
106
|
)
|
|
83
107
|
|
|
84
108
|
|
|
85
|
-
class
|
|
109
|
+
class NoAvailableAPIKeyError(NyaProxyError):
|
|
86
110
|
"""Exception raised when no API keys are available (all key are rate limited)."""
|
|
87
111
|
|
|
88
112
|
def __init__(self, api_name: str):
|
|
@@ -93,7 +117,37 @@ class APIKeyExhaustedError(NyaProxyError):
|
|
|
93
117
|
api_name: Name of the API
|
|
94
118
|
"""
|
|
95
119
|
self.api_name = api_name
|
|
96
|
-
super().__init__(
|
|
120
|
+
super().__init__(
|
|
121
|
+
f"NyaProxy: No available API keys for {api_name} (all rate limited)"
|
|
122
|
+
)
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
class APIKeyNotConfiguredError(NyaProxyError):
|
|
126
|
+
"""Exception raised when there is no API key found in the configuration."""
|
|
127
|
+
|
|
128
|
+
def __init__(self, api_name: str):
|
|
129
|
+
"""
|
|
130
|
+
Initialize API key not found error.
|
|
131
|
+
|
|
132
|
+
Args:
|
|
133
|
+
api_name: Name of the API
|
|
134
|
+
"""
|
|
135
|
+
self.api_name = api_name
|
|
136
|
+
super().__init__(f"NyaProxy: No API key found for {api_name}")
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
class MissingAPIKeyError(NyaProxyError):
|
|
140
|
+
"""Exception raised when an API key is missing in the configuration."""
|
|
141
|
+
|
|
142
|
+
def __init__(self, api_name: str):
|
|
143
|
+
"""
|
|
144
|
+
Initialize missing API key error.
|
|
145
|
+
|
|
146
|
+
Args:
|
|
147
|
+
api_name: Name of the API
|
|
148
|
+
"""
|
|
149
|
+
self.api_name = api_name
|
|
150
|
+
super().__init__(f"NyaProxy: Missing API key for {api_name}")
|
|
97
151
|
|
|
98
152
|
|
|
99
153
|
class APIConfigError(NyaProxyError):
|
|
@@ -113,7 +167,7 @@ class UnknownAPIError(NyaProxyError):
|
|
|
113
167
|
path: Request path
|
|
114
168
|
"""
|
|
115
169
|
self.path = path
|
|
116
|
-
super().__init__(f"Unknown API endpoint for path: {path}")
|
|
170
|
+
super().__init__(f"NyaProxy: Unknown API endpoint for path: {path}")
|
|
117
171
|
|
|
118
172
|
|
|
119
173
|
class ConnectionError(NyaProxyError):
|
|
@@ -130,7 +184,9 @@ class ConnectionError(NyaProxyError):
|
|
|
130
184
|
"""
|
|
131
185
|
self.api_name = api_name
|
|
132
186
|
self.url = url
|
|
133
|
-
super().__init__(
|
|
187
|
+
super().__init__(
|
|
188
|
+
message or f"NyaProxy: Connection error to {api_name} at {url}"
|
|
189
|
+
)
|
|
134
190
|
|
|
135
191
|
|
|
136
192
|
class TimeoutError(NyaProxyError):
|
|
@@ -146,4 +202,42 @@ class TimeoutError(NyaProxyError):
|
|
|
146
202
|
"""
|
|
147
203
|
self.api_name = api_name
|
|
148
204
|
self.timeout = timeout
|
|
149
|
-
super().__init__(
|
|
205
|
+
super().__init__(
|
|
206
|
+
f"NyaProxy: Request to {api_name} timed out after {timeout:.1f}s"
|
|
207
|
+
)
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
class EncounterRetryStatusCodeError(NyaProxyError):
|
|
211
|
+
"""Exception raised when encountering a retry status code defined in the configuration."""
|
|
212
|
+
|
|
213
|
+
def __init__(self, api_name: str, status_code: int):
|
|
214
|
+
"""
|
|
215
|
+
Initialize retry status code error.
|
|
216
|
+
|
|
217
|
+
Args:
|
|
218
|
+
api_name: Name of the API
|
|
219
|
+
status_code: HTTP status code that requires retry
|
|
220
|
+
"""
|
|
221
|
+
self.api_name = api_name
|
|
222
|
+
self.status_code = status_code
|
|
223
|
+
super().__init__(
|
|
224
|
+
f"NyaProxy: Encountered retry status code {status_code} for {api_name}"
|
|
225
|
+
)
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
class ReachedMaxRetriesError(NyaProxyError):
|
|
229
|
+
"""Exception raised when the maximum number of retries is reached."""
|
|
230
|
+
|
|
231
|
+
def __init__(self, api_name: str, max_retries: int):
|
|
232
|
+
"""
|
|
233
|
+
Initialize reached max retries error.
|
|
234
|
+
|
|
235
|
+
Args:
|
|
236
|
+
api_name: Name of the API
|
|
237
|
+
max_retries: Maximum number of retries allowed
|
|
238
|
+
"""
|
|
239
|
+
self.api_name = api_name
|
|
240
|
+
self.max_retries = max_retries
|
|
241
|
+
super().__init__(
|
|
242
|
+
f"NyaProxy: Reached maximum retries ({max_retries}) for {api_name}"
|
|
243
|
+
)
|
|
@@ -5,28 +5,15 @@ Data models for request handling in NyaProxy.
|
|
|
5
5
|
import asyncio
|
|
6
6
|
import time
|
|
7
7
|
from dataclasses import dataclass, field
|
|
8
|
-
from typing import TYPE_CHECKING, Any, Dict,
|
|
8
|
+
from typing import TYPE_CHECKING, Any, Dict, Optional, Union
|
|
9
|
+
|
|
10
|
+
from httpx import Headers
|
|
9
11
|
|
|
10
12
|
if TYPE_CHECKING:
|
|
11
13
|
from fastapi import Request
|
|
12
14
|
from starlette.datastructures import URL
|
|
13
15
|
|
|
14
16
|
|
|
15
|
-
@dataclass
|
|
16
|
-
class AdvancedConfig:
|
|
17
|
-
"""
|
|
18
|
-
Advanced configuration for NyaProxy Request Handling.
|
|
19
|
-
|
|
20
|
-
This class holds the settings that control how streaming responses
|
|
21
|
-
are handled, including chunk size and whether to use a streaming
|
|
22
|
-
response.
|
|
23
|
-
"""
|
|
24
|
-
|
|
25
|
-
# Request Body Substitution
|
|
26
|
-
req_body_subst_enabled: bool = False
|
|
27
|
-
subst_rules: List[Dict[str, Any]] = field(default_factory=list)
|
|
28
|
-
|
|
29
|
-
|
|
30
17
|
@dataclass
|
|
31
18
|
class ProxyRequest:
|
|
32
19
|
"""
|
|
@@ -39,32 +26,29 @@ class ProxyRequest:
|
|
|
39
26
|
# Required request fields
|
|
40
27
|
method: str
|
|
41
28
|
|
|
42
|
-
#
|
|
29
|
+
priority: int # Lower number = higher priority (1=retry, 2=priority, 3=normal)
|
|
30
|
+
|
|
31
|
+
# original url from the proxy request
|
|
43
32
|
_url: Union["URL", str]
|
|
44
33
|
|
|
45
|
-
# final url to be requested
|
|
34
|
+
# final url to be requested
|
|
46
35
|
url: Optional[Union["URL", str]] = None
|
|
47
36
|
|
|
48
|
-
# Optional request fields
|
|
49
|
-
_raw: Optional["Request"] = None
|
|
50
37
|
headers: Dict[str, Any] = field(default_factory=dict)
|
|
51
38
|
content: Optional[bytes] = None
|
|
52
|
-
timeout: float = 30.0
|
|
53
39
|
|
|
54
40
|
# API Related metadata
|
|
55
41
|
api_name: str = "unknown"
|
|
56
42
|
api_key: Optional[str] = None
|
|
57
43
|
|
|
58
|
-
|
|
59
|
-
_attempts: int = 0
|
|
60
|
-
_added_at: float = field(default_factory=time.time)
|
|
61
|
-
_expiry: float = 0.0
|
|
62
|
-
_future: Optional[asyncio.Future] = None
|
|
44
|
+
future: Optional[asyncio.Future] = None
|
|
63
45
|
|
|
64
|
-
|
|
65
|
-
|
|
46
|
+
added_at: float = field(default_factory=time.time) # Timestamp when added to queue
|
|
47
|
+
is_retry: bool = False
|
|
48
|
+
attempts: int = 0 # Number of attempts made for this request
|
|
66
49
|
|
|
67
|
-
|
|
50
|
+
# Whether to apply rate limiting for this request
|
|
51
|
+
_rate_limited: bool = False
|
|
68
52
|
|
|
69
53
|
@staticmethod
|
|
70
54
|
async def from_request(request: "Request") -> "ProxyRequest":
|
|
@@ -74,9 +58,14 @@ class ProxyRequest:
|
|
|
74
58
|
|
|
75
59
|
return ProxyRequest(
|
|
76
60
|
method=request.method,
|
|
61
|
+
priority=3,
|
|
77
62
|
_url=request.url,
|
|
78
|
-
headers=
|
|
63
|
+
headers=Headers(request.headers),
|
|
79
64
|
content=await request.body(),
|
|
80
|
-
_raw=request,
|
|
81
|
-
_added_at=time.time(),
|
|
82
65
|
)
|
|
66
|
+
|
|
67
|
+
def __lt__(self, other):
|
|
68
|
+
"""Compare for heap ordering (priority first, then timestamp)."""
|
|
69
|
+
if self.priority != other.priority:
|
|
70
|
+
return self.priority < other.priority
|
|
71
|
+
return self.added_at < other.added_at
|