Giter VIP home page Giter VIP logo

tornado-openapi3's Introduction

Tornado OpenAPI 3

https://travis-ci.com/correl/tornado-openapi3.svg?branch=master https://codecov.io/gh/correl/tornado-openapi3/branch/master/graph/badge.svg?token=CTYWWDXTL9 https://readthedocs.org/projects/tornado-openapi3/badge/

Tornado OpenAPI 3 request and response validation library.

Provides integration between the Tornado web framework and Openapi-core library for validating request and response objects against an OpenAPI 3 specification.

Full documentation is available at https://tornado-openapi3.readthedocs.io

Usage

Adding validation to request handlers

import tornado.ioloop
import tornado.web
from tornado_openapi3.handler import OpenAPIRequestHandler


class MyRequestHandler(OpenAPIRequestHandler):
    spec_dict = {
        "openapi": "3.0.0",
        "info": {
            "title": "Simple Example",
            "version": "1.0.0",
        },
        "paths": {
            "/": {
                "get": {
                    "responses": {
                        "200": {
                            "description": "Index",
                            "content": {
                                "text/html": {
                                    "schema": {"type": "string"},
                                }
                            },
                        }
                    }
                }
            }
        },
    }


class RootHandler(MyRequestHandler):
    async def get(self):
        self.finish("Hello, World!")


if __name__ == "__main__":
    app = tornado.web.Application([(r"/", RootHandler)])
    app.listen(8888)
    tornado.ioloop.IOLoop.current().start()

Validating responses in tests

import unittest

import tornado.web
from tornado_openapi3.testing import AsyncOpenAPITestCase


class RootHandler(tornado.web.RequestHandler):
    async def get(self):
        self.finish("Hello, World!")


class BaseTestCase(AsyncOpenAPITestCase):
    spec_dict = {
        "openapi": "3.0.0",
        "info": {
            "title": "Simple Example",
            "version": "1.0.0",
        },
        "paths": {
            "/": {
                "get": {
                    "responses": {
                        "200": {
                            "description": "Index",
                            "content": {
                                "text/html": {
                                    "schema": {"type": "string"},
                                }
                            },
                        }
                    }
                }
            }
        },
    }

    def get_app(self):
        return tornado.web.Application([(r"/", RootHandler)])

    def test_root_endpoint(self):
        response = self.fetch("/")
        self.assertEqual(200, response.code)
        self.assertEqual(b"Hello, World!", response.body)


if __name__ == "__main__":
    unittest.main()

Contributing

Getting Started

This project uses Poetry to manage its dependencies. To set up a local development environment, just run:

poetry install

Formatting Code

The Black tool is used by this project to format Python code. It is included as a development dependency, and should be run on all committed code. To format code prior to committing it and submitting a PR, run:

poetry run black .

Running Tests

pytest is the preferred test runner for this project. It is included as a development dependency, and is configured to track code coverage, Flake8 style compliance, and Black code formatting. Tests can be run in your development environment by running:

poetry run pytest

Additionally, tests can be run using tox, which will run the tests using multiple versions of both Python and Tornado to ensure broad compatibility.

Configuring Hypothesis

Many of the tests make use of Hypothesis to specify their expectations and generate a large volume of randomized test input. Because of this, the tests may take a long time to run on slower computers. Two profiles are defined for Hypothesis to use which can be selected by setting the HYPOTHESIS_PROFILE environment variable to one of the following values:

ci
Runs tests using the default Hypothesis settings (100 examples per test) and no completion deadline.
dev
The fastest profile, meant for local development only. Uses only 10 examples per test with no completion deadline.

tornado-openapi3's People

Contributors

correl avatar gmr avatar in-op avatar

Stargazers

Emmanuel I. Obi avatar  avatar  avatar Frédéric Collonval avatar Andrew Rabert avatar

Watchers

 avatar James Cloos avatar  avatar

tornado-openapi3's Issues

Dependency conflicts due to older typing-extensions version

Hi Correl,
we are using your library in our project and it's really helpful, so thanks for the effort. But I am currently running into dependency conflicts with my postgres library, due to the version of typing-extensions you use here. Does it need to be restricted, or could it also be updated to use typing-extensions 4.3.0?


[tool.poetry.dependencies]
python = "^3.7"
tornado = "^4 || ^5 || ^6"
openapi-core = "^0.14.2"
ietfparse = "^1.8.0"
typing-extensions = "^4.0.1"

Cheers,
Judy

Support openapi-core 0.16

openapi-core 0.16.1 was released on 10/10/2022, while tornado-openapi3 still uses 0.14. Version 0.16 includes support for OpenAPI 3.1, as well as supporting Python 3.10 and including typing annotations.

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.