Example #1
0
    def setUp(self):
        self.session = Session(TOKEN)
        # TODO: fix when sp.get_users() is merged
        self.user = User(self.session, {'id': USER_ID})
        self.player = self.user.player()

        # Mock the sp._request method so that we never actually reach Spotify
        self.patcher = patch.object(utils, 'request', autospec=True)

        # Add cleanup to unmock sp._request. Cleanup always called at end.
        self.addCleanup(self.patcher.stop)
        self.request_mock = self.patcher.start()
Example #2
0
    def test_dunder(self):
        other = User(self.session, {'id': USER_ID}).player()

        # Not the same object
        self.assertIsNot(self.player, other)

        # eq and ne
        self.assertEqual(self.player, other)
        self.assertNotEqual(self.player, User(self.session,
                                              {'id': USER_ID * 2}))

        # hash
        self.assertEqual(hash(self.player), hash(other))
Example #3
0
    def current_user(self):
        """
        Returns:
            User: The user associated with the current Spotify API token.

        Raises:
            ValueError: if the Spotify API key is not valid.
            ValueError: if the response is empty.
            HTTPError: if failure or partial failure.

        Calls endpoints:
            - GET /v1/me
        """

        # Construct params for API call
        endpoint = Endpoints.SEARCH_CURRENT_USER

        # Execute requests
        response_json, status_code = utils.request(
            session=self, request_type=const.REQUEST_GET, endpoint=endpoint)

        if status_code != 200:
            raise utils.SpotifyError(status_code, response_json)

        return User(self, response_json)
Example #4
0
    def get_users(self, user_ids):
        """ Gets the users with the given Spotify ids.

        Args:
            user_ids (str, List[str]): The Spotify user id(s) to get.

        Returns:
            Union[User, List[User]]: The requested user(s).

        Raises:
            TypeError: for invalid types in any argument.
            HTTPError: if failure or partial failure.

        Calls endpoints:
            - GET	/v1/users/{user_id}
        """

        # Type validation
        if not isinstance(user_ids, str) and\
            not all(isinstance(x, str) for x in user_ids):
            raise TypeError('user_ids should be str or list of str')

        if isinstance(user_ids, str):
            user_ids = list('user_ids should be str')

        # Construct params for API call
        uri_params = dict()

        # Each API call can return at most 1 user. Therefore there is no need
        # to batch this query.
        result = list()
        for user_id in user_ids:
            # Execute requests
            # TODO: Partial failure - if user with user_id does not exist,
            # status_code is 404
            response_json, status_code = utils.request(
                session=self,
                request_type=const.REQUEST_GET,
                endpoint=Endpoints.SEARCH_USER % user_id,
                uri_params=uri_params)

            if status_code != 200:
                raise utils.SpotifyError(status_code, response_json)

            result.append(User(self, response_json))

        return result if len(result) != 1 else result[0]
Example #5
0
    def __init__(self, session, info):
        """ Get an instance of Playlist. Client should not use the constructor!

        Args:
            session: a Session instance

            info (dict): the playlist's information. Must contain 'owner' and
                'id'.
        """
        self._raw = deepcopy(info)
        self._session = session
        if 'owner' not in info:
            raise ValueError('Playlist owner information missing')
        if 'id' not in info:
            raise ValueError('Playlist id missing')
        self._owner = User(session, info['owner'])
        self._tracks = []
        tracks = info.get('tracks', {})
        for item in tracks.get('items', []):
            if 'track' not in item:
                raise ValueError('Track information missing')
            self._tracks.append(Track(session, item.get('track', {})))
Example #6
0
    def refresh(self):
        """ Refreshes the internal playlist data.

        Calls:
            GET /v1/playlists/{playlist_id}
        """
        endpoint = Endpoints.SEARCH_PLAYLIST % self.spotify_id()
        response_json, status_code = utils.request(
            self._session,
            request_type='GET',
            endpoint=endpoint,
        )
        if status_code != 200:
            raise utils.SpotifyError(status_code, response_json)

        self._raw = response_json
        self._owner = User(self._session, response_json['owner'])
        self._tracks = []
        tracks = response_json.get('tracks', {})
        for item in tracks.get('items', []):
            if 'track' not in item:
                raise ValueError('Track information missing')
            self._tracks.append(Track(self._session, item.get('track', {})))
Example #7
0
 def test_current_user(self):
     session = Session(TOKEN)
     self.request_mock.return_value = (expected_users_json[0], 200)
     expected_user = User(session, info=expected_users_json[0])
     user = session.current_user()
     self.assertEqual(user, expected_user)
Example #8
0
class TestPlayer(unittest.TestCase):
    def setUp(self):
        self.session = Session(TOKEN)
        # TODO: fix when sp.get_users() is merged
        self.user = User(self.session, {'id': USER_ID})
        self.player = self.user.player()

        # Mock the sp._request method so that we never actually reach Spotify
        self.patcher = patch.object(utils, 'request', autospec=True)

        # Add cleanup to unmock sp._request. Cleanup always called at end.
        self.addCleanup(self.patcher.stop)
        self.request_mock = self.patcher.start()

    @unittest.skip('User class udpated. have to update this test')
    def test_dunder(self):
        other = User(self.session, {'id': USER_ID}).player()

        # Not the same object
        self.assertIsNot(self.player, other)

        # eq and ne
        self.assertEqual(self.player, other)
        self.assertNotEqual(self.player, User(self.session,
                                              {'id': USER_ID * 2}))

        # hash
        self.assertEqual(hash(self.player), hash(other))

    @unittest.skip('User class udpated. have to update this test')
    def test_user(self):
        self.assertEqual(self.user, self.player.user())

    @unittest.skip('Not yet implemented')
    def test__player_data(self):
        # Should also test the SpotifyError
        pass

    @unittest.skip('Not yet implemented')
    def test_next(self):
        pass

    @unittest.skip('Not yet implemented')
    def test_previous(self):
        pass

    @unittest.skip('Not yet implemented')
    def test_pause(self):
        pass

    @unittest.skip('Not yet implemented')
    def test_resume(self):
        pass

    @unittest.skip('Not yet implemented')
    def test_play(self):
        pass

    @unittest.skip('Not yet implemented')
    def test_is_paused(self):
        pass

    @unittest.skip('Not yet implemented')
    def test_is_playing(self):
        pass

    @unittest.skip('Not yet implemented')
    def test_get_currently_playing(self):
        pass

    @unittest.skip('Not yet implemented')
    def test_available_devices(self):
        pass

    @unittest.skip('Not yet implemented')
    def test_get_active_device(self):
        pass

    @unittest.skip('Not yet implemented')
    def test_set_active_device(self):
        pass

    @unittest.skip('Not yet implemented')
    def test_get_shuffle(self):
        pass

    @unittest.skip('Not yet implemented')
    def test_set_shuffle(self):
        pass

    @unittest.skip('Not yet implemented')
    def test_get_playback_position(self):
        pass

    @unittest.skip('Not yet implemented')
    def test_set_playback_position(self):
        pass

    @unittest.skip('Not yet implemented')
    def test_get_volume(self):
        pass

    @unittest.skip('Not yet implemented')
    def test_set_volume(self):
        pass

    @unittest.skip('Not yet implemented')
    def test_get_repeat(self):
        pass

    @unittest.skip('Not yet implemented')
    def test_set_repeat(self):
        pass

    @unittest.skip('Not yet implemented')
    def test_enqueue(self):
        pass