from devicehive.transport import Request
from devicehive.transport import Response


class Api(object):
    """Api class."""

    def __init__(self, transport):
        self._transport = transport

    def is_http_transport(self):
        return self._transport.name == 'http'

    def is_websocket_transport(self):
        return self._transport.name == 'websocket'

    def _request(self, url, action, request, **params):
        req = Request(url, action, request, **params)
        response = self._transport.request(req.action, req.request,
                                           **req.params)
        return Response(response)

    def refresh_token(self, refresh_token):
        url = 'token/refresh'
        action = url
        request = {'refreshToken': refresh_token}
        params = {'method': 'POST',
                  'merge_data': True}
        return self._request(url, action, request, **params)

class Request(object):
    """Request class."""

    def __init__(self, url, action, request, **params):
        self.action = action
        self.request = request
        self.params = params
        self.params['url'] = url


class Response(object):
    """Response class."""

    def __init__(self, response):
        self.action = response.pop('action')
        self.is_success = response.pop('status') == 'success'
        self.code = response.pop('code', None)
        self.error = response.pop('error', None)
        self.data = response


class ApiObject(object):

    def __init__(self, transport):
        self._transport = transport

    def is_http_transport(self):
        return self._transport.name == 'http'

    def is_websocket_transport(self):
        return self._transport.name == 'websocket'

    def _request(self, url, action, request, **params):
        req = Request(url, action, request, **params)
        resp = self._transport.request(req.action, req.request, **req.params)
        return Response(resp)


class Token(ApiObject):

    def __init__(self, transport, refresh_toke, access_token=None):
        ApiObject.__init__(self, transport)
        self._refresh_token = refresh_toke
        self._access_token = access_token

    def refresh(self):
        url = 'token/refresh'
        action = url
        request = {'refreshToken': self._refresh_token}
        params = {'method': 'POST',
                  'merge_data': True}
        response = self._request(url, action, request, **params)
        self._access_token = response.data['accessToken']

    def access_token(self):
        return self._access_token


class Api(object):
    """Api class."""

    def __init__(self, transport):
        self._transport = transport

    def token(self, refresh_token, access_token):
        return Token(self._transport, refresh_token, access_token)

Add Request, Response and ApiObject and Token classes
Add Request, Response and ApiObject and Token classes

- from devicehive.transport import Request
- from devicehive.transport import Response
+ class Request(object):
+     """Request class."""
+ 
+     def __init__(self, url, action, request, **params):
+         self.action = action
+         self.request = request
+         self.params = params
+         self.params['url'] = url
  
  
+ class Response(object):
+     """Response class."""
+ 
+     def __init__(self, response):
+         self.action = response.pop('action')
+         self.is_success = response.pop('status') == 'success'
+         self.code = response.pop('code', None)
+         self.error = response.pop('error', None)
+         self.data = response
+ 
+ 
- class Api(object):
+ class ApiObject(object):
-     """Api class."""
  
      def __init__(self, transport):
          self._transport = transport
  
      def is_http_transport(self):
          return self._transport.name == 'http'
  
      def is_websocket_transport(self):
          return self._transport.name == 'websocket'
  
      def _request(self, url, action, request, **params):
          req = Request(url, action, request, **params)
-         response = self._transport.request(req.action, req.request,
+         resp = self._transport.request(req.action, req.request, **req.params)
-                                            **req.params)
-         return Response(response)
+         return Response(resp)
  
+ 
+ class Token(ApiObject):
+ 
+     def __init__(self, transport, refresh_toke, access_token=None):
+         ApiObject.__init__(self, transport)
-     def refresh_token(self, refresh_token):
+         self._refresh_token = refresh_toke
+         self._access_token = access_token
+ 
+     def refresh(self):
          url = 'token/refresh'
          action = url
-         request = {'refreshToken': refresh_token}
+         request = {'refreshToken': self._refresh_token}
          params = {'method': 'POST',
                    'merge_data': True}
-         return self._request(url, action, request, **params)
+         response = self._request(url, action, request, **params)
+         self._access_token = response.data['accessToken']
  
+     def access_token(self):
+         return self._access_token
+ 
+ 
+ class Api(object):
+     """Api class."""
+ 
+     def __init__(self, transport):
+         self._transport = transport
+ 
+     def token(self, refresh_token, access_token):
+         return Token(self._transport, refresh_token, access_token)
+ 
Add Request, Response and ApiObject and Token classes
## Code Before:
from devicehive.transport import Request
from devicehive.transport import Response


class Api(object):
    """Api class."""

    def __init__(self, transport):
        self._transport = transport

    def is_http_transport(self):
        return self._transport.name == 'http'

    def is_websocket_transport(self):
        return self._transport.name == 'websocket'

    def _request(self, url, action, request, **params):
        req = Request(url, action, request, **params)
        response = self._transport.request(req.action, req.request,
                                           **req.params)
        return Response(response)

    def refresh_token(self, refresh_token):
        url = 'token/refresh'
        action = url
        request = {'refreshToken': refresh_token}
        params = {'method': 'POST',
                  'merge_data': True}
        return self._request(url, action, request, **params)

## Instruction:
Add Request, Response and ApiObject and Token classes
## Code After:
class Request(object):
    """Request class."""

    def __init__(self, url, action, request, **params):
        self.action = action
        self.request = request
        self.params = params
        self.params['url'] = url


class Response(object):
    """Response class."""

    def __init__(self, response):
        self.action = response.pop('action')
        self.is_success = response.pop('status') == 'success'
        self.code = response.pop('code', None)
        self.error = response.pop('error', None)
        self.data = response


class ApiObject(object):

    def __init__(self, transport):
        self._transport = transport

    def is_http_transport(self):
        return self._transport.name == 'http'

    def is_websocket_transport(self):
        return self._transport.name == 'websocket'

    def _request(self, url, action, request, **params):
        req = Request(url, action, request, **params)
        resp = self._transport.request(req.action, req.request, **req.params)
        return Response(resp)


class Token(ApiObject):

    def __init__(self, transport, refresh_toke, access_token=None):
        ApiObject.__init__(self, transport)
        self._refresh_token = refresh_toke
        self._access_token = access_token

    def refresh(self):
        url = 'token/refresh'
        action = url
        request = {'refreshToken': self._refresh_token}
        params = {'method': 'POST',
                  'merge_data': True}
        response = self._request(url, action, request, **params)
        self._access_token = response.data['accessToken']

    def access_token(self):
        return self._access_token


class Api(object):
    """Api class."""

    def __init__(self, transport):
        self._transport = transport

    def token(self, refresh_token, access_token):
        return Token(self._transport, refresh_token, access_token)
