Introduce typed config objects, optional adapter injection, and .env loading to simplify testing while preserving env-based defaults for production usage. Co-authored-by: Cursor <[email protected]>
31 lines
692 B
Python
31 lines
692 B
Python
"""Redis connection configuration."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
from dataclasses import dataclass
|
|
|
|
from python_utils import check_env
|
|
|
|
from python_repositories.config.dotenv_loader import load_dotenv
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class RedisConfig:
|
|
"""Configuration for connecting to Redis."""
|
|
|
|
uri: str
|
|
|
|
@classmethod
|
|
def from_env(
|
|
cls,
|
|
uri_env_var_name: str = "REDIS_URI",
|
|
*,
|
|
use_dotenv: bool = True,
|
|
) -> RedisConfig:
|
|
"""Load configuration from environment variables."""
|
|
if use_dotenv:
|
|
load_dotenv()
|
|
check_env(uri_env_var_name)
|
|
return cls(uri=str(os.getenv(uri_env_var_name)))
|