Skip to content

Commit c4bab99

Browse files
authored
Merge pull request #88 from Shreyas-vgr/master
Add helper function to fetch userId
2 parents c8fe0af + dd92eb7 commit c4bab99

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
from .request_util import (
2525
get_slot, get_slot_value, get_account_linking_access_token,
2626
get_api_access_token, get_device_id, get_dialog_state, get_intent_name,
27-
get_locale, get_request_type, is_new_session, get_supported_interfaces)
27+
get_locale, get_request_type, is_new_session, get_supported_interfaces,
28+
get_user_id)
2829

2930

3031
SDK_VERSION = __version__

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,3 +308,22 @@ def is_new_session(handler_input):
308308
return session.new
309309

310310
raise TypeError("The provided request doesn't have a session")
311+
312+
313+
def get_user_id(handler_input):
314+
# type: (HandlerInput) -> Optional[AnyStr]
315+
"""Return the userId in the request.
316+
317+
The method retrieves the ``userId`` from the input request.
318+
This value uniquely identifies the user and is generally used as input for
319+
some Alexa-specific API calls.
320+
More information about this can be found here:
321+
https://developer.amazon.com/docs/custom-skills/request-and-response-json-reference.html#system-object
322+
:param handler_input: The handler input instance that is generally
323+
passed in the sdk's request and exception components
324+
:type handler_input: ask_sdk_core.handler_input.HandlerInput
325+
:return: Users userId or None if not available
326+
:rtype: Optional[str]
327+
"""
328+
user = handler_input.request_envelope.context.system.user
329+
return user.user_id if user else None

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
is_canfulfill_intent_name, is_intent_name, is_request_type, viewport,
3131
get_slot, get_slot_value, get_account_linking_access_token,
3232
get_api_access_token, get_device_id, get_dialog_state, get_intent_name,
33-
get_locale, get_request_type, is_new_session, get_supported_interfaces)
33+
get_locale, get_request_type, is_new_session, get_supported_interfaces,
34+
get_user_id)
3435
from ask_sdk_core.handler_input import HandlerInput
3536
from ask_sdk_core.exceptions import AskSdkException
3637

@@ -414,6 +415,7 @@ def setUp(self):
414415
self.test_slot = Slot(
415416
name=self.test_slot_name, value=self.test_slot_value)
416417
self.test_api_access_token = "foo_api_access_token"
418+
self.test_user_id = "foo_user_id"
417419
self.test_access_token = "foo_account_linking_access_token"
418420
self.test_device_id = "foo_device_id"
419421
self.test_supported_interfaces = SupportedInterfaces(
@@ -436,7 +438,8 @@ def setUp(self):
436438
session=Session(new=self.test_new_session),
437439
context=Context(
438440
system=SystemState(
439-
user=User(access_token=self.test_access_token),
441+
user=User(access_token=self.test_access_token,
442+
user_id=self.test_user_id),
440443
api_access_token=self.test_api_access_token,
441444
device=Device(
442445
device_id=self.test_device_id,
@@ -644,3 +647,19 @@ def test_is_new_session(self):
644647
self.test_new_session,
645648
"is_new_session method returned incorrect session information "
646649
"from input request when a session exists")
650+
651+
def test_get_user_id_with_no_user_info_returns_none(self):
652+
test_input = self._create_handler_input(
653+
request=self.test_launch_request)
654+
test_input.request_envelope.context.system.user = None
655+
self.assertIsNone(
656+
get_user_id(handler_input=test_input),
657+
"get_user_id method returned incorrect user id from input request")
658+
659+
def test_get_user_id(self):
660+
test_input = self._create_handler_input(
661+
request=self.test_launch_request)
662+
663+
self.assertEqual(
664+
get_user_id(handler_input=test_input), self.test_user_id,
665+
"get_user_id method returned incorrect user id from input request")

0 commit comments

Comments
 (0)