Skip to content

Commit 049cc61

Browse files
committed
add possibility to request open spotify api with track id
1 parent eb149e2 commit 049cc61

File tree

1 file changed

+59
-14
lines changed

1 file changed

+59
-14
lines changed

src/main/java/de/labystudio/spotifyapi/open/OpenSpotifyAPI.java

Lines changed: 59 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,20 @@ private AccessTokenResponse generateAccessToken() throws IOException {
7979
* @param callback Response with the buffered image track. It won't be called on an error.
8080
*/
8181
public void requestImageAsync(Track track, Consumer<BufferedImage> callback) {
82+
this.requestImageAsync(track.getId(), callback);
83+
}
84+
85+
/**
86+
* Request the cover image of the given track asynchronously.
87+
* If the track is already in the cache, it will be returned.
88+
*
89+
* @param trackId The track id to lookup
90+
* @param callback Response with the buffered image track. It won't be called on an error.
91+
*/
92+
public void requestImageAsync(String trackId, Consumer<BufferedImage> callback) {
8293
this.executor.execute(() -> {
8394
try {
84-
BufferedImage image = this.requestImage(track);
95+
BufferedImage image = this.requestImage(trackId);
8596
if (image != null) {
8697
callback.accept(image);
8798
}
@@ -95,13 +106,13 @@ public void requestImageAsync(Track track, Consumer<BufferedImage> callback) {
95106
* Request the cover image url of the given track asynchronously.
96107
* If the track is already in the cache, it will be returned.
97108
*
98-
* @param track The track to lookup
109+
* @param trackId The track id to lookup
99110
* @param callback Response with the image url of the track. It won't be called on an error.
100111
*/
101-
public void requestImageUrlAsync(Track track, Consumer<String> callback) {
112+
public void requestImageUrlAsync(String trackId, Consumer<String> callback) {
102113
this.executor.execute(() -> {
103114
try {
104-
String imageUrl = this.requestImageUrl(track);
115+
String imageUrl = this.requestImageUrl(trackId);
105116
if (imageUrl != null) {
106117
callback.accept(imageUrl);
107118
}
@@ -119,9 +130,20 @@ public void requestImageUrlAsync(Track track, Consumer<String> callback) {
119130
* @param callback Response with the open track. It won't be called on an error.
120131
*/
121132
public void requestOpenTrackAsync(Track track, Consumer<OpenTrack> callback) {
133+
this.requestOpenTrackAsync(track.getId(), callback);
134+
}
135+
136+
/**
137+
* Request the track information of the given track asynchronously.
138+
* If the open track is already in the cache, it will be returned.
139+
*
140+
* @param trackId The track id to lookup
141+
* @param callback Response with the open track. It won't be called on an error.
142+
*/
143+
public void requestOpenTrackAsync(String trackId, Consumer<OpenTrack> callback) {
122144
this.executor.execute(() -> {
123145
try {
124-
OpenTrack openTrack = this.requestOpenTrack(track);
146+
OpenTrack openTrack = this.requestOpenTrack(trackId);
125147
if (openTrack != null) {
126148
callback.accept(openTrack);
127149
}
@@ -140,14 +162,26 @@ public void requestOpenTrackAsync(Track track, Consumer<OpenTrack> callback) {
140162
* @throws IOException if the request failed
141163
*/
142164
public BufferedImage requestImage(Track track) throws IOException {
165+
return this.requestImage(track.getId());
166+
}
167+
168+
/**
169+
* Request the cover image of the given track synchronously.
170+
* If the track is already in the cache, it will be returned.
171+
*
172+
* @param trackId The track id to lookup
173+
* @return The buffered image of the track or null if it failed
174+
* @throws IOException if the request failed
175+
*/
176+
public BufferedImage requestImage(String trackId) throws IOException {
143177
// Try to get image from cache by track id
144-
BufferedImage cachedImage = this.imageCache.get(track.getId());
178+
BufferedImage cachedImage = this.imageCache.get(trackId);
145179
if (cachedImage != null) {
146180
return cachedImage;
147181
}
148182

149183
// Request the image url
150-
String url = this.requestImageUrl(track);
184+
String url = this.requestImageUrl(trackId);
151185
if (url == null) {
152186
return null;
153187
}
@@ -159,21 +193,21 @@ public BufferedImage requestImage(Track track) throws IOException {
159193
}
160194

161195
// Cache the image and return it
162-
this.imageCache.push(track.getId(), image);
196+
this.imageCache.push(trackId, image);
163197
return image;
164198
}
165199

166200
/**
167201
* Request the cover image url of the given track.
168202
* If the track is already in the cache, it will be returned.
169203
*
170-
* @param track The track to lookup
204+
* @param trackId The track id to lookup
171205
* @return The url of the track or null if it failed
172206
* @throws IOException if the request failed
173207
*/
174-
private String requestImageUrl(Track track) throws IOException {
208+
private String requestImageUrl(String trackId) throws IOException {
175209
// Request track information
176-
OpenTrack openTrack = this.requestOpenTrack(track);
210+
OpenTrack openTrack = this.requestOpenTrack(trackId);
177211
if (openTrack == null) {
178212
return null;
179213
}
@@ -190,17 +224,28 @@ private String requestImageUrl(Track track) throws IOException {
190224
* @throws IOException if the request failed
191225
*/
192226
public OpenTrack requestOpenTrack(Track track) throws IOException {
193-
OpenTrack cachedOpenTrack = this.openTrackCache.get(track.getId());
227+
return this.requestOpenTrack(track.getId());
228+
}
229+
230+
/**
231+
* Request the track information of the given track.
232+
* If the open track is already in the cache, it will be returned.
233+
*
234+
* @param trackId The track id to lookup
235+
* @throws IOException if the request failed
236+
*/
237+
public OpenTrack requestOpenTrack(String trackId) throws IOException {
238+
OpenTrack cachedOpenTrack = this.openTrackCache.get(trackId);
194239
if (cachedOpenTrack != null) {
195240
return cachedOpenTrack;
196241
}
197242

198243
// Create REST API url
199-
String url = String.format(URL_API_TRACKS, track.getId());
244+
String url = String.format(URL_API_TRACKS, trackId);
200245
OpenTrack openTrack = this.request(url, OpenTrack.class, true);
201246

202247
// Cache the open track and return it
203-
this.openTrackCache.push(track.getId(), openTrack);
248+
this.openTrackCache.push(trackId, openTrack);
204249
return openTrack;
205250
}
206251

0 commit comments

Comments
 (0)