Skip to main content

apidaora

apidaora

Asgi App using typing annotation


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

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


Key Features

  • Declare request object as @dataclass:

    • PathArgs for values on path
    • Query for values on query string
    • Headers for values on headers
    • Body for values on body
  • Declare response object as @dataclass:

    • Headers for values on headers
    • Body for values on body

Requirements

  • Python 3.7+
  • typingjson for json validation/parsing
  • orjson for json/bytes serialization

Instalation

$ pip install apidaora

Basic example

from http import HTTPStatus

from typingjson import typingjson

from apidaora import MethodType, Route, asgi_app
from apidaora.request import Query, Request
from apidaora.response import Body as ResponseBody
from apidaora.response import Response


@typingjson
class MyQuery(Query):
    name: str


@typingjson
class MyRequest(Request):
    query: MyQuery


@typingjson
class MyResponseBody(ResponseBody):
    message: str


@typingjson
class MyResponse(Response):
    body: MyResponseBody


def hello_controller(req: MyRequest) -> MyResponse:
    name = req.query['name']
    body = MyResponseBody(message=f'Hello {name}!')
    return MyResponse(HTTPStatus.OK, body=body)


app = asgi_app([Route('/hello', MethodType.GET, 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: 26

{"message":"Hello World!"}

Example for complete request/response

from http import HTTPStatus

from typingjson import integer, string, typingjson

from apidaora import MethodType, Route, asgi_app
from apidaora.request import Body, Headers, PathArgs, Query, Request
from apidaora.response import Body as ResponseBody
from apidaora.response import Response


@typingjson
class MyPathArgs(PathArgs):
    name: str


@typingjson
class MyQuery(Query):
    location: str


@typingjson
class MyHeaders(Headers):
    x_req_id: str


@typingjson
class MyBody(Body):
    last_name: str
    age: int


@typingjson
class MyRequest(Request):
    path_args: MyPathArgs
    query: MyQuery
    headers: MyHeaders
    body: MyBody


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


@typingjson
class MyResponseBody(ResponseBody):
    hello_message: str
    about_you: You


@typingjson
class MyResponse(Response):
    body: MyResponseBody
    headers: MyHeaders


def hello_controller(req: MyRequest) -> MyResponse:
    body = MyResponseBody(
        hello_message=hello_message(
            req.path_args['name'], req.query['location']
        ),
        about_you=You(
            name=req.path_args['name'],
            last_name=req.body['last_name'],
            location=req.query['location'],
            age=req.body['age'],
        ),
    )
    headers = MyHeaders(x_req_id=req.headers['x_req_id'])
    return MyResponse(HTTPStatus.OK, body=body, headers=headers)


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


app = asgi_app([Route('/hello/{name}', MethodType.PUT, 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-req-id: 1a2b3c4d5e6f7g8h' \
    -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: 1a2b3c4d5e6f7g8h
content-type: application/json
content-length: 123

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

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.3.0.tar.gz (23.4 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.3.0-py3-none-any.whl (26.7 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for apidaora-0.3.0.tar.gz
Algorithm Hash digest
SHA256 cdfd113795686ed28fb51671fce6cb46d9f8995d923ea9b8d7caf60353ad2ed0
MD5 222feebd64b0589f5443318e51199cbc
BLAKE2b-256 24aa98e1e1a7c2d03eed450976d14a674072557cf976017a61d82f998b5490f2

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for apidaora-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cda1999cc1ef1488eeb1be3e50c45775fdde78595a285f0423c1509556689f5c
MD5 6116019c336c3b0a9ff8e702b8c1ff32
BLAKE2b-256 7a931e29967f05f8650bc32f178e09643bcca545840ad83fd50e85d1d04d084b

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