Created
May 21, 2020 13:47
-
-
Save mbuczkowski-gm/b9893c963a56dab411b5c2414165e017 to your computer and use it in GitHub Desktop.
Simplified aiohttp app showing importance of path registration
This file contains hidden or 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
import json | |
from aiohttp import web | |
data = [ | |
{"name": "Andy"}, | |
{"name": "Bob"}, | |
{"name": "Charlie"}, | |
{"name": "Duncan"}, | |
] | |
async def get_single(request): | |
instance_id = int(request.match_info['id']) | |
return web.Response(body=json.dumps(data[instance_id]), content_type="application/json") | |
async def get_all(request): | |
return web.Response(body=json.dumps(data), content_type="application/json") | |
app = web.Application() | |
app.add_routes([web.get('/names/all', get_all)]) | |
app.add_routes([web.get('/names/{id}', get_single)]) | |
web.run_app(app) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment