This repository has been archived on 2026-10-28. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
karldns.de/router_dyndns/schemas.py
T

62 lines
2.2 KiB
Python
Raw Normal View History

2026-05-20 23:55:27 +02:00
from pydantic import BaseModel, ConfigDict, Field
from .ddns_service import DdnsService
class ErrorResponse(BaseModel):
detail: str = Field(examples=["not found"])
class MagicHostnameRequest(BaseModel):
model_config = ConfigDict(extra="forbid")
username: str | None = Field(default=None, max_length=128, examples=["home-router"])
class HostnameCredentials(BaseModel):
hostname: str
username: str
password: str = Field(description="Secret router password. Store it now; it is not recoverable later.")
2026-05-21 10:05:40 +02:00
update_url: str = Field(description="Secret update URL for router DynDNS updates.")
2026-05-20 23:55:27 +02:00
credentialed_update_url: str = Field(
2026-05-21 10:05:40 +02:00
description="Secret full update URL with router username and password included."
2026-05-20 23:55:27 +02:00
)
2026-05-21 10:05:40 +02:00
management_url: str = Field(description="Secret dashboard URL for reading or deleting this hostname.")
2026-05-20 23:55:27 +02:00
class ManagedRecord(BaseModel):
hostname: str
username: str
2026-05-21 10:05:40 +02:00
update_url: str = Field(description="Secret update URL for router DynDNS updates.")
2026-05-20 23:55:27 +02:00
ipv4: str | None = None
ipv6: str | None = None
updated_at: str | None = None
class RouterUpdateResponse(BaseModel):
status: str = Field(examples=["good"])
hostname: str
ip: str
def _credentials_response(service: DdnsService, account: dict[str, str]) -> HostnameCredentials:
return HostnameCredentials(
hostname=account["hostname"],
username=account["username"],
password=account["password"],
update_url=service.fritz_update_url(account),
credentialed_update_url=service.credentialed_update_url(account),
management_url=service.magic_management_url(account),
)
def _managed_record(service: DdnsService, account: dict[str, str | int | None]) -> ManagedRecord:
account_for_url = {"update_slug": str(account["update_slug"])}
return ManagedRecord(
hostname=str(account["hostname"]),
username=str(account["username"]),
update_url=service.fritz_update_url(account_for_url),
ipv4=str(account["ipv4"]) if account.get("ipv4") else None,
ipv6=str(account["ipv6"]) if account.get("ipv6") else None,
updated_at=str(account["updated_at"]) if account.get("updated_at") else None,
)