def get_query_args(): g.query_args = {} def to_int(q_arg, value): try: return int(value) except ValueError as e: log.exception("Query Argument %s = %s is not a valid int" % (q_arg, value)) e = ValueError( "Expecting integer for argument %s, found %r" % (q_arg, str(value)) ) e.codes = ("INVALID_QUERY_ARGUMENT", 400) raise e def to_str(q_arg, value): return value def to_bool(q_arg, value): value = value.strip().lower() if value in set(["1", "true"]): return True elif value in set(["0", "false"]): return False else: log.exception( "Query Argument %s = %s is not a valid boolean" % (q_arg, value) ) e = ValueError( "Expecting boolean for argument %s, found %r" % (q_arg, str(value)) ) e.codes = ("INVALID_QUERY_ARGUMENT", 400) raise e query_args = OrderedDict( [ ("pretty-print", to_bool), ("start-index", to_int), ("max-results", to_int), ("query", to_str), ("order", to_str), ] ) is_post = request.method == "POST" for arg, cast in query_args.items(): if arg in request.args: g.query_args[arg.replace("-", "_")] = cast(arg, request.args.get(arg)) # POST Query Argument overrides GET Query Argument with the same name if is_post and arg in request.form: g.query_args[arg.replace("-", "_")] = cast(arg, request.form[arg])
def get_query_args(): g.query_args = {} def to_int(q_arg, value): try: return int(value) except ValueError as e: log.exception("Query Argument %s = %s is not a valid int" % (q_arg, value)) e = ValueError("Expecting integer for argument %s, found %r" % (q_arg, str(value))) e.codes = ("INVALID_QUERY_ARGUMENT", 400) raise e def to_str(q_arg, value): return value def to_bool(q_arg, value): value = value.strip().lower() if value in set(["1", "true"]): return True elif value in set(["0", "false"]): return False else: log.exception("Query Argument %s = %s is not a valid boolean" % (q_arg, value)) e = ValueError("Expecting boolean for argument %s, found %r" % (q_arg, str(value))) e.codes = ("INVALID_QUERY_ARGUMENT", 400) raise e query_args = OrderedDict([ ("pretty-print", to_bool), ("start-index", to_int), ("max-results", to_int), ("query", to_str), ("order", to_str), ]) is_post = request.method == "POST" for arg, cast in query_args.items(): if arg in request.args: g.query_args[arg.replace("-", "_")] = cast(arg, request.args.get(arg)) # POST Query Argument overrides GET Query Argument with the same name if is_post and arg in request.form: g.query_args[arg.replace("-", "_")] = cast(arg, request.form[arg])
def get_query_args(): g.query_args = {} def to_int(q_arg, value): try: return int(value) except ValueError as e: log.exception("Query Argument {} = {} is not a valid int".format( q_arg, value)) e = ValueError( "Expecting integer for argument {}, found {!r}".format( q_arg, str(value))) e.codes = ("INVALID_QUERY_ARGUMENT", 400) raise e from None def to_str(q_arg, value): return value def to_bool(q_arg, value): value = value.strip().lower() if value in {"1", "true"}: return True elif value in {"0", "false"}: return False else: log.exception( "Query Argument {} = {} is not a valid boolean".format( q_arg, value)) e = ValueError( "Expecting boolean for argument {}, found {!r}".format( q_arg, str(value))) e.codes = ("INVALID_QUERY_ARGUMENT", 400) raise e query_args = OrderedDict([ ("pretty-print", to_bool), ("start-index", to_int), ("max-results", to_int), ("query", to_str), ("order", to_str), ]) for arg, cast in query_args.items(): if arg in request.args: g.query_args[arg.replace("-", "_")] = cast(arg, request.args.get(arg))
def get_query_args(): g.query_args = {} def to_int(q_arg, value): try: return int(value) except ValueError as e: log.exception('Query Argument %s = %s is not a valid int' % (q_arg, value)) e = ValueError('Expecting integer for argument %s, found %r' % (q_arg, str(value))) e.codes = ('INVALID_QUERY_ARGUMENT', 400) raise e def to_str(q_arg, value): return value def to_bool(q_arg, value): value = value.strip().lower() if value in set(['1', 'true']): return True elif value in set(['0', 'false']): return False else: log.exception('Query Argument %s = %s is not a valid boolean' % (q_arg, value)) e = ValueError('Expecting boolean for argument %s, found %r' % (q_arg, str(value))) e.codes = ('INVALID_QUERY_ARGUMENT', 400) raise e query_args = OrderedDict([('pretty-print', to_bool), ('start-index', to_int), ('max-results', to_int), ('query', to_str), ('order', to_str)]) is_post = request.method == 'POST' for arg, cast in query_args.items(): if arg in request.args: g.query_args[arg.replace('-', '_')] = cast(arg, request.args.get(arg)) # POST Query Argument overrides GET Query Argument with the same name if is_post and arg in request.form: g.query_args[arg.replace('-', '_')] = cast(arg, request.form[arg])