# Path: cozify/cloud_api.py
# def get(call, headers=None, base=cloudBase, no_headers=False, json_output=True, raw=False,
#         **kwargs):
# def post(call,
#          headers=None,
#          data=None,
#          params=None,
#          base=cloudBase,
#          no_headers=False,
#          raw=False,
#          **kwargs):
# def put(call, headers=None, data=None, base=cloudBase, no_headers=False, raw=False, **kwargs):
# def requestlogin(email, **kwargs):  # pragma: no cover
# def emaillogin(email, otp, **kwargs):
# def lan_ip(**kwargs):
# def hubkeys(cloud_token, **kwargs):  # pragma: no cover
# def refreshsession(cloud_token, **kwargs):  # pragma: no cover
# def remote(apicall, headers, data=None):
# def _call(*,
#           call,
#           method,
#           headers,
#           params=None,
#           data=None,
#           no_headers=False,
#           json_output=True,
#           raw=False,
#           **kwargs):
# 
# Path: cozify/Error.py
# class APIError(Exception):
#     """Error raised for non-200 API return codes
# 
#     Args:
#         status_code(int): HTTP status code returned by the API
#         message(str): Potential error message returned by the API
#     
#     Attributes:
#         status_code(int): HTTP status code returned by the API
#         message(str): Potential error message returned by the API
#     """
# 
#     def __init__(self, status_code, message):
#         self.status_code = status_code
#         self.message = message
# 
#     def __str__(self):
#         return 'API error, {code}: {message}'.format(code=self.status_code, message=self.message)
# 
# class ConnectionError(ConnectionError):
#     """Error raised for connection level failures,
#     such as a lost internet connection.
# 
#     Args:
#         message(str): Potential error message returned by the requests library
# 
#     Attributes:
#         message(str): Potential error message returned by the requests library
#     """
# 
#     def __init__(self, message):
#         self.message = message
# 
#     def __str__(self):
#         return 'Connection error: {}'.format(self.message)


import requests
import json
from absl import logging
from cozify import cloud_api
from .Error import APIError, ConnectionError

    Args:
        call(str): Full API path to call.
        method(function): requests.get|put function to use for call.
    """
    response = None
    headers = {}
    if 'headers' in kwargs:
        raise ValueError('Headers already defined: {}'.format(kwargs['headers']))
    if hub_token_header:
        if 'hub_token' not in kwargs:
            raise AttributeError('Asked to do a call to the hub but no hub_token provided.')
        headers['Authorization'] = kwargs['hub_token']
    if data is not None:
        data = json.dumps(data)
        headers['content-type'] = 'application/json'

    if 'remote' in kwargs and kwargs['remote']:  # remote call
        if 'cloud_token' not in kwargs:
            raise AttributeError('Asked to do remote call but no cloud_token provided.')
        headers['Authorization'] = kwargs['cloud_token']
        headers['X-Hub-Key'] = kwargs['hub_token']
        response = cloud_api.remote(apicall=call, data=data, headers=headers)
    else:  # local call
        if 'host' not in kwargs or not kwargs['host']:
            raise AttributeError(
                'Local call but no hostname was provided. Either set keyword remote or host.')
        try:
            response = method(_getBase(**kwargs) + call, headers=headers, data=data, timeout=5)
        except requests.exceptions.RequestException as e:  # pragma: no cover

# Path: cozify/cloud_api.py
# def get(call, headers=None, base=cloudBase, no_headers=False, json_output=True, raw=False,
#         **kwargs):
# def post(call,
#          headers=None,
#          data=None,
#          params=None,
#          base=cloudBase,
#          no_headers=False,
#          raw=False,
#          **kwargs):
# def put(call, headers=None, data=None, base=cloudBase, no_headers=False, raw=False, **kwargs):
# def requestlogin(email, **kwargs):  # pragma: no cover
# def emaillogin(email, otp, **kwargs):
# def lan_ip(**kwargs):
# def hubkeys(cloud_token, **kwargs):  # pragma: no cover
# def refreshsession(cloud_token, **kwargs):  # pragma: no cover
# def remote(apicall, headers, data=None):
# def _call(*,
#           call,
#           method,
#           headers,
#           params=None,
#           data=None,
#           no_headers=False,
#           json_output=True,
#           raw=False,
#           **kwargs):
# 
# Path: cozify/Error.py
# class APIError(Exception):
#     """Error raised for non-200 API return codes
# 
#     Args:
#         status_code(int): HTTP status code returned by the API
#         message(str): Potential error message returned by the API
#     
#     Attributes:
#         status_code(int): HTTP status code returned by the API
#         message(str): Potential error message returned by the API
#     """
# 
#     def __init__(self, status_code, message):
#         self.status_code = status_code
#         self.message = message
# 
#     def __str__(self):
#         return 'API error, {code}: {message}'.format(code=self.status_code, message=self.message)
# 
# class ConnectionError(ConnectionError):
#     """Error raised for connection level failures,
#     such as a lost internet connection.
# 
#     Args:
#         message(str): Potential error message returned by the requests library
# 
#     Attributes:
#         message(str): Potential error message returned by the requests library
#     """
# 
#     def __init__(self, message):
#         self.message = message
# 
#     def __str__(self):
#         return 'Connection error: {}'.format(self.message)


# Path: cozify/hub_api.py
import requests
import json
from absl import logging
from cozify import cloud_api
from .Error import APIError, ConnectionError


    Args:
        call(str): Full API path to call.
        method(function): requests.get|put function to use for call.
    """
    response = None
    headers = {}
    if 'headers' in kwargs:
        raise ValueError('Headers already defined: {}'.format(kwargs['headers']))
    if hub_token_header:
        if 'hub_token' not in kwargs:
            raise AttributeError('Asked to do a call to the hub but no hub_token provided.')
        headers['Authorization'] = kwargs['hub_token']
    if data is not None:
        data = json.dumps(data)
        headers['content-type'] = 'application/json'

    if 'remote' in kwargs and kwargs['remote']:  # remote call
        if 'cloud_token' not in kwargs:
            raise AttributeError('Asked to do remote call but no cloud_token provided.')
        headers['Authorization'] = kwargs['cloud_token']
        headers['X-Hub-Key'] = kwargs['hub_token']
        response = cloud_api.remote(apicall=call, data=data, headers=headers)
    else:  # local call
        if 'host' not in kwargs or not kwargs['host']:
            raise AttributeError(
                'Local call but no hostname was provided. Either set keyword remote or host.')
        try:
            response = method(_getBase(**kwargs) + call, headers=headers, data=data, timeout=5)
        except requests.exceptions.RequestException as e:  # pragma: no cover

            raise ConnectionError(str(e)) from None