"""Adresse du visiteur, y compris derrière le proxy Apache."""

from ipaddress import ip_address

from rest_framework.throttling import BaseThrottle


def client_ip(request) -> str | None:
    """Même identification que la limitation d'envois (réglage DRF NUM_PROXIES)."""
    ident = BaseThrottle().get_ident(request)
    try:
        return str(ip_address(ident))
    except ValueError:
        return request.META.get("REMOTE_ADDR")
