Last active
January 4, 2020 02:01
-
-
Save daltonmatos/1bffddc9ca4f36a801cf874b1e37f521 to your computer and use it in GitHub Desktop.
Blog Post: Asyncworker: Handler HTTP recebendo mais do que Request
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@app.route(["/users"], type=RouteTypes.HTTP, methods=["POST"]) | |
async def users(request: Request): | |
user_model = await request.json() | |
return json_response(user_model) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@app.route(["/users"], type=RouteTypes.HTTP, methods=["POST"]) | |
@parse_body | |
async def users(body: UserResource): | |
return json_response(body.dict()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def parse_body(handler): | |
async def _handler_wrapper(request: Request): | |
return await call_http_handler(request, handler) | |
return _handler_wrapper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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