File: falcon.rst

package info (click to toggle)
python-openapi-core 0.19.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,008 kB
  • sloc: python: 18,868; makefile: 47
file content (94 lines) | stat: -rw-r--r-- 2,607 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
Falcon
======

This section describes integration with `Falcon <https://falconframework.org>`__ web framework.
The integration supports Falcon from version 3.0 and above.

Middleware
----------

The Falcon API can be integrated by ``FalconOpenAPIMiddleware`` middleware.

.. code-block:: python
  :emphasize-lines: 1,3,7

    from openapi_core.contrib.falcon.middlewares import FalconOpenAPIMiddleware

    openapi_middleware = FalconOpenAPIMiddleware.from_spec(spec)

    app = falcon.App(
       # ...
       middleware=[openapi_middleware],
    )

Additional customization parameters can be passed to the middleware.

.. code-block:: python
  :emphasize-lines: 5

    from openapi_core.contrib.falcon.middlewares import FalconOpenAPIMiddleware

    openapi_middleware = FalconOpenAPIMiddleware.from_spec(
       spec,
       extra_format_validators=extra_format_validators,
    )

    app = falcon.App(
       # ...
       middleware=[openapi_middleware],
    )

You can skip response validation process: by setting ``response_cls`` to ``None``

.. code-block:: python
  :emphasize-lines: 5

    from openapi_core.contrib.falcon.middlewares import FalconOpenAPIMiddleware

    openapi_middleware = FalconOpenAPIMiddleware.from_spec(
       spec,
       response_cls=None,
    )

    app = falcon.App(
       # ...
       middleware=[openapi_middleware],
    )

After that you will have access to validation result object with all validated request data from Falcon view through request context.

.. code-block:: python

    class ThingsResource:
       def on_get(self, req, resp):
           # get parameters object with path, query, cookies and headers parameters
           validated_params = req.context.openapi.parameters
           # or specific location parameters
           validated_path_params = req.context.openapi.parameters.path

           # get body
           validated_body = req.context.openapi.body

           # get security data
           validated_security = req.context.openapi.security

Low level
---------

You can use ``FalconOpenAPIRequest`` as a Falcon request factory:

.. code-block:: python

    from openapi_core.contrib.falcon import FalconOpenAPIRequest

    openapi_request = FalconOpenAPIRequest(falcon_request)
    result = openapi.unmarshal_request(openapi_request)

You can use ``FalconOpenAPIResponse`` as a Falcon response factory:

.. code-block:: python

    from openapi_core.contrib.falcon import FalconOpenAPIResponse

    openapi_response = FalconOpenAPIResponse(falcon_response)
    result = openapi.unmarshal_response(openapi_request, openapi_response)