Giter Site home page Giter Site logo

openapi-core's Introduction

openapi-core

https://travis-ci.org/p1c2u/openapi-core.svg?branch=master https://img.shields.io/codecov/c/github/p1c2u/openapi-core/master.svg?style=flat

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):

$ pip install openapi-core

Alternatively you can download the code and install from the repository:

$ pip install -e git+https://github.com/p1c2u/openapi-core.git#egg=openapi_core

Usage

Firstly create your specification:

from openapi_core import create_spec

spec = create_spec(spec_dict)

Now you can use it to validate requests

from openapi_core.shortcuts import RequestValidator

validator = RequestValidator(spec)
result = validator.validate(request)

# raise errors if request invalid
result.raise_for_errors()

# get list of errors
errors = result.errors

and unmarshal request data from validation result

# get parameters dictionary with path, query, cookies and headers parameters
validated_params = result.parameters

# get body
validated_body = result.body

or use shortcuts for simple validation

from openapi_core import validate_parameters, validate_body

validated_params = validate_parameters(spec, request)
validated_body = validate_body(spec, request)

Request object should implement BaseOpenAPIRequest interface. You can use FlaskOpenAPIRequest a Flask/Werkzeug request wrapper implementation:

from openapi_core.shortcuts import RequestValidator
from openapi_core.wrappers.flask import FlaskOpenAPIRequest

openapi_request = FlaskOpenAPIRequest(flask_request)
validator = RequestValidator(spec)
result = validator.validate(openapi_request)

or specify request wrapper class for shortcuts

from openapi_core import validate_parameters, validate_body

validated_params = validate_parameters(
    spec, request, wrapper_class=FlaskOpenAPIRequest)
validated_body = validate_body(
    spec, request, wrapper_class=FlaskOpenAPIRequest)

You can also validate responses

from openapi_core.shortcuts import ResponseValidator

validator = ResponseValidator(spec)
result = validator.validate(request, response)

# raise errors if response invalid
result.raise_for_errors()

# get list of errors
errors = result.errors

and unmarshal response data from validation result

# get headers
validated_headers = result.headers

# get data
validated_data = result.data

or use shortcuts for simple validation

from openapi_core import validate_data

validated_data = validate_data(spec, request, response)

Response object should implement BaseOpenAPIResponse interface. You can use FlaskOpenAPIResponse a Flask/Werkzeug response wrapper implementation:

from openapi_core.shortcuts import ResponseValidator
from openapi_core.wrappers.flask import FlaskOpenAPIResponse

openapi_response = FlaskOpenAPIResponse(flask_response)
validator = ResponseValidator(spec)
result = validator.validate(openapi_request, openapi_response)

or specify response wrapper class for shortcuts

from openapi_core import validate_parameters, validate_body

validated_data = validate_data(
    spec, request, response, response_wrapper_class=FlaskOpenAPIResponse)

Related projects

openapi-core's People

Contributors

p1c2u avatar domenkozar avatar novucs avatar pbasista avatar benhowes avatar smarlowucf avatar mcapitani avatar rafaelcaricio avatar grktsh avatar badcure avatar zupo avatar dz0ny avatar jamesrwhite avatar hoverhell avatar erpheus avatar daveavon avatar correl avatar berend avatar

Watchers

Rob Charlwood avatar James Cloos avatar Matt Oates avatar James Bradbury avatar Nitesh Oswal avatar jacob gathercole avatar James Baldwin avatar  avatar Lawrence avatar  avatar  avatar Laura Sobola avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.