commit 4a998817a8ca56749d73e5aebeb74e57c130e3c2 Author: Chris Wacek cwacek@cs.georgetown.edu Date: Tue Jan 15 15:41:56 2013 -0500
Provided default arguments for all parameters.
Now a blank request will no longer fail with a server error and will instead return a default set of data. --- app.py | 40 ++++++++++++++++++++++------------------ 1 files changed, 22 insertions(+), 18 deletions(-)
diff --git a/app.py b/app.py index d68c5b9..73270d7 100644 --- a/app.py +++ b/app.py @@ -8,25 +8,30 @@ from flask import Flask, request, jsonify, render_template,Response app = Flask(__name__)
class Opt(object): - request_types = { - 'by_as':Boolean, - 'by_country':Boolean, - 'inactive':Boolean, - 'exits_only':Boolean, - 'guards_only': Boolean, - 'links':Boolean, - 'sort':NullFn, - 'sort_reverse':Boolean, - 'top':Int, - 'family':NullFn, - 'ases':List, - 'country':JSON, - 'exit_filter':NullFn + option_details = { + 'by_as':(Boolean, False), + 'by_country':(Boolean, False), + 'inactive':( Boolean, False ), + 'exits_only':( Boolean, False ), + 'guards_only': ( Boolean, False), + 'links':( Boolean, True ), + 'sort':( NullFn, "cw" ), + 'sort_reverse':( Boolean, True ), + 'top':( Int , 10), + 'family':( NullFn, "" ), + 'ases':( List, [] ), + 'country':( JSON, [] ), + 'exit_filter':( NullFn, "all_relays" ) }
+ @staticmethod def convert(key,val): - return Opt.request_types[key](val) + return Opt.option_details[key][0](val) + + @staticmethod + def default(key): + return Opt.option_details[key][1]
def __str__(self): return repr(self) @@ -35,12 +40,11 @@ class Opt(object): return str(self.__dict__)
def __init__(self,request): - - for key in Opt.request_types: + for key in Opt.option_details: if key in request: setattr(self,key,Opt.convert(key,request[key])) else: - setattr(self,key,Opt.convert(key,None)) + setattr(self,key,Opt.default(key))
def parse(output_string, grouping=False, sort_key=None): results = []