Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions src/webargs/falconparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from __future__ import annotations

import typing

import falcon
import marshmallow as ma
from falcon.util.uri import parse_query_string
Expand All @@ -18,17 +20,16 @@
def _find_exceptions():
for name in filter(lambda n: n.startswith("HTTP"), dir(falcon.status_codes)):
status = getattr(falcon.status_codes, name)
status_code = int(status.split(" ")[0])
status_code = falcon.http_status_to_code(status)
status_map[status_code] = status


_find_exceptions()
del _find_exceptions


def is_json_request(req: falcon.Request):
content_type = req.get_header("Content-Type")
return content_type and core.is_json(content_type)
def is_json_request(req: falcon.Request) -> bool:
return core.is_json(req.content_type)


# NOTE: Adapted from falcon.request.Request._parse_form_urlencoded
Expand Down Expand Up @@ -115,7 +116,9 @@ def load_form(self, req: falcon.Request, schema):

def load_media(self, req: falcon.Request, schema):
"""Return data unpacked and parsed by one of Falcon's media handlers.
By default, Falcon only handles JSON payloads.

By default, Falcon handles JSON, URL-encoded and multipart form payloads.
(However, :meth:`load_files` does not support Falcon's multipart form yet.)

To configure additional media handlers, see the
`Falcon documentation on media types`__.
Expand All @@ -130,7 +133,7 @@ def load_media(self, req: falcon.Request, schema):
# if there is no body, return missing instead of erroring
if req.content_length in (None, 0):
return core.missing
return req.media
return req.get_media()

def _raw_load_json(self, req: falcon.Request):
"""Return a json payload from the request for the core parser's load_json
Expand Down Expand Up @@ -166,14 +169,14 @@ def get_request_from_view_args(self, view, args, kwargs):
raise TypeError("Argument is not a falcon.Request")
return req

def load_files(self, req: falcon.Request, schema):
def load_files(self, req: falcon.Request, schema) -> typing.NoReturn:
raise NotImplementedError(
f"Parsing files not yet supported by {self.__class__.__name__}"
)

def handle_error(
self, error, req: falcon.Request, schema, *, error_status_code, error_headers
):
) -> typing.NoReturn:
"""Handles errors during parsing."""
status = status_map.get(error_status_code or self.DEFAULT_VALIDATION_STATUS)
if status is None:
Expand Down