diff options
| -rw-r--r-- | thallium-backend/src/app.py | 3 | ||||
| -rw-r--r-- | thallium-backend/src/routes/__init__.py | 8 | ||||
| -rw-r--r-- | thallium-backend/src/routes/debug.py | 29 | ||||
| -rw-r--r-- | thallium-backend/src/settings.py | 15 |
4 files changed, 53 insertions, 2 deletions
diff --git a/thallium-backend/src/app.py b/thallium-backend/src/app.py index 27ec8ab..5907348 100644 --- a/thallium-backend/src/app.py +++ b/thallium-backend/src/app.py @@ -4,12 +4,13 @@ from fastapi import FastAPI, Request from fastapi.exceptions import RequestValidationError from fastapi.responses import JSONResponse +from src.routes import top_level_router from src.settings import CONFIG log = logging.getLogger(__name__) - fastapi_app = FastAPI(debug=CONFIG.debug) +fastapi_app.include_router(top_level_router) @fastapi_app.get("/heartbeat") diff --git a/thallium-backend/src/routes/__init__.py b/thallium-backend/src/routes/__init__.py new file mode 100644 index 0000000..b321e52 --- /dev/null +++ b/thallium-backend/src/routes/__init__.py @@ -0,0 +1,8 @@ +from fastapi import APIRouter + +from src.routes.debug import router as debug_router +from src.settings import CONFIG + +top_level_router = APIRouter(prefix="/api") +if CONFIG.debug: + top_level_router.include_router(debug_router) diff --git a/thallium-backend/src/routes/debug.py b/thallium-backend/src/routes/debug.py new file mode 100644 index 0000000..7fe42da --- /dev/null +++ b/thallium-backend/src/routes/debug.py @@ -0,0 +1,29 @@ +import logging + +from fastapi import APIRouter + +from src.settings import PrintfulClient + +router = APIRouter(tags=["debug"]) +log = logging.getLogger(__name__) + + [email protected]("/templates") +async def get_templates(client: PrintfulClient) -> dict: + """Return all templates in printful.""" + resp = await client.get("/product-templates") + return resp.json() + + [email protected]("/oauth-scopes-v1") +async def get_oauth_scopes(client: PrintfulClient) -> dict: + """Return all templates in printful.""" + resp = await client.get("/oauth/scopes") + return resp.json() + + [email protected]("/oauth-scopes-v2") +async def get_v2_oauth_scopes(client: PrintfulClient) -> dict: + """Return all templates in printful.""" + resp = await client.get("/v2/oauth-scopes") + return resp.json() diff --git a/thallium-backend/src/settings.py b/thallium-backend/src/settings.py index 931fc0d..db292ba 100644 --- a/thallium-backend/src/settings.py +++ b/thallium-backend/src/settings.py @@ -2,6 +2,7 @@ import typing from collections.abc import AsyncGenerator from logging import getLogger +import httpx import pydantic import pydantic_settings from fastapi import Depends @@ -24,7 +25,8 @@ class _Config( git_sha: str = "development" database_url: pydantic.SecretStr - token: pydantic.SecretStr + super_admin_token: pydantic.SecretStr + printful_token: pydantic.SecretStr CONFIG = _Config() @@ -37,6 +39,16 @@ class Connections: DB_SESSION_MAKER = async_sessionmaker(DB_ENGINE) +async def _get_printful_client() -> AsyncGenerator[httpx.AsyncClient, None]: + """Yield an authenticated httpx client for printful, for use with a FastAPI dependency.""" + client = httpx.AsyncClient( + headers={"Authorization": f"Bearer {CONFIG.printful_token.get_secret_value()}"}, + base_url="https://api.printful.com", + ) + async with client as c: + yield c + + async def _get_db_session() -> AsyncGenerator[AsyncSession, None]: """Yield a database session, for use with a FastAPI dependency.""" async with Connections.DB_SESSION_MAKER() as session, session.begin(): @@ -44,3 +56,4 @@ async def _get_db_session() -> AsyncGenerator[AsyncSession, None]: DBSession = typing.Annotated[AsyncSession, Depends(_get_db_session)] +PrintfulClient = typing.Annotated[httpx.AsyncClient, Depends(_get_printful_client)] |