Skip to content
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
4 changes: 2 additions & 2 deletions examples/data/player/GetTheUsersQueueExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static void getTheUsersQueue_Sync() {
try {
final PlaybackQueue playbackQueue = getTheUsersQueueRequest.execute();

System.out.println("Count of tracks in the queue: " + playbackQueue.getQueue().size());
System.out.println("Count of items in the queue: " + playbackQueue.getQueue().size());
} catch (IOException | SpotifyWebApiException | ParseException e) {
System.out.println("Error: " + e.getMessage());
}
Expand All @@ -40,7 +40,7 @@ public static void getTheUsersQueue_Async() {
// Example Only. Never block in production code.
final PlaybackQueue playbackQueue = playbackQueueFuture.join();

System.out.println("Count of tracks in the queue: " + playbackQueue.getQueue().size());
System.out.println("Count of items in the queue: " + playbackQueue.getQueue().size());
} catch (CompletionException e) {
System.out.println("Error: " + e.getCause().getMessage());
} catch (CancellationException e) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/se/michaelthelin/spotify/SpotifyApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -1359,7 +1359,7 @@ public AddItemToUsersPlaybackQueueRequest.Builder addItemToUsersPlaybackQueue(St
}

/**
* Receive all tracks from the user's current playback queue.
* Receive all items from the user's current playback queue.
* @return An {@link GetTheUsersQueueRequest.Builder}.
*/
public GetTheUsersQueueRequest.Builder getTheUsersQueue() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,40 +1,56 @@
package se.michaelthelin.spotify.model_objects.special;

import java.util.ArrayList;
import java.util.List;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import se.michaelthelin.spotify.model_objects.AbstractModelObject;
import se.michaelthelin.spotify.model_objects.IModelObject;
import se.michaelthelin.spotify.model_objects.IPlaylistItem;
import se.michaelthelin.spotify.model_objects.miscellaneous.CurrentlyPlaying;
import se.michaelthelin.spotify.model_objects.specification.*;

import java.util.List;
import se.michaelthelin.spotify.model_objects.specification.Episode;
import se.michaelthelin.spotify.model_objects.specification.Track;

/**
* Get the list of objects that make up the user's queue.
* Get the list of items that make up the user's queue.
*/
@JsonDeserialize(builder = CurrentlyPlaying.Builder.class)
public class PlaybackQueue extends AbstractModelObject{

private final List<Track> queue;
public class PlaybackQueue extends AbstractModelObject {
private final IPlaylistItem currentlyPlaying;
private final List<IPlaylistItem> queue;

private PlaybackQueue(final Builder builder) {
super(builder);
this.currentlyPlaying = builder.currentlyPlaying;
this.queue = builder.queue;
}

/**
* Get the list of objects that make up the user's queue.
* Get the user's currently playing item.
*
* @return The tracks that are in the user's queue for the upcoming playback.
* @return the user's currently playing item
*/
public List<Track> getQueue() {
public IPlaylistItem getCurrentlyPlaying() {
return currentlyPlaying;
}

/**
* Get the list of items that make up the user's queue.
*
* @return The items that are in the user's queue for the upcoming playback.
*/
public List<IPlaylistItem> getQueue() {
return queue;
}

@Override
public String toString() {
return "PlaybackQueue{" +
"queue=" + queue +
"currentlyPlaying=" + currentlyPlaying +
", queue=" + queue +
'}';
}

Expand All @@ -48,15 +64,27 @@ public IModelObject.Builder builder() {
*/
public static final class Builder extends AbstractModelObject.Builder {

private List<Track> queue;
private IPlaylistItem currentlyPlaying;
private List<IPlaylistItem> queue;

/**
* The tracks that are in the user's queue for the upcoming playback setter.
* The item representing the user's currently playing item setter.
*
* @param queue The tracks that are in the user's queue for the upcoming playback.
* @param currentlyPlaying The item representing the user's currently playing item.
* @return A {@link PlaybackQueue.Builder}.
*/
public Builder setQueue(List<Track> queue) {
public Builder setCurrentlyPlaying(IPlaylistItem currentlyPlaying) {
this.currentlyPlaying = currentlyPlaying;
return this;
}

/**
* The items that are in the user's queue for the upcoming playback setter.
*
* @param queue The items that are in the user's queue for the upcoming playback.
* @return A {@link PlaybackQueue.Builder}.
*/
public Builder setQueue(List<IPlaylistItem> queue) {
this.queue = queue;
return this;
}
Expand All @@ -76,13 +104,36 @@ public PlaybackQueue createModelObject(JsonObject jsonObject) {
return null;
}

IPlaylistItem currentlyPlaying = hasAndNotNull(jsonObject, "currently_playing")
? asPlaylistItem(jsonObject.getAsJsonObject("currently_playing"))
: null;

List<IPlaylistItem> queue = new ArrayList<>();
if (hasAndNotNull(jsonObject, "queue")) {
for (JsonElement jsonElement : jsonObject.getAsJsonArray("queue")) {
IPlaylistItem queueItem = asPlaylistItem(jsonElement.getAsJsonObject());
queue.add(queueItem);
}
}

return new PlaybackQueue.Builder()
.setQueue(
hasAndNotNull(jsonObject, "queue")
? List.of(new Track.JsonUtil().createModelObjectArray(
jsonObject.getAsJsonArray("queue")))
: null)
.setCurrentlyPlaying(currentlyPlaying)
.setQueue(queue)
.build();
}

private IPlaylistItem asPlaylistItem(JsonObject trackObj) {
IPlaylistItem item = null;
if (hasAndNotNull(trackObj, "type")) {
String type = trackObj.get("type").getAsString().toLowerCase();

if (type.equals("track")) {
item = new Track.JsonUtil().createModelObject(trackObj);
} else if (type.equals("episode")) {
item = new Episode.JsonUtil().createModelObject(trackObj);
}
}
return item;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
package se.michaelthelin.spotify.requests.data.player;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import java.io.IOException;

import org.apache.hc.core5.http.ParseException;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;

import se.michaelthelin.spotify.exceptions.SpotifyWebApiException;
import se.michaelthelin.spotify.model_objects.special.PlaybackQueue;
import se.michaelthelin.spotify.requests.data.AbstractDataRequest;

import java.io.IOException;

/**
* Get the list of objects that make up the user's queue.
* <p>
* Returns the tracks from the current user’s playback queue. Does not include the current playing track.
* Returns the items from the current user’s playback queue, including the currently playing item.
* <p>
* The endpoint does not support paging since the queue is not expected to be large.
* Therefore, the request will return a {@link PlaybackQueue} object including a List of {@link se.michaelthelin.spotify.model_objects.specification.Track}.
* Therefore, the request will return a {@link PlaybackQueue} object including a List of {@link se.michaelthelin.spotify.model_objects.IPlaylistItem}.
*/
@JsonDeserialize(builder = GetTheUsersQueueRequest.Builder.class)
public class GetTheUsersQueueRequest extends AbstractDataRequest<PlaybackQueue> {
Expand All @@ -29,9 +31,9 @@ private GetTheUsersQueueRequest(final Builder builder) {
}

/**
* Get an user's current playback queue.
* Get the user's current playback queue.
*
* @return An {@link PlaybackQueue} object including a List of {@link se.michaelthelin.spotify.model_objects.specification.Track}.
* @return An {@link PlaybackQueue} object including a List of {@link se.michaelthelin.spotify.model_objects.IPlaylistItem}.
* @throws IOException In case of networking issues.
* @throws SpotifyWebApiException The Web API returned an error further specified in this exception's root cause.
* @throws ParseException In case of an error parsing the response.
Expand Down
Loading