Package: apidaora
apidaoraOpenAPI / HTTP / REST API usingdataclassesandTypedDictannotation for pythonDocumentation:https://dutradda.github.io/apidaoraSource Code:https://github.com/dutradda/apidaoraKey FeaturesDeclaration of request/response as dataclasses and dicts using typing annotationsInput data validation withjsondaoraOne of thefastestpython api frameworkCan run on any asgi serverRequirementsPython 3.8+jsondaorafor json validation/parsingorjsonfor json/bytes serialization (jsondaora dependency)Instalation$ pip install apidaoraSimple examplefromapidaoraimportappdaora,route@route.get('/hello')defhello_controller(name:str)->str:returnf'Hello{name}!'app=appdaora(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: 14

"Hello World!"Basic examplefromtypingimportTypedDictfromjsondaoraimportIntegerField,StringField,jsondaorafromapidaoraimportHeader,appdaora,routeclassInteger(IntegerField,minimum=18):...classString(StringField,max_length=100):...classAge(Header,type=Integer):...@jsondaoraclassYou(TypedDict):name:strlast_name:strlocation:strage:int@jsondaoraclassReqBody(TypedDict):last_name:str@jsondaoraclassHelloOutput(TypedDict):hello_message:strabout_you:You@route.put('/hello/{name}')asyncdefhello_controller(name:str,location:String,age:Age,body:ReqBody)->HelloOutput:you=You(name=name,location=location.value,age=age.value.value,last_name=body['last_name'],)returnHelloOutput(hello_message=awaithello_message(name,location.value),about_you=you)asyncdefhello_message(name:str,location:str)->str:returnf'Hello{name}! Welcome to{location}!'app=appdaora(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-age: 32'\-d'{"last_name":"My Self"}'HTTP/1.1 200 OK
date: Thu, 1st January 1970 00:00:00 GMT
server: uvicorn
content-type: application/json
content-length: 123

{"hello_message":"Hello Me! Welcome to World!","about_you":{"name":"Me","location":"World","age":32,"last_name":"My Self"}}Example for more request/response detailsfromhttpimportHTTPStatusfromtypingimportDictfromjsondaoraimportjsondaorafromapidaoraimportBadRequestError,Header,Response,appdaora,json,route# Domain layer, here are the domain related definitions# it is apidaora/framework/http independent@jsondaoraclassYou:name:strlast_name:strage:intDB:Dict[str,You]={}defadd_you(you:You)->None:ifyou.nameinDB:raiseYouAlreadyBeenAddedError(you.name)DB[you.name]=youdefget_you(name:str)->You:try:returnDB[name]exceptKeyError:raiseYouWereNotFoundError(name)classDBError(Exception):@propertydefinfo(self)->Dict[str,str]:return{'name':self.args[0]}classYouAlreadyBeenAddedError(DBError):name='you-already-been-added'classYouWereNotFoundError(DBError):name='you-were-not-found'# Application layer, here are the http related definitions# See: https://dutrdda.github.io/apidaora/tutorial/headers/classReqID(Header,type=str,http_name='http_req_id'):...@route.post('/you/')asyncdefadd_you_controller(req_id:ReqID,body:You)->Response:try:add_you(body)exceptYouAlreadyBeenAddedErroraserror:raiseBadRequestError(name=error.name,info=error.info)fromerrorreturnjson(body,HTTPStatus.CREATED,headers=(req_id,))@route.get('/you/{name}')asyncdefget_you_controller(name:str,req_id:ReqID)->Response:try:returnjson(get_you(name),headers=(req_id,))exceptYouWereNotFoundErroraserror:raiseBadRequestError(name=error.name,info=error.info)fromerrorapp=appdaora([add_you_controller,get_you_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-XPOST-ilocalhost:8000/you/-H'http_req_id: 1a2b3c4d'-d'{"name":"Me","last_name":"Myself","age":32}'HTTP/1.1 201 Created
date: Thu, 1st January 1970 00:00:00 GMT
server: uvicorn
content-type: application/json
content-length: 43
http_req_id: 1a2b3c4d

{"name":"Me","last_name":"Myself","age":32}curl-ilocalhost:8000/you/Me-H'http_req_id: 4d3c2b1a'HTTP/1.1 200 OK
date: Thu, 1st January 1970 00:00:00 GMT
server: uvicorn
content-type: application/json
content-length: 43
http_req_id: 4d3c2b1a

{"name":"Me","last_name":"Myself","age":32}BenchmarkThe full results can be foundhere