Skip to content

Add transferring playback state #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 29, 2023
Merged
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
21 changes: 17 additions & 4 deletions src/spotifyaio/spotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from typing import Any, Awaitable, Callable, Self

from aiohttp import ClientSession
from aiohttp.hdrs import METH_GET
from aiohttp.hdrs import METH_GET, METH_PUT
from yarl import URL

from spotifyaio.exceptions import SpotifyConnectionError, SpotifyError
Expand Down Expand Up @@ -36,6 +36,7 @@ def authenticate(self, token: str) -> None:

async def _request(
self,
method: str,
uri: str,
*,
data: dict[str, Any] | None = None,
Expand Down Expand Up @@ -63,7 +64,7 @@ async def _request(
try:
async with asyncio.timeout(self.request_timeout):
response = await self.session.request(
METH_GET,
method,
url,
headers=headers,
data=data,
Expand All @@ -84,11 +85,23 @@ async def _request(

return await response.text()

async def _get(self, uri: str) -> str:
"""Handle a GET request to Spotify."""
return await self._request(METH_GET, uri)

async def _put(self, uri: str, data: dict[str, Any]) -> str:
"""Handle a PUT request to Spotify."""
return await self._request(METH_PUT, uri, data=data)

async def get_playback(self) -> PlaybackState:
"""Get devices."""
response = await self._request("v1/me/player")
"""Get playback state."""
response = await self._get("v1/me/player")
return PlaybackState.from_json(response)

async def transfer_playback(self, device_id: str) -> None:
"""Transfer playback."""
await self._put("v1/me/player", {"device_ids": [device_id]})

async def close(self) -> None:
"""Close open client session."""
if self.session and self._close_session:
Expand Down
22 changes: 21 additions & 1 deletion tests/test_playback.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Asynchronous Python client for Spotify."""

import aiohttp
from aiohttp.hdrs import METH_GET
from aiohttp.hdrs import METH_GET, METH_PUT
from aresponses import ResponsesMockServer

from spotifyaio.spotify import SpotifyClient
Expand Down Expand Up @@ -32,3 +32,23 @@ async def test_get_devices(
response = await spotify.get_playback()
assert response == snapshot
await spotify.close()


async def test_transfer_playback(
aresponses: ResponsesMockServer,
) -> None:
"""Test transferring playback."""
aresponses.add(
SPOTIFY_URL,
"/v1/me/player",
METH_PUT,
aresponses.Response(
status=200,
headers={"Content-Type": "application/json"},
),
)
async with aiohttp.ClientSession() as session:
spotify = SpotifyClient(session=session)
spotify.authenticate("test")
await spotify.transfer_playback("test")
await spotify.close()