Archived
Add formatting and type checks
This commit is contained in:
1 parent
c0d82eaf26
commit
24747866bc
38 files changed
+800
-265
No files matched your search
+71
-9
@@ -1,3 +1,5 @@
|
||||
from collections.abc import Mapping, Sequence
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from .services.ddns import DdnsService
|
||||
@@ -12,7 +14,9 @@ class HostnameCreateResponse(BaseModel):
|
||||
status: str = Field(description="Lifecycle state. Newly generated hostnames start as pending until registered.")
|
||||
update_url: str = Field(description="Secret update URL for router DynDNS updates.")
|
||||
dashboard_url: str = Field(description="Secret dashboard URL for reading, rotating, or deleting this hostname.")
|
||||
registration_url: str = Field(description="Secret URL used to register the pending hostname after secrets are saved.")
|
||||
registration_url: str = Field(
|
||||
description="Secret URL used to register the pending hostname after secrets are saved."
|
||||
)
|
||||
|
||||
|
||||
class HostnameStatusResponse(BaseModel):
|
||||
@@ -134,9 +138,9 @@ class AdminEventsResponse(BaseModel):
|
||||
total: int
|
||||
|
||||
|
||||
def _create_response(service: DdnsService, account: dict[str, str]) -> HostnameCreateResponse:
|
||||
def _create_response(service: DdnsService, account: Mapping[str, object]) -> HostnameCreateResponse:
|
||||
return HostnameCreateResponse(
|
||||
hostname=account["hostname"],
|
||||
hostname=str(account["hostname"]),
|
||||
status="active" if account.get("accepted_at") else "pending",
|
||||
update_url=service.fritz_update_url(account),
|
||||
dashboard_url=service.dashboard_url(account),
|
||||
@@ -146,8 +150,8 @@ def _create_response(service: DdnsService, account: dict[str, str]) -> HostnameC
|
||||
|
||||
def _hostname_status(
|
||||
service: DdnsService,
|
||||
account: dict[str, str | int | None],
|
||||
history: list[dict[str, str | int | None]] | None = None,
|
||||
account: Mapping[str, object],
|
||||
history: Sequence[Mapping[str, object]] | None = None,
|
||||
) -> HostnameStatusResponse:
|
||||
account_for_url = {"update_slug": str(account["update_slug"])}
|
||||
account_for_dashboard = {"management_slug": str(account.get("management_slug", ""))}
|
||||
@@ -163,7 +167,9 @@ def _hostname_status(
|
||||
)
|
||||
|
||||
|
||||
def _hostname_rotate_update_response(service: DdnsService, account: dict[str, str | int | None], dashboard_secret: str) -> HostnameRotateUpdateResponse:
|
||||
def _hostname_rotate_update_response(
|
||||
service: DdnsService, account: Mapping[str, object], dashboard_secret: str
|
||||
) -> HostnameRotateUpdateResponse:
|
||||
return HostnameRotateUpdateResponse(
|
||||
hostname=str(account["hostname"]),
|
||||
status="active" if account.get("accepted_at") else "pending",
|
||||
@@ -172,7 +178,7 @@ def _hostname_rotate_update_response(service: DdnsService, account: dict[str, st
|
||||
)
|
||||
|
||||
|
||||
def _hostname_update_event(row: dict[str, str | int | None]) -> HostnameUpdateEvent:
|
||||
def _hostname_update_event(row: Mapping[str, object]) -> HostnameUpdateEvent:
|
||||
return HostnameUpdateEvent(
|
||||
hostname=str(row["hostname"]),
|
||||
ipv4=str(row["ipv4"]) if row.get("ipv4") else None,
|
||||
@@ -180,11 +186,11 @@ def _hostname_update_event(row: dict[str, str | int | None]) -> HostnameUpdateEv
|
||||
status=str(row["status"]),
|
||||
detail=str(row["detail"]),
|
||||
created_at=str(row["created_at"]),
|
||||
count=int(row.get("count") or 1),
|
||||
count=_int_value(row.get("count"), 1),
|
||||
)
|
||||
|
||||
|
||||
def _admin_hostname(account: dict[str, str | int | None]) -> AdminHostname:
|
||||
def _admin_hostname(account: Mapping[str, object]) -> AdminHostname:
|
||||
return AdminHostname(
|
||||
hostname=str(account["hostname"]),
|
||||
status="disabled" if account.get("disabled") else ("active" if account.get("accepted_at") else "pending"),
|
||||
@@ -201,3 +207,59 @@ def _admin_metrics(metrics: dict[str, float]) -> AdminMetrics:
|
||||
samples = metrics.get("update_latency_samples_total", 0)
|
||||
average_latency = metrics.get("update_latency_ms_total", 0) / samples if samples else 0
|
||||
return AdminMetrics(values=metrics, average_update_latency_ms=average_latency)
|
||||
|
||||
|
||||
def _admin_record(row: Mapping[str, object]) -> AdminRecord:
|
||||
return AdminRecord(
|
||||
hostname=str(row["hostname"]),
|
||||
ipv4=str(row["ipv4"]) if row.get("ipv4") else None,
|
||||
ipv6=str(row["ipv6"]) if row.get("ipv6") else None,
|
||||
updated_at=str(row["updated_at"]),
|
||||
)
|
||||
|
||||
|
||||
def _admin_update_event(row: Mapping[str, object]) -> AdminUpdateEvent:
|
||||
return AdminUpdateEvent(
|
||||
hostname=str(row["hostname"]),
|
||||
ipv4=str(row["ipv4"]) if row.get("ipv4") else None,
|
||||
ipv6=str(row["ipv6"]) if row.get("ipv6") else None,
|
||||
status=str(row["status"]),
|
||||
detail=str(row["detail"]),
|
||||
source_ip=str(row["source_ip"]),
|
||||
created_at=str(row["created_at"]),
|
||||
count=_int_value(row.get("count"), 1),
|
||||
)
|
||||
|
||||
|
||||
def _admin_cleanup_run(row: Mapping[str, object]) -> AdminCleanupRun:
|
||||
return AdminCleanupRun(
|
||||
created_at=str(row["created_at"]),
|
||||
unused_accounts_deleted=_int_value(row["unused_accounts_deleted"], 0),
|
||||
)
|
||||
|
||||
|
||||
def _admin_scheduled_task(row: Mapping[str, object]) -> AdminScheduledTask:
|
||||
return AdminScheduledTask(
|
||||
name=str(row["name"]),
|
||||
interval_seconds=_int_value(row["interval_seconds"], 0),
|
||||
next_run_at=str(row["next_run_at"]),
|
||||
lease_owner=str(row["lease_owner"]) if row.get("lease_owner") else None,
|
||||
lease_expires_at=str(row["lease_expires_at"]) if row.get("lease_expires_at") else None,
|
||||
last_started_at=str(row["last_started_at"]) if row.get("last_started_at") else None,
|
||||
last_finished_at=str(row["last_finished_at"]) if row.get("last_finished_at") else None,
|
||||
last_status=str(row["last_status"]),
|
||||
last_error=str(row["last_error"]),
|
||||
updated_at=str(row["updated_at"]),
|
||||
)
|
||||
|
||||
|
||||
def _int_value(value: object, default: int) -> int:
|
||||
if value is None:
|
||||
return default
|
||||
if isinstance(value, int):
|
||||
return value
|
||||
if isinstance(value, float):
|
||||
return int(value)
|
||||
if isinstance(value, str):
|
||||
return int(value)
|
||||
return default
|
||||
Reference in new issue
Block a user