fix(api): Pydantic-settings for model-based env loading
This commit is contained in:
@@ -11,6 +11,7 @@ dependencies = [
|
|||||||
"fastapi>=0.115.5",
|
"fastapi>=0.115.5",
|
||||||
"ebooklib>=0.18",
|
"ebooklib>=0.18",
|
||||||
"python-dotenv>=1.0.1",
|
"python-dotenv>=1.0.1",
|
||||||
|
"pydantic-settings>=2.6.1",
|
||||||
]
|
]
|
||||||
|
|
||||||
[tool.ruff.lint]
|
[tool.ruff.lint]
|
||||||
|
|||||||
+12
-10
@@ -1,41 +1,43 @@
|
|||||||
from typing import Optional
|
from typing import Optional
|
||||||
from dotenv import load_dotenv
|
|
||||||
|
|
||||||
load_dotenv()
|
|
||||||
import re
|
import re
|
||||||
import unicodedata
|
import unicodedata
|
||||||
from os import environ
|
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
import backoff
|
import backoff
|
||||||
|
from dotenv import load_dotenv
|
||||||
from ebooklib import epub
|
from ebooklib import epub
|
||||||
from bs4 import BeautifulSoup
|
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 import ClientResponseError, ClientSession
|
||||||
from aiohttp_client_cache.session import CachedSession
|
from aiohttp_client_cache.session import CachedSession
|
||||||
from aiohttp_client_cache import FileBackend, RedisBackend
|
from aiohttp_client_cache import FileBackend, RedisBackend
|
||||||
|
|
||||||
|
load_dotenv(override=True)
|
||||||
|
|
||||||
|
|
||||||
class CacheTypes(Enum):
|
class CacheTypes(Enum):
|
||||||
file = "file"
|
file = "file"
|
||||||
redis = "redis"
|
redis = "redis"
|
||||||
|
|
||||||
|
|
||||||
class Config(BaseModel):
|
class Config(BaseSettings):
|
||||||
USE_CACHE: bool = True
|
USE_CACHE: bool = True
|
||||||
CACHE_TYPE: CacheTypes = CacheTypes.file
|
CACHE_TYPE: CacheTypes = CacheTypes.file
|
||||||
REDIS_CONNECTION_URL: str = ""
|
REDIS_CONNECTION_URL: str = ""
|
||||||
|
|
||||||
@field_validator("USE_CACHE", mode="before")
|
@field_validator("USE_CACHE", mode="before")
|
||||||
def validate_use_cache(cls, value):
|
def validate_use_cache(cls, value):
|
||||||
# Thanks https://stackoverflow.com/a/78157474
|
# Return default if value is an empty string
|
||||||
if value == "":
|
if value == "":
|
||||||
return cls.model_fields["USE_CACHE"].default
|
return True # Default value for USE_CACHE
|
||||||
|
return value
|
||||||
|
|
||||||
@field_validator("CACHE_TYPE", mode="before")
|
@field_validator("CACHE_TYPE", mode="before")
|
||||||
def validate_cache_type(cls, value):
|
def validate_cache_type(cls, value):
|
||||||
# Thanks https://stackoverflow.com/a/78157474
|
# Thanks https://stackoverflow.com/a/78157474
|
||||||
if value == "":
|
if value == "":
|
||||||
return cls.model_fields["CACHE_TYPE"].default
|
return "file"
|
||||||
|
return value
|
||||||
|
|
||||||
@model_validator(mode="after")
|
@model_validator(mode="after")
|
||||||
def prevent_mismatched_redis_url(self):
|
def prevent_mismatched_redis_url(self):
|
||||||
@@ -53,7 +55,7 @@ class Config(BaseModel):
|
|||||||
return self
|
return self
|
||||||
|
|
||||||
|
|
||||||
config = Config(**environ) # type: ignore
|
config = Config()
|
||||||
|
|
||||||
headers = {
|
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"
|
"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"
|
||||||
|
|||||||
Generated
+15
@@ -191,6 +191,7 @@ dependencies = [
|
|||||||
{ name = "aiohttp-client-cache", extra = ["all"] },
|
{ name = "aiohttp-client-cache", extra = ["all"] },
|
||||||
{ name = "ebooklib" },
|
{ name = "ebooklib" },
|
||||||
{ name = "fastapi" },
|
{ name = "fastapi" },
|
||||||
|
{ name = "pydantic-settings" },
|
||||||
{ name = "python-dotenv" },
|
{ name = "python-dotenv" },
|
||||||
{ name = "rich" },
|
{ name = "rich" },
|
||||||
]
|
]
|
||||||
@@ -201,6 +202,7 @@ requires-dist = [
|
|||||||
{ name = "aiohttp-client-cache", extras = ["all"], specifier = ">=0.10.0" },
|
{ name = "aiohttp-client-cache", extras = ["all"], specifier = ">=0.10.0" },
|
||||||
{ name = "ebooklib", specifier = ">=0.18" },
|
{ name = "ebooklib", specifier = ">=0.18" },
|
||||||
{ name = "fastapi", specifier = ">=0.115.5" },
|
{ name = "fastapi", specifier = ">=0.115.5" },
|
||||||
|
{ name = "pydantic-settings", specifier = ">=2.6.1" },
|
||||||
{ name = "python-dotenv", specifier = ">=1.0.1" },
|
{ name = "python-dotenv", specifier = ">=1.0.1" },
|
||||||
{ name = "rich", specifier = ">=13.9.4" },
|
{ 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 },
|
{ 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]]
|
[[package]]
|
||||||
name = "pygments"
|
name = "pygments"
|
||||||
version = "2.18.0"
|
version = "2.18.0"
|
||||||
|
|||||||
Reference in New Issue
Block a user