Skip to content

Commit

Permalink
Refactor configs
Browse files Browse the repository at this point in the history
  • Loading branch information
madnoberson committed Mar 24, 2024
1 parent fc10314 commit cfbf99b
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 35 deletions.
14 changes: 7 additions & 7 deletions src/amdb/infrastructure/auth/session/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
class SessionConfig:
lifetime: timedelta

@classmethod
def from_toml(cls, path: str) -> "SessionConfig":
toml_as_dict = toml.load(path)
session_section_as_dict = toml_as_dict["auth-session"]
return SessionConfig(
lifetime=timedelta(minutes=session_section_as_dict["lifetime"]),
)

def load_session_config_from_toml(path: str) -> SessionConfig:
toml_as_dict = toml.load(path)
session_section_as_dict = toml_as_dict["auth-session"]
return SessionConfig(
lifetime=timedelta(minutes=session_section_as_dict["lifetime"]),
)
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
class CachingPermissionsMapper:
def __init__(
self,
*,
permissions_mapper: PermissionsMapper,
cache_provider: PermissionsMapperCacheProvider,
) -> None:
Expand Down
10 changes: 5 additions & 5 deletions src/amdb/infrastructure/persistence/redis/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
class RedisConfig:
url: str

@classmethod
def from_toml(cls, path: str) -> "RedisConfig":
toml_as_dict = toml.load(path)
redis_section_as_dict = toml_as_dict["redis"]
return RedisConfig(url=redis_section_as_dict["url"])

def load_redis_config_from_toml(path: str) -> RedisConfig:
toml_as_dict = toml.load(path)
redis_section_as_dict = toml_as_dict["redis"]
return RedisConfig(url=redis_section_as_dict["url"])
10 changes: 5 additions & 5 deletions src/amdb/infrastructure/persistence/sqlalchemy/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
class PostgresConfig:
url: str

@classmethod
def from_toml(cls, path: str) -> "PostgresConfig":
toml_as_dict = toml.load(path)
postgres_section_as_dict = toml_as_dict["postgres"]
return PostgresConfig(url=postgres_section_as_dict["url"])

def load_postgres_config_from_toml(path: str) -> PostgresConfig:
toml_as_dict = toml.load(path)
postgres_section_as_dict = toml_as_dict["postgres"]
return PostgresConfig(url=postgres_section_as_dict["url"])
12 changes: 8 additions & 4 deletions src/amdb/main/cli/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@
from dishka import make_container
from alembic import config

from amdb.infrastructure.persistence.sqlalchemy.config import PostgresConfig
from amdb.infrastructure.persistence.redis.config import RedisConfig
from amdb.infrastructure.persistence.sqlalchemy.config import (
load_postgres_config_from_toml,
)
from amdb.infrastructure.persistence.redis.config import (
load_redis_config_from_toml,
)
from amdb.infrastructure.persistence.alembic.config import ALEMBIC_CONFIG
from amdb.presentation.cli.movie import movie_commands
from amdb.main.providers import (
Expand Down Expand Up @@ -37,8 +41,8 @@ def run_cli() -> None:
message = "Path to config env var is not set"
raise ValueError(message)

postgres_config = PostgresConfig.from_toml(path_to_config)
redis_config = RedisConfig.from_toml(path_to_config)
postgres_config = load_postgres_config_from_toml(path_to_config)
redis_config = load_redis_config_from_toml(path_to_config)

container = make_container(
ConfigsProvider(
Expand Down
18 changes: 12 additions & 6 deletions src/amdb/main/web_api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@
from dishka import make_async_container
from dishka.integrations.fastapi import setup_dishka

from amdb.infrastructure.persistence.sqlalchemy.config import PostgresConfig
from amdb.infrastructure.persistence.redis.config import RedisConfig
from amdb.infrastructure.auth.session.config import SessionConfig
from amdb.infrastructure.persistence.sqlalchemy.config import (
load_postgres_config_from_toml,
)
from amdb.infrastructure.persistence.redis.config import (
load_redis_config_from_toml,
)
from amdb.infrastructure.auth.session.config import (
load_session_config_from_toml,
)
from amdb.presentation.web_api.router import router
from amdb.presentation.web_api.exception_handlers import (
setup_exception_handlers,
Expand Down Expand Up @@ -39,9 +45,9 @@ def run_web_api() -> None:
message = "Path to config env var is not set"
raise ValueError(message)

postgres_config = PostgresConfig.from_toml(path_to_config)
redis_config = RedisConfig.from_toml(path_to_config)
session_config = SessionConfig.from_toml(path_to_config)
postgres_config = load_postgres_config_from_toml(path_to_config)
redis_config = load_redis_config_from_toml(path_to_config)
session_config = load_session_config_from_toml(path_to_config)

app = FastAPI(
title="Awesome Movie Database",
Expand Down
12 changes: 8 additions & 4 deletions src/amdb/main/worker/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@
from dishka import make_async_container
from dishka.integrations.faststream import setup_dishka

from amdb.infrastructure.persistence.sqlalchemy.config import PostgresConfig
from amdb.infrastructure.persistence.redis.config import RedisConfig
from amdb.infrastructure.persistence.sqlalchemy.config import (
load_postgres_config_from_toml,
)
from amdb.infrastructure.persistence.redis.config import (
load_redis_config_from_toml,
)
from amdb.presentation.worker.router import router
from amdb.main.providers import (
ConfigsProvider,
Expand Down Expand Up @@ -35,8 +39,8 @@ def run_worker() -> None:
message = "Path to config env var is not set"
raise ValueError(message)

postgres_config = PostgresConfig.from_toml(path_to_config)
redis_config = RedisConfig.from_toml(path_to_config)
postgres_config = load_postgres_config_from_toml(path_to_config)
redis_config = load_redis_config_from_toml(path_to_config)

broker = RedisBroker(url=redis_config.url)
broker.include_router(router)
Expand Down
12 changes: 8 additions & 4 deletions tests/unit/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,25 @@
import pytest
from redis.client import Redis

from amdb.infrastructure.persistence.sqlalchemy.config import PostgresConfig
from amdb.infrastructure.persistence.redis.config import RedisConfig
from amdb.infrastructure.persistence.sqlalchemy.config import (
load_postgres_config_from_toml,
)
from amdb.infrastructure.persistence.redis.config import (
load_redis_config_from_toml,
)


CONFIG_PATH = os.getenv("TEST_CONFIG_PATH")


@pytest.fixture(scope="session")
def postgres_url() -> str:
postgres_config = PostgresConfig.from_toml(CONFIG_PATH)
postgres_config = load_postgres_config_from_toml(CONFIG_PATH)
return postgres_config.url


@pytest.fixture(scope="session")
def redis() -> Redis:
redis_config = RedisConfig.from_toml(CONFIG_PATH)
redis_config = load_redis_config_from_toml(CONFIG_PATH)
redis = Redis.from_url(redis_config.url, decode_responses=True)
return cast(Redis, redis)

0 comments on commit cfbf99b

Please sign in to comment.