diff --git a/src/api/pyproject.toml b/src/api/pyproject.toml index aed813a..9c0b879 100644 --- a/src/api/pyproject.toml +++ b/src/api/pyproject.toml @@ -11,6 +11,7 @@ dependencies = [ "fastapi>=0.115.5", "ebooklib>=0.18", "python-dotenv>=1.0.1", + "pydantic-settings>=2.6.1", ] [tool.ruff.lint] diff --git a/src/api/src/create_book.py b/src/api/src/create_book.py index e4132ee..5e42436 100644 --- a/src/api/src/create_book.py +++ b/src/api/src/create_book.py @@ -1,41 +1,43 @@ from typing import Optional -from dotenv import load_dotenv - -load_dotenv() import re import unicodedata -from os import environ from enum import Enum import backoff +from dotenv import load_dotenv from ebooklib import epub from bs4 import BeautifulSoup -from pydantic import BaseModel, field_validator, model_validator +from pydantic import model_validator, field_validator +from pydantic_settings import BaseSettings from aiohttp import ClientResponseError, ClientSession from aiohttp_client_cache.session import CachedSession from aiohttp_client_cache import FileBackend, RedisBackend +load_dotenv(override=True) + class CacheTypes(Enum): file = "file" redis = "redis" -class Config(BaseModel): +class Config(BaseSettings): USE_CACHE: bool = True CACHE_TYPE: CacheTypes = CacheTypes.file REDIS_CONNECTION_URL: str = "" @field_validator("USE_CACHE", mode="before") def validate_use_cache(cls, value): - # Thanks https://stackoverflow.com/a/78157474 + # Return default if value is an empty string if value == "": - return cls.model_fields["USE_CACHE"].default + return True # Default value for USE_CACHE + return value @field_validator("CACHE_TYPE", mode="before") def validate_cache_type(cls, value): # Thanks https://stackoverflow.com/a/78157474 if value == "": - return cls.model_fields["CACHE_TYPE"].default + return "file" + return value @model_validator(mode="after") def prevent_mismatched_redis_url(self): @@ -53,7 +55,7 @@ class Config(BaseModel): return self -config = Config(**environ) # type: ignore +config = Config() headers = { "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36" diff --git a/src/api/uv.lock b/src/api/uv.lock index 156561f..85a9400 100644 --- a/src/api/uv.lock +++ b/src/api/uv.lock @@ -191,6 +191,7 @@ dependencies = [ { name = "aiohttp-client-cache", extra = ["all"] }, { name = "ebooklib" }, { name = "fastapi" }, + { name = "pydantic-settings" }, { name = "python-dotenv" }, { name = "rich" }, ] @@ -201,6 +202,7 @@ requires-dist = [ { name = "aiohttp-client-cache", extras = ["all"], specifier = ">=0.10.0" }, { name = "ebooklib", specifier = ">=0.18" }, { name = "fastapi", specifier = ">=0.115.5" }, + { name = "pydantic-settings", specifier = ">=2.6.1" }, { name = "python-dotenv", specifier = ">=1.0.1" }, { name = "rich", specifier = ">=13.9.4" }, ] @@ -616,6 +618,19 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/33/72/f881b5e18fbb67cf2fb4ab253660de3c6899dbb2dba409d0b757e3559e3d/pydantic_core-2.27.1-pp310-pypy310_pp73-win_amd64.whl", hash = "sha256:981fb88516bd1ae8b0cbbd2034678a39dedc98752f264ac9bc5839d3923fa04c", size = 2001864 }, ] +[[package]] +name = "pydantic-settings" +version = "2.6.1" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "pydantic" }, + { name = "python-dotenv" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/b5/d4/9dfbe238f45ad8b168f5c96ee49a3df0598ce18a0795a983b419949ce65b/pydantic_settings-2.6.1.tar.gz", hash = "sha256:e0f92546d8a9923cb8941689abf85d6601a8c19a23e97a34b2964a2e3f813ca0", size = 75646 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/5e/f9/ff95fd7d760af42f647ea87f9b8a383d891cdb5e5dbd4613edaeb094252a/pydantic_settings-2.6.1-py3-none-any.whl", hash = "sha256:7fb0637c786a558d3103436278a7c4f1cfd29ba8973238a50c5bb9a55387da87", size = 28595 }, +] + [[package]] name = "pygments" version = "2.18.0"