<commit_before>from limits import storage
from limits import strategies
from limits import parse
from sanic import exceptions


class TooManyRequests(exceptions.InvalidUsage):
    status_code = 429


_MEMORY_STORAGE = storage.MemoryStorage()
_MOVING_WINDOW = strategies.MovingWindowRateLimiter(_MEMORY_STORAGE)

_LIMIT_MESSAGE = "Paste creation limited at 6 per minute."


def limiter(frequency):
    frequency = parse(frequency)

    def decorator(function):
        print(function)
        def wrapper(request, *args, **kwargs):
            namespace = f"{function.__module__}.{function.__name__}"
            ip = request.ip[0] or '127.0.0.1'
            print(namespace, ip)
            print(_MOVING_WINDOW.get_window_stats(frequency, namespace, ip))
            if not _MOVING_WINDOW.hit(frequency, namespace, ip):
                raise TooManyRequests(_LIMIT_MESSAGE)
            return function(request, *args, **kwargs)
        return wrapper
    return decorator<commit_msg>Handle Heroku's IP forwarding<commit_after>from limits import storage
from limits import strategies
from limits import parse
from sanic import exceptions


class TooManyRequests(exceptions.InvalidUsage):
    status_code = 429


_MEMORY_STORAGE = storage.MemoryStorage()
_MOVING_WINDOW = strategies.MovingWindowRateLimiter(_MEMORY_STORAGE)

_LIMIT_MESSAGE = "Paste creation limited at 6 per minute."


def limiter(frequency):
    frequency = parse(frequency)

    def decorator(function):
        def wrapper(request, *args, **kwargs):
            namespace = f"{function.__module__}.{function.__name__}"
            ip = request.ip[0] or '127.0.0.1'
            print(ip)
            ip = request.headers.get("X-Forwarded-For", ip)
            print(namespace, ip)
            print(_MOVING_WINDOW.get_window_stats(frequency, namespace, ip))
            if not _MOVING_WINDOW.hit(frequency, namespace, ip):
                raise TooManyRequests(_LIMIT_MESSAGE)
            return function(request, *args, **kwargs)
        return wrapper
    return decorator
{'fraction_non_alphanumeric': 0.08667972575905974, 'fraction_numerical': 0.010773751224289911, 'mean_word_length': 3.292016806722689, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4098930', 'n_tokens_mistral': 589, 'n_tokens_neox': 564, 'n_words': 143}