Skip to content
This repository has been archived by the owner on May 22, 2024. It is now read-only.

Commit

Permalink
Fix inheritance in Handler class
Browse files Browse the repository at this point in the history
Fixes unfoldingWord/door43.org#436

Double underscores in the `__handle` method were the main problem.

Also made `Handler` an Abstract Base Class, and `_handle` and abstract method.
  • Loading branch information
phillip-hopper committed Feb 22, 2017
1 parent b86fcec commit dffd284
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 11 deletions.
2 changes: 1 addition & 1 deletion lambda_handlers/client_callback_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

class ClientCallbackHandler(Handler):

def __handle(self, event, context):
def _handle(self, event, context):
"""
:param dict event:
:param context:
Expand Down
2 changes: 1 addition & 1 deletion lambda_handlers/client_webhook_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

class ClientWebhookHandler(Handler):

def __handle(self, event, context):
def _handle(self, event, context):
"""
:param dict event:
:param context:
Expand Down
2 changes: 1 addition & 1 deletion lambda_handlers/convert_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def __init__(self, *args, **kwargs):
self.converter_class = args.pop()
Handler.__init__(self, *args, **kwargs)

def __handle(self, event, context):
def _handle(self, event, context):
"""
:param dict event:
:param context:
Expand Down
2 changes: 1 addition & 1 deletion lambda_handlers/door43_deploy_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

class Door43DeployHandler(Handler):

def __handle(self, event, context):
def _handle(self, event, context):
"""
:param dict event:
:param context:
Expand Down
9 changes: 6 additions & 3 deletions lambda_handlers/handler.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from __future__ import unicode_literals, print_function
from abc import ABCMeta, abstractmethod


class Handler(object):
__metaclass__ = ABCMeta

def handle(self, event, context):
"""
Expand All @@ -10,19 +12,20 @@ def handle(self, event, context):
:return dict:
"""
try:
return self.__handle(event, context)
return self._handle(event, context)
except Exception as e:
e.message = 'Bad Request: {0}'.format(e.message)
raise e

def __handle(self, event, context):
@abstractmethod
def _handle(self, event, context):
"""
Dummy function for handlers. Override this so handle() will catch the exception and make it a "Bad Request: "
:param dict event:
:param context:
:return dict:
"""
pass
raise NotImplementedError()

@staticmethod
def retrieve(dictionary, key, dict_name=None):
Expand Down
2 changes: 1 addition & 1 deletion lambda_handlers/list_endpoints_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

class ListEndpointsHandler(Handler):

def __handle(self, event, context):
def _handle(self, event, context):
"""
:param dict event:
:param context:
Expand Down
2 changes: 1 addition & 1 deletion lambda_handlers/register_module_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

class RegisterModuleHandler(Handler):

def __handle(self, event, context):
def _handle(self, event, context):
"""
:param dict event:
:param context:
Expand Down
2 changes: 1 addition & 1 deletion lambda_handlers/request_job_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

class RequestJobHandler(Handler):

def __handle(self, event, context):
def _handle(self, event, context):
"""
:param dict event:
:param context:
Expand Down
2 changes: 1 addition & 1 deletion lambda_handlers/start_job_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

class StartJobHandler(Handler):

def __handle(self, event, context):
def _handle(self, event, context):
"""
:param dict event:
:param context:
Expand Down
Empty file.
36 changes: 36 additions & 0 deletions tests/lambda_handlers_tests/test_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from __future__ import unicode_literals, print_function
from unittest import TestCase
from mock import patch
from lambda_handlers.handler import Handler


class MockHandler(Handler):

def _handle(self, event, context):
"""
Test if this method is called
:param event:
:param context:
:return:
"""
pass


class TestHandler(TestCase):

def test_inheritance(self):
"""
This tests if the inheritance from Handler is working correctly
:return: None
"""

event = {'key1': 'value1'}
context = {'key2': 'value2'}

handler = MockHandler()

# noinspection PyUnresolvedReferences
with patch.object(handler, '_handle') as mock:
handler.handle(event, context)

mock.assert_called_with(event, context)

0 comments on commit dffd284

Please sign in to comment.