Skip to content

Commit c453c77

Browse files
committed
add samples for all public endpoints
1 parent d59529e commit c453c77

File tree

9 files changed

+68
-11
lines changed

9 files changed

+68
-11
lines changed

samples/src/main/kotlin/public/ArtistApiSample.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,19 @@ fun main() {
99
System.getenv("SPOTIFY_CLIENT_ID"),
1010
System.getenv("SPOTIFY_CLIENT_SECRET")
1111
).build()
12+
13+
// get artist Cody G's total follower count
14+
println(api.artists.getArtist("spotify:artist:26HkLAAIMh5qOFet57d1rg").complete()!!.followers.total)
15+
16+
// get artist Cody G and a non-existant artist
17+
println(api.artists.getArtists("spotify:artist:26HkLAAIMh5qOFet57d1rg", "nonexistantartist"))
18+
19+
// get all of artist Cody G's albums
20+
println(api.artists.getArtistAlbums("spotify:artist:26HkLAAIMh5qOFet57d1rg").getAllItems().complete().map { it.name })
21+
22+
// get Cody G's top tracks, in descending order, in France
23+
println(api.artists.getArtistTopTracks("spotify:artist:26HkLAAIMh5qOFet57d1rg").complete().map { it.name })
24+
25+
// get Cody G's related artists
26+
println(api.artists.getRelatedArtists("spotify:artist:26HkLAAIMh5qOFet57d1rg").complete().map { it.name })
1227
}

samples/src/main/kotlin/public/FollowingApiSample.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,10 @@ fun main() {
99
System.getenv("SPOTIFY_CLIENT_ID"),
1010
System.getenv("SPOTIFY_CLIENT_SECRET")
1111
).build()
12+
13+
// check if adamratzman and adamratzman1 are following "Hit Rewind"
14+
println(api.following.areFollowingPlaylist("spotify:playlist:37i9dQZF1DX0s5kDXi1oC5", "adamratzman", "adamratzman1"))
15+
16+
// check if adamratzman1 is following "Hit Rewind"
17+
println(api.following.isFollowingPlaylist("spotify:playlist:37i9dQZF1DX0s5kDXi1oC5", "adamratzman1"))
1218
}

samples/src/main/kotlin/public/PlaylistApiSample.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,24 @@
22
package public
33

44
import com.adamratzman.spotify.SpotifyApi.Companion.spotifyAppApi
5+
import com.adamratzman.spotify.utils.Market
56

67
fun main() {
78
// instantiate api
89
val api = spotifyAppApi(
910
System.getenv("SPOTIFY_CLIENT_ID"),
1011
System.getenv("SPOTIFY_CLIENT_SECRET")
1112
).build()
13+
14+
// get all playlists followed/owned by adamratzman1
15+
println(api.playlists.getUserPlaylists("adamratzman1").getAllItems().complete().map { it.name })
16+
17+
// get playlist "Hit Rewind"'s description in Andorra's market
18+
println(api.playlists.getPlaylist("spotify:playlist:37i9dQZF1DX0s5kDXi1oC5", Market.AD).complete()!!.description)
19+
20+
// get up to 20 tracks for playlist "Hit Rewind"
21+
println(api.playlists.getPlaylistTracks("spotify:playlist:37i9dQZF1DX0s5kDXi1oC5").complete().items.map { it.track!!.name })
22+
23+
// get playlist covers for "Hit Rewind"
24+
println(api.playlists.getPlaylistCovers("spotify:playlist:37i9dQZF1DX0s5kDXi1oC5").complete())
1225
}

samples/src/main/kotlin/public/SearchApiSample.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,19 @@
22
package public
33

44
import com.adamratzman.spotify.SpotifyApi.Companion.spotifyAppApi
5+
import com.adamratzman.spotify.endpoints.public.SearchApi.SearchType.ALBUM
6+
import com.adamratzman.spotify.endpoints.public.SearchApi.SearchType.TRACK
57

68
fun main() {
79
// instantiate api
810
val api = spotifyAppApi(
911
System.getenv("SPOTIFY_CLIENT_ID"),
1012
System.getenv("SPOTIFY_CLIENT_SECRET")
1113
).build()
14+
15+
// search tracks and albums with the word "name" in them
16+
println(api.search.search("name", TRACK, ALBUM).complete())
17+
18+
// get the first result of playlist "Today's Top Hits"
19+
println(api.search.searchPlaylist("Today's Top Hits").complete().items[0])
1220
}

samples/src/main/kotlin/public/TrackApiSample.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,19 @@ fun main() {
99
System.getenv("SPOTIFY_CLIENT_ID"),
1010
System.getenv("SPOTIFY_CLIENT_SECRET")
1111
).build()
12+
13+
// get track "I'm Good" by the Mowgli's and check its popularity
14+
println(api.tracks.getTrack("spotify:track:5KQDGl3vAkNGyfvSbaW89E").complete()!!.popularity)
15+
16+
// get track "I'm Good" and a non-existant track
17+
println(api.tracks.getTracks("spotify:track:5KQDGl3vAkNGyfvSbaW89E", "nonexistanttrack").complete())
18+
19+
// get audio analysis for track "I'm Good"
20+
println(api.tracks.getAudioAnalysis("spotify:track:5KQDGl3vAkNGyfvSbaW89E").complete())
21+
22+
// get audio features for track "I'm Good"
23+
println(api.tracks.getAudioFeatures("spotify:track:5KQDGl3vAkNGyfvSbaW89E").complete())
24+
25+
// get audio features for an existing and non-existant track
26+
println(api.tracks.getAudioFeatures("spotify:track:5KQDGl3vAkNGyfvSbaW89E", "nonexistanttrack"))
1227
}

samples/src/main/kotlin/public/UserApiSample.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,11 @@ fun main() {
99
System.getenv("SPOTIFY_CLIENT_ID"),
1010
System.getenv("SPOTIFY_CLIENT_SECRET")
1111
).build()
12+
13+
// get profile for adamratzman1
14+
println(api.users.getProfile("adamratzman1").complete())
15+
16+
17+
// get profile of non-existant user
18+
println(api.users.getProfile("nonexistantuserjjjjjjjjjjj"))
1219
}

src/commonMain/kotlin/com.adamratzman.spotify/endpoints/client/ClientFollowingApi.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,16 @@ class ClientFollowingApi(api: SpotifyApi<*, *>) : FollowingApi(api) {
5555
*
5656
* **[Api Reference](https://developer.spotify.com/documentation/web-api/reference/follow/check-user-following-playlist/)**
5757
*
58-
* @param playlistOwner id or uri of the creator of the playlist
5958
* @param playlistId playlist id or uri
6059
*
6160
* @return Boolean representing whether the user follows the playlist
6261
*
6362
* @throws [BadRequestException] if the playlist is not found
6463
* @return Whether the current user is following [playlistId]
6564
*/
66-
fun isFollowingPlaylist(playlistOwner: String, playlistId: String): SpotifyRestAction<Boolean> {
65+
fun isFollowingPlaylist(playlistId: String): SpotifyRestAction<Boolean> {
6766
return toAction {
6867
isFollowingPlaylist(
69-
playlistOwner,
7068
playlistId,
7169
(api as SpotifyClientApi).userId
7270
).complete()

src/commonMain/kotlin/com.adamratzman.spotify/endpoints/public/FollowingApi.kt

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ open class FollowingApi(api: SpotifyApi<*, *>) : SpotifyEndpoint(api) {
2727
*
2828
* **[Api Reference](https://developer.spotify.com/documentation/web-api/reference/follow/check-user-following-playlist/)**
2929
*
30-
* @param playlistOwner id or uri of the creator of the playlist
3130
* @param playlist playlist id or uri
3231
* @param users user ids or uris to check
3332
*
@@ -36,14 +35,12 @@ open class FollowingApi(api: SpotifyApi<*, *>) : SpotifyEndpoint(api) {
3635
* @throws [BadRequestException] if the playlist is not found OR any user in the list does not exist
3736
*/
3837
fun areFollowingPlaylist(
39-
playlistOwner: String,
4038
playlist: String,
4139
vararg users: String
4240
): SpotifyRestAction<List<Boolean>> {
4341
return toAction {
44-
val user = UserUri(playlistOwner)
4542
get(
46-
EndpointBuilder("/users/${user.id.encodeUrl()}/playlists/${PlaylistUri(playlist).id.encodeUrl()}/followers/contains")
43+
EndpointBuilder("/playlists/${PlaylistUri(playlist).id.encodeUrl()}/followers/contains")
4744
.with("ids", users.joinToString(",") { UserUri(it).id.encodeUrl() }).toString()
4845
).toList(Boolean.serializer().list, api, json)
4946
}
@@ -54,18 +51,16 @@ open class FollowingApi(api: SpotifyApi<*, *>) : SpotifyEndpoint(api) {
5451
*
5552
* **[Api Reference](https://developer.spotify.com/documentation/web-api/reference/follow/check-user-following-playlist/)**
5653
*
57-
* @param playlistOwner id or uri of the creator of the playlist
5854
* @param playlist playlist id or uri
5955
* @param user Spotify user id
6056
*
6157
* @return booleans representing whether the user follows the playlist. User IDs **not** found will return false
6258
*
6359
* @throws [BadRequestException] if the playlist is not found or if the user does not exist
6460
*/
65-
fun isFollowingPlaylist(playlistOwner: String, playlist: String, user: String): SpotifyRestAction<Boolean> {
61+
fun isFollowingPlaylist(playlist: String, user: String): SpotifyRestAction<Boolean> {
6662
return toAction {
6763
areFollowingPlaylist(
68-
playlistOwner,
6964
playlist,
7065
users = *arrayOf(user)
7166
).complete()[0]

src/commonMain/kotlin/com.adamratzman.spotify/models/Search.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ package com.adamratzman.spotify.models
44
import kotlinx.serialization.Serializable
55

66
@Serializable
7-
class SpotifySearchResult(
7+
data class SpotifySearchResult(
88
val albums: PagingObject<SimpleAlbum>? = null,
99
val artists: PagingObject<Artist>? = null,
1010
val playlists: PagingObject<SimplePlaylist>? = null,

0 commit comments

Comments
 (0)