diff --git a/shotgun_api3/shotgun.py b/shotgun_api3/shotgun.py index 6ef3f8ad..7e887bf9 100644 --- a/shotgun_api3/shotgun.py +++ b/shotgun_api3/shotgun.py @@ -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) @@ -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: """ diff --git a/tests/test_client.py b/tests/test_client.py index c94d805f..753eefd7 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -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"""