From 2e11553f3a9dede6a85e61c3b99fe607c4ea39bd Mon Sep 17 00:00:00 2001 From: p1c2u Date: Sun, 20 Oct 2019 00:39:13 +0100 Subject: [PATCH] README update with Django usage --- README.rst | 120 ++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 95 insertions(+), 25 deletions(-) diff --git a/README.rst b/README.rst index 9a414f4..22eac86 100644 --- a/README.rst +++ b/README.rst @@ -1,3 +1,4 @@ +************ openapi-core ************ @@ -15,13 +16,13 @@ openapi-core :target: https://pypi.python.org/pypi/openapi-core About -===== +##### Openapi-core is a Python library that adds client-side and server-side support for the `OpenAPI Specification v3.0.0 `__. Installation -============ +############ Recommended way (via pip): @@ -37,7 +38,7 @@ Alternatively you can download the code and install from the repository: Usage -===== +##### Firstly create your specification: @@ -47,6 +48,9 @@ Firstly create your specification: spec = create_spec(spec_dict) +Request +******* + Now you can use it to validate requests .. code-block:: python @@ -83,27 +87,10 @@ or use shortcuts for simple validation validated_params = validate_parameters(spec, request) validated_body = validate_body(spec, request) -Request object should be instance of OpenAPIRequest class. You can use FlaskOpenAPIRequest a Flask/Werkzeug request factory: +Request object should be instance of OpenAPIRequest class (See `Integrations`_). -.. code-block:: python - - from openapi_core.shortcuts import RequestValidator - from openapi_core.contrib.flask import FlaskOpenAPIRequest - - openapi_request = FlaskOpenAPIRequest(flask_request) - validator = RequestValidator(spec) - result = validator.validate(openapi_request) - -or simply specify request factory for shortcuts - -.. code-block:: python - - from openapi_core import validate_parameters, validate_body - - validated_params = validate_parameters( - spec, request, request_factory=FlaskOpenAPIRequest) - validated_body = validate_body( - spec, request, request_factory=FlaskOpenAPIRequest) +Response +******** You can also validate responses @@ -138,7 +125,85 @@ or use shortcuts for simple validation validated_data = validate_data(spec, request, response) -Response object should be instance of OpenAPIResponse class. You can use FlaskOpenAPIResponse a Flask/Werkzeug response factory: +Response object should be instance of OpenAPIResponse class (See `Integrations`_). + + +Integrations +############ + +Django +****** + +You can use DjangoOpenAPIRequest a Django request factory: + +.. code-block:: python + + from openapi_core.shortcuts import RequestValidator + from openapi_core.contrib.django import DjangoOpenAPIRequest + + openapi_request = DjangoOpenAPIRequest(django_request) + validator = RequestValidator(spec) + result = validator.validate(openapi_request) + +or simply specify request factory for shortcuts + +.. code-block:: python + + from openapi_core import validate_parameters, validate_body + + validated_params = validate_parameters( + spec, request, request_factory=DjangoOpenAPIRequest) + validated_body = validate_body( + spec, request, request_factory=DjangoOpenAPIRequest) + +You can use DjangoOpenAPIResponse as a Django response factory: + +.. code-block:: python + + from openapi_core.shortcuts import ResponseValidator + from openapi_core.contrib.django import DjangoOpenAPIResponse + + openapi_response = DjangoOpenAPIResponse(django_response) + validator = ResponseValidator(spec) + result = validator.validate(openapi_request, openapi_response) + +or simply specify response factory for shortcuts + +.. code-block:: python + + from openapi_core import validate_parameters, validate_body + + validated_data = validate_data( + spec, request, response, + request_factory=DjangoOpenAPIRequest, + response_factory=DjangoOpenAPIResponse) + +Flask +***** + +You can use FlaskOpenAPIRequest a Flask/Werkzeug request factory: + +.. code-block:: python + + from openapi_core.shortcuts import RequestValidator + from openapi_core.contrib.flask import FlaskOpenAPIRequest + + openapi_request = FlaskOpenAPIRequest(flask_request) + validator = RequestValidator(spec) + result = validator.validate(openapi_request) + +or simply specify request factory for shortcuts + +.. code-block:: python + + from openapi_core import validate_parameters, validate_body + + validated_params = validate_parameters( + spec, request, request_factory=FlaskOpenAPIRequest) + validated_body = validate_body( + spec, request, request_factory=FlaskOpenAPIRequest) + +You can use FlaskOpenAPIResponse as a Flask/Werkzeug response factory: .. code-block:: python @@ -160,7 +225,12 @@ or simply specify response factory for shortcuts request_factory=FlaskOpenAPIRequest, response_factory=FlaskOpenAPIResponse) +Pyramid +******* + +See `pyramid_openapi3 `_ project. + Related projects -================ +################ * `openapi-spec-validator `__ * `pyramid_openapi3 `__