@@ -68,22 +68,22 @@ internal const val base = "https://api.spotify.com/v1"
68
68
*
69
69
*/
70
70
sealed class SpotifyApi <T : SpotifyApi <T , B >, B : ISpotifyApiBuilder <T , B >>(
71
- val clientId : String? ,
72
- val clientSecret : String? ,
73
- var token : Token ,
74
- useCache : Boolean ,
75
- var cacheLimit : Int? ,
76
- var automaticRefresh : Boolean ,
77
- var retryWhenRateLimited : Boolean ,
78
- enableLogger : Boolean ,
79
- testTokenValidity : Boolean ,
80
- var defaultLimit : Int ,
81
- var allowBulkRequests : Boolean ,
82
- var requestTimeoutMillis : Long? ,
83
- var json : Json ,
84
- var refreshTokenProducer : suspend (SpotifyApi <* , * >) -> Token ,
85
- var onTokenRefresh : (suspend (SpotifyApi <* , * >) -> Unit )? ,
86
- requiredScopes : List <SpotifyScope >?
71
+ val clientId : String? ,
72
+ val clientSecret : String? ,
73
+ var token : Token ,
74
+ useCache : Boolean ,
75
+ var cacheLimit : Int? ,
76
+ var automaticRefresh : Boolean ,
77
+ var retryWhenRateLimited : Boolean ,
78
+ enableLogger : Boolean ,
79
+ testTokenValidity : Boolean ,
80
+ var defaultLimit : Int ,
81
+ var allowBulkRequests : Boolean ,
82
+ var requestTimeoutMillis : Long? ,
83
+ var json : Json ,
84
+ var refreshTokenProducer : suspend (SpotifyApi <* , * >) -> Token ,
85
+ var onTokenRefresh : (suspend (SpotifyApi <* , * >) -> Unit )? ,
86
+ requiredScopes : List <SpotifyScope >?
87
87
) {
88
88
var useCache = useCache
89
89
set(value) {
@@ -116,18 +116,6 @@ sealed class SpotifyApi<T : SpotifyApi<T, B>, B : ISpotifyApiBuilder<T, B>>(
116
116
)
117
117
}
118
118
}
119
-
120
- if (testTokenValidity) {
121
- if (! isTokenValid().isValid)
122
- try {
123
- refreshToken()
124
- } catch (e: BadRequestException ) {
125
- throw SpotifyException .AuthenticationException (
126
- " Invalid token and refresh token supplied. Cannot refresh to a fresh token." ,
127
- e
128
- )
129
- }
130
- }
131
119
}
132
120
133
121
/* *
@@ -241,8 +229,8 @@ sealed class SpotifyApi<T : SpotifyApi<T, B>, B : ISpotifyApiBuilder<T, B>>(
241
229
242
230
@JvmOverloads
243
231
suspend fun suspendIsTokenValid (
244
- makeTestRequest : Boolean = true,
245
- context : CoroutineContext = Dispatchers .Default
232
+ makeTestRequest : Boolean = true,
233
+ context : CoroutineContext = Dispatchers .Default
246
234
): TokenValidityResponse {
247
235
if (token.shouldRefresh()) return TokenValidityResponse (
248
236
false ,
@@ -271,6 +259,19 @@ sealed class SpotifyApi<T : SpotifyApi<T, B>, B : ISpotifyApiBuilder<T, B>>(
271
259
}
272
260
273
261
companion object {
262
+ internal fun testTokenValidity (api : GenericSpotifyApi ) {
263
+ if (! api.isTokenValid().isValid) {
264
+ try {
265
+ api.refreshToken()
266
+ } catch (e: BadRequestException ) {
267
+ throw SpotifyException .AuthenticationException (
268
+ " Invalid token and refresh token supplied. Cannot refresh to a fresh token." ,
269
+ e
270
+ )
271
+ }
272
+ }
273
+ }
274
+
274
275
/*
275
276
Builder tools
276
277
*/
@@ -285,12 +286,12 @@ sealed class SpotifyApi<T : SpotifyApi<T, B>, B : ISpotifyApiBuilder<T, B>>(
285
286
* @param state This provides protection against attacks such as cross-site request forgery.
286
287
*/
287
288
fun getAuthUrlFull (
288
- vararg scopes : SpotifyScope ,
289
- clientId : String ,
290
- redirectUri : String ,
291
- isImplicitGrantFlow : Boolean = false,
292
- shouldShowDialog : Boolean = false,
293
- state : String? = null
289
+ vararg scopes : SpotifyScope ,
290
+ clientId : String ,
291
+ redirectUri : String ,
292
+ isImplicitGrantFlow : Boolean = false,
293
+ shouldShowDialog : Boolean = false,
294
+ state : String? = null
294
295
): String {
295
296
return " https://accounts.spotify.com/authorize/?client_id=$clientId " +
296
297
" &response_type=${if (isImplicitGrantFlow) " token" else " code" } " +
@@ -312,11 +313,11 @@ sealed class SpotifyApi<T : SpotifyApi<T, B>, B : ISpotifyApiBuilder<T, B>>(
312
313
* @param state This provides protection against attacks such as cross-site request forgery.
313
314
*/
314
315
fun getPkceAuthUrlFull (
315
- vararg scopes : SpotifyScope ,
316
- clientId : String ,
317
- redirectUri : String ,
318
- codeChallenge : String ,
319
- state : String? = null
316
+ vararg scopes : SpotifyScope ,
317
+ clientId : String ,
318
+ redirectUri : String ,
319
+ codeChallenge : String ,
320
+ state : String? = null
320
321
): String {
321
322
return " https://accounts.spotify.com/authorize/?client_id=$clientId " +
322
323
" &response_type=code" +
@@ -361,22 +362,22 @@ sealed class SpotifyApi<T : SpotifyApi<T, B>, B : ISpotifyApiBuilder<T, B>>(
361
362
* client authentication
362
363
*/
363
364
class SpotifyAppApi internal constructor(
364
- clientId : String? ,
365
- clientSecret : String? ,
366
- token : Token ,
367
- useCache : Boolean ,
368
- cacheLimit : Int? ,
369
- automaticRefresh : Boolean ,
370
- retryWhenRateLimited : Boolean ,
371
- enableLogger : Boolean ,
372
- testTokenValidity : Boolean ,
373
- defaultLimit : Int ,
374
- allowBulkRequests : Boolean ,
375
- requestTimeoutMillis : Long? ,
376
- json : Json ,
377
- refreshTokenProducer : (suspend (GenericSpotifyApi ) -> Token )? ,
378
- onTokenRefresh : (suspend (GenericSpotifyApi ) -> Unit )? ,
379
- requiredScopes : List <SpotifyScope >?
365
+ clientId : String? ,
366
+ clientSecret : String? ,
367
+ token : Token ,
368
+ useCache : Boolean ,
369
+ cacheLimit : Int? ,
370
+ automaticRefresh : Boolean ,
371
+ retryWhenRateLimited : Boolean ,
372
+ enableLogger : Boolean ,
373
+ testTokenValidity : Boolean ,
374
+ defaultLimit : Int ,
375
+ allowBulkRequests : Boolean ,
376
+ requestTimeoutMillis : Long? ,
377
+ json : Json ,
378
+ refreshTokenProducer : (suspend (GenericSpotifyApi ) -> Token )? ,
379
+ onTokenRefresh : (suspend (GenericSpotifyApi ) -> Unit )? ,
380
+ requiredScopes : List <SpotifyScope >?
380
381
) : SpotifyApi<SpotifyAppApi, SpotifyAppApiBuilder>(
381
382
clientId,
382
383
clientSecret,
@@ -396,10 +397,10 @@ class SpotifyAppApi internal constructor(
396
397
requiredScopes
397
398
) {
398
399
constructor (
399
- clientId: String ,
400
- clientSecret: String ,
401
- token: Token ,
402
- options: SpotifyApiOptions = SpotifyApiOptionsBuilder ().build()
400
+ clientId: String ,
401
+ clientSecret: String ,
402
+ token: Token ,
403
+ options: SpotifyApiOptions = SpotifyApiOptionsBuilder ().build()
403
404
) : this (
404
405
clientId,
405
406
clientSecret,
@@ -481,24 +482,24 @@ class SpotifyAppApi internal constructor(
481
482
* managed through the scopes exposed in [token]
482
483
*/
483
484
open class SpotifyClientApi (
484
- clientId : String? ,
485
- clientSecret : String? ,
486
- var redirectUri : String? ,
487
- token : Token ,
488
- useCache : Boolean ,
489
- cacheLimit : Int? ,
490
- automaticRefresh : Boolean ,
491
- retryWhenRateLimited : Boolean ,
492
- enableLogger : Boolean ,
493
- testTokenValidity : Boolean ,
494
- defaultLimit : Int ,
495
- allowBulkRequests : Boolean ,
496
- requestTimeoutMillis : Long? ,
497
- json : Json ,
498
- refreshTokenProducer : (suspend (GenericSpotifyApi ) -> Token )? ,
499
- val usesPkceAuth : Boolean ,
500
- onTokenRefresh : (suspend (GenericSpotifyApi ) -> Unit )? ,
501
- requiredScopes : List <SpotifyScope >?
485
+ clientId : String? ,
486
+ clientSecret : String? ,
487
+ var redirectUri : String? ,
488
+ token : Token ,
489
+ useCache : Boolean ,
490
+ cacheLimit : Int? ,
491
+ automaticRefresh : Boolean ,
492
+ retryWhenRateLimited : Boolean ,
493
+ enableLogger : Boolean ,
494
+ testTokenValidity : Boolean ,
495
+ defaultLimit : Int ,
496
+ allowBulkRequests : Boolean ,
497
+ requestTimeoutMillis : Long? ,
498
+ json : Json ,
499
+ refreshTokenProducer : (suspend (GenericSpotifyApi ) -> Token )? ,
500
+ val usesPkceAuth : Boolean ,
501
+ onTokenRefresh : (suspend (GenericSpotifyApi ) -> Unit )? ,
502
+ requiredScopes : List <SpotifyScope >?
502
503
) : SpotifyApi<SpotifyClientApi, SpotifyClientApiBuilder>(
503
504
clientId,
504
505
clientSecret,
@@ -518,12 +519,12 @@ open class SpotifyClientApi(
518
519
requiredScopes
519
520
) {
520
521
constructor (
521
- clientId: String ,
522
- clientSecret: String ,
523
- redirectUri: String ,
524
- token: Token ,
525
- usesPkceAuth: Boolean ,
526
- options: SpotifyApiOptions = SpotifyApiOptionsBuilder ().build()
522
+ clientId: String ,
523
+ clientSecret: String ,
524
+ redirectUri: String ,
525
+ token: Token ,
526
+ usesPkceAuth: Boolean ,
527
+ options: SpotifyApiOptions = SpotifyApiOptionsBuilder ().build()
527
528
) : this (
528
529
clientId,
529
530
clientSecret,
@@ -699,6 +700,7 @@ open class SpotifyClientApi(
699
700
api as SpotifyClientApi
700
701
701
702
require(api.clientId != null ) { " The client id is not set" }
703
+
702
704
val response = if (! api.usesPkceAuth) {
703
705
require(api.clientSecret != null ) { " The client secret is not set" }
704
706
executeTokenRequest(
@@ -753,20 +755,20 @@ open class SpotifyClientApi(
753
755
* managed through the scopes exposed in [token]. [token] is not refreshable and is only accessible for limited time.
754
756
*/
755
757
class SpotifyImplicitGrantApi (
756
- clientId : String? ,
757
- clientSecret : String? ,
758
- redirectUri : String? ,
759
- token : Token ,
760
- useCache : Boolean ,
761
- cacheLimit : Int? ,
762
- retryWhenRateLimited : Boolean ,
763
- enableLogger : Boolean ,
764
- testTokenValidity : Boolean ,
765
- defaultLimit : Int ,
766
- allowBulkRequests : Boolean ,
767
- requestTimeoutMillis : Long? ,
768
- json : Json ,
769
- requiredScopes : List <SpotifyScope >?
758
+ clientId : String? ,
759
+ clientSecret : String? ,
760
+ redirectUri : String? ,
761
+ token : Token ,
762
+ useCache : Boolean ,
763
+ cacheLimit : Int? ,
764
+ retryWhenRateLimited : Boolean ,
765
+ enableLogger : Boolean ,
766
+ testTokenValidity : Boolean ,
767
+ defaultLimit : Int ,
768
+ allowBulkRequests : Boolean ,
769
+ requestTimeoutMillis : Long? ,
770
+ json : Json ,
771
+ requiredScopes : List <SpotifyScope >?
770
772
) : SpotifyClientApi(
771
773
clientId,
772
774
clientSecret,
@@ -814,9 +816,9 @@ typealias GenericSpotifyApi = SpotifyApi<*, *>
814
816
suspend fun getCredentialedToken (clientId : String , clientSecret : String , api : GenericSpotifyApi ? , json : Json ): Token = SpotifyApi .getCredentialedToken(clientId, clientSecret, api, json)
815
817
816
818
internal suspend fun executeTokenRequest (
817
- httpConnection : HttpConnection ,
818
- clientId : String ,
819
- clientSecret : String
819
+ httpConnection : HttpConnection ,
820
+ clientId : String ,
821
+ clientSecret : String
820
822
): HttpResponse {
821
823
return httpConnection.execute(
822
824
listOf (
0 commit comments