"""Throttling utilities."""

# DRF
from rest_framework.throttling import SimpleRateThrottle


class DeviceAnonRateThrottle(SimpleRateThrottle):
    """
    Limits the rate of API calls that may be made by a anonymous users.

    The IP address of the request will be used as the unique cache key.
    """

    scope = "anon"

    def get_cache_key(self, request, view):
        if request.user and hasattr(request, "device"):
            return None  # Only throttle unauthenticated requests.

        return self.cache_format % {
            "scope": self.scope,
            "ident": self.get_ident(request),
        }
