77
88import time
99
10+
1011def timeit (f ):
1112 """
1213 Utility used to time the duration of code execution. This script can be composed with any other script.
13-
14+
1415 Usage::\n
1516 def f(n):
1617 return n**n
@@ -25,78 +26,84 @@ def timed(*args, **kw):
2526 ts = time .time ()
2627 result = f (* args , ** kw )
2728 te = time .time ()
28- if type (result )== tuple :
29- return result + ((te - ts ),)
29+ if type (result ) == tuple :
30+ return result + ((te - ts ),)
3031 else :
31- return result ,(te - ts )
32+ return result , (te - ts )
3233 return timed
3334
3435
3536def attach_meta (response , meta , ** kwargs ):
3637 """
3738 Attach a meta dictionary to a response dictionary.
38-
39+
3940 Inputs: :\n
40- (dict) response, (dict) meta, (key-value pairs) kwargs - optional messages to add to mets
41+ (dict) response, (dict) meta, (key-value pairs) kwargs - optional messages to add to mets
4142
4243 Usage: :\n
4344 """
4445 for k , v in kwargs .iteritems ():
4546 meta [k ] = v
46-
47+
4748 response ["meta" ] = meta
4849 return response
4950
51+
5052verification_error = {
51- 'message' :'Failed to Authenticate' ,
52- 'status' :'FAIL' ,
53- 'code' :401
54- }
53+ 'message' : 'Failed to Authenticate' ,
54+ 'status' : 'FAIL' ,
55+ 'code' : 401
56+ }
5557
5658
5759from flask_restful import Api
58- import sys , traceback
60+ import sys
61+ import traceback
62+
63+
5964class NextBackendApi (Api ):
6065 """
6166 Subclass of the default Api class of Flask-Restful with custom error handling for 500 requests
62-
67+
6368 All other errors are passed onto the default handle_error.
6469 """
70+
6571 def handle_error (self , e , ** kwargs ):
6672 exc_type , exc_value , tb = sys .exc_info ()
67- backend_error = traceback .format_exc (tb )
68- print "backend_error" , backend_error ,exc_type , exc_value , tb , traceback .format_exc (tb )
73+ backend_error = traceback .format_exc (tb )
74+ print "backend_error" , backend_error , exc_type , exc_value , tb , traceback .format_exc (tb )
6975
7076 # Catch internal system errors
7177 code = getattr (e , 'code' , 500 )
72- if code == 500 :
78+ if code == 500 :
7379 response = {
74- 'meta' :{
80+ 'meta' : {
7581 'status' : 'FAIL' ,
7682 'code' : 500 ,
7783 'message' : 'Internal Server Error' ,
7884 'backend_error' : backend_error
7985 }
8086 }
81- return self .make_response (response , 500 )
82- return super (NextBackendApi , self ).handle_error (e )
83-
87+ return self .make_response (response , 500 )
88+ return super (NextBackendApi , self ).handle_error (e )
8489
8590
8691from flask_restful .reqparse import Argument
8792from flask_restful import abort
8893
94+
8995class APIArgument (Argument ):
9096 """
9197 Subclass of the standard flask restful Argument class to provide a custom meta message.
9298 Passes up a 400 message if arguments are not correctly parsed.
9399 """
100+
94101 def __init__ (self , * args , ** kwargs ):
95102 """
96103 Pass up the default arguments.
97104 """
98105 super (APIArgument , self ).__init__ (* args , ** kwargs )
99-
106+
100107 def handle_validation_error (self , error , bundle_errors ):
101108 """
102109 Called when an error is raised while parsing. Aborts the request
@@ -110,12 +117,13 @@ def handle_validation_error(self, error, bundle_errors):
110117 msg = '[%s]: %s%s' % (self .name , help_str , str (error ))
111118 if bundle_errors :
112119 return error , msg
113- return abort (400 , meta = {'message' :msg , 'code' :400 , 'status' :'FAIL' })
120+ return abort (400 , meta = {'message' : msg , 'code' : 400 , 'status' : 'FAIL' })
114121
115122
116123# Custom Exception types for the api. These should just pass.
117124class DatabaseException (Exception ):
118125 pass
119126
127+
120128class BackendConnectionException (Exception ):
121129 pass
0 commit comments