Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save daltonmatos/1bffddc9ca4f36a801cf874b1e37f521 to your computer and use it in GitHub Desktop.
Save daltonmatos/1bffddc9ca4f36a801cf874b1e37f521 to your computer and use it in GitHub Desktop.
Blog Post: Asyncworker: Handler HTTP recebendo mais do que Request
from aiohttp.web import Request, json_response
from pydantic import BaseModel
from asyncworker import App, RouteTypes
from asyncworker.routes import call_http_handler
app = App()
class UserResource(BaseModel):
name: str
def parse_body(handler):
async def _handler_wrapper(request: Request):
body_data = await request.json()
user_resource = UserResource(**body_data)
request["types_registry"].set(user_resource)
return await call_http_handler(request, handler)
return _handler_wrapper
@app.route(["/users"], type=RouteTypes.HTTP, methods=["POST"])
@parse_body
async def users(body: UserResource):
return json_response(body.dict())
app.run()
@app.route(["/users"], type=RouteTypes.HTTP, methods=["POST"])
async def users(request: Request):
user_model = await request.json()
return json_response(user_model)
from aiohttp.web import Request, json_response
from asyncworker import App, RouteTypes
app = App()
@app.route(["/users"], type=RouteTypes.HTTP, methods=["POST"])
async def users(request: Request):
user_model = await request.json()
return json_response(user_model)
app.run()
@app.route(["/users"], type=RouteTypes.HTTP, methods=["POST"])
@parse_body
async def users(body: UserResource):
return json_response(body.dict())
def parse_body(handler):
async def _handler_wrapper(request: Request):
body_data = await request.json()
user_resource = UserResource(**body_data)
request["types_registry"].set(user_resource)
return await call_http_handler(request, handler)
return _handler_wrapper
def parse_body(handler):
async def _handler_wrapper(request: Request):
return await call_http_handler(request, handler)
return _handler_wrapper
from pydantic import BaseModel
class UserResource(BaseModel):
name: str
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment