Created
January 17, 2015 02:30
-
-
Save imbolc/0deb6a5fad8fab4c678c to your computer and use it in GitHub Desktop.
aiohttp get_argement helpers
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
from aiohttp import web | |
def get_qs_argument(request, *args, **kwargs): | |
return _get_argument(request.GET, *args, **kwargs) | |
def get_url_argument(request, *args, **kwargs): | |
return _get_argument(request.match_info, *args, **kwargs) | |
def _get_argument(container, name, default=None, *, cls=None): | |
arg = container.get(name, default) | |
if arg is None: | |
raise web.HTTPBadRequest( | |
reason='Missing required argument: {}'.format(name)) | |
if cls: | |
try: | |
arg = cls(arg) | |
except Exception: | |
raise web.HTTPBadRequest( | |
reason='Argument is incorrect: {}'.format(name)) | |
return arg |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment