
from .request import Request
from .response import Response, response

import logging
log = logging.getLogger()


class View:

    def __call__(self, event, context):
        request = Request(event, context)
        resp = self.prepare(request)
        if resp:
            return resp
        kwargs = event.get('pathParameters') or {}
        func = getattr(self, request.method.lower())
        try:
            resp = func(request, **kwargs)
        except:
            import traceback
            log.error(self)
            log.error(traceback.format_exc())
            return response(body='Internal server Error', status=500)
        if isinstance(resp, Response):
            resp = resp.render()
        return resp

    def prepare(self, request):
        pass


from .request import Request
from .response import Response, response

import logging
log = logging.getLogger()


class View:

    def __call__(self, event, context):
        kwargs = event.get('pathParameters') or {}
        self.dispatch(request, **kwargs)

    def dispatch(self, request, **kwargs):
        func = getattr(self, request.method.lower())
        try:
            resp = func(request, **kwargs)
        except:
            import traceback
            log.error(self)
            log.error(traceback.format_exc())
            return response(body='Internal server Error', status=500)
        if isinstance(resp, Response):
            resp = resp.render()
        return resp

    def prepare(self, request):
        pass

Break out dispatch, and drop prepare. Easier testing
Break out dispatch, and drop prepare. Easier testing

  
  from .request import Request
  from .response import Response, response
  
  import logging
  log = logging.getLogger()
  
  
  class View:
  
      def __call__(self, event, context):
-         request = Request(event, context)
-         resp = self.prepare(request)
-         if resp:
-             return resp
          kwargs = event.get('pathParameters') or {}
+         self.dispatch(request, **kwargs)
+ 
+     def dispatch(self, request, **kwargs):
          func = getattr(self, request.method.lower())
          try:
              resp = func(request, **kwargs)
          except:
              import traceback
              log.error(self)
              log.error(traceback.format_exc())
              return response(body='Internal server Error', status=500)
          if isinstance(resp, Response):
              resp = resp.render()
          return resp
  
      def prepare(self, request):
          pass
  
Break out dispatch, and drop prepare. Easier testing
## Code Before:

from .request import Request
from .response import Response, response

import logging
log = logging.getLogger()


class View:

    def __call__(self, event, context):
        request = Request(event, context)
        resp = self.prepare(request)
        if resp:
            return resp
        kwargs = event.get('pathParameters') or {}
        func = getattr(self, request.method.lower())
        try:
            resp = func(request, **kwargs)
        except:
            import traceback
            log.error(self)
            log.error(traceback.format_exc())
            return response(body='Internal server Error', status=500)
        if isinstance(resp, Response):
            resp = resp.render()
        return resp

    def prepare(self, request):
        pass

## Instruction:
Break out dispatch, and drop prepare. Easier testing
## Code After:

from .request import Request
from .response import Response, response

import logging
log = logging.getLogger()


class View:

    def __call__(self, event, context):
        kwargs = event.get('pathParameters') or {}
        self.dispatch(request, **kwargs)

    def dispatch(self, request, **kwargs):
        func = getattr(self, request.method.lower())
        try:
            resp = func(request, **kwargs)
        except:
            import traceback
            log.error(self)
            log.error(traceback.format_exc())
            return response(body='Internal server Error', status=500)
        if isinstance(resp, Response):
            resp = resp.render()
        return resp

    def prepare(self, request):
        pass
