f4e5a9f8e0
- Fix Invalid Date display in Dashboard notifications - Fix timezone offset (8 hours) in Trading Records time display - Fix position closing failures due to commission discrepancies (fetch actual exchange position size for reduce_only orders) - Fix IBKR connection error 'no current event loop in thread' by ensuring asyncio event loop exists - Fix duplicate orders on same candle by extending signal deduplication to close signals - Add responsive design for Profile page (mobile-friendly) - Remove unused strategy_code module and database table - Fix LLM service to support multiple providers (OpenRouter, OpenAI, DeepSeek, Grok, Google) - Add auto-detection of configured LLM provider based on API key availability - Fix AI code generation to use unified LLMService with proper provider selection - Fix crypto symbol format handling (ETH/USDT no longer becomes ETH/USDT/USDT) - Fix Commission display showing '0E-8' in Trading Records - Fix P&L display for signal-only trades (show '--' for unrealized P&L) - Fix OAuth login not updating last_login_at for new users - Add migration script for notification_settings column - Update env.example with new LLM provider configurations - Remove ESLint rule that was not defined in config
89 lines
3.0 KiB
Python
89 lines
3.0 KiB
Python
"""
|
|
API key configuration.
|
|
All third-party keys should be provided via environment variables (recommended: backend_api_python/.env).
|
|
"""
|
|
import os
|
|
|
|
class MetaAPIKeys(type):
|
|
"""API Keys 元类,用于支持类属性的动态获取"""
|
|
|
|
@property
|
|
def FINNHUB_API_KEY(cls):
|
|
from app.utils.config_loader import load_addon_config
|
|
val = load_addon_config().get('finnhub', {}).get('api_key')
|
|
return val if val else os.getenv('FINNHUB_API_KEY', '')
|
|
|
|
@property
|
|
def TIINGO_API_KEY(cls):
|
|
from app.utils.config_loader import load_addon_config
|
|
val = load_addon_config().get('tiingo', {}).get('api_key')
|
|
return val if val else os.getenv('TIINGO_API_KEY', '')
|
|
|
|
@property
|
|
def OPENROUTER_API_KEY(cls):
|
|
# Always check env var first to avoid stale cache issues
|
|
env_val = os.getenv('OPENROUTER_API_KEY', '').strip()
|
|
if env_val:
|
|
return env_val
|
|
from app.utils.config_loader import load_addon_config
|
|
val = load_addon_config().get('openrouter', {}).get('api_key')
|
|
return val if val else ''
|
|
|
|
@property
|
|
def OPENAI_API_KEY(cls):
|
|
"""OpenAI direct API key"""
|
|
env_val = os.getenv('OPENAI_API_KEY', '').strip()
|
|
if env_val:
|
|
return env_val
|
|
from app.utils.config_loader import load_addon_config
|
|
val = load_addon_config().get('openai', {}).get('api_key')
|
|
return val if val else ''
|
|
|
|
@property
|
|
def GOOGLE_API_KEY(cls):
|
|
"""Google Gemini API key"""
|
|
env_val = os.getenv('GOOGLE_API_KEY', '').strip()
|
|
if env_val:
|
|
return env_val
|
|
from app.utils.config_loader import load_addon_config
|
|
val = load_addon_config().get('google', {}).get('api_key')
|
|
return val if val else ''
|
|
|
|
@property
|
|
def DEEPSEEK_API_KEY(cls):
|
|
"""DeepSeek API key"""
|
|
env_val = os.getenv('DEEPSEEK_API_KEY', '').strip()
|
|
if env_val:
|
|
return env_val
|
|
from app.utils.config_loader import load_addon_config
|
|
val = load_addon_config().get('deepseek', {}).get('api_key')
|
|
return val if val else ''
|
|
|
|
@property
|
|
def GROK_API_KEY(cls):
|
|
"""xAI Grok API key"""
|
|
env_val = os.getenv('GROK_API_KEY', '').strip()
|
|
if env_val:
|
|
return env_val
|
|
from app.utils.config_loader import load_addon_config
|
|
val = load_addon_config().get('grok', {}).get('api_key')
|
|
return val if val else ''
|
|
|
|
|
|
class APIKeys(metaclass=MetaAPIKeys):
|
|
"""API 密钥配置类"""
|
|
|
|
@classmethod
|
|
def get(cls, key_name: str, default: str = '') -> str:
|
|
"""获取 API 密钥"""
|
|
# 尝试从类属性获取
|
|
if hasattr(cls, key_name):
|
|
return getattr(cls, key_name)
|
|
return default
|
|
|
|
@classmethod
|
|
def is_configured(cls, key_name: str) -> bool:
|
|
"""检查 API 密钥是否已配置"""
|
|
value = cls.get(key_name)
|
|
return bool(value and value.strip())
|