refactor(V2): IBKR settings — TypedDict return + docstrings
Code review polish: - credentials() returns IBKRCredentials TypedDict (was bare dict) - Method docstring matching Deribit pattern - Inline comment explaining account_id env-only design Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,10 +1,22 @@
|
|||||||
"""Pydantic Settings: legge .env e variabili d'ambiente."""
|
"""Pydantic Settings: legge .env e variabili d'ambiente."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from typing import TypedDict
|
||||||
|
|
||||||
from pydantic import Field, SecretStr
|
from pydantic import Field, SecretStr
|
||||||
from pydantic_settings import BaseSettings, SettingsConfigDict
|
from pydantic_settings import BaseSettings, SettingsConfigDict
|
||||||
|
|
||||||
|
|
||||||
|
class IBKRCredentials(TypedDict):
|
||||||
|
consumer_key: str
|
||||||
|
access_token: str
|
||||||
|
access_token_secret: str
|
||||||
|
signature_key_path: str
|
||||||
|
encryption_key_path: str
|
||||||
|
account_id: str
|
||||||
|
dh_prime: str
|
||||||
|
|
||||||
|
|
||||||
class _Sub(BaseSettings):
|
class _Sub(BaseSettings):
|
||||||
"""Base per sub-settings, condivide model_config con env_file."""
|
"""Base per sub-settings, condivide model_config con env_file."""
|
||||||
model_config = SettingsConfigDict(
|
model_config = SettingsConfigDict(
|
||||||
@@ -111,6 +123,7 @@ class IBKRSettings(_Sub):
|
|||||||
access_token_secret_testnet: SecretStr | None = None
|
access_token_secret_testnet: SecretStr | None = None
|
||||||
signature_key_path_testnet: str | None = None
|
signature_key_path_testnet: str | None = None
|
||||||
encryption_key_path_testnet: str | None = None
|
encryption_key_path_testnet: str | None = None
|
||||||
|
# account_id has no base variant: paper and live accounts are always distinct
|
||||||
account_id_testnet: str | None = None
|
account_id_testnet: str | None = None
|
||||||
|
|
||||||
consumer_key_live: str | None = None
|
consumer_key_live: str | None = None
|
||||||
@@ -128,7 +141,12 @@ class IBKRSettings(_Sub):
|
|||||||
ws_max_subscriptions: int = 80
|
ws_max_subscriptions: int = 80
|
||||||
ws_idle_timeout_s: int = 300
|
ws_idle_timeout_s: int = 300
|
||||||
|
|
||||||
def credentials(self, env: str) -> dict:
|
def credentials(self, env: str) -> IBKRCredentials:
|
||||||
|
"""Return credential dict for given env.
|
||||||
|
Prefers env-specific (_TESTNET / _LIVE) values; falls back to base
|
||||||
|
(IBKR_CONSUMER_KEY etc.) for legacy single-pair setups.
|
||||||
|
ValueError if any required field missing.
|
||||||
|
"""
|
||||||
if env == "testnet":
|
if env == "testnet":
|
||||||
ck = self.consumer_key_testnet or self.consumer_key
|
ck = self.consumer_key_testnet or self.consumer_key
|
||||||
at = self.access_token_testnet or self.access_token
|
at = self.access_token_testnet or self.access_token
|
||||||
|
|||||||
Reference in New Issue
Block a user