Skip to content

Commit cc3bca2

Browse files
committed
OpenAPI app and high level integration
1 parent 46639b5 commit cc3bca2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+1496
-857
lines changed

README.rst

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,22 +57,20 @@ Alternatively you can download the code and install from the repository:
5757
First steps
5858
###########
5959

60-
Firstly create your specification object.
60+
Firstly create your OpenAPI object.
6161

6262
.. code-block:: python
6363
64-
from jsonschema_path import SchemaPath
64+
from openapi_core import OpenAPI
6565
66-
spec = SchemaPath.from_file_path('openapi.json')
66+
openapi = OpenAPI.from_file_path('openapi.json')
6767
6868
Now you can use it to validate and unmarshal against requests and/or responses.
6969

7070
.. code-block:: python
7171
72-
from openapi_core import unmarshal_request
73-
7472
# raises error if request is invalid
75-
result = unmarshal_request(request, spec=spec)
73+
result = openapi.unmarshal_request(request)
7674
7775
Retrieve validated and unmarshalled request data
7876

docs/customizations.rst

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ By default, the specified specification is also validated.
99
If you know you have a valid specification already, disabling the validator can improve the performance.
1010

1111
.. code-block:: python
12-
:emphasize-lines: 4
12+
:emphasize-lines: 1,4,6
1313
14-
validate_request(
15-
request,
16-
spec=spec,
14+
from openapi_core import Config
15+
16+
config = Config(
1717
spec_validator_cls=None,
1818
)
19+
openapi = OpenAPI.from_file_path('openapi.json', config=config)
20+
openapi.validate_request(request)
1921
2022
Media type deserializers
2123
------------------------
@@ -25,7 +27,7 @@ OpenAPI comes with a set of built-in media type deserializers such as: ``applica
2527
You can also define your own ones. Pass custom defined media type deserializers dictionary with supported mimetypes as a key to `unmarshal_response` function:
2628

2729
.. code-block:: python
28-
:emphasize-lines: 13
30+
:emphasize-lines: 11
2931
3032
def protobuf_deserializer(message):
3133
feature = route_guide_pb2.Feature()
@@ -36,11 +38,12 @@ You can also define your own ones. Pass custom defined media type deserializers
3638
'application/protobuf': protobuf_deserializer,
3739
}
3840
39-
result = unmarshal_response(
40-
request, response,
41-
spec=spec,
41+
config = Config(
4242
extra_media_type_deserializers=extra_media_type_deserializers,
4343
)
44+
openapi = OpenAPI.from_file_path('openapi.json', config=config)
45+
46+
result = openapi.unmarshal_response(request, response)
4447
4548
Format validators
4649
-----------------
@@ -52,7 +55,7 @@ OpenAPI comes with a set of built-in format validators, but it's also possible t
5255
Here's how you could add support for a ``usdate`` format that handles dates of the form MM/DD/YYYY:
5356

5457
.. code-block:: python
55-
:emphasize-lines: 13
58+
:emphasize-lines: 11
5659
5760
import re
5861
@@ -63,11 +66,12 @@ Here's how you could add support for a ``usdate`` format that handles dates of t
6366
'usdate': validate_usdate,
6467
}
6568
66-
validate_response(
67-
request, response,
68-
spec=spec,
69+
config = Config(
6970
extra_format_validators=extra_format_validators,
7071
)
72+
openapi = OpenAPI.from_file_path('openapi.json', config=config)
73+
74+
openapi.validate_response(request, response)
7175
7276
Format unmarshallers
7377
--------------------
@@ -79,7 +83,7 @@ Openapi-core comes with a set of built-in format unmarshallers, but it's also po
7983
Here's an example with the ``usdate`` format that converts a value to date object:
8084

8185
.. code-block:: python
82-
:emphasize-lines: 13
86+
:emphasize-lines: 11
8387
8488
from datetime import datetime
8589
@@ -90,8 +94,9 @@ Here's an example with the ``usdate`` format that converts a value to date objec
9094
'usdate': unmarshal_usdate,
9195
}
9296
93-
result = unmarshal_response(
94-
request, response,
95-
spec=spec,
97+
config = Config(
9698
extra_format_unmarshallers=extra_format_unmarshallers,
9799
)
100+
openapi = OpenAPI.from_file_path('openapi.json', config=config)
101+
102+
result = openapi.unmarshal_response(request, response)

docs/index.rst

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,22 +45,20 @@ Installation
4545
First steps
4646
-----------
4747

48-
Firstly create your specification object.
48+
Firstly create your OpenAPI object.
4949

5050
.. code-block:: python
5151
52-
from jsonschema_path import SchemaPath
52+
from openapi_core import OpenAPI
5353
54-
spec = SchemaPath.from_file_path('openapi.json')
54+
openapi = OpenAPI.from_file_path('openapi.json')
5555
5656
Now you can use it to validate and unmarshal your requests and/or responses.
5757

5858
.. code-block:: python
5959
60-
from openapi_core import unmarshal_request
61-
6260
# raises error if request is invalid
63-
result = unmarshal_request(request, spec=spec)
61+
result = openapi.unmarshal_request(request)
6462
6563
Retrieve validated and unmarshalled request data
6664

docs/integrations.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,35 +48,35 @@ The integration supports Django from version 3.0 and above.
4848
Middleware
4949
~~~~~~~~~~
5050

51-
Django can be integrated by middleware. Add ``DjangoOpenAPIMiddleware`` to your ``MIDDLEWARE`` list and define ``OPENAPI_SPEC``.
51+
Django can be integrated by middleware. Add ``DjangoOpenAPIMiddleware`` to your ``MIDDLEWARE`` list and define ``OPENAPI``.
5252

5353
.. code-block:: python
5454
:emphasize-lines: 6,9
5555
5656
# settings.py
57-
from jsonschema_path import SchemaPath
57+
from openapi_core import OpenAPI
5858
5959
MIDDLEWARE = [
6060
# ...
6161
'openapi_core.contrib.django.middlewares.DjangoOpenAPIMiddleware',
6262
]
6363
64-
OPENAPI_SPEC = SchemaPath.from_dict(spec_dict)
64+
OPENAPI = OpenAPI.from_dict(spec_dict)
6565
6666
You can skip response validation process: by setting ``OPENAPI_RESPONSE_CLS`` to ``None``
6767

6868
.. code-block:: python
6969
:emphasize-lines: 10
7070
7171
# settings.py
72-
from jsonschema_path import SchemaPath
72+
from openapi_core import OpenAPI
7373
7474
MIDDLEWARE = [
7575
# ...
7676
'openapi_core.contrib.django.middlewares.DjangoOpenAPIMiddleware',
7777
]
7878
79-
OPENAPI_SPEC = SchemaPath.from_dict(spec_dict)
79+
OPENAPI = OpenAPI.from_dict(spec_dict)
8080
OPENAPI_RESPONSE_CLS = None
8181
8282
After that you have access to unmarshal result object with all validated request data from Django view through request object.

openapi_core/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
"""OpenAPI core module"""
2+
from openapi_core.app import OpenAPI
3+
from openapi_core.configurations import Config
24
from openapi_core.shortcuts import unmarshal_apicall_request
35
from openapi_core.shortcuts import unmarshal_apicall_response
46
from openapi_core.shortcuts import unmarshal_request
@@ -11,7 +13,7 @@
1113
from openapi_core.shortcuts import validate_response
1214
from openapi_core.shortcuts import validate_webhook_request
1315
from openapi_core.shortcuts import validate_webhook_response
14-
from openapi_core.spec import Spec
16+
from openapi_core.spec.paths import Spec
1517
from openapi_core.unmarshalling.request import V3RequestUnmarshaller
1618
from openapi_core.unmarshalling.request import V3WebhookRequestUnmarshaller
1719
from openapi_core.unmarshalling.request import V30RequestUnmarshaller
@@ -40,6 +42,8 @@
4042
__license__ = "BSD 3-Clause License"
4143

4244
__all__ = [
45+
"OpenAPI",
46+
"Config",
4347
"Spec",
4448
"unmarshal_request",
4549
"unmarshal_response",

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy