Skip to content

Conversation

@loloicci
Copy link
Contributor

Fix #259

@loloicci loloicci force-pushed the fix-whhandler-wrapedfunc branch 3 times, most recently from 0583314 to 206ef2f Compare April 15, 2020 09:35
@loloicci loloicci force-pushed the fix-whhandler-wrapedfunc branch from 206ef2f to ec635ca Compare April 15, 2020 09:38
@loloicci
Copy link
Contributor Author

Sorry, I had done some force-push to pass the CI test.

@okue
Copy link
Member

okue commented Apr 15, 2020

no problem.


@staticmethod
def __get_args_count(func):
HANDLER_ARGSIZE_MAXIMAM = 2
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[imo]
How about returning the flag that function has varadic arguments or not in addition to the number of non-varadic arguments?

In this way, we should modify

args_count = self.__get_args_count(func)
if args_count == 0:
func()
elif args_count == 1:
func(event)
else:
func(event, payload.destination)

to following, but I think that it is more clear.

has_varargs, args_count = self.__get_args_count(func)
if has_varargs or args_count >= 2:
    func(event, payload.destination)
elif args_count == 1:
    func(event)
else:
    func()

Comment on lines +532 to +622
def wrap(func):
def wrapper(*args):
if PY3:
arg_spec = inspect.getfullargspec(func)
else:
arg_spec = inspect.getargspec(func)
return func(*args[0:len(arg_spec.args)])
return wrapper


class TestWebhookHandlerWithWrappedFunction(unittest.TestCase):
def setUp(self):
self.handler = WebhookHandler('channel_secret')

@self.handler.add(MessageEvent, message=TextMessage)
@wrap
def message_text(event, destination):
self.assertEqual('message', event.type)
self.assertEqual('text', event.message.type)
self.assertEqual('U123', destination)

@self.handler.add(MessageEvent,
message=(ImageMessage, VideoMessage, AudioMessage))
@wrap
def message_content(event):
self.assertEqual('message', event.type)
self.assertIn(
event.message.type,
['image', 'video', 'audio']
)

@self.handler.add(MessageEvent, message=StickerMessage)
@wrap
def message_sticker(event):
self.assertEqual('message', event.type)
self.assertEqual('sticker', event.message.type)

@self.handler.add(MessageEvent)
@wrap
def message(event):
self.assertEqual('message', event.type)
self.assertNotIn(
event.message.type,
['text', 'image', 'video', 'audio', 'sticker']
)

@self.handler.add(FollowEvent)
@wrap
def follow(event, destination):
self.assertEqual('follow', event.type)
self.assertEqual('U123', destination)

@self.handler.add(JoinEvent)
@wrap
def join(event):
self.assertEqual('join', event.type)

@self.handler.add(PostbackEvent)
@wrap
def postback(event):
self.assertEqual('postback', event.type)

@self.handler.add(BeaconEvent)
@wrap
def beacon(event):
self.assertEqual('beacon', event.type)

@self.handler.add(AccountLinkEvent)
@wrap
def account_link(event):
self.assertEqual('accountLink', event.type)

@self.handler.default()
def default(event):
self.assertNotIn(
event.type,
['message', 'follow', 'join', 'postback', 'beacon', 'accountLink']
)

def test_handler_with_wrapped_function(self):
file_dir = os.path.dirname(__file__)
webhook_sample_json_path = os.path.join(file_dir, 'text', 'webhook.json')
with open(webhook_sample_json_path) as fp:
body = fp.read()

# mock
self.handler.parser.signature_validator.validate = lambda a, b: True

self.handler.handle(body, 'signature')


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[imo]
Test seems good, but it seems redundant to makeTestWebhookHandlerWithWrappedFunction.

If we separate the part which invokes function from WebhookHandler.handle like following and test it, the test will be more clear and simple I think.

    def __invoke_func(self, func, event, payload):
        has_varargs, args_count = self.__get_args_count(func)
        if has_varargs or args_count >= 2:
            func(event, payload.destination)
        elif args_count == 1:
            func(event)
        else:
            func()

@loloicci
Copy link
Contributor Author

created a new PR (#276) that reflected the review.

@okue okue added this to the 1.18.0 milestone Jun 26, 2020
@cryeo
Copy link
Member

cryeo commented Jun 26, 2020

created a new PR (#276) that reflected the review.

Could you tell us the reason to create new PR?

@loloicci
Copy link
Contributor Author

Because almost all differences with the master are changed and it looks better to make a new PR than revert the commits for me.
Sorry, I should have asked which is better.
Should I close #276 and rebase the changes to this branch?

@okue
Copy link
Member

okue commented Jun 30, 2020

OK, let's move to #276.

@okue okue closed this Jun 30, 2020
@okue okue removed this from the 1.18.0 milestone Jun 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

WebhookHandler.add cannot cope with handler function with *args in its parameters

3 participants