Openapi-core is a Python library that adds client-side and server-side support for the OpenAPI Specification v3.0.0.
Recommended way (via pip):
$ pip install openapi-core
Alternatively you can download the code and install from the repository:
Firstly create your specification:
Now you can use it to validate requests
and unmarshal request data from validation result
Request object should be instance of OpenAPIRequest class (See Integrations).
You can also validate responses
and unmarshal response data from validation result
Response object should be instance of OpenAPIResponse class (See Integrations).
openapi-core supports security for authentication and authorization process. Security data for security schemas are accessible from security attribute of RequestValidationResult object.
For given security specification:
you can access your security data the following:
Supported security types:
- http – for Basic and Bearer HTTP authentications schemes
- apiKey – for API keys and cookie authentication
Pass custom defined media type deserializers dictionary with supported mimetypes as a key to RequestValidator or ResponseValidator constructor:
def protobuf_deserializer(message):
feature = route_guide_pb2.Feature()
feature.ParseFromString(message)
return feature
custom_media_type_deserializers = {
'application/protobuf': protobuf_deserializer,
}
validator = ResponseValidator(
spec, custom_media_type_deserializers=custom_media_type_deserializers)
result = validator.validate(request, response)
For Django 2.2 you can use DjangoOpenAPIRequest a Django request factory:
You can use DjangoOpenAPIResponse as a Django response factory:
Flask views can be integrated by FlaskOpenAPIViewDecorator decorator.
If you want to decorate class based view you can use the decorators attribute:
As an alternative to the decorator-based integration, Flask method based views can be integrated by inheritance from FlaskOpenAPIView class.
In Flask, all unmarshalled request data are provided as Flask request object's openapi.parameters attribute
You can use FlaskOpenAPIRequest a Flask/Werkzeug request factory:
You can use FlaskOpenAPIResponse as a Flask/Werkzeug response factory:
See pyramid_openapi3 project.