Skip to content

Commit 240e15f

Browse files
authored
Merge pull request #46 from timothyaaron/canfulfill.intent
Add helper function for matching CanFulfill Intent name
2 parents b2641c1 + 86e8c9a commit 240e15f

File tree

3 files changed

+77
-4
lines changed

3 files changed

+77
-4
lines changed

ask-sdk-core/ask_sdk_core/utils/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import sys
1919

2020
from ..__version__ import __version__
21-
from .predicate import is_intent_name, is_request_type
21+
from .predicate import is_canfulfill_intent_name, is_intent_name, is_request_type
2222
from ask_sdk_runtime.utils import user_agent_info
2323

2424

ask-sdk-core/ask_sdk_core/utils/predicate.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,38 @@
1818
import typing
1919

2020
from ask_sdk_model import IntentRequest
21+
from ask_sdk_model.canfulfill import CanFulfillIntentRequest
2122

2223
if typing.TYPE_CHECKING:
2324
from typing import Callable
2425
from ..handler_input import HandlerInput
2526

2627

28+
def is_canfulfill_intent_name(name):
29+
# type: (str) -> Callable[[HandlerInput], bool]
30+
"""A predicate function returning a boolean, when name matches the
31+
intent name in a CanFulfill Intent Request.
32+
33+
The function can be applied on a
34+
:py:class:`ask_sdk_core.handler_input.HandlerInput`, to
35+
check if the input is of
36+
:py:class:`ask_sdk_model.intent_request.CanFulfillIntentRequest` type and if the
37+
name of the request matches with the passed name.
38+
39+
:param name: Name to be matched with the CanFulfill Intent Request Name
40+
:type name: str
41+
:return: Predicate function that can be used to check name of the
42+
request
43+
:rtype: Callable[[HandlerInput], bool]
44+
"""
45+
def can_handle_wrapper(handler_input):
46+
# type: (HandlerInput) -> bool
47+
return (isinstance(
48+
handler_input.request_envelope.request, CanFulfillIntentRequest) and
49+
handler_input.request_envelope.request.intent.name == name)
50+
return can_handle_wrapper
51+
52+
2753
def is_intent_name(name):
2854
# type: (str) -> Callable[[HandlerInput], bool]
2955
"""A predicate function returning a boolean, when name matches the

ask-sdk-core/tests/unit/test_utils.py

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,62 @@
2020

2121
from ask_sdk_model import (
2222
IntentRequest, RequestEnvelope, Intent, SessionEndedRequest, Context)
23+
from ask_sdk_model.canfulfill import CanFulfillIntentRequest
2324
from ask_sdk_core.utils import (
24-
is_intent_name, is_request_type, viewport)
25+
is_canfulfill_intent_name, is_intent_name, is_request_type, viewport)
2526
from ask_sdk_core.handler_input import HandlerInput
2627
from ask_sdk_core.exceptions import AskSdkException
2728
from ask_sdk_model.interfaces.viewport import ViewportState, Shape
2829

2930

31+
def test_is_canfulfill_intent_name_match():
32+
test_canfulfill_intent_name = "TestIntent"
33+
test_handler_input = HandlerInput(
34+
request_envelope=RequestEnvelope(request=CanFulfillIntentRequest(
35+
intent=Intent(name=test_canfulfill_intent_name))))
36+
37+
canfulfill_intent_name_wrapper = is_canfulfill_intent_name(test_canfulfill_intent_name)
38+
assert canfulfill_intent_name_wrapper(
39+
test_handler_input), "is_canfulfill_intent_name matcher didn't match with the " \
40+
"correct intent name"
41+
42+
43+
def test_is_canfulfill_intent_name_not_match():
44+
test_canfulfill_intent_name = "TestIntent"
45+
test_handler_input = HandlerInput(
46+
request_envelope=RequestEnvelope(request=CanFulfillIntentRequest(
47+
intent=Intent(name=test_canfulfill_intent_name))))
48+
49+
canfulfill_intent_name_wrapper = is_canfulfill_intent_name("TestIntent1")
50+
assert not canfulfill_intent_name_wrapper(
51+
test_handler_input), "is_canfulfill_intent_name matcher matched with the " \
52+
"incorrect intent name"
53+
54+
55+
def test_is_canfulfill_intent_not_match_intent():
56+
test_canfulfill_intent_name = "TestIntent"
57+
test_canfulfill_handler_input = HandlerInput(
58+
request_envelope=RequestEnvelope(request=CanFulfillIntentRequest(
59+
intent=Intent(name=test_canfulfill_intent_name))))
60+
61+
intent_name_wrapper = is_intent_name(test_canfulfill_intent_name)
62+
assert not intent_name_wrapper(
63+
test_canfulfill_handler_input), "is_intent_name matcher matched with the " \
64+
"incorrect request type"
65+
66+
67+
def test_is_intent_not_match_canfulfill_intent():
68+
test_intent_name = "TestIntent"
69+
test_handler_input = HandlerInput(
70+
request_envelope=RequestEnvelope(request=IntentRequest(
71+
intent=Intent(name=test_intent_name))))
72+
73+
canfulfill_intent_name_wrapper = is_canfulfill_intent_name(test_intent_name)
74+
assert not canfulfill_intent_name_wrapper(
75+
test_handler_input), "is_canfulfill_intent_name matcher matched with the " \
76+
"incorrect request type"
77+
78+
3079
def test_is_intent_name_match():
3180
test_intent_name = "TestIntent"
3281
test_handler_input = HandlerInput(
@@ -344,5 +393,3 @@ def test_viewport_map_to_unknown_for_no_viewport(self):
344393
assert (viewport.get_viewport_profile(test_request_env)
345394
is viewport.ViewportProfile.UNKNOWN_VIEWPORT_PROFILE), (
346395
"Viewport profile couldn't resolve UNKNOWN_VIEWPORT_PROFILE")
347-
348-

0 commit comments

Comments
 (0)