-
-
Notifications
You must be signed in to change notification settings - Fork 948
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(recipes): clean up recent recipe additions (+misc touchups) (#2425)
* chore(AUTHORS): aggregate 4.1.0 contributors so far * docs: happy new year * chore: remove carriage-returns (we only use LF for indentation) * docs(recipes): improve the plain text media handler recipe * docs(recipes): clean up request-id log recipes
- Loading branch information
Showing
27 changed files
with
125 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,40 @@ | ||
.. _plain_text_handler_recipe: | ||
|
||
Handling Plain Text as Media | ||
============================ | ||
|
||
This example demonstrates how to create a custom handler in Falcon to | ||
process the ``text/plain`` media type. The handler implements serialization | ||
and deserialization of textual content, respecting the charset specified | ||
in the ``Content-Type`` header (or defaulting to ``utf-8`` when no charset is provided). | ||
|
||
.. literalinclude:: ../../../examples/recipes/plain_text_main.py | ||
:language: python | ||
|
||
To use this handler, register it in the Falcon application's media | ||
options for both request and response: | ||
|
||
.. code:: python | ||
import falcon | ||
from your_module import TextHandler | ||
app = falcon.App() | ||
app.req_options.media_handlers['text/plain'] = TextHandler() | ||
app.resp_options.media_handlers['text/plain'] = TextHandler() | ||
With this setup, the application can handle textual data directly | ||
as ``text/plain``, ensuring support for various character encodings as needed. | ||
|
||
.. warning:: | ||
Be sure to validate and limit the size of incoming data when | ||
working with textual content to prevent server overload or denial-of-service attacks. | ||
.. _plain_text_handler_recipe: | ||
|
||
Handling Plain Text as Media | ||
============================ | ||
|
||
Normally, it is easiest to render a plain text response by setting | ||
:attr:`resp.text <falcon.Response.text>` | ||
(see also: :ref:`resp_media_data_text`). | ||
However, if plain text is just one of | ||
the :ref:`Internet media types <media>` that your application speaks, it may be | ||
useful to generalize handling plain text as :ref:`media <media>` too. | ||
|
||
This recipe demonstrates how to create a | ||
:ref:`custom media handler <custom-media-handler-type>` to process the | ||
``text/plain`` media type. The handler implements serialization and | ||
deserialization of textual content, respecting the charset specified in the | ||
``Content-Type`` header | ||
(or defaulting to ``utf-8`` when no charset is provided). | ||
|
||
.. literalinclude:: ../../../examples/recipes/plain_text_main.py | ||
:language: python | ||
|
||
To use this handler, :ref:`register <custom_media_handlers>` it in the Falcon | ||
application's media options for both request and response: | ||
|
||
.. code:: python | ||
import falcon | ||
from your_module import TextHandler | ||
app = falcon.App() | ||
app.req_options.media_handlers['text/plain'] = TextHandler() | ||
app.resp_options.media_handlers['text/plain'] = TextHandler() | ||
With this setup, the application can handle textual data directly | ||
as ``text/plain``, ensuring support for various character encodings as needed. | ||
|
||
.. warning:: | ||
Be sure to validate and limit the size of incoming data when working with | ||
textual content to prevent server overload or denial-of-service attacks. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
# context.py | ||
|
||
import contextvars | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.