Template
Add campaign UTM links and piha.li shortener as a core helper.
Keep mint/UTM in always-on core so email_sms and directmail stay optional, and brand utm_source from SITE_NAME (or UTM_SOURCE) instead of a hardcoded client name. Closes #3
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
"""Server-to-server client for url_shortening_service (piha.li).
|
||||
|
||||
Call the API host (`SHORTENER_BASE_URL`), never the public short domain.
|
||||
Auth: Authorization: Bearer <SHORTENER_API_TOKEN> (name:secret).
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
|
||||
import requests
|
||||
from django.conf import settings
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def is_configured() -> bool:
|
||||
base = (getattr(settings, "SHORTENER_BASE_URL", None) or "").strip()
|
||||
token = (getattr(settings, "SHORTENER_API_TOKEN", None) or "").strip()
|
||||
return bool(base and token)
|
||||
|
||||
|
||||
def mint_short_url(
|
||||
target_url: str,
|
||||
*,
|
||||
title: str = "",
|
||||
external_ref: str = "",
|
||||
) -> str | None:
|
||||
"""POST /api/links/. Return short_url, or None if unconfigured / failed.
|
||||
|
||||
Does not raise. Compose must still work when the shortener is down or unset.
|
||||
"""
|
||||
if not is_configured():
|
||||
return None
|
||||
target = (target_url or "").strip()
|
||||
if not target.lower().startswith("https://"):
|
||||
logger.info("shortener skip: target is not https")
|
||||
return None
|
||||
|
||||
base = str(settings.SHORTENER_BASE_URL).rstrip("/")
|
||||
token = str(settings.SHORTENER_API_TOKEN).strip()
|
||||
timeout = int(getattr(settings, "SHORTENER_TIMEOUT_SECONDS", 10) or 10)
|
||||
try:
|
||||
response = requests.post(
|
||||
f"{base}/api/links/",
|
||||
headers={
|
||||
"Authorization": f"Bearer {token}",
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
json={
|
||||
"target_url": target,
|
||||
"title": (title or "")[:200],
|
||||
"external_ref": (external_ref or "")[:64],
|
||||
},
|
||||
timeout=timeout,
|
||||
)
|
||||
except requests.RequestException:
|
||||
logger.exception("shortener request failed")
|
||||
return None
|
||||
|
||||
if response.status_code not in (200, 201):
|
||||
logger.warning(
|
||||
"shortener mint failed status=%s",
|
||||
response.status_code,
|
||||
)
|
||||
return None
|
||||
try:
|
||||
data = response.json()
|
||||
except ValueError:
|
||||
logger.warning("shortener mint returned non-json")
|
||||
return None
|
||||
if not isinstance(data, dict):
|
||||
return None
|
||||
short = (data.get("short_url") or "").strip()
|
||||
return short or None
|
||||
|
||||
|
||||
def resolve_display_url(
|
||||
target_url: str,
|
||||
*,
|
||||
title: str = "",
|
||||
external_ref: str = "",
|
||||
) -> str:
|
||||
"""Short URL when mint succeeds, otherwise the original target."""
|
||||
return mint_short_url(
|
||||
target_url, title=title, external_ref=external_ref
|
||||
) or target_url
|
||||
Reference in New Issue
Block a user