Skip to content

Commit a5c2448

Browse files
committed
condense ClientLibraryAPI methods (remove redundancy)
1 parent 3f90c14 commit a5c2448

File tree

3 files changed

+27
-85
lines changed

3 files changed

+27
-85
lines changed

src/main/kotlin/com/adamratzman/spotify/endpoints/client/ClientLibraryAPI.kt

Lines changed: 21 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -34,140 +34,77 @@ class ClientLibraryAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
3434
}
3535

3636
/**
37-
* Check if a track is already saved in the current Spotify user’s ‘Your Music’ library.
37+
* Check if the [LibraryType] with id [id] is already saved in the current Spotify user’s ‘Your Music’ library.
3838
*
3939
* @throws BadRequestException if [id] is not found
4040
*/
41-
fun doesLibraryContainTrack(id: String): SpotifyRestAction<Boolean> {
41+
fun doesLibraryContain(type: LibraryType, id: String): SpotifyRestAction<Boolean> {
4242
return toAction(Supplier {
43-
doesLibraryContainTracks(id).complete()[0]
43+
doesLibraryContain(type, ids = *arrayOf(id)).complete()[0]
4444
})
4545
}
4646

4747
/**
48-
* Check if a track is already saved in the current Spotify user’s ‘Your Music’ library.
49-
*
50-
* @throws BadRequestException if a provided id is not found
51-
*/
52-
fun doesLibraryContainTracks(vararg ids: String): SpotifyRestAction<List<Boolean>> {
53-
return toAction(Supplier {
54-
get(EndpointBuilder("/me/tracks/contains").with("ids", ids.joinToString(",") { it.encode() })
55-
.toString()).toObject<List<Boolean>>(api)
56-
})
57-
}
58-
59-
/**
60-
* Check if an album is already saved in the current Spotify user’s ‘Your Music’ library.
61-
*
62-
* @throws BadRequestException if id is not found
63-
*/
64-
fun doesLibraryContainAlbum(id: String): SpotifyRestAction<Boolean> {
65-
return toAction(Supplier {
66-
doesLibraryContainAlbums(id).complete()[0]
67-
})
68-
}
69-
70-
/**
71-
* Check if one or more albums is already saved in the current Spotify user’s ‘Your Music’ library.
48+
* Check if one or more of [LibraryType] is already saved in the current Spotify user’s ‘Your Music’ library.
7249
*
7350
* @throws BadRequestException if any of the provided ids is invalid
7451
*/
75-
fun doesLibraryContainAlbums(vararg ids: String): SpotifyRestAction<List<Boolean>> {
52+
fun doesLibraryContain(type: LibraryType, vararg ids: String): SpotifyRestAction<List<Boolean>> {
7653
return toAction(Supplier {
77-
get(EndpointBuilder("/me/albums/contains").with("ids", ids.joinToString(",") { it.encode() })
54+
get(EndpointBuilder("/me/$type/contains").with("ids", ids.joinToString(",") { it.encode() })
7855
.toString()).toObject<List<Boolean>>(api)
7956
})
8057
}
8158

8259
/**
83-
* Save a track to the current user’s ‘Your Music’ library.
60+
* Save one of [LibraryType] to the current user’s ‘Your Music’ library.
8461
*
8562
* @throws BadRequestException if the id is invalid
8663
*/
87-
fun addTrackToLibrary(id: String): SpotifyRestAction<Unit> {
64+
fun addToLibrary(type: LibraryType, id: String): SpotifyRestAction<Unit> {
8865
return toAction(Supplier {
89-
addTracksToLibrary(id).complete()
66+
addToLibrary(type, ids = *arrayOf(id)).complete()
9067
})
9168
}
9269

9370
/**
94-
* Save one or more tracks to the current user’s ‘Your Music’ library.
71+
* Save one or more of [LibraryType] to the current user’s ‘Your Music’ library.
9572
*
9673
* @throws BadRequestException if any of the provided ids is invalid
9774
*/
98-
fun addTracksToLibrary(vararg ids: String): SpotifyRestAction<Unit> {
75+
fun addToLibrary(type: LibraryType, vararg ids: String): SpotifyRestAction<Unit> {
9976
return toAction(Supplier {
100-
put(EndpointBuilder("/me/tracks").with("ids", ids.joinToString(",") { it.encode() }).toString())
77+
put(EndpointBuilder("/me/$type").with("ids", ids.joinToString(",") { it.encode() }).toString())
10178
Unit
10279
})
10380
}
10481

10582
/**
106-
* Save an album to the current user’s ‘Your Music’ library.
107-
*
108-
* @throws BadRequestException if the id is invalid
109-
*/
110-
fun addAlbumToLibrary(id: String): SpotifyRestAction<Unit> {
111-
return toAction(Supplier {
112-
addAlbumsToLibrary(id).complete()
113-
})
114-
}
115-
116-
/**
117-
* Save one or more albums to the current user’s ‘Your Music’ library.
83+
* Remove one of [LibraryType] (track or album) from the current user’s ‘Your Music’ library.
11884
*
11985
* @throws BadRequestException if any of the provided ids is invalid
12086
*/
121-
fun addAlbumsToLibrary(vararg ids: String): SpotifyRestAction<Unit> {
87+
fun removeFromLibrary(type: LibraryType, id: String): SpotifyRestAction<Unit> {
12288
return toAction(Supplier {
123-
put(EndpointBuilder("/me/albums").with("ids", ids.joinToString(",") { it.encode() }).toString())
124-
Unit
89+
removeFromLibrary(type, ids = *arrayOf(id)).complete()
12590
})
12691
}
12792

12893
/**
129-
* Remove a track from the current user’s ‘Your Music’ library.
130-
*
131-
* @throws BadRequestException if the provided id is invalid
132-
*/
133-
fun removeTrackFromLibrary(id: String): SpotifyRestAction<Unit> {
134-
return toAction(Supplier {
135-
removeTracksFromLibrary(id).complete()
136-
})
137-
}
138-
139-
/**
140-
* Remove one or more tracks from the current user’s ‘Your Music’ library.
94+
* Remove one or more of the [LibraryType] (tracks or albums) from the current user’s ‘Your Music’ library.
14195
*
14296
* @throws BadRequestException if any of the provided ids is invalid
14397
*/
144-
fun removeTracksFromLibrary(vararg ids: String): SpotifyRestAction<Unit> {
98+
fun removeFromLibrary(type: LibraryType, vararg ids: String): SpotifyRestAction<Unit> {
14599
return toAction(Supplier {
146-
delete(EndpointBuilder("/me/tracks").with("ids", ids.joinToString(",") { it.encode() }).toString())
100+
delete(EndpointBuilder("/me/$type").with("ids", ids.joinToString(",") { it.encode() }).toString())
147101
Unit
148102
})
149103
}
150104

151-
/**
152-
* Remove an album from the current user’s ‘Your Music’ library.
153-
*
154-
* @throws BadRequestException if any of the provided ids is invalid
155-
*/
156-
fun removeAlbumFromLibrary(id: String): SpotifyRestAction<Unit> {
157-
return toAction(Supplier {
158-
removeAlbumsFromLibrary(id).complete()
159-
})
160-
}
105+
enum class LibraryType(private val value: String) {
106+
TRACK("tracks"), ALBUM("albums");
161107

162-
/**
163-
* Remove one or more albums from the current user’s ‘Your Music’ library.
164-
*
165-
* @throws BadRequestException if any of the provided ids is invalid
166-
*/
167-
fun removeAlbumsFromLibrary(vararg ids: String): SpotifyRestAction<Unit> {
168-
return toAction(Supplier {
169-
delete(EndpointBuilder("/me/albums").with("ids", ids.joinToString(",") { it.encode() }).toString())
170-
Unit
171-
})
108+
override fun toString() = value
172109
}
173110
}

src/main/kotlin/com/adamratzman/spotify/endpoints/public/PlaylistsAPI.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ open class PlaylistsAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
7474
*/
7575
fun getPlaylistCovers(playlistId: String): SpotifyRestAction<List<SpotifyImage>> {
7676
return toAction(Supplier {
77-
println(get(EndpointBuilder("/playlists/${playlistId.encode()}/images").toString()))
7877
get(EndpointBuilder("/playlists/${playlistId.encode()}/images").toString()).toObject<List<SpotifyImage>>(api)
7978
})
8079
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
package com.adamratzman.spotify.private
22

3+
import com.adamratzman.spotify.api
4+
import com.adamratzman.spotify.main.SpotifyClientAPI
35
import org.spekframework.spek2.Spek
6+
import org.spekframework.spek2.style.specification.describe
47

58
class ClientLibraryAPITest: Spek({
9+
describe("Client Library tests") {
10+
if (api !is SpotifyClientAPI) return@describe
611

12+
}
713
})

0 commit comments

Comments
 (0)