Skip to main content

dataclassesapi

dataclassesapi

Asgi App using typing annotation


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

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


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

Instalation

$ pip install dataclassesapi

Basic example

from dataclasses import dataclass
from http import HTTPStatus

from dataclassesjson import dataclassjson

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


@dataclass
class MyQuery(Query):
    name: str


@dataclass
class MyRequest(Request):
    query: MyQuery


@dataclass
class MyResponseBody(ResponseBody):
    message: str


@dataclassjson
@dataclass
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 dataclasses import dataclass
from http import HTTPStatus

from dataclassesjson import dataclassjson, integer, string

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


@dataclass
class MyPathArgs(PathArgs):
    name: str


@dataclass
class MyQuery(Query):
    location: str


@dataclass
class MyHeaders(Headers):
    x_req_id: str


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


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


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


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


@dataclassjson
@dataclass
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}}

Metadata

Release files for dataclassesapi 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for dataclassesapi 0.2.0
File Size Uploaded
dataclassesapi-0.2.0.tar.gz 23.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for dataclassesapi 0.2.0
File Interpreter ABI Platform
dataclassesapi-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 49.6 kB

Release files / dataclassesapi-0.2.0.tar.gz

Download URL dataclassesapi-0.2.0.tar.gz
Size 23.5 kB
Tags Source
SHA-256 checksum
How to use checksums
b63727d480f22d690ae03f5136872f1dc10f26a2dca3b519d263d62ee3226346
BLAKE2b-256 checksum
How to use checksums
29717e439dcb4f38df3f3b3c779d9aa986486fa32c869796dbce679bed3e7fd5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.22.0

Release files / dataclassesapi-0.2.0-py3-none-any.whl

Download URL dataclassesapi-0.2.0-py3-none-any.whl
Size 26.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
03148da85c78b7be32bd43308c0383740013fb8fa9cf2de77801ed60165fc685
BLAKE2b-256 checksum
How to use checksums
a42a79eef2e3f88de83cf1eaaf04f61785d6b48e6d93e3ef0d39d0f37c5025d2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.22.0

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page