"""
Python package to interact with UniFi Controller
"""
import shutil
import time
import warnings
import json
import logging

import requests
from urllib3.exceptions import InsecureRequestWarning


"""For testing purposes:
logging.basicConfig(filename='pyunifi.log', level=logging.WARN,
                    format='%(asctime)s %(message)s')
"""  # pylint: disable=W0105
CONS_LOG = logging.getLogger(__name__)


class APIError(Exception):
    """API Error exceptions"""


def retry_login(func, *args, **kwargs):  # pylint: disable=w0613
    """To reattempt login if requests exception(s) occur at time of call"""

    def wrapper(*args, **kwargs):
        try:
            try:
                return func(*args, **kwargs)
            except (requests.exceptions.RequestException, APIError) as err:
                CONS_LOG.warning("Failed to perform %s due to %s", func, err)
                controller = args[0]
                controller._login()  # pylint: disable=w0212
                return func(*args, **kwargs)
        except Exception as err:
            raise APIError(err)

    return wrapper


class Controller:  # pylint: disable=R0902,R0904

    """Interact with a UniFi controller.

    Uses the JSON interface on port 8443 (HTTPS) to communicate with a UniFi
    controller. Operations will raise unifi.controller.APIError on obvious