Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

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

  • Declare request objects as @jsondaora (can be TypedDict or @dataclass)
  • Declare response objects as @jsondaora (can be TypedDict or @dataclass)

Requirements

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

Instalation

$ pip install apidaora

Basic example

from dataclasses import dataclass

from apidaora import JSONResponse, MethodType, appdaora, path


@dataclass
class Response(JSONResponse):
    body: str


@path('/hello', MethodType.GET)
def controller(name: str) -> Response:
    message = f'Hello {name}!'
    return Response(body=message)


app = appdaora(operations=[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!"

Example for more request/response details

from typing import Optional, TypedDict, Union

from jsondaora import integer, jsondaora, string

from apidaora import (
    JSONRequestBody,
    JSONResponse,
    MethodType,
    appdaora,
    header_param,
    path,
)


# Domain


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


@jsondaora
class HelloMessage(TypedDict):
    message: str
    about_you: You


async def hello_you_message(you: You, location: str) -> HelloMessage:
    return HelloMessage(
        message=await hello_message(you['name'], location), about_you=you
    )


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


# Application


@jsondaora
class RequestBody(JSONRequestBody):
    Content = TypedDict('Content', {'last_name': str, 'age': str})
    content: Content


@jsondaora
class Response(JSONResponse):
    Headers = TypedDict('Headers', {'x_req_id': int})
    headers: Headers
    body: Union[HelloMessage, str]


@path('/hello/{name}', MethodType.PUT)
async def controller(
    name: str,
    location: string(max_length=100),
    req_id: header_param(schema=Optional[int], name='x-req-id'),
    queries: Optional[str] = None,
    body: Optional[RequestBody] = None,
) -> Response:
    if body:
        message = await hello_you_message(
            You(
                name=name,
                last_name=body.content['last_name'],
                age=body.content['age'],
                location=location,
            ),
            location,
        )

    else:
        message = await hello_message(name, location)

    return Response(body=message, headers=Response.Headers(x_req_id=req_id))


app = appdaora(operations=[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-req-id: 1243567890' \
    -d '{"last_name":"My Self","age":32}'
HTTP/1.1 200 OK
date: Thu, 1st January 1970 00:00:00 GMT
server: uvicorn
x-req-id: 1243567890
content-type: application/json
content-length: 117

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

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.6.0a5.tar.gz (31.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.6.0a5-py3-none-any.whl (51.8 kB view details)

Uploaded Python 3

File details

Details for the file apidaora-0.6.0a5.tar.gz.

File metadata

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

File hashes

Hashes for apidaora-0.6.0a5.tar.gz
Algorithm Hash digest
SHA256 6bcee36933af7bf9a13a0e1d34a4daecc848e890acfaf6fe902ded25ef7567af
MD5 367e1969eccc1589957ae4178e22d28a
BLAKE2b-256 1ab1e7036eff31761c0ad887e5a43bc66a5923838231a881cbe25c55a9a6144a

See more details on using hashes here.

File details

Details for the file apidaora-0.6.0a5-py3-none-any.whl.

File metadata

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

File hashes

Hashes for apidaora-0.6.0a5-py3-none-any.whl
Algorithm Hash digest
SHA256 da2d6018480c7c77f6c30720f8265b3af9216d5c7e570dd1ba51b0efaf17fec4
MD5 f3a51d5a793bc85319521e5b509fdd87
BLAKE2b-256 9f12dca220f65bebec54313b458903f2f68c8765d3843895de086f663c2bef85

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