# -*- coding: utf-8 -*-

"""Main module."""

from urllib.parse import urljoin

import requests

from . import api_versions
from .api_versions import Portainer


def portainer_for_host(host) -> Portainer:
    """
    Finds and returns the right Portainer class for the version
    running on this host
    """
    url = urljoin(host, "api/status")

    try:
        r = requests.request("GET", url).json()
    except requests.exceptions.RequestException as err:
        raise ConnectionError(err)

    return api_versions.get_closest_api_version(r.get("Version"))(host)
