def ratelimit():
 def decorator(f):
  @wraps(f)
  def rate_limited(*args, **kwargs):
   method_limit = app.config.get(('RATE_LIMIT_' + request.method))
   if (method_limit and app.redis):
    limit = method_limit[0]
    period = method_limit[1]
    key = ('rate-limit/%s' % (request.authorization.username if request.authorization else request.remote_addr))
    rlimit = RateLimit(key, limit, period, True)
    if rlimit.over_limit:
     return Response('Rate limit exceeded', 429)
    g._rate_limit = rlimit
   else:
    g._rate_limit = None
   return f(*args, **kwargs)
  return rate_limited
 return decorator

def ratelimit ( ) : def decorator ( f ) : @ wraps ( f ) def rate_limited ( * args , ** kwargs ) : method_limit = app . config . get ( ( 'RATE_LIMIT_' + request . method ) ) if ( method_limit and app . redis ) : limit = method_limit [ 0 ] period = method_limit [ 1 ] key = ( 'rate-limit/%s' % ( request . authorization . username if request . authorization else request . remote_addr ) ) rlimit = RateLimit ( key , limit , period , True ) if rlimit . over_limit : return Response ( 'Rate limit exceeded' , 429 ) g . _rate_limit = rlimit else : g . _rate_limit = None return f ( * args , ** kwargs ) return rate_limited return decorator
enables support for rate-limits on api methods the key is constructed by default from the remote address or the authorization .