add default region env variable

This commit is contained in:
Mark
2025-07-18 09:13:55 +02:00
parent ef02295625
commit 8a454d702c
6 changed files with 18 additions and 5 deletions
+1
View File
@@ -3,3 +3,4 @@ ABR_APP__DEBUG=true # Default: false
ABR_APP__OPENAPI_ENABLED=true # Default: false
ABR_APP__LOG_LEVEL=DEBUG
ABR_APP__BASE_URL=
ABR_APP__DEFAULT_REGION=de # Audible region. Default: us
+1
View File
@@ -94,6 +94,7 @@ In the case of an OIDC misconfiguration, i.e. changing a setting like your clien
| `ABR_APP__LOG_LEVEL` | One of `DEBUG`, `INFO`, `WARN`, `ERROR`. | INFO |
| `ABR_APP__BASE_URL` | Defines the base url the website is hosted at. If the website is accessed at `example.org/abr/`, set the base URL to `/abr/` | |
| `ABR_DB__SQLITE_PATH` | If relative, path and name of the sqlite database in relation to `ABR_APP__CONFIG_DIR`. If absolute (path starts with `/`), the config dir is ignored and only the absolute path is used. | db.sqlite |
| `ABR_APP__DEFAULT_REGION` | Default audible region to use for the search. Has to be one of `us, ca, uk, au, fr, de, jp, it, in, es, br`. | us |
---
+11 -3
View File
@@ -8,6 +8,7 @@ import pydantic
from aiohttp import ClientSession
from sqlmodel import Session, col, select
from app.internal.env_settings import Settings
from app.internal.models import BookRequest
from app.util.log import logger
@@ -41,6 +42,13 @@ audible_regions: dict[audible_region_type, str] = {
}
def get_region_from_settings() -> audible_region_type:
region = Settings().app.default_region
if region not in audible_regions:
return "us"
return region
async def _get_audnexus_book(
session: ClientSession,
asin: str,
@@ -108,7 +116,7 @@ async def _get_audimeta_book(
async def get_book_by_asin(
session: ClientSession,
asin: str,
audible_region: audible_region_type = "us",
audible_region: audible_region_type = get_region_from_settings(),
) -> Optional[BookRequest]:
book = await _get_audimeta_book(session, asin, audible_region)
if book:
@@ -139,7 +147,7 @@ search_suggestions_cache: dict[str, CacheResult[list[str]]] = {}
async def get_search_suggestions(
client_session: ClientSession,
query: str,
audible_region: audible_region_type = "us",
audible_region: audible_region_type = get_region_from_settings(),
) -> list[str]:
cache_result = search_suggestions_cache.get(query)
if cache_result and time.time() - cache_result.timestamp < REFETCH_TTL:
@@ -182,7 +190,7 @@ async def list_audible_books(
query: str,
num_results: int = 20,
page: int = 0,
audible_region: audible_region_type = "us",
audible_region: audible_region_type = get_region_from_settings(),
) -> list[BookRequest]:
"""
https://audible.readthedocs.io/en/latest/misc/external_api.html#get--1.0-catalog-products
+1
View File
@@ -16,6 +16,7 @@ class ApplicationSettings(BaseModel):
version: str = "local"
log_level: str = "INFO"
base_url: str = ""
default_region: str = "us"
class Settings(BaseSettings):
+3 -2
View File
@@ -18,6 +18,7 @@ from app.internal.book_search import (
audible_region_type,
audible_regions,
get_book_by_asin,
get_region_from_settings,
list_audible_books,
)
from app.internal.models import (
@@ -74,7 +75,7 @@ async def read_search(
query: Annotated[Optional[str], Query(alias="q")] = None,
num_results: int = 20,
page: int = 0,
region: audible_region_type = "us",
region: audible_region_type = get_region_from_settings(),
):
if audible_regions.get(region) is None:
raise HTTPException(status_code=400, detail="Invalid region")
@@ -118,7 +119,7 @@ async def search_suggestions(
request: Request,
user: Annotated[DetailedUser, Depends(get_authenticated_user())],
query: Annotated[str, Query(alias="q")],
region: audible_region_type = "us",
region: audible_region_type = get_region_from_settings(),
):
async with ClientSession() as client_session:
suggestions = await book_search.get_search_suggestions(
@@ -14,6 +14,7 @@ date: 2025-06-09T13:46:33+02:00
| `ABR_APP__LOG_LEVEL` | One of `DEBUG`, `INFO`, `WARN`, `ERROR`. | INFO |
| `ABR_APP__BASE_URL` | Defines the base url the website is hosted at. If the website is accessed at `example.org/abr/`, set the base URL to `/abr/` | |
| `ABR_DB__SQLITE_PATH` | If relative, path and name of the sqlite database in relation to `ABR_APP__CONFIG_DIR`. If absolute (path starts with `/`), the config dir is ignored and only the absolute path is used. | db.sqlite |
| `ABR_APP__DEFAULT_REGION` | Default audible region to use for the search. Has to be one of `us, ca, uk, au, fr, de, jp, it, in, es, br`. | us |
{{< alert title="Note" >}} There are two underscores (`__`) between the first
and second part of each environment variable like between `ABR_APP` and `PORT`.