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:
PathArgsfor values on pathQueryfor values on query stringHeadersfor values on headersBodyfor values on body
-
Declare response object as @dataclass:
Headersfor values on headersBodyfor values on body
Requirements
Instalation
$ pip install apidaora
Basic example
from http import HTTPStatus
from jsondaora import jsondaora
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
@jsondaora
class MyQuery(Query):
name: str
@jsondaora
class MyRequest(Request):
query: MyQuery
@jsondaora
class MyResponseBody(ResponseBody):
message: str
@jsondaora
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 jsondaora import integer, jsondaora, string
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
@jsondaora
class MyPathArgs(PathArgs):
name: str
@jsondaora
class MyQuery(Query):
location: str
@jsondaora
class MyHeaders(Headers):
x_req_id: str
@jsondaora
class MyBody(Body):
last_name: str
age: int
@jsondaora
class MyRequest(Request):
path_args: MyPathArgs
query: MyQuery
headers: MyHeaders
body: MyBody
@jsondaora
class You:
name: str
last_name: str
location: string(max_length=100)
age: integer(minimum=18)
@jsondaora
class MyResponseBody(ResponseBody):
hello_message: str
about_you: You
@jsondaora
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file apidaora-0.4.0.tar.gz.
File metadata
- Download URL: apidaora-0.4.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
501883697e1fb3220d704a895dcacd73fd4d8ee546a0d4d8017a280a03255029
|
|
| MD5 |
a1ab3344725db15f6dee5273fe33d92b
|
|
| BLAKE2b-256 |
0f1e70f5a84d23137fe49f9aed0db84a6d7c0dcac99c20ff340d66fac8a294f9
|
File details
Details for the file apidaora-0.4.0-py3-none-any.whl.
File metadata
- Download URL: apidaora-0.4.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4274eb6b5f0e3af7a0a1f05740b2121f96dceead14964c9c629c00b61958c853
|
|
| MD5 |
5fba3191eb789444dda4e032dcc80113
|
|
| BLAKE2b-256 |
1ef49a7a25aacf93149403f7b514db06a90ec3c64f29c20685cbad4178e74a6c
|