184 lines
5.5 KiB
Python
184 lines
5.5 KiB
Python
"""Application configuration split by architecture component."""
|
|
|
|
from decimal import Decimal
|
|
from functools import lru_cache
|
|
from pathlib import Path
|
|
import sys
|
|
from typing import Any
|
|
|
|
from pydantic import BaseModel, Field, field_validator
|
|
from pydantic_settings import (
|
|
BaseSettings,
|
|
PydanticBaseSettingsSource,
|
|
SettingsConfigDict,
|
|
YamlConfigSettingsSource,
|
|
)
|
|
|
|
DEFAULT_CONFIG_FILE = "/config.yaml"
|
|
TEST_CONFIG_FILE = "config.test.yaml"
|
|
|
|
|
|
class ControllerConfig(BaseModel):
|
|
api_prefix: str = "/api/v1"
|
|
request_id_header: str = "X-Request-ID"
|
|
|
|
|
|
class ServiceConfig(BaseModel):
|
|
provider_timeout_seconds: float = 10.0
|
|
max_parallel_providers: int = 8
|
|
|
|
|
|
class BusinessLogicConfig(BaseModel):
|
|
weight_round_scale: int = 2
|
|
provider_price_multiplier: Decimal = Field(..., gt=0)
|
|
|
|
|
|
class RepositoryConfig(BaseModel):
|
|
redis_dsn: str = "redis://localhost:6379/0"
|
|
price_cache_ttl_seconds: int = 900
|
|
|
|
|
|
class AdapterConfig(BaseModel):
|
|
cdek_base_url: str = "https://api.cdek.ru/v2"
|
|
cdek_client_id: str = ""
|
|
cdek_client_secret: str = ""
|
|
cdek_retry_attempts: int = Field(default=2, ge=0)
|
|
cdek_retry_backoff_seconds: float = Field(default=0.2, ge=0)
|
|
cdek_timeout_seconds: float = Field(default=10.0, gt=0)
|
|
cdek_cache_ttl_seconds: int = Field(default=900, gt=0)
|
|
|
|
|
|
class TBankPaymentAuthConfig(BaseModel):
|
|
terminal_key: str = Field(..., min_length=1)
|
|
password: str = Field(..., min_length=1)
|
|
|
|
|
|
class TBankPaymentConfig(BaseModel):
|
|
init_url: str = Field(..., min_length=1)
|
|
auth: TBankPaymentAuthConfig
|
|
timeout_seconds: float = Field(default=10.0, gt=0)
|
|
retry_attempts: int = Field(default=2, ge=0)
|
|
retry_backoff_seconds: float = Field(default=0.2, ge=0)
|
|
|
|
|
|
class DadataAddressSuggestionsConfig(BaseModel):
|
|
url: str = "https://suggestions.dadata.ru/suggestions/api/4_1/rs/suggest/address"
|
|
api_key: str = ""
|
|
timeout_seconds: float = Field(default=10.0, gt=0)
|
|
|
|
|
|
class YandexGeosuggestAddressSuggestionsConfig(BaseModel):
|
|
url: str = "https://suggest-maps.yandex.ru/v1/suggest"
|
|
api_key: str = ""
|
|
timeout_seconds: float = Field(default=10.0, gt=0)
|
|
|
|
|
|
class TomTomAddressSuggestionsConfig(BaseModel):
|
|
url: str = "https://api.tomtom.com/search/2/search"
|
|
api_key: str = ""
|
|
timeout_seconds: float = Field(default=10.0, gt=0)
|
|
|
|
|
|
class AddressSuggestionsConfig(BaseModel):
|
|
country_to_provider: dict[str, str] = Field(default_factory=dict)
|
|
dadata: DadataAddressSuggestionsConfig = Field(
|
|
default_factory=DadataAddressSuggestionsConfig
|
|
)
|
|
yandex_geosuggest: YandexGeosuggestAddressSuggestionsConfig = Field(
|
|
default_factory=YandexGeosuggestAddressSuggestionsConfig
|
|
)
|
|
tomtom: TomTomAddressSuggestionsConfig = Field(
|
|
default_factory=TomTomAddressSuggestionsConfig
|
|
)
|
|
|
|
@field_validator("country_to_provider")
|
|
@classmethod
|
|
def _normalize_country_to_provider(
|
|
cls,
|
|
value: dict[str, str],
|
|
) -> dict[str, str]:
|
|
return {
|
|
str(country_code).strip().upper(): str(provider_id).strip().lower()
|
|
for country_code, provider_id in value.items()
|
|
}
|
|
|
|
|
|
class ObservabilityConfig(BaseModel):
|
|
enabled: bool = False
|
|
service_name: str = Field(..., min_length=1)
|
|
otlp_endpoint: str = Field(..., min_length=1)
|
|
otlp_insecure: bool = True
|
|
|
|
|
|
class Settings(BaseSettings):
|
|
model_config = SettingsConfigDict(
|
|
extra="ignore",
|
|
)
|
|
|
|
controller: ControllerConfig = Field(default_factory=ControllerConfig)
|
|
service: ServiceConfig = Field(default_factory=ServiceConfig)
|
|
business_logic: BusinessLogicConfig = Field(default_factory=BusinessLogicConfig)
|
|
repository: RepositoryConfig = Field(default_factory=RepositoryConfig)
|
|
adapter: AdapterConfig = Field(default_factory=AdapterConfig)
|
|
tbank_payment: TBankPaymentConfig
|
|
address_suggestions: AddressSuggestionsConfig = Field(
|
|
default_factory=AddressSuggestionsConfig
|
|
)
|
|
observability: ObservabilityConfig
|
|
|
|
@classmethod
|
|
def settings_customise_sources(
|
|
cls,
|
|
settings_cls: type[BaseSettings],
|
|
init_settings: PydanticBaseSettingsSource,
|
|
env_settings: PydanticBaseSettingsSource,
|
|
dotenv_settings: PydanticBaseSettingsSource,
|
|
file_secret_settings: PydanticBaseSettingsSource,
|
|
) -> tuple[PydanticBaseSettingsSource, ...]:
|
|
config_file = _resolve_runtime_config_file()
|
|
yaml_source = YamlConfigSettingsSource(
|
|
settings_cls,
|
|
yaml_file=config_file,
|
|
)
|
|
return (
|
|
init_settings,
|
|
yaml_source,
|
|
)
|
|
|
|
|
|
class _RequiredYamlSections(BaseModel):
|
|
controller: dict[str, Any]
|
|
service: dict[str, Any]
|
|
business_logic: dict[str, Any]
|
|
repository: dict[str, Any]
|
|
adapter: dict[str, Any]
|
|
tbank_payment: dict[str, Any]
|
|
address_suggestions: dict[str, Any]
|
|
observability: dict[str, Any]
|
|
|
|
|
|
def _resolve_runtime_config_file() -> str:
|
|
test_config_path = Path(TEST_CONFIG_FILE)
|
|
if "pytest" in sys.modules and test_config_path.is_file():
|
|
return TEST_CONFIG_FILE
|
|
return DEFAULT_CONFIG_FILE
|
|
|
|
|
|
def _validate_required_yaml_sections() -> None:
|
|
config_file = _resolve_runtime_config_file()
|
|
config_path = Path(config_file)
|
|
if not config_path.is_file():
|
|
return
|
|
|
|
yaml_source = YamlConfigSettingsSource(Settings, yaml_file=config_file)
|
|
yaml_data = yaml_source.yaml_data
|
|
if not isinstance(yaml_data, dict):
|
|
yaml_data = {}
|
|
_RequiredYamlSections.model_validate(yaml_data)
|
|
|
|
|
|
@lru_cache(maxsize=1)
|
|
def get_settings() -> Settings:
|
|
_validate_required_yaml_sections()
|
|
return Settings()
|