Skip to content

Instantly share code, notes, and snippets.

@imbolc
Created January 17, 2015 02:30
Show Gist options
  • Save imbolc/0deb6a5fad8fab4c678c to your computer and use it in GitHub Desktop.
Save imbolc/0deb6a5fad8fab4c678c to your computer and use it in GitHub Desktop.
aiohttp get_argement helpers
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