Skip to main content

Minimal OpenAPI asynchronous server application

Project description

aio-openapi

PyPI version Python versions Build Coverage Status Documentation Status Downloads

Asynchronous web middleware for aiohttp and serving Rest APIs with OpenAPI v 3 specification and with optional PostgreSql database bindings.

Table of Contents

Installation

pip install aio-openapi

Development

Clone the repository and create a virtual environment venv.

Install dependencies by running the install script

./dev/install

To run tests

pytest --cov

By default tests are run against a database with the following connection string postgresql://postgres:postgres@localhost:5432/openapi. To use a different DB, create a .env file with a different connection string, for example:

DATASTORE=postgresql://postgres:postgres@localhost:6432/openapi

Features

  • Asynchronous web routes with aiohttp
  • Data validation, serialization and unserialization with python dataclasses
  • OpenApi v 3 auto documentation
  • SqlAlchemy expression language
  • Asynchronous DB interaction with asyncpg
  • Migrations with alembic
  • SqlAlchemy tables as python dataclasses
  • Support click command line interface
  • Optional sentry middleware

Web App

To create an openapi RESTful application follow this schema (lets call the file main.py)

from openapi.rest import rest

def create_app():
    return rest(
        openapi=dict(
            title='A REST API',
            ...
        ),
        base_path='/v1',
        allowed_tags=[...],
        validate_docs=True,
        setup_app=setup_app,
        commands=[...]
    )


def setup_app(app):
    app.router.add_routes(...)
    return app


if __name__ == '__main__':
    create_app().main()

The create_app function creates the aiohttp server application by invoking the rest function. This function adds the click command in the cli mapping entry and add documentation for routes which support OpenAPI docs. The setup_app function is used to actually setup the custom application, usually by adding middleware, routes, shutdown callbacks, database integration and so forth.

OpenAPI Documentation

The library provide tools for creating OpenAPI v 3 compliant endpoints and auto-document them.

An example from test tests/example directory

from typing import List

from aiohttp import web

from openapi.db.path import SqlApiPath
from openapi.spec import op


routes = web.RouteTableDef()


@routes.view('/tasks')
class TasksPath(SqlApiPath):
    """
    ---
    summary: Create and query Tasks
    tags:
        - name: Task
          description: Task tag description
    """
    table = 'tasks'

    @op(query_schema=TaskOrderableQuery, response_schema=List[Task])
    async def get(self) -> web.Response:
        """
        ---
        summary: Retrieve Tasks
        description: Retrieve a list of Tasks
        responses:
            200:
                description: Authenticated tasks
        """
        paginated = await self.get_list()
        return paginated.json_response()

    @op(response_schema=Task, body_schema=TaskAdd)
    async def post(self) -> web.Response:
        """
        ---
        summary: Create a Task
        description: Create a new Task
        responses:
            201:
                description: the task was successfully added
            422:
                description: Failed validation
        """
        data = await self.create_one()
        return self.json_response(data, status=201)

Database Integration

This library provides integration with asyncpg, an high performant asynchronous connector with PostgreSql database. To add the database extension simply use the get_db function in the applicatiuon setup_app function:

from aiohttp import web

from openapi.db import get_db

def setup_app(app: web.Application) -> None:
    db = get_db(app)
    meta = db.metadata

This will enable database connection and command line tools (most of them from alembic):

python main.py db --help

The database container is available at the db app key:

app['db']

Environment Variables

Several environment variables are used by the library to support testing and deployment.

  • DATASTORE: PostgreSql connection string (same as SqlAlchemy syntax)
  • DBPOOL_MIN_SIZE: minimum size of database connection pool (default is 10)
  • DBPOOL_MAX_SIZE: maximum size of database connection pool (default is 10)

Project details


Release history Release notifications | RSS feed

This version

2.1.1

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aio-openapi-2.1.1.tar.gz (66.6 kB view details)

Uploaded Source

Built Distributions

aio_openapi-2.1.1-py39-none-any.whl (55.6 kB view details)

Uploaded Python 3.9

aio_openapi-2.1.1-py38-none-any.whl (55.6 kB view details)

Uploaded Python 3.8

aio_openapi-2.1.1-py37-none-any.whl (55.6 kB view details)

Uploaded Python 3.7

aio_openapi-2.1.1-py36-none-any.whl (55.6 kB view details)

Uploaded Python 3.6

File details

Details for the file aio-openapi-2.1.1.tar.gz.

File metadata

  • Download URL: aio-openapi-2.1.1.tar.gz
  • Upload date:
  • Size: 66.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.1.1 requests-toolbelt/0.9.1 tqdm/4.55.1 CPython/3.9.1

File hashes

Hashes for aio-openapi-2.1.1.tar.gz
Algorithm Hash digest
SHA256 adf720b3a350afa454172540b406ac9de2266e69d31b834b03498942f1579719
MD5 e817ab5b679635616ef50c65872ebf23
BLAKE2b-256 743a288fb02bb14eb89c7753964de3e0eff121efe4f5d38305876d3610265af2

See more details on using hashes here.

File details

Details for the file aio_openapi-2.1.1-py39-none-any.whl.

File metadata

  • Download URL: aio_openapi-2.1.1-py39-none-any.whl
  • Upload date:
  • Size: 55.6 kB
  • Tags: Python 3.9
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.1.1 requests-toolbelt/0.9.1 tqdm/4.55.1 CPython/3.9.1

File hashes

Hashes for aio_openapi-2.1.1-py39-none-any.whl
Algorithm Hash digest
SHA256 96c4463cf7d9446b36643bc3b26cce514701c3e1d9c07a72eaa6dbf013d141d2
MD5 a280a17810f253e0141c114c4b137486
BLAKE2b-256 de7e2b846409786a3502b30038ebe95c60ad5ee7a472003d0180b3791aff59ea

See more details on using hashes here.

File details

Details for the file aio_openapi-2.1.1-py38-none-any.whl.

File metadata

  • Download URL: aio_openapi-2.1.1-py38-none-any.whl
  • Upload date:
  • Size: 55.6 kB
  • Tags: Python 3.8
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.1.1 requests-toolbelt/0.9.1 tqdm/4.55.1 CPython/3.8.6

File hashes

Hashes for aio_openapi-2.1.1-py38-none-any.whl
Algorithm Hash digest
SHA256 3abf3d4e68f39dfd3a1d5d243b6734736d71d32d0f1a1ddfb2edde70f21898ed
MD5 691bf2555e48dc394a3954c3b6b58118
BLAKE2b-256 1ffdedd4bee77ac782947eab6c021b5421be370d447bebebdaadebbbd607c05b

See more details on using hashes here.

File details

Details for the file aio_openapi-2.1.1-py37-none-any.whl.

File metadata

  • Download URL: aio_openapi-2.1.1-py37-none-any.whl
  • Upload date:
  • Size: 55.6 kB
  • Tags: Python 3.7
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.1.1 requests-toolbelt/0.9.1 tqdm/4.55.1 CPython/3.7.9

File hashes

Hashes for aio_openapi-2.1.1-py37-none-any.whl
Algorithm Hash digest
SHA256 7f71f059b78571353393a0c03f9d518fc257e30ebedf9c4479e2f0db4b0e0104
MD5 4ebb499f9cc0a32bacd0a4e2a9bff250
BLAKE2b-256 8ae7c89440413116a9493687f3f64f0bbb6ac45bdb0a45ec118e0fc1f52a387f

See more details on using hashes here.

File details

Details for the file aio_openapi-2.1.1-py36-none-any.whl.

File metadata

  • Download URL: aio_openapi-2.1.1-py36-none-any.whl
  • Upload date:
  • Size: 55.6 kB
  • Tags: Python 3.6
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.1.1 requests-toolbelt/0.9.1 tqdm/4.55.1 CPython/3.6.12

File hashes

Hashes for aio_openapi-2.1.1-py36-none-any.whl
Algorithm Hash digest
SHA256 0d51fe991d1a2324e757a0b52d1fee7f34a48a503439dadb1f49ce251ebf7473
MD5 73925953e998b0eb0dbbfa628ce0c79f
BLAKE2b-256 383d102cb25dab6b0055e2b53f90c581c8c224546a5de092e0a5000b08e63d77

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page