Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 11 additions & 12 deletions shotgun_api3/shotgun.py
Original file line number Diff line number Diff line change
Expand Up @@ -3810,14 +3810,14 @@ def preferences_read(self, prefs: Optional[List] = None) -> Dict[str, Any]:

return self._call_rpc("preferences_read", {"prefs": prefs})

def user_subscriptions_read(self) -> List:
def user_subscriptions_read(self) -> Dict[str, Union[str, List[str], None]]:
"""
Get the list of user subscriptions.
Get the user subscriptions.

:returns: A list of user subscriptions where each subscription is a
dictionary containing the ``humanUserId`` and ``subscription``
fields.
:rtype: list
:returns: A dictionary of user subscriptions keyed by HumanUser id
(as a string). Each subscription is ``None``, a single string, or
an array of strings.
:rtype: dict
"""

return self._call_rpc("user_subscriptions_read", None)
Expand All @@ -3834,16 +3834,15 @@ def user_subscriptions_create(
The ``subscription`` is either ``None``, a single string, or an
array of strings with subscription information.

:returns: ``True`` if the request succedeed, ``False`` if otherwise.
:returns: ``True`` if the request succeeded, ``False`` if otherwise.
:rtype: bool
"""

response = self._call_rpc("user_subscriptions_create", {"users": users})

if not isinstance(response, dict):
return False
# The server expects each subscription keyed by its HumanUser id.
params = {str(user["humanUserId"]): user["subscription"] for user in users}
response = self._call_rpc("user_subscriptions_create", params)

return response.get("status") == "success"
return isinstance(response, dict)

def _build_opener(self, handler) -> urllib.request.OpenerDirector:
"""
Expand Down
35 changes: 35 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,41 @@ def test_thumb_url(self):
self._mock_http(resp, headers={"content-type": "text/plain"})
self.assertRaises(RuntimeError, self.sg._build_thumb_url, "FakeAsset", 456)

def test_user_subscriptions_read_returns_subscriptions_keyed_by_user_id(self):
"""user_subscriptions_read returns the server's subscriptions keyed by user id"""
subscriptions = {"1": "not_for_resale", "2": None}
self._mock_http({"results": subscriptions})

result = self.sg.user_subscriptions_read()

self._assert_http_method("user_subscriptions_read", None)
self.assertEqual(subscriptions, result, "subscriptions returned as sent")

def test_user_subscriptions_create_sends_subscriptions_keyed_by_user_id(self):
"""user_subscriptions_create sends each subscription under its user id"""
self._mock_http({"results": {"1": None, "2": None}})

self.sg.user_subscriptions_create(
[
{"humanUserId": 1, "subscription": "not_for_resale"},
{"humanUserId": 2, "subscription": None},
]
)

self._assert_http_method(
"user_subscriptions_create", {"1": "not_for_resale", "2": None}
)

def test_user_subscriptions_create_returns_true_when_accepted(self):
"""user_subscriptions_create returns True when the server accepts the call"""
self._mock_http({"results": {"1": None}})

result = self.sg.user_subscriptions_create(
[{"humanUserId": 1, "subscription": "not_for_resale"}]
)

self.assertTrue(result, "an accepted call returns True")


class TestShotgunClientInterface(base.MockTestBase):
"""Tests expected interface for shotgun module and client"""
Expand Down
Loading