Skip to main content

apidaora

apidaora

OpenAPI / HTTP / REST API using dataclasses and TypedDict annotation for python


Documentation: https://dutradda.github.io/apidaora

Source Code: https://github.com/dutradda/apidaora


Key Features

  • Declaration of request/response as dataclasses and dicts using typing annotations
  • Input data validation with jsondaora
  • One of the fastest python api framework
  • Can run on any asgi server

Requirements

  • Python 3.8+
  • jsondaora for json validation/parsing
  • orjson for json/bytes serialization (jsondaora dependency)

Instalation

$ pip install apidaora

Simple example

from apidaora import appdaora, route


@route.get('/hello')
def hello_controller(name: str):
    return f'Hello {name}!'


app = appdaora(hello_controller)

Running the server (needs uvicorn installed):

uvicorn myapp:app
INFO: Started server process [16220]
INFO: Waiting for application startup.
INFO: ASGI 'lifespan' protocol appears unsupported.
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)

Quering the server (needs curl installed):

curl -i localhost:8000/hello?name=World
HTTP/1.1 200 OK
date: Thu, 1st January 1970 00:00:00 GMT
server: uvicorn
content-type: application/json
content-length: 14

"Hello World!"

Basic example

from typing import TypedDict

from jsondaora import integer, jsondaora, string

from apidaora import appdaora, header, route


Age = header(type=int)


@jsondaora
class You(TypedDict):
    name: str
    last_name: str
    location: string(max_length=100)
    age: integer(minimum=18)


@jsondaora
class ReqBody(TypedDict):
    last_name: str


@jsondaora
class HelloOutput(TypedDict):
    hello_message: str
    abot_you: You


@route.put('/hello/{name}')
async def hello_controller(
    name: str, location: str, age: Age, body: ReqBody
) -> HelloOutput:
    you = You(name=name, location=location, age=age, **body)
    return HelloOutput(
        hello_message=await hello_message(name, location), about_you=you
    )


async def hello_message(name: str, location: str) -> str:
    return f'Hello {name}! Welcome to {location}!'


app = appdaora(hello_controller)

Running the server:

uvicorn myapp:app
INFO: Started server process [16220]
INFO: Waiting for application startup.
INFO: ASGI 'lifespan' protocol appears unsupported.
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)

Quering the server:

curl -i -X PUT localhost:8000/hello/Me?location=World \
    -H 'x-age: 32' \
    -d '{"last_name":"My Self"}'
HTTP/1.1 200 OK
date: Thu, 1st January 1970 00:00:00 GMT
server: uvicorn
content-type: application/json
content-length: 123

{"hello_message":"Hello Me! Welcome to World!","about_you":{"name":"Me","location":"World","age":32,"last_name":"My Self"}}

Example for more request/response details

from http import HTTPStatus

from jsondaora import jsondaora

from apidaora import BadRequestError, JSONResponse, appdaora, header, route


# Domain layer, here are the domain related definitions
# it is apidaora/framework/http independent


@jsondaora
class You:
    name: str
    last_name: str
    age: int


DB = {}


def add_you(you):
    if you.name in DB:
        raise YouAlreadyBeenAddedError(you.name)
    DB[you.name] = you


def get_you(name):
    try:
        return DB[name]
    except KeyError:
        raise YouWereNotFoundError(name)


class DBError(Exception):
    @property
    def info(self):
        return {'name': self.args[0]}


class YouAlreadyBeenAddedError(DBError):
    name = 'you-already-been-added'


class YouWereNotFoundError(DBError):
    name = 'you-were-not-found'


# Application layer, here are the http related definitions

# See: https://dutrdda.github.io/apidaora/tutorial/headers/
ReqID = header(type=str, http_name='http_req_id')


@route.post('/you/')
async def add_you_controller(req_id: ReqID, body: You) -> JSONResponse:
    try:
        add_you(body)
    except YouAlreadyBeenAddedError as error:
        raise BadRequestError(name=error.name, info=error.info) from error

    return JSONResponse(body=body, status=HTTPStatus.CREATED, headers=[req_id])


@route.get('/you/{name}')
async def get_you_controller(name: str, req_id: ReqID) -> JSONResponse:
    try:
        return JSONResponse(get_you(name), headers=[req_id])
    except YouWereNotFoundError as error:
        raise BadRequestError(name=error.name, info=error.info) from error


app = appdaora([add_you_controller, get_you_controller])

Running the server:

uvicorn myapp:app
INFO: Started server process [16220]
INFO: Waiting for application startup.
INFO: ASGI 'lifespan' protocol appears unsupported.
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)

Quering the server:

curl -X POST -i localhost:8000/you/ -H 'http_req_id: 1a2b3c4d' -d '{"name":"Me","last_name":"Myself","age":32}'
HTTP/1.1 201 Created
date: Thu, 1st January 1970 00:00:00 GMT
server: uvicorn
content-type: application/json
content-length: 43

{"name":"Me","last_name":"Myself","age":32}

Benchmark

techempower benchmark

The full results can be found here

Download files

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

Source Distribution

apidaora-0.7.0.tar.gz (132.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

apidaora-0.7.0-py3-none-any.whl (31.6 kB view details)

Uploaded Python 3

File details

Details for the file apidaora-0.7.0.tar.gz.

File metadata

  • Download URL: apidaora-0.7.0.tar.gz
  • Upload date:
  • Size: 132.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.22.0

File hashes

Hashes for apidaora-0.7.0.tar.gz
Algorithm Hash digest
SHA256 df789d3a9c57fae522de65024fa8aca42bbec6abd2322f42e2b754bdc122cab7
MD5 b6859a5630a99395a1734963df38e96a
BLAKE2b-256 3a92129cc641ed160cab1666f0bba11507a036d543b0c4f8ced00df99941f92e

See more details on using hashes here.

File details

Details for the file apidaora-0.7.0-py3-none-any.whl.

File metadata

  • Download URL: apidaora-0.7.0-py3-none-any.whl
  • Upload date:
  • Size: 31.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.22.0

File hashes

Hashes for apidaora-0.7.0-py3-none-any.whl
Algorithm Hash digest
SHA256 db43590d411f219a88ebacf61825e30df2791219540eb0ec273f17c82322a94e
MD5 8aed45c2b09bee055ec22e692fdcf23d
BLAKE2b-256 672a1b52ef6daa78bb59482150211b9f48d83ef3eb2135e9dd2530dde690cf7f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page