#NEWLINE# def rate_limit( limit , per , scope_func = ( lambda : request . remote_addr ) ) : #NEWLINE# #INDENT# def decorator( f ) : #NEWLINE# #INDENT# @ functools . wraps ( f ) #NEWLINE# def wrapped( * args , **kwargs ) : #NEWLINE# #INDENT# if current_app . config [ 'USE_RATE_LIMITS' ] : #NEWLINE# #INDENT# key = ( 'rate-limit/%s/%s/' % ( f . __name__ , scope_func ( ) ) ) #NEWLINE# limiter = RateLimit ( key , limit , per ) #NEWLINE# if ( not limiter . over_limit ) : #NEWLINE# #INDENT# rv = f ( * args , ** kwargs ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# rv = too_many_requests ( 'You have exceeded your request rate' ) #NEWLINE# #UNINDENT# g . headers = { #NEWLINE# #INDENT# 'X-RateLimit-Remaining' : str ( limiter . remaining ) , #NEWLINE# 'X-RateLimit-Limit' : str ( limiter . limit ) , #NEWLINE# 'X-RateLimit-Reset' : str ( limiter . reset ) , #NEWLINE# #UNINDENT# } #NEWLINE# return rv #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# return f ( * args , ** kwargs ) #NEWLINE# #UNINDENT# #UNINDENT# return wrapped #NEWLINE# #UNINDENT# return decorator