parse_client: Migrate has_request_variables to typed_endpoint.

Refactor `parse_client` view to use `typed_endpoint decorator`
instead of `has_request_variables`. This change improves code consistency
and enhances codebase comprehension.
This commit is contained in:
Mahhheshh 2024-03-13 00:35:37 +05:30 committed by Tim Abbott
parent f16208d9f1
commit 22bd8048b1
1 changed files with 9 additions and 5 deletions

View File

@ -22,7 +22,7 @@ from django.utils.translation import gettext as _
from django_scim.middleware import SCIMAuthCheckMiddleware from django_scim.middleware import SCIMAuthCheckMiddleware
from django_scim.settings import scim_settings from django_scim.settings import scim_settings
from sentry_sdk import set_tag from sentry_sdk import set_tag
from typing_extensions import Concatenate, ParamSpec, override from typing_extensions import Annotated, Concatenate, ParamSpec, override
from zerver.lib.cache import get_remote_cache_requests, get_remote_cache_time from zerver.lib.cache import get_remote_cache_requests, get_remote_cache_time
from zerver.lib.db import reset_queries from zerver.lib.db import reset_queries
@ -32,7 +32,7 @@ from zerver.lib.html_to_text import get_content_description
from zerver.lib.markdown import get_markdown_requests, get_markdown_time from zerver.lib.markdown import get_markdown_requests, get_markdown_time
from zerver.lib.per_request_cache import flush_per_request_caches from zerver.lib.per_request_cache import flush_per_request_caches
from zerver.lib.rate_limiter import RateLimitResult from zerver.lib.rate_limiter import RateLimitResult
from zerver.lib.request import REQ, RequestNotes, has_request_variables from zerver.lib.request import RequestNotes
from zerver.lib.response import ( from zerver.lib.response import (
AsynchronousResponse, AsynchronousResponse,
json_response, json_response,
@ -40,6 +40,7 @@ from zerver.lib.response import (
json_unauthorized, json_unauthorized,
) )
from zerver.lib.subdomains import get_subdomain from zerver.lib.subdomains import get_subdomain
from zerver.lib.typed_endpoint import INTENTIONALLY_UNDOCUMENTED, ApiParamConfig, typed_endpoint
from zerver.lib.user_agent import parse_user_agent from zerver.lib.user_agent import parse_user_agent
from zerver.models import Realm from zerver.models import Realm
from zerver.models.realms import get_realm from zerver.models.realms import get_realm
@ -226,15 +227,18 @@ def write_log_line(
logger.info("status=%3d, data=%s, uid=%s", status_code, error_data, requester_for_logs) logger.info("status=%3d, data=%s, uid=%s", status_code, error_data, requester_for_logs)
# We take advantage of `has_request_variables` being called multiple times # We take advantage of `typed_endpoint` being called multiple times
# when processing a request in order to process any `client` parameter that # when processing a request in order to process any `client` parameter that
# may have been sent in the request content. # may have been sent in the request content.
@has_request_variables @typed_endpoint
def parse_client( def parse_client(
request: HttpRequest, request: HttpRequest,
# As `client` is a common element to all API endpoints, we choose # As `client` is a common element to all API endpoints, we choose
# not to document on every endpoint's individual parameters. # not to document on every endpoint's individual parameters.
req_client: Optional[str] = REQ("client", default=None, intentionally_undocumented=True), *,
req_client: Annotated[
Optional[str], ApiParamConfig("client", documentation_status=INTENTIONALLY_UNDOCUMENTED)
] = None,
) -> Tuple[str, Optional[str]]: ) -> Tuple[str, Optional[str]]:
# If the API request specified a client in the request content, # If the API request specified a client in the request content,
# that has priority. Otherwise, extract the client from the # that has priority. Otherwise, extract the client from the