# Path: devicehive/api_request.py
# class ApiRequest(object):
#     """Api request class."""
# 
#     def __init__(self, api):
#         self._api = api
#         self._action = None
#         self._request = {}
#         self._params = {'subscription_request': {},
#                         'response_subscription_id_key': 'subscriptionId',
#                         'remove_subscription_request': {},
#                         'method': 'GET',
#                         'url': None,
#                         'request_delete_keys': [],
#                         'request_key': None,
#                         'params': {},
#                         'headers': {},
#                         'response_key': None}
# 
#     @staticmethod
#     def _uuid():
#         return str(uuid.uuid4())
# 
#     @property
#     def http_transport(self):
#         return self._api.transport.name == 'http'
# 
#     @property
#     def websocket_transport(self):
#         return self._api.transport.name == 'websocket'
# 
#     def action(self, action):
#         self._action = action
# 
#     def set(self, key, value, request_key=False):
#         if not value:
#             return
#         self._request[key] = value
#         if not request_key:
#             return
#         self._params['request_key'] = key
# 
#     def subscription_request(self, subscription_api_request):
#         request = subscription_api_request.extract(self._uuid(), self._uuid())
#         self._params['subscription_request'] = request
# 
#     def response_subscription_id_key(self, key):
#         self._params['response_subscription_id_key'] = key
# 
#     def remove_subscription_request(self, remove_subscription_api_request):
#         request = remove_subscription_api_request.extract()
#         self._params['remove_subscription_request'] = request
# 
#     def method(self, method):
#         self._params['method'] = method
# 
#     def url(self, url, **args):
#         for key in args:
#             value = args[key]
#             url = url.replace('{%s}' % key, str(value))
#             self._request[key] = value
#             self._params['request_delete_keys'].append(key)
#         self._params['url'] = url
# 
#     def param(self, key, value):
#         if not value:
#             return
#         self._request[key] = value
#         self._params['request_delete_keys'].append(key)
#         self._params['params'][key] = value
# 
#     def header(self, name, value):
#         self._params['headers'][name] = value
# 
#     def response_key(self, key):
#         self._params['response_key'] = key
# 
#     def execute(self, error_message):
#         request_id = self._uuid()
#         request = self._request.copy()
#         logger.debug('Request id: %s. Action: %s. Request: %s. Params: %s.',
#                      request_id, self._action, request, self._params)
#         response = self._api.transport.request(request_id, self._action,
#                                                request, **self._params)
#         api_response = ApiResponse(response, self._params['response_key'])
#         logger.debug('Response id: %s. Action: %s. Success: %s. Response: %s.',
#                      api_response.id, api_response.action, api_response.success,
#                      api_response.response)
#         if api_response.success:
#             return api_response.response
#         raise ApiResponseError(error_message, self._api.transport.name,
#                                api_response.code, api_response.error)
# 
# Path: devicehive/api_request.py
# class ApiRequestError(TransportError):
#     """Api request error."""


from devicehive.api_request import ApiRequest
from devicehive.api_request import ApiRequestError
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# =============================================================================




class Token(object):
    """Token class."""

    AUTH_HEADER_NAME = 'Authorization'
    AUTH_HEADER_VALUE_PREFIX = 'Bearer '

    def __init__(self, api, auth):
        self._api = api
        self._login = auth.get('login')
        self._password = auth.get('password')
        self._refresh_token = auth.get('refresh_token')
        self._access_token = auth.get('access_token')

    def _auth(self):
        api_request = ApiRequest(self._api)
        if not api_request.websocket_transport:
            return
        api_request.action('authenticate')
        api_request.set('token', self._access_token)
        api_request.execute('Authentication failure.')

    def _tokens(self):

# Path: devicehive/api_request.py
# class ApiRequest(object):
#     """Api request class."""
# 
#     def __init__(self, api):
#         self._api = api
#         self._action = None
#         self._request = {}
#         self._params = {'subscription_request': {},
#                         'response_subscription_id_key': 'subscriptionId',
#                         'remove_subscription_request': {},
#                         'method': 'GET',
#                         'url': None,
#                         'request_delete_keys': [],
#                         'request_key': None,
#                         'params': {},
#                         'headers': {},
#                         'response_key': None}
# 
#     @staticmethod
#     def _uuid():
#         return str(uuid.uuid4())
# 
#     @property
#     def http_transport(self):
#         return self._api.transport.name == 'http'
# 
#     @property
#     def websocket_transport(self):
#         return self._api.transport.name == 'websocket'
# 
#     def action(self, action):
#         self._action = action
# 
#     def set(self, key, value, request_key=False):
#         if not value:
#             return
#         self._request[key] = value
#         if not request_key:
#             return
#         self._params['request_key'] = key
# 
#     def subscription_request(self, subscription_api_request):
#         request = subscription_api_request.extract(self._uuid(), self._uuid())
#         self._params['subscription_request'] = request
# 
#     def response_subscription_id_key(self, key):
#         self._params['response_subscription_id_key'] = key
# 
#     def remove_subscription_request(self, remove_subscription_api_request):
#         request = remove_subscription_api_request.extract()
#         self._params['remove_subscription_request'] = request
# 
#     def method(self, method):
#         self._params['method'] = method
# 
#     def url(self, url, **args):
#         for key in args:
#             value = args[key]
#             url = url.replace('{%s}' % key, str(value))
#             self._request[key] = value
#             self._params['request_delete_keys'].append(key)
#         self._params['url'] = url
# 
#     def param(self, key, value):
#         if not value:
#             return
#         self._request[key] = value
#         self._params['request_delete_keys'].append(key)
#         self._params['params'][key] = value
# 
#     def header(self, name, value):
#         self._params['headers'][name] = value
# 
#     def response_key(self, key):
#         self._params['response_key'] = key
# 
#     def execute(self, error_message):
#         request_id = self._uuid()
#         request = self._request.copy()
#         logger.debug('Request id: %s. Action: %s. Request: %s. Params: %s.',
#                      request_id, self._action, request, self._params)
#         response = self._api.transport.request(request_id, self._action,
#                                                request, **self._params)
#         api_response = ApiResponse(response, self._params['response_key'])
#         logger.debug('Response id: %s. Action: %s. Success: %s. Response: %s.',
#                      api_response.id, api_response.action, api_response.success,
#                      api_response.response)
#         if api_response.success:
#             return api_response.response
#         raise ApiResponseError(error_message, self._api.transport.name,
#                                api_response.code, api_response.error)
# 
# Path: devicehive/api_request.py
# class ApiRequestError(TransportError):
#     """Api request error."""


# Path: devicehive/token.py
from devicehive.api_request import ApiRequest
from devicehive.api_request import ApiRequestError

# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# =============================================================================




class Token(object):
    """Token class."""

    AUTH_HEADER_NAME = 'Authorization'
    AUTH_HEADER_VALUE_PREFIX = 'Bearer '

    def __init__(self, api, auth):
        self._api = api
        self._login = auth.get('login')
        self._password = auth.get('password')
        self._refresh_token = auth.get('refresh_token')
        self._access_token = auth.get('access_token')

    def _auth(self):
        api_request = ApiRequest(self._api)
        if not api_request.websocket_transport:
            return
        api_request.action('authenticate')
        api_request.set('token', self._access_token)
        api_request.execute('Authentication failure.')

    def _tokens(self):

        api_request = ApiRequest(self._api)