Skip to content

Commit 95bcbcb

Browse files
committed
document endpoint categories, start documentation of each endpoint, add helper in LinkedResult
Signed-off-by: Adam Ratzman <adam@adamratzman.com>
1 parent f1b1303 commit 95bcbcb

File tree

22 files changed

+205
-43
lines changed

22 files changed

+205
-43
lines changed

README.md

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ This is the [Kotlin](https://kotlinlang.org/) implementation of the [Spotify Web
33

44
## Contents
55
1. **[Downloading](#downloading)**
6-
2. **[Creating a SpotifyAPI or SpotifyClientAPI object](#creating-a-spotifyAPI-or-spotifyClientAPI-object)**
7-
3. **[What is the SpotifyRestAction class?](#what-is-the-spotifyRestAction-class?)**
6+
2. **[Creating a SpotifyAPI or SpotifyClientAPI object](#creating-a-spotifyapi-or-spotifyclientapi-object)**
7+
3. **[What is the SpotifyRestAction class?](#what-is-the-spotifyrestaction-class?)**
88
4. **[Using the Library](#using-the-library)**
99

1010
## Downloading
@@ -76,6 +76,9 @@ time, this will likely be accurate within a few milliseconds.
7676
- `asFuture()` transforms the supplier into a `CompletableFuture`
7777

7878
## Using the Library
79+
### The benefits of LinkedResults, PagingObjects, and Cursor-based Paging Objects
80+
tbd
81+
7982
### Generic Request
8083
```kotlin
8184
val api = SpotifyAPI.Builder("appId", "appSecret").build()
@@ -95,6 +98,9 @@ time, this will likely be accurate within a few milliseconds.
9598
}
9699
```
97100

101+
### Track Relinking
102+
tbd
103+
98104
### Endpoint List
99105
#### SpotifyAPI:
100106
- **[AlbumAPI (SpotifyAPI.albums)](https://developer.spotify.com/web-api/album-endpoints/)**
@@ -136,4 +142,44 @@ time, this will likely be accurate within a few milliseconds.
136142
5. `getAudioFeatures(vararg trackIds: String)` returns an ordered list of AudioFeatures. Time is *not* linear by track amount
137143
- **[PublicUserAPI (SpotifyAPI.users)](https://developer.spotify.com/web-api/user-profile-endpoints/)**
138144
1. `getProfile` returns the corresponding SpotifyPublicUser object. Pay attention to nullable parameters.
139-
to be continued..
145+
#### SpotifyClientAPI:
146+
Make sure your application has requested the proper [Scopes](https://developer.spotify.com/web-api/using-scopes/) in order to
147+
ensure proper function of this library.
148+
149+
Check to see which Scope is necessary with the corresponding endpoint using the
150+
links provided for each API below
151+
- **[PersonalizationAPI (SpotifyClientAPI.personalization)](https://developer.spotify.com/web-api/web-api-personalization-endpoints/)**
152+
1. `getTopArtists` returns an Artist `PagingObject` representing the most played Artists by the user
153+
2. `getTopTracks` returns a Track `PagingObject` representing the most played Tracks by the user
154+
- **[ClientUserAPI (SpotifyClientAPI.userProfile)](https://developer.spotify.com/web-api/user-profile-endpoints/)**
155+
1. `getUserInformation` returns SpotifyUserInformation object, a much more detailed version of the public user object.
156+
- **[UserLibraryAPI (SpotifyClientAPI.userLibrary)](https://developer.spotify.com/web-api/library-endpoints/)**
157+
1. `getSavedTracks` returns a `PagingObject` of saved tracks in the user's library
158+
2. `getSavedAlbums` returns a `PagingObject` of saved albums in the user's library
159+
3. `savedTracksContains` returns an ordered List of Booleans of whether the track exists in the user's library
160+
corresponding to entered ids
161+
4. `savedAlbumsContains` returns an ordered List of Booleans of whether the album exists in the user's library
162+
corresponding to entered ids.
163+
5. `saveTracks` saves the entered tracks to the user's library. Returns nothing
164+
6. `saveAlbums` saves the entered albums to the user's library. Returns nothing
165+
7. `removeSavedTracks` removes the entered tracks from the user's library. Nothing happens if the track is not found. Returns nothing
166+
8. `removeSavedAlbums` removes the entered albums from the user's library. Nothing happens if the album is not found in the library. Returns nothing
167+
- **[FollowingAPI (SpotifyClientAPI.userFollowing)](https://developer.spotify.com/web-api/web-api-follow-endpoints/)**
168+
1. `followingUsers` returns an ordered List of Booleans representing if the user follows the specified users
169+
2. `followingArtists` returns an ordered List of Booleans representing if the user follows the specified artists
170+
3. `getFollowedArtists` returns a [Cursor-Based Paging Object](https://developer.spotify.com/web-api/object-model/#cursor-based-paging-object) of followed Artists.
171+
4. `followUsers` follows the specified users. Cannot be used with artists. Returns nothing
172+
5. `followArtists` follows the specified artists. Cannot be mixed with users. Returns nothing
173+
6. `followPlaylist` follows the specified playlist. Returns nothing
174+
7. `unfollowUsers` unfollows the specified users. Cannot be used with artists. Returns nothing
175+
8. `unfollowArtists` unfollows the specified artists. Cannot be mixed with users. Returns nothing
176+
9. `unfollowPlaylist` unfollows the specified playlist. Returns nothing
177+
- **[PlayerAPI (SpotifyClientAPI.player)](https://developer.spotify.com/web-api/web-api-connect-endpoint-reference/)**
178+
The methods in this API are in beta and in flux as per Spotify. They will be documented in the near future.
179+
- **[ClientPlaylistsAPI (SpotifyClientAPI.clientPlaylists)](https://developer.spotify.com/web-api/playlist-endpoints/)**
180+
1. `createPlaylist` creates the playlist and returns its full Playlist representation
181+
2. `addTrackToPlaylist` adds the entered tracks to the playlist. Returns nothing
182+
3. `changePlaylistDescription` changes the description of the playlist. Returns nothing
183+
4. `getUserPlaylists` returns a `PagingObject` of SimplePlaylists the user has created
184+
5. `reorderTracks` reorders the tracks in the playlist and returns the current Snapshot
185+
6. `replaceTracks` replaces tracks in the playlist and returns the current Snapshot

src/main/kotlin/com/adamratzman/spotify/endpoints/priv/follow/FollowingAPI.kt renamed to src/main/kotlin/com/adamratzman/spotify/endpoints/priv/follow/UserFollowAPI.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import com.adamratzman.spotify.main.SpotifyAPI
44
import com.adamratzman.spotify.utils.*
55
import java.util.function.Supplier
66

7-
class FollowingAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
7+
/**
8+
* These endpoints allow you manage the artists, users and playlists that a Spotify user follows.
9+
*/
10+
class UserFollowAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
811
fun followingUsers(vararg userIds: String): SpotifyRestAction<List<Boolean>> {
912
return toAction(Supplier {
1013
get("https://api.spotify.com/v1/me/following/contains?type=user&ids=${userIds.joinToString(",") { it.encode() }} { it.encode() }}").toObject<List<Boolean>>(api)

src/main/kotlin/com/adamratzman/spotify/endpoints/priv/library/UserLibraryAPI.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import com.adamratzman.spotify.main.SpotifyAPI
44
import com.adamratzman.spotify.utils.*
55
import java.util.function.Supplier
66

7+
/**
8+
* Endpoints for retrieving information about, and managing, tracks that the current user has saved in their “Your Music” library.
9+
*/
710
class UserLibraryAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
811
fun getSavedTracks(): SpotifyRestAction<PagingObject<SavedTrack>> {
912
return toAction(Supplier {

src/main/kotlin/com/adamratzman/spotify/endpoints/priv/personalization/PersonalizationAPI.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import com.adamratzman.spotify.main.SpotifyAPI
44
import com.adamratzman.spotify.utils.*
55
import java.util.function.Supplier
66

7+
/**
8+
* Endpoints for retrieving information about the user’s listening habits.
9+
*/
710
class PersonalizationAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
811
fun getTopArtists(): SpotifyRestAction<PagingObject<Artist>> {
912
return toAction(Supplier {

src/main/kotlin/com/adamratzman/spotify/endpoints/priv/player/PlayerAPI.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import com.adamratzman.spotify.utils.*
55
import java.util.function.Supplier
66

77
/**
8-
* This endpoint is in beta per the spotify documentation. These methods may or may not work
8+
* These endpoints allow for viewing and controlling user playback. Please view [the official documentation](https://developer.spotify.com/web-api/working-with-connect/)
9+
* for more information on how this works. This is in beta and is available for **premium users only**. Endpoints are **not** guaranteed to work
910
*/
1011
class PlayerAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
1112
fun getDevices(): SpotifyRestAction<List<Device>> {

src/main/kotlin/com/adamratzman/spotify/endpoints/priv/playlists/ClientPlaylistsAPI.kt renamed to src/main/kotlin/com/adamratzman/spotify/endpoints/priv/playlists/UserPlaylistAPI.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ import com.adamratzman.spotify.utils.*
55
import org.json.JSONObject
66
import java.util.function.Supplier
77

8-
class ClientPlaylistsAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
8+
/**
9+
* Endpoints for retrieving information about a user’s playlists and for managing a user’s playlists.
10+
*/
11+
class UserPlaylistAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
912
fun createPlaylist(userId: String, name: String, description: String, public: Boolean = true): SpotifyRestAction<Playlist> {
1013
return toAction(Supplier {
1114
post("https://api.spotify.com/v1/users/${userId.encode()}/playlists",

src/main/kotlin/com/adamratzman/spotify/endpoints/priv/users/ClientUserAPI.kt renamed to src/main/kotlin/com/adamratzman/spotify/endpoints/priv/users/UserAPI.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ import com.adamratzman.spotify.utils.SpotifyUserInformation
77
import com.adamratzman.spotify.utils.toObject
88
import java.util.function.Supplier
99

10-
class ClientUserAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
10+
/**
11+
* Endpoints for retrieving information about a user’s profile.
12+
*/
13+
class UserAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
1114
fun getUserProfile(): SpotifyRestAction<SpotifyUserInformation> {
1215
return toAction(Supplier {
1316
get("https://api.spotify.com/v1/me").toObject<SpotifyUserInformation>(api)

src/main/kotlin/com/adamratzman/spotify/endpoints/pub/album/AlbumAPI.kt

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,45 @@ import com.adamratzman.spotify.utils.*
55
import java.util.function.Supplier
66
import java.util.stream.Collectors
77

8+
/**
9+
* Endpoints for retrieving information about one or more albums from the Spotify catalog.
10+
*/
811
class AlbumAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
12+
/**
13+
* Get Spotify catalog information for a single album.
14+
* @param albumId The Spotify ID for the album.
15+
* @param market Provide this parameter if you want to apply [Track Relinking](https://github.com/adamint/spotify-web-api-kotlin/blob/master/README.md#track-relinking)
16+
*
17+
* @throws BadRequestException if the [albumId] is not found
18+
*/
919
fun getAlbum(albumId: String, market: Market? = null): SpotifyRestAction<Album> {
1020
return toAction(Supplier {
1121
get("https://api.spotify.com/v1/albums/$albumId${if (market != null) "?market=${market.code}" else ""}").toObject<Album>(api)
1222
})
1323
}
1424

15-
fun getAlbums(market: Market? = null, vararg albumIds: String): SpotifyRestAction<List<Album>> {
25+
/**
26+
* Get Spotify catalog information for multiple albums identified by their Spotify IDs. **Albums not found are returned as null inside the ordered list**
27+
* @param albumIds List of the Spotify IDs for the albums.
28+
* @param market Provide this parameter if you want to apply [Track Relinking](https://github.com/adamint/spotify-web-api-kotlin/blob/master/README.md#track-relinking)
29+
*/
30+
fun getAlbums(vararg albumIds: String, market: Market? = null): SpotifyRestAction<List<Album?>> {
1631
if (albumIds.isEmpty()) throw BadRequestException(ErrorObject(404, "You cannot send a request with no album ids!"))
1732
return toAction(Supplier {
1833
get("https://api.spotify.com/v1/albums?ids=${albumIds.map { it.encode() }.toList().stream().collect(Collectors.joining(","))}${if (market != null) "&market=${market.code}" else ""}")
1934
.toObject<AlbumsResponse>(api).albums
2035
})
2136
}
2237

38+
/**
39+
* Get Spotify catalog information about an album’s tracks. Optional parameters can be used to limit the number of tracks returned.
40+
* @param albumId The Spotify ID for the album.
41+
* @param limit The maximum number of tracks to return. Default: 20. Minimum: 1. Maximum: 50.
42+
* @param offset The index of the first track to return. Default: 0 (the first object). Use with limit to get the next set of tracks.
43+
* @param market Provide this parameter if you want to apply [Track Relinking](https://github.com/adamint/spotify-web-api-kotlin/blob/master/README.md#track-relinking)
44+
*
45+
* @throws BadRequestException if the [albumId] is not found, or positioning of [limit] or [offset] is illegal.
46+
*/
2347
fun getAlbumTracks(albumId: String, limit: Int = 20, offset: Int = 0, market: Market? = null): SpotifyRestAction<LinkedResult<SimpleTrack>> {
2448
return toAction(Supplier {
2549
get("https://api.spotify.com/v1/albums/${albumId.encode()}/tracks?limit=$limit&offset=$offset${if (market != null) "&market=${market.code}" else ""}").toLinkedResult<SimpleTrack>(api)

src/main/kotlin/com/adamratzman/spotify/endpoints/pub/artists/ArtistsAPI.kt

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,67 @@ import com.adamratzman.spotify.utils.*
55
import java.util.function.Supplier
66
import java.util.stream.Collectors
77

8+
/**
9+
* Endpoints for retrieving information about one or more artists from the Spotify catalog.
10+
*/
811
class ArtistsAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
12+
/**
13+
* Get Spotify catalog information for a single artist identified by their unique Spotify ID.
14+
* @param artistId The Spotify ID for the artist.
15+
*
16+
* @throws BadRequestException if the [artistId] is not found
17+
*/
918
fun getArtist(artistId: String): SpotifyRestAction<Artist> {
1019
return toAction(Supplier {
1120
get("https://api.spotify.com/v1/artists/${artistId.encode()}").toObject<Artist>(api)
1221
})
1322

1423
}
1524

16-
fun getArtists(vararg artistIds: String): SpotifyRestAction<List<Artist>> {
25+
/**
26+
* Get Spotify catalog information for several artists based on their Spotify IDs. **Artists not found are returned as null inside the ordered list**
27+
* @param artistIds List of the Spotify IDs representing the artists.
28+
*/
29+
fun getArtists(vararg artistIds: String): SpotifyRestAction<List<Artist?>> {
1730
return toAction(Supplier {
1831
get("https://api.spotify.com/v1/artists?ids=${artistIds.map { it.encode() }.toList().stream().collect(Collectors.joining(","))}")
19-
.toObject<ArtistPNList>(api).artists
32+
.toObject<ArtistList>(api).artists
2033
})
2134
}
2235

23-
fun getArtistAlbums(artistId: String): SpotifyRestAction<LinkedResult<SimpleAlbum>> {
36+
/**
37+
* Get Spotify catalog information about an artist’s albums.
38+
* @param artistId Spotify ID for the artist
39+
* @param market Supply this parameter to limit the response to one particular geographical market.
40+
* @param limit The number of album objects to return. Default: 20. Minimum: 1. Maximum: 50.
41+
* @param offset The index of the first album to return. Default: 0 (i.e., the first album). Use with limit to get the next set of albums.
42+
* @param include List of keywords that will be used to filter the response. If not supplied, all album groups will be returned.
43+
*
44+
* @throws BadRequestException if [artistId] is not found, or filter parameters are illegal
45+
*/
46+
fun getArtistAlbums(artistId: String, market: Market?, limit: Int = 20, offset: Int = 0, vararg include: AlbumInclusionStrategy): SpotifyRestAction<LinkedResult<SimpleAlbum>> {
2447
return toAction(Supplier {
25-
get("https://api.spotify.com/v1/artists/${artistId.encode()}/albums").toLinkedResult<SimpleAlbum>(api)
48+
get("https://api.spotify.com/v1/artists/${artistId.encode()}/albums?limit=$limit&offset=$offset" +
49+
if (market != null) "&market=${market.code}" else "" +
50+
if (include.isNotEmpty()) "&include_groups=${include.joinToString(",") { it.keyword }}" else "")
51+
.toLinkedResult<SimpleAlbum>(api)
2652
})
2753
}
2854

55+
enum class AlbumInclusionStrategy(val keyword: String) {
56+
ALBUM("album"), SINGLE("single"), APPEARS_ON("appears_on"), COMPILATION("compilation")
57+
}
58+
2959
fun getArtistTopTracks(artistId: String, market: Market): SpotifyRestAction<List<Track>> {
3060
return toAction(Supplier {
31-
get("https://api.spotify.com/v1/artists/${artistId.encode()}/top-tracks?country=${market.code}").toObject<TrackList>(api).tracks
61+
get("https://api.spotify.com/v1/artists/${artistId.encode()}/top-tracks?country=${market.code}").toObject<TrackList>(api).tracks.map { it!! }
3262
})
3363

3464
}
3565

3666
fun getRelatedArtists(artistId: String): SpotifyRestAction<List<Artist>> {
3767
return toAction(Supplier {
38-
get("https://api.spotify.com/v1/artists/${artistId.encode()}/related-artists").toObject<ArtistList>(api).artists
68+
get("https://api.spotify.com/v1/artists/${artistId.encode()}/related-artists").toObject<ArtistList>(api).artists.map { it!! }
3969
})
4070

4171
}

src/main/kotlin/com/adamratzman/spotify/endpoints/pub/browse/BrowseAPI.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import com.adamratzman.spotify.utils.*
55
import java.util.function.Supplier
66
import java.util.stream.Collectors
77

8+
/**
9+
* Endpoints for getting playlists and new album releases featured on Spotify’s Browse tab.
10+
*/
811
class BrowseAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
912
fun getNewReleases(limit: Int = 20, offset: Int = 0, market: Market? = null): SpotifyRestAction<PagingObject<Album>> {
1013
return toAction(Supplier {

0 commit comments

Comments
 (0)