# Path: devicehive/api_response.py
# class ApiResponse(object):
#     """Api response class."""
# 
#     SUCCESS_STATUS = 'success'
#     ID_KEY = 'requestId'
#     ACTION_KEY = 'action'
#     STATUS_KEY = 'status'
#     CODE_KEY = 'code'
#     ERROR_KEY = 'error'
# 
#     def __init__(self, response, key=None):
#         self._id = response.pop(self.ID_KEY)
#         self._action = response.pop(self.ACTION_KEY)
#         self._success = response.pop(self.STATUS_KEY) == self.SUCCESS_STATUS
#         self._code = response.pop(self.CODE_KEY, None)
#         self._error = response.pop(self.ERROR_KEY, None)
#         if not key:
#             self._response = response
#             return
#         self._response = response.get(key)
# 
#     @property
#     def id(self):
#         return self._id
# 
#     @property
#     def action(self):
#         return self._action
# 
#     @property
#     def success(self):
#         return self._success
# 
#     @property
#     def code(self):
#         return self._code
# 
#     @property
#     def error(self):
#         return self._error
# 
#     @property
#     def response(self):
#         return self._response
# 
# Path: devicehive/api_response.py
# class ApiResponseError(TransportError):
#     """Api response error."""
# 
#     def __init__(self, message, transport_name, code, error):
#         message = '%s Transport: %s. Code: %s. Error: %s' % (message,
#                                                              transport_name,
#                                                              code, error)
#         super(ApiResponseError, self).__init__(message)
#         self._transport_name = transport_name
#         self._code = code
#         self._error = error
# 
#     @property
#     def transport_name(self):
#         return self._transport_name
# 
#     @property
#     def code(self):
#         return self._code
# 
#     @property
#     def error(self):
#         return self._error
# 
# Path: devicehive/transports/transport.py
# class TransportError(IOError):
#     """Transport error."""


from devicehive.api_response import ApiResponse
from devicehive.api_response import ApiResponseError
from devicehive.transports.transport import TransportError
import uuid
import logging
    def response_timestamp_key(self, key):
        self._params['response_timestamp_key'] = key

    def extract(self, subscription_id, request_id):
        return {'subscription_id': subscription_id,
                'request_id': request_id,
                'action': self._action,
                'request': self._request,
                'params': self._params}


class AuthSubscriptionApiRequest(SubscriptionApiRequest):
    """Auth subscription api request class."""

    def __init__(self, api):
        super(AuthSubscriptionApiRequest, self).__init__()
        auth_header_name, auth_header_value = api.token.auth_header
        self._params['headers'][auth_header_name] = auth_header_value
        self._params['response_error_handler'] = self.response_error_handler
        self._params['response_error_handler_args'] = [api.token]

    @staticmethod
    def response_error_handler(params, response_code, token):
        if response_code != 401:
            return
        try:
            token.auth()
            auth_header_name, auth_header_value = token.auth_header
            params['headers'][auth_header_name] = auth_header_value
            return True

# Path: devicehive/api_response.py
# class ApiResponse(object):
#     """Api response class."""
# 
#     SUCCESS_STATUS = 'success'
#     ID_KEY = 'requestId'
#     ACTION_KEY = 'action'
#     STATUS_KEY = 'status'
#     CODE_KEY = 'code'
#     ERROR_KEY = 'error'
# 
#     def __init__(self, response, key=None):
#         self._id = response.pop(self.ID_KEY)
#         self._action = response.pop(self.ACTION_KEY)
#         self._success = response.pop(self.STATUS_KEY) == self.SUCCESS_STATUS
#         self._code = response.pop(self.CODE_KEY, None)
#         self._error = response.pop(self.ERROR_KEY, None)
#         if not key:
#             self._response = response
#             return
#         self._response = response.get(key)
# 
#     @property
#     def id(self):
#         return self._id
# 
#     @property
#     def action(self):
#         return self._action
# 
#     @property
#     def success(self):
#         return self._success
# 
#     @property
#     def code(self):
#         return self._code
# 
#     @property
#     def error(self):
#         return self._error
# 
#     @property
#     def response(self):
#         return self._response
# 
# Path: devicehive/api_response.py
# class ApiResponseError(TransportError):
#     """Api response error."""
# 
#     def __init__(self, message, transport_name, code, error):
#         message = '%s Transport: %s. Code: %s. Error: %s' % (message,
#                                                              transport_name,
#                                                              code, error)
#         super(ApiResponseError, self).__init__(message)
#         self._transport_name = transport_name
#         self._code = code
#         self._error = error
# 
#     @property
#     def transport_name(self):
#         return self._transport_name
# 
#     @property
#     def code(self):
#         return self._code
# 
#     @property
#     def error(self):
#         return self._error
# 
# Path: devicehive/transports/transport.py
# class TransportError(IOError):
#     """Transport error."""


# Path: devicehive/api_request.py
from devicehive.api_response import ApiResponse
from devicehive.api_response import ApiResponseError
from devicehive.transports.transport import TransportError
import uuid
import logging

    def response_timestamp_key(self, key):
        self._params['response_timestamp_key'] = key

    def extract(self, subscription_id, request_id):
        return {'subscription_id': subscription_id,
                'request_id': request_id,
                'action': self._action,
                'request': self._request,
                'params': self._params}


class AuthSubscriptionApiRequest(SubscriptionApiRequest):
    """Auth subscription api request class."""

    def __init__(self, api):
        super(AuthSubscriptionApiRequest, self).__init__()
        auth_header_name, auth_header_value = api.token.auth_header
        self._params['headers'][auth_header_name] = auth_header_value
        self._params['response_error_handler'] = self.response_error_handler
        self._params['response_error_handler_args'] = [api.token]

    @staticmethod
    def response_error_handler(params, response_code, token):
        if response_code != 401:
            return
        try:
            token.auth()
            auth_header_name, auth_header_value = token.auth_header
            params['headers'][auth_header_name] = auth_header_value
            return True

        except ApiResponseError: