Skip to content

Commit c8efa32

Browse files
Support new resource for interacting with playlists
1 parent ae2137f commit c8efa32

4 files changed

Lines changed: 382 additions & 0 deletions

File tree

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package engineer.nightowl.sonos.api.domain;
2+
3+
import org.apache.commons.lang3.builder.EqualsBuilder;
4+
import org.apache.commons.lang3.builder.HashCodeBuilder;
5+
6+
import java.util.List;
7+
8+
public class SonosPlaylist
9+
{
10+
private String id;
11+
private String name;
12+
private String type;
13+
private Integer trackCount;
14+
private List<SonosPlaylistTrack> tracks;
15+
16+
public SonosPlaylist()
17+
{
18+
}
19+
20+
public SonosPlaylist(String id, String name, String type, Integer trackCount, List<SonosPlaylistTrack> tracks)
21+
{
22+
this.id = id;
23+
this.name = name;
24+
this.type = type;
25+
this.trackCount = trackCount;
26+
this.tracks = tracks;
27+
}
28+
29+
public String getId()
30+
{
31+
return id;
32+
}
33+
34+
public void setId(String id)
35+
{
36+
this.id = id;
37+
}
38+
39+
public String getName()
40+
{
41+
return name;
42+
}
43+
44+
public void setName(String name)
45+
{
46+
this.name = name;
47+
}
48+
49+
public String getType()
50+
{
51+
return type;
52+
}
53+
54+
public void setType(String type)
55+
{
56+
this.type = type;
57+
}
58+
59+
public Integer getTrackCount()
60+
{
61+
return trackCount;
62+
}
63+
64+
public void setTrackCount(Integer trackCount)
65+
{
66+
this.trackCount = trackCount;
67+
}
68+
69+
public List<SonosPlaylistTrack> getTracks()
70+
{
71+
return tracks;
72+
}
73+
74+
public void setTracks(List<SonosPlaylistTrack> tracks)
75+
{
76+
this.tracks = tracks;
77+
}
78+
79+
@Override
80+
public String toString()
81+
{
82+
return "SonosPlaylist{" +
83+
"id='" + id + '\'' +
84+
", name='" + name + '\'' +
85+
", type='" + type + '\'' +
86+
", trackCount=" + trackCount +
87+
", tracks=" + tracks +
88+
'}';
89+
}
90+
91+
@Override
92+
public boolean equals(Object o)
93+
{
94+
if (this == o) return true;
95+
96+
if (o == null || getClass() != o.getClass()) return false;
97+
98+
SonosPlaylist that = (SonosPlaylist) o;
99+
100+
return new EqualsBuilder()
101+
.append(id, that.id)
102+
.append(name, that.name)
103+
.append(type, that.type)
104+
.append(trackCount, that.trackCount)
105+
.append(tracks, that.tracks)
106+
.isEquals();
107+
}
108+
109+
@Override
110+
public int hashCode()
111+
{
112+
return new HashCodeBuilder(17, 37)
113+
.append(id)
114+
.append(name)
115+
.append(type)
116+
.append(trackCount)
117+
.append(tracks)
118+
.toHashCode();
119+
}
120+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package engineer.nightowl.sonos.api.domain;
2+
3+
import org.apache.commons.lang3.builder.EqualsBuilder;
4+
import org.apache.commons.lang3.builder.HashCodeBuilder;
5+
6+
import java.util.List;
7+
8+
public class SonosPlaylistList
9+
{
10+
private String version;
11+
private List<SonosPlaylist> playlists;
12+
13+
public SonosPlaylistList()
14+
{
15+
}
16+
17+
public SonosPlaylistList(String version, List<SonosPlaylist> playlists)
18+
{
19+
this.version = version;
20+
this.playlists = playlists;
21+
}
22+
23+
public String getVersion()
24+
{
25+
return version;
26+
}
27+
28+
public void setVersion(String version)
29+
{
30+
this.version = version;
31+
}
32+
33+
public List<SonosPlaylist> getPlaylists()
34+
{
35+
return playlists;
36+
}
37+
38+
public void setPlaylists(List<SonosPlaylist> playlists)
39+
{
40+
this.playlists = playlists;
41+
}
42+
43+
@Override
44+
public String toString()
45+
{
46+
return "SonosPlaylistList{" +
47+
"version='" + version + '\'' +
48+
", playlists=" + playlists +
49+
'}';
50+
}
51+
52+
@Override
53+
public boolean equals(Object o)
54+
{
55+
if (this == o) return true;
56+
57+
if (o == null || getClass() != o.getClass()) return false;
58+
59+
SonosPlaylistList that = (SonosPlaylistList) o;
60+
61+
return new EqualsBuilder()
62+
.append(version, that.version)
63+
.append(playlists, that.playlists)
64+
.isEquals();
65+
}
66+
67+
@Override
68+
public int hashCode()
69+
{
70+
return new HashCodeBuilder(17, 37)
71+
.append(version)
72+
.append(playlists)
73+
.toHashCode();
74+
}
75+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package engineer.nightowl.sonos.api.domain;
2+
3+
import org.apache.commons.lang3.builder.EqualsBuilder;
4+
import org.apache.commons.lang3.builder.HashCodeBuilder;
5+
6+
public class SonosPlaylistTrack
7+
{
8+
private String name;
9+
private String artist;
10+
private String album;
11+
12+
public SonosPlaylistTrack()
13+
{
14+
}
15+
16+
public SonosPlaylistTrack(String name, String artist, String album)
17+
{
18+
this.name = name;
19+
this.artist = artist;
20+
this.album = album;
21+
}
22+
23+
public String getName()
24+
{
25+
return name;
26+
}
27+
28+
public void setName(String name)
29+
{
30+
this.name = name;
31+
}
32+
33+
public String getArtist()
34+
{
35+
return artist;
36+
}
37+
38+
public void setArtist(String artist)
39+
{
40+
this.artist = artist;
41+
}
42+
43+
public String getAlbum()
44+
{
45+
return album;
46+
}
47+
48+
public void setAlbum(String album)
49+
{
50+
this.album = album;
51+
}
52+
53+
@Override
54+
public String toString()
55+
{
56+
return "SonosPlaylistTrack{" +
57+
"name='" + name + '\'' +
58+
", artist='" + artist + '\'' +
59+
", album='" + album + '\'' +
60+
'}';
61+
}
62+
63+
@Override
64+
public boolean equals(Object o)
65+
{
66+
if (this == o) return true;
67+
68+
if (o == null || getClass() != o.getClass()) return false;
69+
70+
SonosPlaylistTrack that = (SonosPlaylistTrack) o;
71+
72+
return new EqualsBuilder()
73+
.append(name, that.name)
74+
.append(artist, that.artist)
75+
.append(album, that.album)
76+
.isEquals();
77+
}
78+
79+
@Override
80+
public int hashCode()
81+
{
82+
return new HashCodeBuilder(17, 37)
83+
.append(name)
84+
.append(artist)
85+
.append(album)
86+
.toHashCode();
87+
}
88+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package engineer.nightowl.sonos.api.resource;
2+
3+
import engineer.nightowl.sonos.api.SonosApiClient;
4+
import engineer.nightowl.sonos.api.domain.SonosPlayMode;
5+
import engineer.nightowl.sonos.api.domain.SonosPlaylist;
6+
import engineer.nightowl.sonos.api.domain.SonosPlaylistList;
7+
import engineer.nightowl.sonos.api.domain.SonosSuccess;
8+
import engineer.nightowl.sonos.api.exception.SonosApiClientException;
9+
import engineer.nightowl.sonos.api.exception.SonosApiError;
10+
11+
import java.util.HashMap;
12+
import java.util.Map;
13+
14+
/**
15+
* Control playlists in a household.
16+
*
17+
* @see <a href="https://developer.sonos.com/reference/control-api/playlists/">Sonos docs</a>
18+
*/
19+
public class PlaylistResource extends SubscribableResource
20+
{
21+
/**
22+
* <p>Constructor for PlaylistResource.</p>
23+
*
24+
* @param apiClient a {@link engineer.nightowl.sonos.api.SonosApiClient} object.
25+
*/
26+
public PlaylistResource(final SonosApiClient apiClient)
27+
{
28+
super(apiClient);
29+
}
30+
31+
/** {@inheritDoc} */
32+
@Override
33+
String getSubscriptionPath()
34+
{
35+
return "/v1/households/%s/playlists/subscription";
36+
}
37+
38+
/**
39+
* Get playlists for a household
40+
*
41+
* @see <a href="https://developer.sonos.com/reference/control-api/playlists/getplaylists/">Sonos docs</a>
42+
* @param clientToken for the user
43+
* @param householdId the household for which you want to retrieve playlists
44+
* @return the playlists for the specified household
45+
* @throws engineer.nightowl.sonos.api.exception.SonosApiClientException if an error occurs during the call
46+
* @throws engineer.nightowl.sonos.api.exception.SonosApiError if there is an error from the API
47+
*/
48+
public SonosPlaylistList getPlaylists(final String clientToken, final String householdId) throws SonosApiClientException, SonosApiError
49+
{
50+
return getFromApi(SonosPlaylistList.class, clientToken, String.format("/v1/households/%s/playlists", householdId));
51+
}
52+
53+
/**
54+
* Get a playlist for a household
55+
*
56+
* @see <a href="https://developer.sonos.com/reference/control-api/playlists/getplaylist/">Sonos docs</a>
57+
* @param clientToken for the user
58+
* @param householdId the household for which the playlist belongs to
59+
* @param playlistId the specific playlist you want to retrieve
60+
* @return the specified playlist
61+
* @throws engineer.nightowl.sonos.api.exception.SonosApiClientException if an error occurs during the call
62+
* @throws engineer.nightowl.sonos.api.exception.SonosApiError if there is an error from the API
63+
*/
64+
public SonosPlaylist getPlaylist(final String clientToken, final String householdId, final String playlistId) throws SonosApiClientException, SonosApiError
65+
{
66+
return getFromApi(SonosPlaylist.class, clientToken, String.format("/v1/households/%s/playlists/%s", householdId, playlistId));
67+
}
68+
69+
/**
70+
* Activate a playlist in a group
71+
*
72+
* @see <a href="https://developer.sonos.com/reference/control-api/playlists/loadplaylist/">Sonos docs</a>
73+
* @param clientToken for the user
74+
* @param groupId to load the playlist in
75+
* @param playlistId of the playlist
76+
* @param playOnCompletion (optional) start playing once loaded
77+
* @param playMode (optional) provide playback options, where supported
78+
* @return if the call was successful
79+
* @throws engineer.nightowl.sonos.api.exception.SonosApiClientException if an error occurs during the call.
80+
* @throws engineer.nightowl.sonos.api.exception.SonosApiError if there is an error from the API.
81+
*/
82+
public SonosSuccess loadPlaylist(final String clientToken, final String groupId, final String playlistId, final Boolean playOnCompletion,
83+
final SonosPlayMode playMode) throws SonosApiClientException, SonosApiError
84+
{
85+
validateNotNull(playlistId, "playlistId");
86+
final Map<String, Object> payload = new HashMap<>();
87+
if (playOnCompletion != null)
88+
{
89+
payload.put("playOnCompletion", playOnCompletion);
90+
}
91+
if (playMode != null)
92+
{
93+
payload.put("playModes", playMode);
94+
}
95+
payload.put("playlistId", playlistId);
96+
97+
return postToApi(SonosSuccess.class, clientToken, String.format("/v1/groups/%s/playlists", groupId), payload);
98+
}
99+
}

0 commit comments

Comments
 (0)