2323import com .spotify .clienttoken .http .v0 .ClientToken ;
2424import com .spotify .connectstate .Connect ;
2525import com .spotify .extendedmetadata .ExtendedMetadata ;
26+ import com .spotify .extendedmetadata .ExtensionKindOuterClass ;
2627import com .spotify .metadata .Metadata ;
2728import com .spotify .playlist4 .Playlist4ApiProto ;
2829import okhttp3 .*;
@@ -98,7 +99,7 @@ public void sendAsync(@NotNull String method, @NotNull String suffix, @Nullable
9899 * Sends a request to the Spotify API.
99100 *
100101 * @param method The request method
101- * @param suffix The suffix to be appended to {@link #baseUrl} also know as path
102+ * @param suffix The suffix to be appended to {@link #baseUrl} also known as path
102103 * @param headers Additional headers
103104 * @param body The request body
104105 * @param tries How many times the request should be reattempted (0 = none)
@@ -143,57 +144,47 @@ else if (resp.code() != 200)
143144
144145 @ NotNull
145146 public Metadata .Track getMetadata4Track (@ NotNull TrackId track ) throws IOException , TokenProvider .TokenException {
146- try (Response resp = send ("GET" , "/metadata/4/track/" + track .hexId (), null , null )) {
147- StatusCodeException .checkStatus (resp );
147+ ExtendedMetadata .BatchedExtensionResponse response = getExtendedMetadata (ExtensionKindOuterClass .ExtensionKind .TRACK_V4 , track );
148148
149- ResponseBody body ;
150- if ((body = resp .body ()) == null ) throw new IOException ();
151- return Metadata .Track .parseFrom (body .byteStream ());
152- }
149+ checkExtendedMetadataResponse (response );
150+
151+ return Metadata .Track .parseFrom (response .getExtendedMetadata (0 ).getExtensionData (0 ).getExtensionData ().getValue ());
153152 }
154153
155154 @ NotNull
156155 public Metadata .Episode getMetadata4Episode (@ NotNull EpisodeId episode ) throws IOException , TokenProvider .TokenException {
157- try (Response resp = send ("GET" , "/metadata/4/episode/" + episode .hexId (), null , null )) {
158- StatusCodeException .checkStatus (resp );
156+ ExtendedMetadata .BatchedExtensionResponse response = getExtendedMetadata (ExtensionKindOuterClass .ExtensionKind .EPISODE_V4 , episode );
159157
160- ResponseBody body ;
161- if ((body = resp .body ()) == null ) throw new IOException ();
162- return Metadata .Episode .parseFrom (body .byteStream ());
163- }
158+ checkExtendedMetadataResponse (response );
159+
160+ return Metadata .Episode .parseFrom (response .getExtendedMetadata (0 ).getExtensionData (0 ).getExtensionData ().getValue ());
164161 }
165162
166163 @ NotNull
167164 public Metadata .Album getMetadata4Album (@ NotNull AlbumId album ) throws IOException , TokenProvider .TokenException {
168- try (Response resp = send ("GET" , "/metadata/4/album/" + album .hexId (), null , null )) {
169- StatusCodeException .checkStatus (resp );
165+ ExtendedMetadata .BatchedExtensionResponse response = getExtendedMetadata (ExtensionKindOuterClass .ExtensionKind .ALBUM_V4 , album );
170166
171- ResponseBody body ;
172- if ((body = resp .body ()) == null ) throw new IOException ();
173- return Metadata .Album .parseFrom (body .byteStream ());
174- }
167+ checkExtendedMetadataResponse (response );
168+
169+ return Metadata .Album .parseFrom (response .getExtendedMetadata (0 ).getExtensionData (0 ).getExtensionData ().getValue ());
175170 }
176171
177172 @ NotNull
178173 public Metadata .Artist getMetadata4Artist (@ NotNull ArtistId artist ) throws IOException , TokenProvider .TokenException {
179- try (Response resp = send ("GET" , "/metadata/4/artist/" + artist .hexId (), null , null )) {
180- StatusCodeException .checkStatus (resp );
174+ ExtendedMetadata .BatchedExtensionResponse response = getExtendedMetadata (ExtensionKindOuterClass .ExtensionKind .ARTIST_V4 , artist );
181175
182- ResponseBody body ;
183- if ((body = resp .body ()) == null ) throw new IOException ();
184- return Metadata .Artist .parseFrom (body .byteStream ());
185- }
176+ checkExtendedMetadataResponse (response );
177+
178+ return Metadata .Artist .parseFrom (response .getExtendedMetadata (0 ).getExtensionData (0 ).getExtensionData ().getValue ());
186179 }
187180
188181 @ NotNull
189182 public Metadata .Show getMetadata4Show (@ NotNull ShowId show ) throws IOException , TokenProvider .TokenException {
190- try (Response resp = send ("GET" , "/metadata/4/show/" + show .hexId (), null , null )) {
191- StatusCodeException .checkStatus (resp );
183+ ExtendedMetadata .BatchedExtensionResponse response = getExtendedMetadata (ExtensionKindOuterClass .ExtensionKind .SHOW_V4 , show );
192184
193- ResponseBody body ;
194- if ((body = resp .body ()) == null ) throw new IOException ();
195- return Metadata .Show .parseFrom (body .byteStream ());
196- }
185+ checkExtendedMetadataResponse (response );
186+
187+ return Metadata .Show .parseFrom (response .getExtendedMetadata (0 ).getExtensionData (0 ).getExtensionData ().getValue ());
197188 }
198189
199190 @ NotNull
@@ -207,6 +198,17 @@ public EntityCanvazResponse getCanvases(@NotNull EntityCanvazRequest req) throws
207198 }
208199 }
209200
201+ public void checkExtendedMetadataResponse (ExtendedMetadata .BatchedExtensionResponse response ) throws IOException {
202+ if (response .getExtendedMetadataCount () == 0 )
203+ throw new IOException ("No metadata in BatchedExtensionResponse" );
204+
205+ if (response .getExtendedMetadata (0 ).getExtensionDataCount () == 0 )
206+ throw new IOException ("No metadata in ExtendedMetadata in BatchedExtensionResponse" );
207+
208+ if (response .getExtendedMetadata (0 ).getExtensionData (0 ).getHeader ().getStatusCode () != 200 )
209+ throw new IOException ("Bad status code for metadata: " + response .getExtendedMetadata (0 ).getExtensionData (0 ).getHeader ().getStatusCode ());
210+ }
211+
210212 @ NotNull
211213 public ExtendedMetadata .BatchedExtensionResponse getExtendedMetadata (@ NotNull ExtendedMetadata .BatchedEntityRequest req ) throws IOException , TokenProvider .TokenException {
212214 try (Response resp = send ("POST" , "/extended-metadata/v0/extended-metadata" , null , protoBody (req ))) {
@@ -218,6 +220,24 @@ public ExtendedMetadata.BatchedExtensionResponse getExtendedMetadata(@NotNull Ex
218220 }
219221 }
220222
223+ @ NotNull
224+ public ExtendedMetadata .BatchedExtensionResponse getExtendedMetadata (@ NotNull ExtensionKindOuterClass .ExtensionKind extensionKind , @ NotNull SpotifyId spotifyId ) throws IOException , TokenProvider .TokenException {
225+ try (Response resp = send ("POST" , "/extended-metadata/v0/extended-metadata" , null , protoBody (ExtendedMetadata .BatchedEntityRequest .newBuilder ()
226+ .addEntityRequest (ExtendedMetadata .EntityRequest .newBuilder ()
227+ .setEntityUri (spotifyId .toSpotifyUri ())
228+ .addQuery (ExtendedMetadata .ExtensionQuery .newBuilder ()
229+ .setExtensionKind (extensionKind )
230+ .build ())
231+ .build ())
232+ .build ()))) {
233+ StatusCodeException .checkStatus (resp );
234+
235+ ResponseBody body ;
236+ if ((body = resp .body ()) == null ) throw new IOException ();
237+ return ExtendedMetadata .BatchedExtensionResponse .parseFrom (body .byteStream ());
238+ }
239+ }
240+
221241 @ NotNull
222242 public Playlist4ApiProto .SelectedListContent getPlaylist (@ NotNull PlaylistId id ) throws IOException , TokenProvider .TokenException {
223243 try (Response resp = send ("GET" , "/playlist/v2/playlist/" + id .id (), null , null )) {
0 commit comments