import requests


def poll_ngrok_for_url(host: str) -> str:
    """
    Poll Ngrok client API to get public https:// URL. Maximum time: 10 seconds

    :param host: Ngrok host to poll
    :return:
    """
    # noinspection HttpUrlsUsage
    url = f'http://{host}:4040/api/tunnels'
    with requests.Session() as session:
        with session.get(url) as response:
            response.raise_for_status()
            data = response.json()
    # we are looking for an HTTPS tunnel. KeyError and StopIteration indicate that no HTTPS tunnel exists ... yet
    tunnel = next(tunnel for tunnel in data['tunnels']
                  if tunnel['proto'] == 'https')
    return tunnel['public_url']