nya-proxy 0.3.0__tar.gz → 0.3.2__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.3.0 → nya_proxy-0.3.2}/PKG-INFO +1 -1
- nya_proxy-0.3.2/nya/_version.py +1 -0
- {nya_proxy-0.3.0 → nya_proxy-0.3.2}/nya/common/exceptions.py +66 -15
- nya_proxy-0.3.2/nya/common/models.py +81 -0
- {nya_proxy-0.3.0 → nya_proxy-0.3.2}/nya/config/manager.py +159 -53
- {nya_proxy-0.3.0 → nya_proxy-0.3.2}/nya/config.yaml +6 -5
- {nya_proxy-0.3.0 → nya_proxy-0.3.2}/nya/core/handler.py +16 -2
- {nya_proxy-0.3.0 → nya_proxy-0.3.2}/nya/core/proxy.py +21 -9
- nya_proxy-0.3.2/nya/core/queue.py +348 -0
- {nya_proxy-0.3.0 → nya_proxy-0.3.2}/nya/core/request.py +28 -31
- {nya_proxy-0.3.0 → nya_proxy-0.3.2}/nya/schema.json +30 -15
- {nya_proxy-0.3.0 → nya_proxy-0.3.2}/nya/server/app.py +66 -22
- {nya_proxy-0.3.0 → nya_proxy-0.3.2}/nya/server/auth.py +12 -4
- nya_proxy-0.3.2/nya/services/control.py +265 -0
- {nya_proxy-0.3.0 → nya_proxy-0.3.2}/nya/services/lb.py +4 -4
- {nya_proxy-0.3.0 → nya_proxy-0.3.2}/nya/services/limit.py +60 -29
- {nya_proxy-0.3.0 → nya_proxy-0.3.2}/nya/services/metrics.py +12 -6
- {nya_proxy-0.3.0 → nya_proxy-0.3.2}/nya/utils/header.py +40 -0
- {nya_proxy-0.3.0 → nya_proxy-0.3.2}/nya/utils/helper.py +11 -15
- {nya_proxy-0.3.0 → nya_proxy-0.3.2}/nya_proxy.egg-info/PKG-INFO +1 -1
- {nya_proxy-0.3.0 → nya_proxy-0.3.2}/pyproject.toml +1 -1
- nya_proxy-0.3.0/nya/_version.py +0 -1
- nya_proxy-0.3.0/nya/common/models.py +0 -71
- nya_proxy-0.3.0/nya/core/queue.py +0 -289
- nya_proxy-0.3.0/nya/services/control.py +0 -136
- {nya_proxy-0.3.0 → nya_proxy-0.3.2}/LICENSE +0 -0
- {nya_proxy-0.3.0 → nya_proxy-0.3.2}/README.md +0 -0
- {nya_proxy-0.3.0 → nya_proxy-0.3.2}/nya/__init__.py +0 -0
- {nya_proxy-0.3.0 → nya_proxy-0.3.2}/nya/common/__init__.py +0 -0
- {nya_proxy-0.3.0 → nya_proxy-0.3.2}/nya/common/constants.py +0 -0
- {nya_proxy-0.3.0 → nya_proxy-0.3.2}/nya/config/__init__.py +0 -0
- {nya_proxy-0.3.0 → nya_proxy-0.3.2}/nya/core/__init__.py +0 -0
- {nya_proxy-0.3.0 → nya_proxy-0.3.2}/nya/core/streaming.py +0 -0
- {nya_proxy-0.3.0 → nya_proxy-0.3.2}/nya/dashboard/__init__.py +0 -0
- {nya_proxy-0.3.0 → nya_proxy-0.3.2}/nya/dashboard/api.py +0 -0
- {nya_proxy-0.3.0 → nya_proxy-0.3.2}/nya/html/favicon.ico +0 -0
- {nya_proxy-0.3.0 → nya_proxy-0.3.2}/nya/html/index.html +0 -0
- {nya_proxy-0.3.0 → nya_proxy-0.3.2}/nya/html/login.html +0 -0
- {nya_proxy-0.3.0 → nya_proxy-0.3.2}/nya/html/static/css/styles.css +0 -0
- {nya_proxy-0.3.0 → nya_proxy-0.3.2}/nya/html/static/js/dashboard.js +0 -0
- {nya_proxy-0.3.0 → nya_proxy-0.3.2}/nya/html/static/logo.svg +0 -0
- {nya_proxy-0.3.0 → nya_proxy-0.3.2}/nya/server/__init__.py +0 -0
- {nya_proxy-0.3.0 → nya_proxy-0.3.2}/nya/services/__init__.py +0 -0
- {nya_proxy-0.3.0 → nya_proxy-0.3.2}/nya/utils/__init__.py +0 -0
- {nya_proxy-0.3.0 → nya_proxy-0.3.2}/nya_proxy.egg-info/SOURCES.txt +0 -0
- {nya_proxy-0.3.0 → nya_proxy-0.3.2}/nya_proxy.egg-info/dependency_links.txt +0 -0
- {nya_proxy-0.3.0 → nya_proxy-0.3.2}/nya_proxy.egg-info/entry_points.txt +0 -0
- {nya_proxy-0.3.0 → nya_proxy-0.3.2}/nya_proxy.egg-info/requires.txt +0 -0
- {nya_proxy-0.3.0 → nya_proxy-0.3.2}/nya_proxy.egg-info/top_level.txt +0 -0
- {nya_proxy-0.3.0 → nya_proxy-0.3.2}/setup.cfg +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: nya-proxy
|
|
3
|
-
Version: 0.3.
|
|
3
|
+
Version: 0.3.2
|
|
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
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.3.2"
|
|
@@ -4,7 +4,9 @@ Improved custom exceptions for NyaProxy.
|
|
|
4
4
|
|
|
5
5
|
|
|
6
6
|
class NyaProxyError(Exception):
|
|
7
|
-
"""
|
|
7
|
+
"""
|
|
8
|
+
Base exception class for all NyaProxy errors.
|
|
9
|
+
"""
|
|
8
10
|
|
|
9
11
|
def __init__(self, message: str = None):
|
|
10
12
|
"""
|
|
@@ -20,7 +22,9 @@ class NyaProxyError(Exception):
|
|
|
20
22
|
|
|
21
23
|
|
|
22
24
|
class ConfigurationError(NyaProxyError):
|
|
23
|
-
"""
|
|
25
|
+
"""
|
|
26
|
+
Exception raised for configuration errors.
|
|
27
|
+
"""
|
|
24
28
|
|
|
25
29
|
def __init__(self, message: str):
|
|
26
30
|
"""
|
|
@@ -36,7 +40,9 @@ class ConfigurationError(NyaProxyError):
|
|
|
36
40
|
|
|
37
41
|
|
|
38
42
|
class VariablesConfigurationError(ConfigurationError):
|
|
39
|
-
"""
|
|
43
|
+
"""
|
|
44
|
+
Exception raised for errors in variable configuration.
|
|
45
|
+
"""
|
|
40
46
|
|
|
41
47
|
def __init__(self, message: str):
|
|
42
48
|
"""
|
|
@@ -50,7 +56,9 @@ class VariablesConfigurationError(ConfigurationError):
|
|
|
50
56
|
|
|
51
57
|
|
|
52
58
|
class EndpointRateLimitExceededError(NyaProxyError):
|
|
53
|
-
"""
|
|
59
|
+
"""
|
|
60
|
+
Exception raised when rate limits are exceeded.
|
|
61
|
+
"""
|
|
54
62
|
|
|
55
63
|
def __init__(
|
|
56
64
|
self, api_name: str, message: str = None, reset_in_seconds: float = None
|
|
@@ -71,7 +79,9 @@ class EndpointRateLimitExceededError(NyaProxyError):
|
|
|
71
79
|
|
|
72
80
|
|
|
73
81
|
class QueueFullError(NyaProxyError):
|
|
74
|
-
"""
|
|
82
|
+
"""
|
|
83
|
+
Exception raised when a request queue is full.
|
|
84
|
+
"""
|
|
75
85
|
|
|
76
86
|
def __init__(self, api_name: str, max_size: int):
|
|
77
87
|
"""
|
|
@@ -89,7 +99,9 @@ class QueueFullError(NyaProxyError):
|
|
|
89
99
|
|
|
90
100
|
|
|
91
101
|
class RequestExpiredError(NyaProxyError):
|
|
92
|
-
"""
|
|
102
|
+
"""
|
|
103
|
+
Exception raised when a queued request expires.
|
|
104
|
+
"""
|
|
93
105
|
|
|
94
106
|
def __init__(self, api_name: str, wait_time: float):
|
|
95
107
|
"""
|
|
@@ -107,7 +119,9 @@ class RequestExpiredError(NyaProxyError):
|
|
|
107
119
|
|
|
108
120
|
|
|
109
121
|
class NoAvailableAPIKeyError(NyaProxyError):
|
|
110
|
-
"""
|
|
122
|
+
"""
|
|
123
|
+
Exception raised when no API keys are available (all key are rate limited).
|
|
124
|
+
"""
|
|
111
125
|
|
|
112
126
|
def __init__(self, api_name: str):
|
|
113
127
|
"""
|
|
@@ -116,6 +130,7 @@ class NoAvailableAPIKeyError(NyaProxyError):
|
|
|
116
130
|
Args:
|
|
117
131
|
api_name: Name of the API
|
|
118
132
|
"""
|
|
133
|
+
|
|
119
134
|
self.api_name = api_name
|
|
120
135
|
super().__init__(
|
|
121
136
|
f"NyaProxy: No available API keys for {api_name} (all rate limited)"
|
|
@@ -123,7 +138,9 @@ class NoAvailableAPIKeyError(NyaProxyError):
|
|
|
123
138
|
|
|
124
139
|
|
|
125
140
|
class APIKeyNotConfiguredError(NyaProxyError):
|
|
126
|
-
"""
|
|
141
|
+
"""
|
|
142
|
+
Exception raised when there is no API key found in the configuration.
|
|
143
|
+
"""
|
|
127
144
|
|
|
128
145
|
def __init__(self, api_name: str):
|
|
129
146
|
"""
|
|
@@ -137,7 +154,9 @@ class APIKeyNotConfiguredError(NyaProxyError):
|
|
|
137
154
|
|
|
138
155
|
|
|
139
156
|
class MissingAPIKeyError(NyaProxyError):
|
|
140
|
-
"""
|
|
157
|
+
"""
|
|
158
|
+
Exception raised when an API key is missing in the configuration.
|
|
159
|
+
"""
|
|
141
160
|
|
|
142
161
|
def __init__(self, api_name: str):
|
|
143
162
|
"""
|
|
@@ -151,13 +170,17 @@ class MissingAPIKeyError(NyaProxyError):
|
|
|
151
170
|
|
|
152
171
|
|
|
153
172
|
class APIConfigError(NyaProxyError):
|
|
154
|
-
"""
|
|
173
|
+
"""
|
|
174
|
+
Exception raised for API configuration errors.
|
|
175
|
+
"""
|
|
155
176
|
|
|
156
177
|
pass
|
|
157
178
|
|
|
158
179
|
|
|
159
180
|
class UnknownAPIError(NyaProxyError):
|
|
160
|
-
"""
|
|
181
|
+
"""
|
|
182
|
+
Exception raised when requesting an unknown API.
|
|
183
|
+
"""
|
|
161
184
|
|
|
162
185
|
def __init__(self, path: str):
|
|
163
186
|
"""
|
|
@@ -171,7 +194,9 @@ class UnknownAPIError(NyaProxyError):
|
|
|
171
194
|
|
|
172
195
|
|
|
173
196
|
class ConnectionError(NyaProxyError):
|
|
174
|
-
"""
|
|
197
|
+
"""
|
|
198
|
+
Exception raised for connection errors to target APIs.
|
|
199
|
+
"""
|
|
175
200
|
|
|
176
201
|
def __init__(self, api_name: str, url: str, message: str = None):
|
|
177
202
|
"""
|
|
@@ -190,7 +215,9 @@ class ConnectionError(NyaProxyError):
|
|
|
190
215
|
|
|
191
216
|
|
|
192
217
|
class TimeoutError(NyaProxyError):
|
|
193
|
-
"""
|
|
218
|
+
"""
|
|
219
|
+
Exception raised for request timeouts.
|
|
220
|
+
"""
|
|
194
221
|
|
|
195
222
|
def __init__(self, api_name: str, timeout: float):
|
|
196
223
|
"""
|
|
@@ -207,8 +234,30 @@ class TimeoutError(NyaProxyError):
|
|
|
207
234
|
)
|
|
208
235
|
|
|
209
236
|
|
|
237
|
+
class RequestRateLimited(NyaProxyError):
|
|
238
|
+
"""
|
|
239
|
+
Exception raised when a request is rate limited.
|
|
240
|
+
"""
|
|
241
|
+
|
|
242
|
+
def __init__(self, api_name: str, retry_after: float):
|
|
243
|
+
"""
|
|
244
|
+
Initialize request rate limited error.
|
|
245
|
+
|
|
246
|
+
Args:
|
|
247
|
+
api_name: Name of the API
|
|
248
|
+
retry_after: Time to wait before retrying in seconds
|
|
249
|
+
"""
|
|
250
|
+
self.api_name = api_name
|
|
251
|
+
self.retry_after = retry_after
|
|
252
|
+
super().__init__(
|
|
253
|
+
f"NyaProxy: Request to {api_name} is rate limited, retry after {retry_after:.1f}s"
|
|
254
|
+
)
|
|
255
|
+
|
|
256
|
+
|
|
210
257
|
class EncounterRetryStatusCodeError(NyaProxyError):
|
|
211
|
-
"""
|
|
258
|
+
"""
|
|
259
|
+
Exception raised when encountering a retry status code defined in the configuration.
|
|
260
|
+
"""
|
|
212
261
|
|
|
213
262
|
def __init__(self, api_name: str, status_code: int):
|
|
214
263
|
"""
|
|
@@ -226,7 +275,9 @@ class EncounterRetryStatusCodeError(NyaProxyError):
|
|
|
226
275
|
|
|
227
276
|
|
|
228
277
|
class ReachedMaxRetriesError(NyaProxyError):
|
|
229
|
-
"""
|
|
278
|
+
"""
|
|
279
|
+
Exception raised when the maximum number of retries is reached.
|
|
280
|
+
"""
|
|
230
281
|
|
|
231
282
|
def __init__(self, api_name: str, max_retries: int):
|
|
232
283
|
"""
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Data models for request handling in NyaProxy.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
import asyncio
|
|
6
|
+
import time
|
|
7
|
+
from typing import TYPE_CHECKING, Optional
|
|
8
|
+
|
|
9
|
+
from httpx import Headers
|
|
10
|
+
|
|
11
|
+
if TYPE_CHECKING:
|
|
12
|
+
from fastapi import Request
|
|
13
|
+
from starlette.datastructures import URL
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class ProxyRequest:
|
|
17
|
+
"""
|
|
18
|
+
Structured representation of an API request for processing.
|
|
19
|
+
|
|
20
|
+
This class encapsulates all the data and metadata needed to handle
|
|
21
|
+
a request throughout the proxy processing pipeline.
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
def __init__(
|
|
25
|
+
self,
|
|
26
|
+
method: str,
|
|
27
|
+
_url: "URL",
|
|
28
|
+
headers: Optional[Headers],
|
|
29
|
+
content: Optional[bytes],
|
|
30
|
+
ip: str = None,
|
|
31
|
+
):
|
|
32
|
+
self.method = method
|
|
33
|
+
|
|
34
|
+
# Lower number = higher priority (1=retry, 2=priority, 3=normal)
|
|
35
|
+
self.priority = 3
|
|
36
|
+
|
|
37
|
+
# original url from the proxy request
|
|
38
|
+
self._url = _url
|
|
39
|
+
|
|
40
|
+
# final url to be requested, will be set later
|
|
41
|
+
self.url = None
|
|
42
|
+
|
|
43
|
+
self.headers = headers
|
|
44
|
+
self.content = content
|
|
45
|
+
|
|
46
|
+
# API Related metadata
|
|
47
|
+
self.api_name: str = "unknow"
|
|
48
|
+
self.api_key: Optional[str] = None
|
|
49
|
+
self.ip = ip
|
|
50
|
+
|
|
51
|
+
# Number of attempts made for this request
|
|
52
|
+
self.attempts = 0
|
|
53
|
+
# Timestamp when added to queue
|
|
54
|
+
self.added_at = time.time()
|
|
55
|
+
# Whether to apply rate limiting for this request
|
|
56
|
+
self._rate_limited = False
|
|
57
|
+
|
|
58
|
+
self.future: Optional[asyncio.Future] = None
|
|
59
|
+
self._original_future: Optional[asyncio.Future] = None
|
|
60
|
+
|
|
61
|
+
@classmethod
|
|
62
|
+
async def from_request(cls, request: "Request") -> "ProxyRequest":
|
|
63
|
+
"""
|
|
64
|
+
Create a ProxyRequest instance from a FastAPI Request object.
|
|
65
|
+
"""
|
|
66
|
+
|
|
67
|
+
return cls(
|
|
68
|
+
method=request.method,
|
|
69
|
+
_url=request.url,
|
|
70
|
+
headers=Headers(request.headers),
|
|
71
|
+
content=await request.body(),
|
|
72
|
+
ip=request.client.host,
|
|
73
|
+
)
|
|
74
|
+
|
|
75
|
+
def __lt__(self, other):
|
|
76
|
+
"""
|
|
77
|
+
Compare for heap ordering (priority first, then timestamp).
|
|
78
|
+
"""
|
|
79
|
+
if self.priority != other.priority:
|
|
80
|
+
return self.priority < other.priority
|
|
81
|
+
return self.added_at < other.added_at
|
|
@@ -24,7 +24,9 @@ class ConfigManager:
|
|
|
24
24
|
_initialized = False
|
|
25
25
|
|
|
26
26
|
def __new__(cls, *args, **kwargs):
|
|
27
|
-
"""
|
|
27
|
+
"""
|
|
28
|
+
Create a singleton instance.
|
|
29
|
+
"""
|
|
28
30
|
if cls._instance is None:
|
|
29
31
|
cls._instance = super(ConfigManager, cls).__new__(cls)
|
|
30
32
|
return cls._instance
|
|
@@ -75,19 +77,25 @@ class ConfigManager:
|
|
|
75
77
|
|
|
76
78
|
@classmethod
|
|
77
79
|
def reset(cls):
|
|
78
|
-
"""
|
|
80
|
+
"""
|
|
81
|
+
Reset the singleton instance (useful for testing).
|
|
82
|
+
"""
|
|
79
83
|
cls._instance = None
|
|
80
84
|
cls._initialized = False
|
|
81
85
|
|
|
82
86
|
@classmethod
|
|
83
87
|
def get_instance(cls) -> "ConfigManager":
|
|
84
|
-
"""
|
|
88
|
+
"""
|
|
89
|
+
Get the singleton instance, creating it if needed.
|
|
90
|
+
"""
|
|
85
91
|
if cls._instance is None:
|
|
86
92
|
cls._instance = ConfigManager()
|
|
87
93
|
return cls._instance
|
|
88
94
|
|
|
89
95
|
def init_config_client(self) -> NekoConf:
|
|
90
|
-
"""
|
|
96
|
+
"""
|
|
97
|
+
Initialize the NekoConf.
|
|
98
|
+
"""
|
|
91
99
|
|
|
92
100
|
storage: Union[FileStorageBackend, RemoteStorageBackend, None] = None
|
|
93
101
|
|
|
@@ -148,39 +156,45 @@ class ConfigManager:
|
|
|
148
156
|
return server
|
|
149
157
|
|
|
150
158
|
def get_port(self) -> int:
|
|
151
|
-
"""
|
|
159
|
+
"""
|
|
160
|
+
Get the port for the proxy server.
|
|
161
|
+
"""
|
|
152
162
|
return self.config.get_int("server.port", 8080)
|
|
153
163
|
|
|
154
164
|
def get_host(self) -> str:
|
|
155
|
-
"""
|
|
165
|
+
"""
|
|
166
|
+
Get the host for the proxy server.
|
|
167
|
+
"""
|
|
156
168
|
return self.config.get_str("server.host", "0.0.0.0")
|
|
157
169
|
|
|
158
170
|
def get_debug_level(self) -> str:
|
|
159
|
-
"""
|
|
171
|
+
"""
|
|
172
|
+
Get the debug level for logging.
|
|
173
|
+
"""
|
|
160
174
|
return self.config.get_str("server.debug_level", "INFO")
|
|
161
175
|
|
|
162
176
|
def get_dashboard_enabled(self) -> bool:
|
|
163
|
-
"""
|
|
177
|
+
"""
|
|
178
|
+
Check if dashboard is enabled.
|
|
179
|
+
"""
|
|
164
180
|
return self.config.get_bool("server.dashboard.enabled", True)
|
|
165
181
|
|
|
166
182
|
def get_retry_mode(self) -> str:
|
|
167
|
-
"""
|
|
183
|
+
"""
|
|
184
|
+
Get the retry mode for failed requests.
|
|
185
|
+
"""
|
|
168
186
|
return self.config.get_str("server.retry.mode", "default")
|
|
169
187
|
|
|
170
188
|
def get_retry_config(self) -> Dict[str, Any]:
|
|
171
|
-
"""
|
|
189
|
+
"""
|
|
190
|
+
Get the retry configuration.
|
|
191
|
+
"""
|
|
172
192
|
return self.config.get_dict("server.retry", {})
|
|
173
193
|
|
|
174
|
-
def get_queue_size(self) -> int:
|
|
175
|
-
"""Get the maximum queue size."""
|
|
176
|
-
return self.config.get_int("server.queue.max_size", 100)
|
|
177
|
-
|
|
178
|
-
def get_queue_expiry(self) -> int:
|
|
179
|
-
"""Get the default expiry time for queued requests in seconds."""
|
|
180
|
-
return self.config.get_int("server.queue.expiry_seconds", 300)
|
|
181
|
-
|
|
182
194
|
def get_api_key(self) -> Union[None, str, List[str]]:
|
|
183
|
-
"""
|
|
195
|
+
"""
|
|
196
|
+
Get the API key(s) for authenticating with the proxy.
|
|
197
|
+
"""
|
|
184
198
|
|
|
185
199
|
api_key = self.config.get("server.api_key", None)
|
|
186
200
|
|
|
@@ -192,7 +206,9 @@ class ConfigManager:
|
|
|
192
206
|
return str(api_key)
|
|
193
207
|
|
|
194
208
|
def get_apis(self) -> Dict[str, Any]:
|
|
195
|
-
"""
|
|
209
|
+
"""
|
|
210
|
+
Get the configured APIs.
|
|
211
|
+
"""
|
|
196
212
|
apis = self.config.get_dict("apis", {})
|
|
197
213
|
if not apis:
|
|
198
214
|
raise ConfigurationError("No APIs configured. Please add at least one API.")
|
|
@@ -200,12 +216,16 @@ class ConfigManager:
|
|
|
200
216
|
return apis
|
|
201
217
|
|
|
202
218
|
def get_api_config(self, api_name: str) -> Optional[Dict[str, Any]]:
|
|
203
|
-
"""
|
|
219
|
+
"""
|
|
220
|
+
Get the configuration for a specific API.
|
|
221
|
+
"""
|
|
204
222
|
apis = self.get_apis()
|
|
205
223
|
return apis.get(api_name, None)
|
|
206
224
|
|
|
207
225
|
def get_logging_config(self) -> Dict[str, Any]:
|
|
208
|
-
"""
|
|
226
|
+
"""
|
|
227
|
+
Get the logging configuration.
|
|
228
|
+
"""
|
|
209
229
|
return {
|
|
210
230
|
"enabled": self.config.get_bool("server.logging.enabled", True),
|
|
211
231
|
"level": self.config.get_str("server.logging.level", "INFO"),
|
|
@@ -213,43 +233,61 @@ class ConfigManager:
|
|
|
213
233
|
}
|
|
214
234
|
|
|
215
235
|
def get_proxy_enabled(self) -> bool:
|
|
216
|
-
"""
|
|
236
|
+
"""
|
|
237
|
+
Check if the proxy is enabled.
|
|
238
|
+
"""
|
|
217
239
|
return self.config.get_bool("server.proxy.enabled", False)
|
|
218
240
|
|
|
219
241
|
def get_proxy_address(self) -> str:
|
|
220
|
-
"""
|
|
242
|
+
"""
|
|
243
|
+
Get the proxy address.
|
|
244
|
+
"""
|
|
221
245
|
return self.config.get_str("server.proxy.address", "")
|
|
222
246
|
|
|
223
247
|
def get_cors_allow_origins(self) -> List[str]:
|
|
224
|
-
"""
|
|
248
|
+
"""
|
|
249
|
+
Get the CORS allow origin for the proxy.
|
|
250
|
+
"""
|
|
225
251
|
return self.config.get_list("server.cors.allow_origins", "*")
|
|
226
252
|
|
|
227
253
|
def get_cors_allow_methods(self) -> List[str]:
|
|
228
|
-
"""
|
|
254
|
+
"""
|
|
255
|
+
Get the CORS allow methods for the proxy.
|
|
256
|
+
"""
|
|
229
257
|
return self.config.get_list(
|
|
230
258
|
"server.cors.allow_methods", "GET, POST, PUT, DELETE, OPTIONS"
|
|
231
259
|
)
|
|
232
260
|
|
|
233
261
|
def get_cors_allow_headers(self) -> List[str]:
|
|
234
|
-
"""
|
|
262
|
+
"""
|
|
263
|
+
Get the CORS allow headers for the proxy.
|
|
264
|
+
"""
|
|
235
265
|
return self.config.get_list(
|
|
236
266
|
"server.cors.allow_headers", "Content-Type, Authorization"
|
|
237
267
|
)
|
|
238
268
|
|
|
239
269
|
def get_cors_allow_credentials(self) -> bool:
|
|
240
|
-
"""
|
|
270
|
+
"""
|
|
271
|
+
Check if CORS allow credentials is enabled for the proxy.
|
|
272
|
+
"""
|
|
241
273
|
return self.config.get_bool("server.cors.allow_credentials", False)
|
|
242
274
|
|
|
243
275
|
def get_default_settings(self) -> Dict[str, Any]:
|
|
244
|
-
"""
|
|
276
|
+
"""
|
|
277
|
+
Get the default settings for endpoints.
|
|
278
|
+
"""
|
|
245
279
|
return self.config.get_dict("default_settings", {})
|
|
246
280
|
|
|
247
281
|
def get_default_timeout(self) -> int:
|
|
248
|
-
"""
|
|
282
|
+
"""
|
|
283
|
+
Get the default timeout for API requests.
|
|
284
|
+
"""
|
|
249
285
|
return self.config.get_int("server.timeouts.request_timeout_seconds", 30)
|
|
250
286
|
|
|
251
287
|
def get_default_setting(self, setting_path: str, default_value: Any = None) -> Any:
|
|
252
|
-
"""
|
|
288
|
+
"""
|
|
289
|
+
Get a default setting value.
|
|
290
|
+
"""
|
|
253
291
|
return self.config.get(f"default_settings.{setting_path}", default_value)
|
|
254
292
|
|
|
255
293
|
def get_api_setting(
|
|
@@ -293,7 +331,9 @@ class ConfigManager:
|
|
|
293
331
|
return self.config.get_str(f"apis.{api_name}.{setting_path}", default_value)
|
|
294
332
|
|
|
295
333
|
def get_api_request_body_substitution_enabled(self, api_name: str) -> bool:
|
|
296
|
-
"""
|
|
334
|
+
"""
|
|
335
|
+
Get whether request body substitution is enabled for an API.
|
|
336
|
+
"""
|
|
297
337
|
return self.get_api_setting(
|
|
298
338
|
api_name, "request_body_substitution.enabled", "bool"
|
|
299
339
|
)
|
|
@@ -301,79 +341,141 @@ class ConfigManager:
|
|
|
301
341
|
def get_api_request_body_substitution_rules(
|
|
302
342
|
self, api_name: str
|
|
303
343
|
) -> List[Dict[str, Any]]:
|
|
304
|
-
"""
|
|
344
|
+
"""
|
|
345
|
+
Get request body substitution rules.
|
|
346
|
+
"""
|
|
305
347
|
return self.get_api_setting(api_name, "request_body_substitution.rules", "list")
|
|
306
348
|
|
|
307
349
|
def get_api_default_timeout(self, api_name: str) -> int:
|
|
308
|
-
"""
|
|
350
|
+
"""
|
|
351
|
+
Get default timeout for API requests.
|
|
352
|
+
"""
|
|
309
353
|
return self.get_api_setting(api_name, "timeouts.request_timeout_seconds", "int")
|
|
310
354
|
|
|
311
355
|
def get_api_key_variable(self, api_name: str) -> str:
|
|
312
|
-
"""
|
|
356
|
+
"""
|
|
357
|
+
Get key variable name.
|
|
358
|
+
"""
|
|
313
359
|
return self.get_api_setting(api_name, "key_variable", "str")
|
|
314
360
|
|
|
361
|
+
def get_api_key_concurrency(self, api_name: str) -> bool:
|
|
362
|
+
"""
|
|
363
|
+
Get key concurrency setting.
|
|
364
|
+
"""
|
|
365
|
+
return self.get_api_setting(api_name, "key_concurrency", "bool")
|
|
366
|
+
|
|
315
367
|
def get_api_custom_headers(self, api_name: str) -> Dict[str, Any]:
|
|
316
|
-
"""
|
|
368
|
+
"""
|
|
369
|
+
Get custom headers.
|
|
370
|
+
"""
|
|
317
371
|
return self.get_api_setting(api_name, "headers", "dict")
|
|
318
372
|
|
|
319
373
|
def get_api_endpoint(self, api_name: str) -> str:
|
|
320
|
-
"""
|
|
374
|
+
"""
|
|
375
|
+
Get API endpoint URL.
|
|
376
|
+
"""
|
|
321
377
|
return self.get_api_setting(api_name, "endpoint", "str")
|
|
322
378
|
|
|
323
379
|
def get_api_load_balancing_strategy(self, api_name: str) -> str:
|
|
324
|
-
"""
|
|
380
|
+
"""
|
|
381
|
+
Get load balancing strategy.
|
|
382
|
+
"""
|
|
325
383
|
return self.get_api_setting(api_name, "load_balancing_strategy", "str")
|
|
326
384
|
|
|
385
|
+
def get_api_queue_size(self, api_name: str) -> int:
|
|
386
|
+
"""
|
|
387
|
+
Get the queue size for the API.
|
|
388
|
+
"""
|
|
389
|
+
return self.get_api_setting(api_name, "queue.max_size", "int")
|
|
390
|
+
|
|
391
|
+
def get_api_queue_expiry(self, api_name: str) -> float:
|
|
392
|
+
"""
|
|
393
|
+
Get the queue expiry time for the API.
|
|
394
|
+
"""
|
|
395
|
+
return self.get_api_setting(api_name, "queue.expiry_seconds", "float")
|
|
396
|
+
|
|
327
397
|
def get_api_rate_limit_enabled(self, api_name: str) -> bool:
|
|
328
|
-
"""
|
|
398
|
+
"""
|
|
399
|
+
Get rate limit enabled status.
|
|
400
|
+
"""
|
|
329
401
|
return self.get_api_setting(api_name, "rate_limit.enabled", "bool")
|
|
330
402
|
|
|
331
403
|
def get_api_endpoint_rate_limit(self, api_name: str) -> str:
|
|
332
|
-
"""
|
|
404
|
+
"""
|
|
405
|
+
Get endpoint rate limit.
|
|
406
|
+
"""
|
|
333
407
|
return self.get_api_setting(api_name, "rate_limit.endpoint_rate_limit", "str")
|
|
334
408
|
|
|
335
409
|
def get_api_key_rate_limit(self, api_name: str) -> str:
|
|
336
|
-
"""
|
|
410
|
+
"""
|
|
411
|
+
Get key rate limit.
|
|
412
|
+
"""
|
|
337
413
|
return self.get_api_setting(api_name, "rate_limit.key_rate_limit", "str")
|
|
338
414
|
|
|
415
|
+
def get_api_ip_rate_limit(self, api_name: str) -> str:
|
|
416
|
+
"""
|
|
417
|
+
Get IP rate limit.
|
|
418
|
+
"""
|
|
419
|
+
return self.get_api_setting(api_name, "rate_limit.ip_rate_limit", "str")
|
|
420
|
+
|
|
339
421
|
def get_api_retry_enabled(self, api_name: str) -> bool:
|
|
340
|
-
"""
|
|
422
|
+
"""
|
|
423
|
+
Get retry enabled status.
|
|
424
|
+
"""
|
|
341
425
|
return self.get_api_setting(api_name, "retry.enabled", "bool")
|
|
342
426
|
|
|
343
427
|
def get_api_retry_mode(self, api_name: str) -> str:
|
|
344
|
-
"""
|
|
428
|
+
"""
|
|
429
|
+
Get retry mode.
|
|
430
|
+
"""
|
|
345
431
|
return self.get_api_setting(api_name, "retry.mode", "str")
|
|
346
432
|
|
|
347
433
|
def get_api_retry_attempts(self, api_name: str) -> int:
|
|
348
|
-
"""
|
|
434
|
+
"""
|
|
435
|
+
Get retry attempts count.
|
|
436
|
+
"""
|
|
349
437
|
return self.get_api_setting(api_name, "retry.attempts", "int")
|
|
350
438
|
|
|
351
439
|
def get_api_retry_after_seconds(self, api_name: str) -> float:
|
|
352
|
-
"""
|
|
440
|
+
"""
|
|
441
|
+
Get retry delay in seconds.
|
|
442
|
+
"""
|
|
353
443
|
return self.get_api_setting(api_name, "retry.retry_after_seconds", "float")
|
|
354
444
|
|
|
355
445
|
def get_api_retry_status_codes(self, api_name: str) -> List[int]:
|
|
356
|
-
"""
|
|
446
|
+
"""
|
|
447
|
+
Get retry status codes.
|
|
448
|
+
"""
|
|
357
449
|
return self.get_api_setting(api_name, "retry.retry_status_codes", "list")
|
|
358
450
|
|
|
359
451
|
def get_api_retry_request_methods(self, api_name: str) -> List[str]:
|
|
360
|
-
"""
|
|
452
|
+
"""
|
|
453
|
+
Get retry request methods.
|
|
454
|
+
"""
|
|
361
455
|
return self.get_api_setting(api_name, "retry.retry_request_methods", "list")
|
|
362
456
|
|
|
363
457
|
def get_api_rate_limit_paths(self, api_name: str) -> List[str]:
|
|
364
|
-
"""
|
|
458
|
+
"""
|
|
459
|
+
Get rate limit path patterns.
|
|
460
|
+
"""
|
|
365
461
|
return self.get_api_setting(api_name, "rate_limit.rate_limit_paths", "list")
|
|
366
462
|
|
|
367
463
|
def get_api_variables(self, api_name: str) -> Dict[str, List[Any]]:
|
|
368
|
-
"""
|
|
464
|
+
"""
|
|
465
|
+
Get all variables defined for an API.
|
|
466
|
+
"""
|
|
369
467
|
return self.get_api_config(api_name).get("variables", {})
|
|
370
468
|
|
|
371
469
|
def get_api_aliases(self, api_name: str) -> List[str]:
|
|
372
|
-
"""
|
|
470
|
+
"""
|
|
471
|
+
Get the aliases defined for an API.
|
|
472
|
+
"""
|
|
373
473
|
return self.get_api_config(api_name).get("aliases", [])
|
|
374
474
|
|
|
375
475
|
def get_api_variable_values(self, api_name: str, variable_name: str) -> List[Any]:
|
|
376
|
-
"""
|
|
476
|
+
"""
|
|
477
|
+
Get variable values for an API.
|
|
478
|
+
"""
|
|
377
479
|
api_config = self.get_api_config(api_name)
|
|
378
480
|
if not api_config:
|
|
379
481
|
return []
|
|
@@ -392,7 +494,9 @@ class ConfigManager:
|
|
|
392
494
|
return [str(values)]
|
|
393
495
|
|
|
394
496
|
def get_api_request_subst_rules(self, api_name: str) -> Dict[str, Any]:
|
|
395
|
-
"""
|
|
497
|
+
"""
|
|
498
|
+
Get request body substitution rules if enabled.
|
|
499
|
+
"""
|
|
396
500
|
|
|
397
501
|
enable = self.get_api_request_body_substitution_enabled(api_name)
|
|
398
502
|
|
|
@@ -401,7 +505,9 @@ class ConfigManager:
|
|
|
401
505
|
return self.get_api_request_body_substitution_rules(api_name)
|
|
402
506
|
|
|
403
507
|
def reload(self) -> None:
|
|
404
|
-
"""
|
|
508
|
+
"""
|
|
509
|
+
Reload the configuration from disk.
|
|
510
|
+
"""
|
|
405
511
|
try:
|
|
406
512
|
self.config = self.init_config_client()
|
|
407
513
|
|