import json


def get_and_reserve_next_free_address(prefix: dict, session, fqdn) -> dict:
    """Call API to allocate a new address from prefix."""

    data = {"description": "test_netbox allocated this address"}
    id = prefix["id"]
    result = session.post(
        f"http://{ fqdn }/api/ipam/prefixes/{ id }/available-ips/",
        data=json.dumps(data),
    )

    if result.status_code == 204:
        raise RuntimeError

    return result.json()