diff options
| author | Dennis Fink | 2026-11-13 20:23:13 +0100 |
|---|---|---|
| committer | Dennis Fink | 2026-11-13 20:23:13 +0100 |
| commit | 8b6326f070c941510ebf1d27e53f501610406ea4 (patch) | |
| tree | 1db02dbc58207e3ef557e8a6f6d7020363ef3c7b | |
| parent | 1f784f2b87ed0b2fd478e46d0d292c33f3150a3b (diff) | |
| download | webmentions-ssg-8b6326f070c941510ebf1d27e53f501610406ea4.tar.gz webmentions-ssg-8b6326f070c941510ebf1d27e53f501610406ea4.zip | |
refactor(core): replace HTTP status magic numbers
Use `HTTPStatus` constants for standard response codes and a named upper
bound for retryable server errors so network and task status handling is
self-documenting.
| -rw-r--r-- | webmentions_ssg/net.py | 12 | ||||
| -rw-r--r-- | webmentions_ssg/tasks/discovery.py | 3 | ||||
| -rw-r--r-- | webmentions_ssg/tasks/receiver.py | 5 |
3 files changed, 16 insertions, 4 deletions
diff --git a/webmentions_ssg/net.py b/webmentions_ssg/net.py index 0314810..7c29edc 100644 --- a/webmentions_ssg/net.py +++ b/webmentions_ssg/net.py @@ -9,6 +9,7 @@ import socket import time from collections.abc import Iterable from contextlib import ExitStack +from http import HTTPStatus from urllib.parse import urlsplit import dns.exception @@ -20,6 +21,7 @@ from httpcore._backends.sync import SyncStream NAT64_PREFIX = ipaddress.IPv6Network("64:ff9b::/96") IPV4_COMPATIBLE_PREFIX = ipaddress.IPv6Network("::/96") +NETWORK_CONNECT_TIMOUT_ERROR = 599 class AddressResolutionError(Exception): @@ -285,4 +287,12 @@ def read_limited_body(response: httpx.Response, max_bytes: int, *, label: str) - def is_temporary_status(status: int) -> bool: """Return whether an HTTP status is normally retryable.""" - return status in {408, 425, 429} or 500 <= status <= 599 + return ( + status + in { + HTTPStatus.REQUEST_TIMEOUT, + HTTPStatus.TOO_EARLY, + HTTPStatus.TOO_MANY_REQUESTS, + } + or HTTPStatus.INTERNAL_SERVER_ERROR <= status <= NETWORK_CONNECT_TIMOUT_ERROR + ) diff --git a/webmentions_ssg/tasks/discovery.py b/webmentions_ssg/tasks/discovery.py index 456e47e..107d30a 100644 --- a/webmentions_ssg/tasks/discovery.py +++ b/webmentions_ssg/tasks/discovery.py @@ -5,6 +5,7 @@ """Discover a target Webmention endpoint and submit a POST.""" import re +from http import HTTPStatus from urllib.parse import urljoin import httpx @@ -198,7 +199,7 @@ def post_webmention( ) as response: status_url = None - if response.status_code == 201 and ( + if response.status_code == HTTPStatus.CREATED and ( location := response.headers.get("Location") ): try: diff --git a/webmentions_ssg/tasks/receiver.py b/webmentions_ssg/tasks/receiver.py index cbb42b6..78089af 100644 --- a/webmentions_ssg/tasks/receiver.py +++ b/webmentions_ssg/tasks/receiver.py @@ -6,6 +6,7 @@ import uuid from datetime import UTC, datetime, timedelta +from http import HTTPStatus from typing import Any, cast import httpx @@ -62,9 +63,9 @@ def fetch_source(source_url: str) -> tuple[httpx.Response, bytes]: ) as client, client.stream("GET", source_url) as response, ): - if response.status_code == 200: + if response.status_code == HTTPStatus.OK: pass - elif response.status_code == 410: + elif response.status_code == HTTPStatus.GONE: raise SourceGoneError("Source returned HTTP 410") elif is_temporary_status(response.status_code): raise TemporaryFetchError( |
