Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OpenAPI app and high level integration #716

Merged
Merged
Show file tree
Hide file tree
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
10 changes: 4 additions & 6 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,20 @@ Alternatively you can download the code and install from the repository:
First steps
###########

Firstly create your specification object.
Firstly create your OpenAPI object.

.. code-block:: python

from jsonschema_path import SchemaPath
from openapi_core import OpenAPI

spec = SchemaPath.from_file_path('openapi.json')
openapi = OpenAPI.from_file_path('openapi.json')

Now you can use it to validate and unmarshal against requests and/or responses.

.. code-block:: python

from openapi_core import unmarshal_request

# raises error if request is invalid
result = unmarshal_request(request, spec=spec)
result = openapi.unmarshal_request(request)

Retrieve validated and unmarshalled request data

Expand Down
37 changes: 21 additions & 16 deletions docs/customizations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ By default, the specified specification is also validated.
If you know you have a valid specification already, disabling the validator can improve the performance.

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

validate_request(
request,
spec=spec,
from openapi_core import Config

config = Config(
spec_validator_cls=None,
)
openapi = OpenAPI.from_file_path('openapi.json', config=config)
openapi.validate_request(request)

Media type deserializers
------------------------
Expand All @@ -25,7 +27,7 @@ OpenAPI comes with a set of built-in media type deserializers such as: ``applica
You can also define your own ones. Pass custom defined media type deserializers dictionary with supported mimetypes as a key to `unmarshal_response` function:

.. code-block:: python
:emphasize-lines: 13
:emphasize-lines: 11

def protobuf_deserializer(message):
feature = route_guide_pb2.Feature()
Expand All @@ -36,11 +38,12 @@ You can also define your own ones. Pass custom defined media type deserializers
'application/protobuf': protobuf_deserializer,
}

result = unmarshal_response(
request, response,
spec=spec,
config = Config(
extra_media_type_deserializers=extra_media_type_deserializers,
)
openapi = OpenAPI.from_file_path('openapi.json', config=config)

result = openapi.unmarshal_response(request, response)

Format validators
-----------------
Expand All @@ -52,7 +55,7 @@ OpenAPI comes with a set of built-in format validators, but it's also possible t
Here's how you could add support for a ``usdate`` format that handles dates of the form MM/DD/YYYY:

.. code-block:: python
:emphasize-lines: 13
:emphasize-lines: 11

import re

Expand All @@ -63,11 +66,12 @@ Here's how you could add support for a ``usdate`` format that handles dates of t
'usdate': validate_usdate,
}

validate_response(
request, response,
spec=spec,
config = Config(
extra_format_validators=extra_format_validators,
)
openapi = OpenAPI.from_file_path('openapi.json', config=config)

openapi.validate_response(request, response)

Format unmarshallers
--------------------
Expand All @@ -79,7 +83,7 @@ Openapi-core comes with a set of built-in format unmarshallers, but it's also po
Here's an example with the ``usdate`` format that converts a value to date object:

.. code-block:: python
:emphasize-lines: 13
:emphasize-lines: 11

from datetime import datetime

Expand All @@ -90,8 +94,9 @@ Here's an example with the ``usdate`` format that converts a value to date objec
'usdate': unmarshal_usdate,
}

result = unmarshal_response(
request, response,
spec=spec,
config = Config(
extra_format_unmarshallers=extra_format_unmarshallers,
)
openapi = OpenAPI.from_file_path('openapi.json', config=config)

result = openapi.unmarshal_response(request, response)
10 changes: 4 additions & 6 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,20 @@ Installation
First steps
-----------

Firstly create your specification object.
Firstly create your OpenAPI object.

.. code-block:: python

from jsonschema_path import SchemaPath
from openapi_core import OpenAPI

spec = SchemaPath.from_file_path('openapi.json')
openapi = OpenAPI.from_file_path('openapi.json')

Now you can use it to validate and unmarshal your requests and/or responses.

.. code-block:: python

from openapi_core import unmarshal_request

# raises error if request is invalid
result = unmarshal_request(request, spec=spec)
result = openapi.unmarshal_request(request)

Retrieve validated and unmarshalled request data

Expand Down
10 changes: 5 additions & 5 deletions docs/integrations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -48,35 +48,35 @@ The integration supports Django from version 3.0 and above.
Middleware
~~~~~~~~~~

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

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

# settings.py
from jsonschema_path import SchemaPath
from openapi_core import OpenAPI

MIDDLEWARE = [
# ...
'openapi_core.contrib.django.middlewares.DjangoOpenAPIMiddleware',
]

OPENAPI_SPEC = SchemaPath.from_dict(spec_dict)
OPENAPI = OpenAPI.from_dict(spec_dict)

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

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

# settings.py
from jsonschema_path import SchemaPath
from openapi_core import OpenAPI

MIDDLEWARE = [
# ...
'openapi_core.contrib.django.middlewares.DjangoOpenAPIMiddleware',
]

OPENAPI_SPEC = SchemaPath.from_dict(spec_dict)
OPENAPI = OpenAPI.from_dict(spec_dict)
OPENAPI_RESPONSE_CLS = None

After that you have access to unmarshal result object with all validated request data from Django view through request object.
Expand Down
6 changes: 5 additions & 1 deletion openapi_core/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""OpenAPI core module"""
from openapi_core.app import OpenAPI
from openapi_core.configurations import Config
from openapi_core.shortcuts import unmarshal_apicall_request
from openapi_core.shortcuts import unmarshal_apicall_response
from openapi_core.shortcuts import unmarshal_request
Expand All @@ -11,7 +13,7 @@
from openapi_core.shortcuts import validate_response
from openapi_core.shortcuts import validate_webhook_request
from openapi_core.shortcuts import validate_webhook_response
from openapi_core.spec import Spec
from openapi_core.spec.paths import Spec
from openapi_core.unmarshalling.request import V3RequestUnmarshaller
from openapi_core.unmarshalling.request import V3WebhookRequestUnmarshaller
from openapi_core.unmarshalling.request import V30RequestUnmarshaller
Expand Down Expand Up @@ -40,6 +42,8 @@
__license__ = "BSD 3-Clause License"

__all__ = [
"OpenAPI",
"Config",
"Spec",
"unmarshal_request",
"unmarshal_response",
Expand Down
Loading
Loading