From 752cb31482e8a8fc71d21dc48f51cb2231ca2257 Mon Sep 17 00:00:00 2001 From: NEFORCEO Date: Tue, 31 Mar 2026 22:08:41 +0700 Subject: [PATCH 1/4] feat: create status.py --- fasthttp/status.py | 177 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 177 insertions(+) create mode 100644 fasthttp/status.py diff --git a/fasthttp/status.py b/fasthttp/status.py new file mode 100644 index 0000000..26fc02e --- /dev/null +++ b/fasthttp/status.py @@ -0,0 +1,177 @@ +""" +HTTP Status Codes +Based on IANA + RFC 9110 +""" + +from __future__ import annotations + +import warnings + +__all__ = [ + # 1xx + "HTTP_100_CONTINUE", + "HTTP_101_SWITCHING_PROTOCOLS", + "HTTP_102_PROCESSING", + "HTTP_103_EARLY_HINTS", + + # 2xx + "HTTP_200_OK", + "HTTP_201_CREATED", + "HTTP_202_ACCEPTED", + "HTTP_203_NON_AUTHORITATIVE_INFORMATION", + "HTTP_204_NO_CONTENT", + "HTTP_205_RESET_CONTENT", + "HTTP_206_PARTIAL_CONTENT", + "HTTP_207_MULTI_STATUS", + "HTTP_208_ALREADY_REPORTED", + "HTTP_226_IM_USED", + + # 3xx + "HTTP_300_MULTIPLE_CHOICES", + "HTTP_301_MOVED_PERMANENTLY", + "HTTP_302_FOUND", + "HTTP_303_SEE_OTHER", + "HTTP_304_NOT_MODIFIED", + "HTTP_307_TEMPORARY_REDIRECT", + "HTTP_308_PERMANENT_REDIRECT", + + # 4xx + "HTTP_400_BAD_REQUEST", + "HTTP_401_UNAUTHORIZED", + "HTTP_402_PAYMENT_REQUIRED", + "HTTP_403_FORBIDDEN", + "HTTP_404_NOT_FOUND", + "HTTP_405_METHOD_NOT_ALLOWED", + "HTTP_406_NOT_ACCEPTABLE", + "HTTP_407_PROXY_AUTHENTICATION_REQUIRED", + "HTTP_408_REQUEST_TIMEOUT", + "HTTP_409_CONFLICT", + "HTTP_410_GONE", + "HTTP_411_LENGTH_REQUIRED", + "HTTP_412_PRECONDITION_FAILED", + "HTTP_413_CONTENT_TOO_LARGE", + "HTTP_414_URI_TOO_LONG", + "HTTP_415_UNSUPPORTED_MEDIA_TYPE", + "HTTP_416_RANGE_NOT_SATISFIABLE", + "HTTP_417_EXPECTATION_FAILED", + "HTTP_418_IM_A_TEAPOT", + "HTTP_421_MISDIRECTED_REQUEST", + "HTTP_422_UNPROCESSABLE_CONTENT", + "HTTP_423_LOCKED", + "HTTP_424_FAILED_DEPENDENCY", + "HTTP_425_TOO_EARLY", + "HTTP_426_UPGRADE_REQUIRED", + "HTTP_428_PRECONDITION_REQUIRED", + "HTTP_429_TOO_MANY_REQUESTS", + "HTTP_431_REQUEST_HEADER_FIELDS_TOO_LARGE", + "HTTP_451_UNAVAILABLE_FOR_LEGAL_REASONS", + + # 5xx + "HTTP_500_INTERNAL_SERVER_ERROR", + "HTTP_501_NOT_IMPLEMENTED", + "HTTP_502_BAD_GATEWAY", + "HTTP_503_SERVICE_UNAVAILABLE", + "HTTP_504_GATEWAY_TIMEOUT", + "HTTP_505_HTTP_VERSION_NOT_SUPPORTED", + "HTTP_506_VARIANT_ALSO_NEGOTIATES", + "HTTP_507_INSUFFICIENT_STORAGE", + "HTTP_508_LOOP_DETECTED", + "HTTP_510_NOT_EXTENDED", + "HTTP_511_NETWORK_AUTHENTICATION_REQUIRED", +] + +# 1xx +HTTP_100_CONTINUE = 100 +HTTP_101_SWITCHING_PROTOCOLS = 101 +HTTP_102_PROCESSING = 102 +HTTP_103_EARLY_HINTS = 103 + +# 2xx +HTTP_200_OK = 200 +HTTP_201_CREATED = 201 +HTTP_202_ACCEPTED = 202 +HTTP_203_NON_AUTHORITATIVE_INFORMATION = 203 +HTTP_204_NO_CONTENT = 204 +HTTP_205_RESET_CONTENT = 205 +HTTP_206_PARTIAL_CONTENT = 206 +HTTP_207_MULTI_STATUS = 207 +HTTP_208_ALREADY_REPORTED = 208 +HTTP_226_IM_USED = 226 + +# 3xx +HTTP_300_MULTIPLE_CHOICES = 300 +HTTP_301_MOVED_PERMANENTLY = 301 +HTTP_302_FOUND = 302 +HTTP_303_SEE_OTHER = 303 +HTTP_304_NOT_MODIFIED = 304 +HTTP_307_TEMPORARY_REDIRECT = 307 +HTTP_308_PERMANENT_REDIRECT = 308 + +# 4xx +HTTP_400_BAD_REQUEST = 400 +HTTP_401_UNAUTHORIZED = 401 +HTTP_402_PAYMENT_REQUIRED = 402 +HTTP_403_FORBIDDEN = 403 +HTTP_404_NOT_FOUND = 404 +HTTP_405_METHOD_NOT_ALLOWED = 405 +HTTP_406_NOT_ACCEPTABLE = 406 +HTTP_407_PROXY_AUTHENTICATION_REQUIRED = 407 +HTTP_408_REQUEST_TIMEOUT = 408 +HTTP_409_CONFLICT = 409 +HTTP_410_GONE = 410 +HTTP_411_LENGTH_REQUIRED = 411 +HTTP_412_PRECONDITION_FAILED = 412 +HTTP_413_CONTENT_TOO_LARGE = 413 +HTTP_414_URI_TOO_LONG = 414 +HTTP_415_UNSUPPORTED_MEDIA_TYPE = 415 +HTTP_416_RANGE_NOT_SATISFIABLE = 416 +HTTP_417_EXPECTATION_FAILED = 417 +HTTP_418_IM_A_TEAPOT = 418 +HTTP_421_MISDIRECTED_REQUEST = 421 +HTTP_422_UNPROCESSABLE_CONTENT = 422 +HTTP_423_LOCKED = 423 +HTTP_424_FAILED_DEPENDENCY = 424 +HTTP_425_TOO_EARLY = 425 +HTTP_426_UPGRADE_REQUIRED = 426 +HTTP_428_PRECONDITION_REQUIRED = 428 +HTTP_429_TOO_MANY_REQUESTS = 429 +HTTP_431_REQUEST_HEADER_FIELDS_TOO_LARGE = 431 +HTTP_451_UNAVAILABLE_FOR_LEGAL_REASONS = 451 + +# 5xx +HTTP_500_INTERNAL_SERVER_ERROR = 500 +HTTP_501_NOT_IMPLEMENTED = 501 +HTTP_502_BAD_GATEWAY = 502 +HTTP_503_SERVICE_UNAVAILABLE = 503 +HTTP_504_GATEWAY_TIMEOUT = 504 +HTTP_505_HTTP_VERSION_NOT_SUPPORTED = 505 +HTTP_506_VARIANT_ALSO_NEGOTIATES = 506 +HTTP_507_INSUFFICIENT_STORAGE = 507 +HTTP_508_LOOP_DETECTED = 508 +HTTP_510_NOT_EXTENDED = 510 +HTTP_511_NETWORK_AUTHENTICATION_REQUIRED = 511 + + +# deprecated aliases +__deprecated__ = { + "HTTP_413_REQUEST_ENTITY_TOO_LARGE": 413, + "HTTP_414_REQUEST_URI_TOO_LONG": 414, + "HTTP_416_REQUESTED_RANGE_NOT_SATISFIABLE": 416, + "HTTP_422_UNPROCESSABLE_ENTITY": 422, +} + + +def __getattr__(name: str) -> int: + if name in __deprecated__: + warnings.warn( + f"{name} is deprecated", + DeprecationWarning, + stacklevel=2, + ) + return __deprecated__[name] + + raise AttributeError(f"module 'fasthttp.status' has no attribute '{name}'") + + +def __dir__() -> list: + return sorted(list(__all__) + list(__deprecated__.keys())) From ea87c0d6e2c90e160c6ec3692d891a9266af6642 Mon Sep 17 00:00:00 2001 From: NEFORCEO Date: Tue, 31 Mar 2026 22:08:52 +0700 Subject: [PATCH 2/4] update fasthttp/__init__.py --- fasthttp/__init__.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/fasthttp/__init__.py b/fasthttp/__init__.py index 7135c31..3b347e3 100644 --- a/fasthttp/__init__.py +++ b/fasthttp/__init__.py @@ -1,3 +1,5 @@ +import status as status + from .__meta__ import __version__ from .app import FastHTTP from .dependencies import Depends @@ -11,5 +13,6 @@ "FastHTTP", "MiddlewareManager", "Router", + "status" "__version__" ) From b36f5888d81da249a888356f6e446e4606ebbc81 Mon Sep 17 00:00:00 2001 From: NEFORCEO Date: Tue, 31 Mar 2026 22:09:01 +0700 Subject: [PATCH 3/4] update routes.py --- fasthttp/openapi/routes.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/fasthttp/openapi/routes.py b/fasthttp/openapi/routes.py index 3e81a6f..19f2e0f 100644 --- a/fasthttp/openapi/routes.py +++ b/fasthttp/openapi/routes.py @@ -7,7 +7,7 @@ from annotated_doc import Doc from fasthttp.response import Response - +from fasthttp import status from .generator import generate_openapi_schema from .swagger import get_swagger_html, get_not_found_html from .urls import build_docs_urls @@ -38,7 +38,7 @@ async def handle_docs( request_url=urls["request_url"], ) return Response( - status=200, + status=status.HTTP_200_OK, text=html, headers={"Content-Type": "text/html"}, ) @@ -64,7 +64,7 @@ async def handle_openapi_json( schema = generate_openapi_schema(app, server_url=urls["request_url"]) json_str = json.dumps(schema, indent=2, ensure_ascii=False) return Response( - status=200, + status=status.HTTP_200_OK, text=json_str, headers={"Content-Type": "application/json"}, ) @@ -92,7 +92,7 @@ async def handle_not_found( openapi_url=urls["openapi_url"], ) return Response( - status=404, + status=status.HTTP_404_NOT_FOUND, text=html, headers={"Content-Type": "text/html"}, ) @@ -127,7 +127,7 @@ async def handle_request( if not url: return Response( - status=400, + status=status.HTTP_400_BAD_REQUEST, text=json.dumps({"error": "URL is required"}), headers={"Content-Type": "application/json"}, ) @@ -160,26 +160,26 @@ async def handle_request( pass return Response( - status=200, + status=status.HTTP_200_OK, text=json.dumps(result, indent=2, ensure_ascii=False), headers={"Content-Type": "application/json"}, ) except httpx.ConnectError as e: return Response( - status=502, + status=status.HTTP_502_BAD_GATEWAY, text=json.dumps({"error": f"Connection error: {str(e)}"}), headers={"Content-Type": "application/json"}, ) except httpx.TimeoutException as e: return Response( - status=504, + status=status.HTTP_504_GATEWAY_TIMEOUT, text=json.dumps({"error": f"Timeout: {str(e)}"}), headers={"Content-Type": "application/json"}, ) except Exception as e: return Response( - status=500, + status=status.HTTP_500_INTERNAL_SERVER_ERROR, text=json.dumps({"error": f"Request failed: {str(e)}"}), headers={"Content-Type": "application/json"}, ) From 9685cbe45d968a1af127e681b0813e5794c4004b Mon Sep 17 00:00:00 2001 From: NEFORCEO Date: Tue, 31 Mar 2026 22:14:45 +0700 Subject: [PATCH 4/4] update fasthttp/__init__.py --- fasthttp/__init__.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/fasthttp/__init__.py b/fasthttp/__init__.py index 3b347e3..78027f1 100644 --- a/fasthttp/__init__.py +++ b/fasthttp/__init__.py @@ -1,5 +1,4 @@ -import status as status - +from . import status from .__meta__ import __version__ from .app import FastHTTP from .dependencies import Depends @@ -13,6 +12,6 @@ "FastHTTP", "MiddlewareManager", "Router", - "status" - "__version__" + "__version__", + "status", )