0
0
Fork 0
mirror of https://github.com/healthchecks/healthchecks.git synced 2025-04-14 17:08:29 +00:00

Improve type hints in hc.api.decorators

This commit is contained in:
Pēteris Caune 2023-10-20 18:41:11 +03:00
parent 5ebc7696d7
commit 33a0f65144
No known key found for this signature in database
GPG key ID: E28D7679E9A9EDE2

View file

@ -18,11 +18,11 @@ class ApiRequest(HttpRequest):
v: int v: int
def error(msg, status=400): def error(msg: str, status: int = 400) -> JsonResponse:
return JsonResponse({"error": msg}, status=status) return JsonResponse({"error": msg}, status=status)
def _get_api_version(request) -> int: def _get_api_version(request: HttpRequest) -> int:
if request.path_info.startswith("/api/v3/"): if request.path_info.startswith("/api/v3/"):
return 3 return 3
if request.path_info.startswith("/api/v2/"): if request.path_info.startswith("/api/v2/"):
@ -32,7 +32,7 @@ def _get_api_version(request) -> int:
def authorize(f: ViewFunc) -> ViewFunc: def authorize(f: ViewFunc) -> ViewFunc:
@wraps(f) @wraps(f)
def wrapper(request, *args, **kwds): def wrapper(request: ApiRequest, *args: Any, **kwds: Any) -> HttpResponse:
# For POST requests, we may need to look for the API key inside the # For POST requests, we may need to look for the API key inside the
# request body. Parse the body and put it in request.json # request body. Parse the body and put it in request.json
# so views can avoid parsing it again. # so views can avoid parsing it again.
@ -70,7 +70,7 @@ def authorize(f: ViewFunc) -> ViewFunc:
def authorize_read(f: ViewFunc) -> ViewFunc: def authorize_read(f: ViewFunc) -> ViewFunc:
@wraps(f) @wraps(f)
def wrapper(request, *args, **kwds): def wrapper(request: ApiRequest, *args: Any, **kwds: Any) -> HttpResponse:
if "HTTP_X_API_KEY" in request.META: if "HTTP_X_API_KEY" in request.META:
api_key = request.META["HTTP_X_API_KEY"] api_key = request.META["HTTP_X_API_KEY"]
else: else:
@ -100,7 +100,7 @@ def cors(*methods: str) -> Callable[[ViewFunc], ViewFunc]:
def decorator(f: ViewFunc) -> ViewFunc: def decorator(f: ViewFunc) -> ViewFunc:
@wraps(f) @wraps(f)
def wrapper(request, *args, **kwds): def wrapper(request: HttpRequest, *args: Any, **kwds: Any) -> HttpResponse:
if request.method == "OPTIONS": if request.method == "OPTIONS":
# Handle OPTIONS here # Handle OPTIONS here
response = HttpResponse(status=204) response = HttpResponse(status=204)