"""Util for SLICES_FORCE_IP_VERSION env var support."""

import os

from httpx import BaseTransport, HTTPTransport


def get_httpx_transport_for_ip_version() -> BaseTransport | None:
    """Get the httpx transport needed according to SLICES_FORCE_IP_VERSION.

    SLICES_FORCE_IP_VERSION can be left unset, or be set to `4` or `6`.
    It forces the library to use only IPv4 or only IPv6 when making API calls.
    """
    if os.environ.get("SLICES_FORCE_IP_VERSION") == "4":
        return HTTPTransport(local_address="0.0.0.0")  # noqa: S104 this only determines local IP address. This does not bind.
    if os.environ.get("SLICES_FORCE_IP_VERSION") == "6":
        return HTTPTransport(local_address="::")  # noqa: S104 this only determines local IP address. This does not bind.
    return None
