Skip to content

Commit fa80ca4

Browse files
committed
update license, add api reference urls to methods, write browse api sample
1 parent 74b7645 commit fa80ca4

File tree

86 files changed

+2232
-174
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+2232
-174
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/* Spotify Web API, Kotlin Wrapper; MIT License, 2017-2020; Original author: Adam Ratzman */
2+
package public
3+
4+
import com.adamratzman.spotify.SpotifyApi.Companion.spotifyAppApi
5+
6+
fun main() {
7+
// instantiate api
8+
val api = spotifyAppApi(
9+
System.getenv("SPOTIFY_CLIENT_ID"),
10+
System.getenv("SPOTIFY_CLIENT_SECRET")
11+
).build()
12+
13+
// get album "Kids in Love" by the Mowgli's
14+
println(api.albums.getAlbum("spotify:album:4M2p2BIRHIeBu8Ew9IBQ0s").complete()!!.releaseDate)
15+
16+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/* Spotify Web API, Kotlin Wrapper; MIT License, 2017-2020; Original author: Adam Ratzman */
2+
package public
3+
4+
import com.adamratzman.spotify.SpotifyApi.Companion.spotifyAppApi
5+
6+
fun main() {
7+
// instantiate api
8+
val api = spotifyAppApi(
9+
System.getenv("SPOTIFY_CLIENT_ID"),
10+
System.getenv("SPOTIFY_CLIENT_SECRET")
11+
).build()
12+
13+
}
Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,48 @@
1-
/* Spotify Web API - Kotlin Wrapper; MIT License, 2019; Original author: Adam Ratzman */
1+
/* Spotify Web API, Kotlin Wrapper; MIT License, 2017-2020; Original author: Adam Ratzman */
22
package public
33

4-
import com.adamratzman.spotify.spotifyAppApi
4+
import com.adamratzman.spotify.SpotifyApi.Companion.spotifyAppApi
5+
import com.adamratzman.spotify.endpoints.public.TrackAttribute
6+
import com.adamratzman.spotify.endpoints.public.TuneableTrackAttribute
7+
import com.adamratzman.spotify.utils.Locale
8+
import com.adamratzman.spotify.utils.Market
59

610
fun main() {
11+
// instantiate api
712
val api = spotifyAppApi(
813
System.getenv("SPOTIFY_CLIENT_ID"),
914
System.getenv("SPOTIFY_CLIENT_SECRET")
10-
)
15+
).build()
16+
17+
// get and print all Spotify genre seeds, along with the total amount of seeds
18+
val seeds = api.browse.getAvailableGenreSeeds().complete()
19+
20+
println("Total seeds: ${seeds.size} | Seeds: $seeds")
21+
22+
// get and print top 20 Spotify categories
23+
println(api.browse.getCategoryList(limit = 20).complete().items.map { it.name })
24+
25+
// get and print all Spotify categories in the French market, with German strings
26+
println(api.browse.getCategoryList(market = Market.FR, locale = Locale.de_DE).getAllItems().complete().map { "${it.name} (${it.id})" })
27+
28+
// get the "Pop" category
29+
println(api.browse.getCategory("pop").complete())
30+
31+
// get 35 new featured album releases, starting with the 4th featured album (index=3)
32+
println(api.browse.getNewReleases(limit = 35, offset = 3).getAllItems().complete().map { release -> "${release.name} by ${release.artists.map { it.name }}" })
33+
34+
// get available genre seeds
35+
println(api.browse.getAvailableGenreSeeds().complete())
36+
37+
// get featured playlists message
38+
println(api.browse.getFeaturedPlaylists().complete().message)
39+
40+
// get and print recommendation seeds and tracks
41+
val recommendationResponse = api.browse.getTrackRecommendations(
42+
seedTracks = listOf("spotify:track:2LYAG9jlH9rul11nalRxR0"),
43+
seedGenres = listOf("indie-pop"),
44+
minAttributes = listOf(TrackAttribute.create(TuneableTrackAttribute.Danceability, 0.6f))
45+
).complete()
46+
47+
println("Recommendation tracks: ${recommendationResponse.tracks.map { track -> "${track.name} by ${track.artists.map { it.name }}" }}")
1148
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/* Spotify Web API, Kotlin Wrapper; MIT License, 2017-2020; Original author: Adam Ratzman */
2+
package public
3+
4+
import com.adamratzman.spotify.SpotifyApi.Companion.spotifyAppApi
5+
6+
fun main() {
7+
// instantiate api
8+
val api = spotifyAppApi(
9+
System.getenv("SPOTIFY_CLIENT_ID"),
10+
System.getenv("SPOTIFY_CLIENT_SECRET")
11+
).build()
12+
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/* Spotify Web API, Kotlin Wrapper; MIT License, 2017-2020; Original author: Adam Ratzman */
2+
package public
3+
4+
import com.adamratzman.spotify.SpotifyApi.Companion.spotifyAppApi
5+
6+
fun main() {
7+
// instantiate api
8+
val api = spotifyAppApi(
9+
System.getenv("SPOTIFY_CLIENT_ID"),
10+
System.getenv("SPOTIFY_CLIENT_SECRET")
11+
).build()
12+
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/* Spotify Web API, Kotlin Wrapper; MIT License, 2017-2020; Original author: Adam Ratzman */
2+
package public
3+
4+
import com.adamratzman.spotify.SpotifyApi.Companion.spotifyAppApi
5+
6+
fun main() {
7+
// instantiate api
8+
val api = spotifyAppApi(
9+
System.getenv("SPOTIFY_CLIENT_ID"),
10+
System.getenv("SPOTIFY_CLIENT_SECRET")
11+
).build()
12+
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/* Spotify Web API, Kotlin Wrapper; MIT License, 2017-2020; Original author: Adam Ratzman */
2+
package public
3+
4+
import com.adamratzman.spotify.SpotifyApi.Companion.spotifyAppApi
5+
6+
fun main() {
7+
// instantiate api
8+
val api = spotifyAppApi(
9+
System.getenv("SPOTIFY_CLIENT_ID"),
10+
System.getenv("SPOTIFY_CLIENT_SECRET")
11+
).build()
12+
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/* Spotify Web API, Kotlin Wrapper; MIT License, 2017-2020; Original author: Adam Ratzman */
2+
package public
3+
4+
import com.adamratzman.spotify.SpotifyApi.Companion.spotifyAppApi
5+
6+
fun main() {
7+
// instantiate api
8+
val api = spotifyAppApi(
9+
System.getenv("SPOTIFY_CLIENT_ID"),
10+
System.getenv("SPOTIFY_CLIENT_SECRET")
11+
).build()
12+
13+
}

src/commonMain/kotlin/com.adamratzman.spotify/Builder.kt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
/* Spotify Web API - Kotlin Wrapper; MIT License, 2019; Original author: Adam Ratzman */
1+
/* Spotify Web API, Kotlin Wrapper; MIT License, 2017-2020; Original author: Adam Ratzman */
22
package com.adamratzman.spotify
33

4+
import com.adamratzman.spotify.SpotifyApi.Companion.getCredentialedToken
45
import com.adamratzman.spotify.SpotifyApi.Companion.spotifyAppApi
56
import com.adamratzman.spotify.SpotifyApi.Companion.spotifyClientApi
67
import com.adamratzman.spotify.http.HttpConnection
@@ -198,7 +199,6 @@ interface ISpotifyApiBuilder<T : SpotifyApi<T, B>, B : ISpotifyApiBuilder<T, B>>
198199
*/
199200
var authorization: SpotifyUserAuthorization
200201

201-
202202
/**
203203
* Allows you to override default values for caching, token refresh, and logging
204204
*/
@@ -308,7 +308,7 @@ class SpotifyClientApiBuilder(
308308
) : ISpotifyClientApiBuilder {
309309
override fun getAuthorizationUrl(vararg scopes: SpotifyScope): String {
310310
require(credentials.redirectUri != null && credentials.clientId != null) { "You didn't specify a redirect uri or client id in the credentials block!" }
311-
return getAuthUrlFull(*scopes, clientId = credentials.clientId!!, redirectUri = credentials.redirectUri!!)
311+
return SpotifyApi.getAuthUrlFull(*scopes, clientId = credentials.clientId!!, redirectUri = credentials.redirectUri!!)
312312
}
313313

314314
override suspend fun suspendBuild(): SpotifyClientApi {
@@ -396,13 +396,11 @@ class SpotifyClientApiBuilder(
396396
}
397397
}
398398

399-
400399
/**
401400
* App Api builder interface
402401
*/
403402
interface ISpotifyAppApiBuilder : ISpotifyApiBuilder<SpotifyAppApi, SpotifyAppApiBuilder>
404403

405-
406404
/**
407405
* [SpotifyAppApi] builder for api creation using client authorization
408406
*/

src/commonMain/kotlin/com.adamratzman.spotify/SpotifyApi.kt

Lines changed: 52 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Spotify Web API - Kotlin Wrapper; MIT License, 2019; Original author: Adam Ratzman */
1+
/* Spotify Web API, Kotlin Wrapper; MIT License, 2017-2020; Original author: Adam Ratzman */
22
package com.adamratzman.spotify
33

44
import com.adamratzman.spotify.SpotifyException.BadRequestException
@@ -28,10 +28,10 @@ import com.adamratzman.spotify.models.TokenValidityResponse
2828
import com.adamratzman.spotify.models.serialization.toObject
2929
import com.adamratzman.spotify.utils.asList
3030
import com.adamratzman.spotify.utils.runBlocking
31-
import kotlinx.coroutines.Dispatchers
32-
import kotlinx.serialization.json.Json
3331
import kotlin.coroutines.CoroutineContext
3432
import kotlin.jvm.JvmOverloads
33+
import kotlinx.coroutines.Dispatchers
34+
import kotlinx.serialization.json.Json
3535

3636
/**
3737
* Base url for Spotify web api calls
@@ -60,16 +60,16 @@ internal const val base = "https://api.spotify.com/v1"
6060
*
6161
*/
6262
sealed class SpotifyApi<T : SpotifyApi<T, B>, B : ISpotifyApiBuilder<T, B>>(
63-
val clientId: String?,
64-
val clientSecret: String?,
65-
var token: Token,
66-
useCache: Boolean,
67-
var cacheLimit: Int?,
68-
var automaticRefresh: Boolean,
69-
var retryWhenRateLimited: Boolean,
70-
enableLogger: Boolean,
71-
testTokenValidity: Boolean,
72-
var json: Json
63+
val clientId: String?,
64+
val clientSecret: String?,
65+
var token: Token,
66+
useCache: Boolean,
67+
var cacheLimit: Int?,
68+
var automaticRefresh: Boolean,
69+
var retryWhenRateLimited: Boolean,
70+
enableLogger: Boolean,
71+
testTokenValidity: Boolean,
72+
var json: Json
7373
) {
7474
var useCache = useCache
7575
set(value) {
@@ -182,8 +182,8 @@ sealed class SpotifyApi<T : SpotifyApi<T, B>, B : ISpotifyApiBuilder<T, B>>(
182182

183183
@JvmOverloads
184184
suspend fun suspendIsTokenValid(
185-
makeTestRequest: Boolean = true,
186-
context: CoroutineContext = Dispatchers.Default
185+
makeTestRequest: Boolean = true,
186+
context: CoroutineContext = Dispatchers.Default
187187
): TokenValidityResponse {
188188
if (token.shouldRefresh()) return TokenValidityResponse(
189189
false,
@@ -255,7 +255,6 @@ sealed class SpotifyApi<T : SpotifyApi<T, B>, B : ISpotifyApiBuilder<T, B>>(
255255
throw BadRequestException(response.body.toObject(AuthenticationError.serializer(), null, json))
256256
}
257257

258-
259258
// ==============================================
260259

261260
/*
@@ -310,10 +309,10 @@ sealed class SpotifyApi<T : SpotifyApi<T, B>, B : ISpotifyApiBuilder<T, B>>(
310309
* @return Configurable [SpotifyClientApiBuilder] that, when built, creates a new [SpotifyClientApi]
311310
*/
312311
fun spotifyClientApi(
313-
clientId: String,
314-
clientSecret: String,
315-
redirectUri: String,
316-
block: SpotifyClientApiBuilder.() -> Unit
312+
clientId: String,
313+
clientSecret: String,
314+
redirectUri: String,
315+
block: SpotifyClientApiBuilder.() -> Unit
317316
) = SpotifyClientApiBuilder().apply(block).apply {
318317
credentials {
319318
this.clientId = clientId
@@ -341,16 +340,16 @@ sealed class SpotifyApi<T : SpotifyApi<T, B>, B : ISpotifyApiBuilder<T, B>>(
341340
* client authentication
342341
*/
343342
class SpotifyAppApi internal constructor(
344-
clientId: String?,
345-
clientSecret: String?,
346-
token: Token,
347-
useCache: Boolean,
348-
cacheLimit: Int?,
349-
automaticRefresh: Boolean,
350-
retryWhenRateLimited: Boolean,
351-
enableLogger: Boolean,
352-
testTokenValidity: Boolean,
353-
json: Json
343+
clientId: String?,
344+
clientSecret: String?,
345+
token: Token,
346+
useCache: Boolean,
347+
cacheLimit: Int?,
348+
automaticRefresh: Boolean,
349+
retryWhenRateLimited: Boolean,
350+
enableLogger: Boolean,
351+
testTokenValidity: Boolean,
352+
json: Json
354353
) : SpotifyApi<SpotifyAppApi, SpotifyAppApiBuilder>(
355354
clientId,
356355
clientSecret,
@@ -364,10 +363,10 @@ class SpotifyAppApi internal constructor(
364363
json
365364
) {
366365
constructor(
367-
clientId: String,
368-
clientSecret: String,
369-
token: Token,
370-
options: SpotifyApiOptions = SpotifyApiOptionsBuilder().build()
366+
clientId: String,
367+
clientSecret: String,
368+
token: Token,
369+
options: SpotifyApiOptions = SpotifyApiOptionsBuilder().build()
371370
) : this(
372371
clientId,
373372
clientSecret,
@@ -444,17 +443,17 @@ class SpotifyAppApi internal constructor(
444443
* managed through the scopes exposed in [token]
445444
*/
446445
class SpotifyClientApi internal constructor(
447-
clientId: String?,
448-
clientSecret: String?,
449-
var redirectUri: String?,
450-
token: Token,
451-
useCache: Boolean,
452-
cacheLimit: Int?,
453-
automaticRefresh: Boolean,
454-
retryWhenRateLimited: Boolean,
455-
enableLogger: Boolean,
456-
testTokenValidity: Boolean,
457-
json: Json
446+
clientId: String?,
447+
clientSecret: String?,
448+
var redirectUri: String?,
449+
token: Token,
450+
useCache: Boolean,
451+
cacheLimit: Int?,
452+
automaticRefresh: Boolean,
453+
retryWhenRateLimited: Boolean,
454+
enableLogger: Boolean,
455+
testTokenValidity: Boolean,
456+
json: Json
458457
) : SpotifyApi<SpotifyClientApi, SpotifyClientApiBuilder>(
459458
clientId,
460459
clientSecret,
@@ -468,11 +467,11 @@ class SpotifyClientApi internal constructor(
468467
json
469468
) {
470469
constructor(
471-
clientId: String,
472-
clientSecret: String,
473-
redirectUri: String,
474-
token: Token,
475-
options: SpotifyApiOptions = SpotifyApiOptionsBuilder().build()
470+
clientId: String,
471+
clientSecret: String,
472+
redirectUri: String,
473+
token: Token,
474+
options: SpotifyApiOptions = SpotifyApiOptionsBuilder().build()
476475
) : this(
477476
clientId,
478477
clientSecret,
@@ -660,7 +659,6 @@ typealias SpotifyClientAPI = SpotifyClientApi
660659
@Deprecated("API name has been updated for kotlin convention consistency", ReplaceWith("SpotifyAppApi"))
661660
typealias SpotifyAppAPI = SpotifyAppApi
662661

663-
664662
/**
665663
* Get the authorization url for the provided [clientId] and [redirectUri] application settings, when attempting to authorize with
666664
* specified [scopes]
@@ -685,9 +683,9 @@ fun getAuthUrlFull(vararg scopes: SpotifyScope, clientId: String, redirectUri: S
685683
suspend fun getCredentialedToken(clientId: String, clientSecret: String, api: SpotifyApi<*, *>?, json: Json): Token = SpotifyApi.getCredentialedToken(clientId, clientSecret, api, json)
686684

687685
internal suspend fun executeTokenRequest(
688-
httpConnection: HttpConnection,
689-
clientId: String,
690-
clientSecret: String
686+
httpConnection: HttpConnection,
687+
clientId: String,
688+
clientSecret: String
691689
): HttpResponse {
692690
return httpConnection.execute(
693691
listOf(

0 commit comments

Comments
 (0)