返回提交历史
Modified
g4f/api/__init__.py
+11
-1
Modified
g4f/mcp/pa_provider.py
+4
-0
XFEstudio/gpt4free
Address review: document PaProviderRegistry TTL, extract auth path check into helper
Agent-Logs-Url: https://github.com/xtekky/gpt4free/sessions/e0daf662-ee35-43ac-bdef-27dd570bc00d Co-authored-by: hlohaus <983577+hlohaus@users.noreply.github.com>
28fb1c0b
代码差异
2 个文件
+15
-1
@@ -218,6 +218,16 @@ class Api:
218
218
if has_crypto:
219
219
private_key, _ = create_or_read_keys()
220
220
session_key = get_session_key()
221
222
def _requires_api_key(path: str, demo: bool) -> bool:
223
"""Return ``True`` when *path* must present a G4F API key."""
224
return (
225
path.startswith("/v1")
226
or path.startswith("/api/")
227
or path.startswith("/pa/")
228
or (demo and path == "/backend-api/v2/upload_cookies")
229
)
230
221
231
@self.app.middleware("http")
222
232
async def authorization(request: Request, call_next):
223
233
user = None
@@ -267,7 +277,7 @@ class Api:
267
277
else:
268
278
user = "admin"
269
279
path = request.url.path
270
if path.startswith("/v1") or path.startswith("/api/") or path.startswith("/pa/") or (AppConfig.demo and path == '/backend-api/v2/upload_cookies'):
280
if _requires_api_key(path, AppConfig.demo):
271
281
if request.method != "OPTIONS" and not path.endswith("/models"):
272
282
if not user_g4f_api_key:
273
283
return ErrorResponse.from_message("G4F API key required", HTTP_401_UNAUTHORIZED)
@@ -501,6 +501,10 @@ class PaProviderRegistry:
501
501
"""
502
502
503
503
#: How long (in seconds) the cached entries remain valid.
504
#: A short TTL (5 s) is intentional: PA provider files are typically edited
505
#: interactively during development, so near-instant pick-up of changes is
506
#: more important than avoiding the cheap filesystem scan. Production
507
#: deployments that want to reduce I/O can increase this value.
504
508
TTL: float = 5.0
505
509
506
510
def __init__(self) -> None: