File: flask.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 (118 lines) | stat: -rw-r--r-- 3,060 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
Flask
======

This section describes integration with `Flask <https://flask.palletsprojects.com>`__ web framework.

View decorator
--------------

Flask can be integrated by `view decorator <https://flask.palletsprojects.com/en/latest/patterns/viewdecorators/>`__ to apply OpenAPI validation to your application's specific views.

Use ``FlaskOpenAPIViewDecorator`` with OpenAPI object to create the decorator.

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

    from openapi_core.contrib.flask.decorators import FlaskOpenAPIViewDecorator

    openapi_validated = FlaskOpenAPIViewDecorator(openapi)

    @app.route('/home')
    @openapi_validated
    def home():
       return "Welcome home"

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

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

    from openapi_core.contrib.flask.decorators import FlaskOpenAPIViewDecorator

    openapi_validated = FlaskOpenAPIViewDecorator(
       openapi,
       response_cls=None,
    )

If you want to decorate class based view you can use the decorators attribute:

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

    class MyView(View):
       decorators = [openapi_validated]

       def dispatch_request(self):
           return "Welcome home"

    app.add_url_rule('/home', view_func=MyView.as_view('home'))

View
----

As an alternative to the decorator-based integration, a Flask method based views can be integrated by inheritance from ``FlaskOpenAPIView`` class.

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

    from openapi_core.contrib.flask.views import FlaskOpenAPIView

    class MyView(FlaskOpenAPIView):
       def get(self):
           return "Welcome home"

    app.add_url_rule(
       '/home',
       view_func=MyView.as_view('home', spec),
    )

Additional customization parameters can be passed to the view.

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

    from openapi_core.contrib.flask.views import FlaskOpenAPIView

    class MyView(FlaskOpenAPIView):
       def get(self):
           return "Welcome home"

    app.add_url_rule(
       '/home',
       view_func=MyView.as_view(
           'home', spec,
           extra_format_validators=extra_format_validators,
       ),
    )

Request parameters
------------------

In Flask, all unmarshalled request data are provided as Flask request object's ``openapi.parameters`` attribute

.. code-block:: python
  :emphasize-lines: 6,7

    from flask.globals import request

    @app.route('/browse/<id>/')
    @openapi
    def browse(id):
       browse_id = request.openapi.parameters.path['id']
       page = request.openapi.parameters.query.get('page', 1)

       return f"Browse {browse_id}, page {page}"

Low level
---------

You can use ``FlaskOpenAPIRequest`` as a Flask request factory:

.. code-block:: python

    from openapi_core.contrib.flask import FlaskOpenAPIRequest

    openapi_request = FlaskOpenAPIRequest(flask_request)
    result = openapi.unmarshal_request(openapi_request)

For response factory see `Werkzeug <werkzeug.rst>`_ integration.