Package: dataclassesapi
dataclassesapiAsgi App using typing annotationDocumentation:https://dutradda.github.io/dataclassesapiSource Code:https://github.com/dutradda/dataclassesapiKey FeaturesDeclare request object as @dataclass:PathArgsfor values on pathQueryfor values on query stringHeadersfor values on headersBodyfor values on bodyDeclare response object as @dataclass:Headersfor values on headersBodyfor values on bodyRequirementsPython 3.7+dataclassesjsonfor json validation/parsingorjsonfor json/bytes serializationInstalation$ pip install dataclassesapiBasic examplefromdataclassesimportdataclassfromhttpimportHTTPStatusfromdataclassesjsonimportdataclassjsonfromdataclassesapiimportMethodType,Route,asgi_appfromdataclassesapi.requestimportQuery,Requestfromdataclassesapi.responseimportBodyasResponseBodyfromdataclassesapi.responseimportResponse@dataclassclassMyQuery(Query):name:str@dataclassclassMyRequest(Request):query:MyQuery@dataclassclassMyResponseBody(ResponseBody):message:str@dataclassjson@dataclassclassMyResponse(Response):body:MyResponseBodydefhello_controller(req:MyRequest)->MyResponse:name=req.query.namebody=MyResponseBody(message=f'Hello{name}!')returnMyResponse(HTTPStatus.OK,body=body)app=asgi_app([Route('/hello',MethodType.GET,hello_controller)])Running the server (needs uvicorninstalled):uvicornmyapp:appINFO: 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 curlinstalled):curl-ilocalhost:8000/hello?name=WorldHTTP/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/responsefromdataclassesimportdataclassfromhttpimportHTTPStatusfromdataclassesjsonimportdataclassjson,integer,stringfromdataclassesapiimportMethodType,Route,asgi_appfromdataclassesapi.requestimportBody,Headers,PathArgs,Query,Requestfromdataclassesapi.responseimportBodyasResponseBodyfromdataclassesapi.responseimportResponse@dataclassclassMyPathArgs(PathArgs):name:str@dataclassclassMyQuery(Query):location:str@dataclassclassMyHeaders(Headers):x_req_id:str@dataclassclassMyBody(Body):last_name:strage:int@dataclassclassMyRequest(Request):path_args:MyPathArgsquery:MyQueryheaders:MyHeadersbody:MyBody@dataclassclassYou:name:strlast_name:strlocation:string(max_length=100)age:integer(minimum=18)@dataclassclassMyResponseBody(ResponseBody):hello_message:strabout_you:You@dataclassjson@dataclassclassMyResponse(Response):body:MyResponseBodyheaders:MyHeadersdefhello_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)returnMyResponse(HTTPStatus.OK,body=body,headers=headers)defhello_message(name:str,location:str)->str:returnf'Hello{name}! Welcome to{location}!'app=asgi_app([Route('/hello/{name}',MethodType.PUT,hello_controller)])Running the server:uvicornmyapp:appINFO: 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-XPUTlocalhost: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}}