@@ -29,6 +29,7 @@ import com.adamratzman.spotify.models.serialization.toObject
29
29
import com.adamratzman.spotify.utils.asList
30
30
import com.adamratzman.spotify.utils.runBlocking
31
31
import kotlinx.coroutines.Dispatchers
32
+ import kotlinx.serialization.json.Json
32
33
import kotlin.coroutines.CoroutineContext
33
34
import kotlin.jvm.JvmOverloads
34
35
@@ -64,7 +65,8 @@ sealed class SpotifyApi<T : SpotifyApi<T, B>, B : ISpotifyApiBuilder<T, B>>(
64
65
var automaticRefresh : Boolean ,
65
66
var retryWhenRateLimited : Boolean ,
66
67
enableLogger : Boolean ,
67
- testTokenValidity : Boolean
68
+ testTokenValidity : Boolean ,
69
+ var json : Json
68
70
) {
69
71
var useCache = useCache
70
72
set(value) {
@@ -87,11 +89,15 @@ sealed class SpotifyApi<T : SpotifyApi<T, B>, B : ISpotifyApiBuilder<T, B>>(
87
89
88
90
init {
89
91
if (testTokenValidity) {
90
- try {
91
- isTokenValid(true )
92
- } catch (e: Exception ) {
93
- throw SpotifyAuthenticationException (" Invalid token provided on initialization" , e)
94
- }
92
+ if (! isTokenValid().isValid)
93
+ try {
94
+ refreshToken()
95
+ } catch (e: SpotifyException .BadRequestException ) {
96
+ throw SpotifyException .AuthenticationException (
97
+ " Invalid token and refresh token supplied. Cannot refresh to a fresh token." ,
98
+ e
99
+ )
100
+ }
95
101
}
96
102
}
97
103
@@ -172,10 +178,13 @@ sealed class SpotifyApi<T : SpotifyApi<T, B>, B : ISpotifyApiBuilder<T, B>>(
172
178
}
173
179
174
180
@JvmOverloads
175
- suspend fun suspendIsTokenValid (makeTestRequest : Boolean = true, context : CoroutineContext = Dispatchers .Default ): TokenValidityResponse {
181
+ suspend fun suspendIsTokenValid (
182
+ makeTestRequest : Boolean = true,
183
+ context : CoroutineContext = Dispatchers .Default
184
+ ): TokenValidityResponse {
176
185
if (token.shouldRefresh()) return TokenValidityResponse (
177
186
false ,
178
- SpotifyAuthenticationException (" Token needs to be refreshed (is it expired?)" )
187
+ SpotifyException . AuthenticationException (" Token needs to be refreshed (is it expired?)" )
179
188
)
180
189
if (! makeTestRequest) return TokenValidityResponse (true , null )
181
190
@@ -210,7 +219,8 @@ class SpotifyAppApi internal constructor(
210
219
automaticRefresh : Boolean ,
211
220
retryWhenRateLimited : Boolean ,
212
221
enableLogger : Boolean ,
213
- testTokenValidity : Boolean
222
+ testTokenValidity : Boolean ,
223
+ json : Json
214
224
) : SpotifyApi<SpotifyAppApi, SpotifyAppApiBuilder>(
215
225
clientId,
216
226
clientSecret,
@@ -220,7 +230,8 @@ class SpotifyAppApi internal constructor(
220
230
automaticRefresh,
221
231
retryWhenRateLimited,
222
232
enableLogger,
223
- testTokenValidity
233
+ testTokenValidity,
234
+ json
224
235
) {
225
236
constructor (
226
237
clientId: String ,
@@ -236,7 +247,8 @@ class SpotifyAppApi internal constructor(
236
247
options.automaticRefresh,
237
248
options.retryWhenRateLimited,
238
249
options.enableLogger,
239
- options.testTokenValidity
250
+ options.testTokenValidity,
251
+ options.json
240
252
)
241
253
242
254
override val search: SearchApi = SearchApi (this )
@@ -264,7 +276,7 @@ class SpotifyAppApi internal constructor(
264
276
require(clientId != null && clientSecret != null ) { " Either the client id or the client secret is not set" }
265
277
val currentToken = this .token
266
278
267
- token = getCredentialedToken(clientId, clientSecret, this )
279
+ token = getCredentialedToken(clientId, clientSecret, this , json )
268
280
269
281
return currentToken
270
282
}
@@ -311,7 +323,8 @@ class SpotifyClientApi internal constructor(
311
323
automaticRefresh : Boolean ,
312
324
retryWhenRateLimited : Boolean ,
313
325
enableLogger : Boolean ,
314
- testTokenValidity : Boolean
326
+ testTokenValidity : Boolean ,
327
+ json : Json
315
328
) : SpotifyApi<SpotifyClientApi, SpotifyClientApiBuilder>(
316
329
clientId,
317
330
clientSecret,
@@ -321,7 +334,8 @@ class SpotifyClientApi internal constructor(
321
334
automaticRefresh,
322
335
retryWhenRateLimited,
323
336
enableLogger,
324
- testTokenValidity
337
+ testTokenValidity,
338
+ json
325
339
) {
326
340
constructor (
327
341
clientId: String ,
@@ -339,7 +353,8 @@ class SpotifyClientApi internal constructor(
339
353
options.automaticRefresh,
340
354
options.retryWhenRateLimited,
341
355
options.enableLogger,
342
- options.testTokenValidity
356
+ options.testTokenValidity,
357
+ options.json
343
358
)
344
359
345
360
override val search: SearchApi = SearchApi (this )
@@ -431,7 +446,7 @@ class SpotifyClientApi internal constructor(
431
446
)
432
447
433
448
if (response.responseCode / 200 == 1 ) {
434
- val tempToken = response.body.toObject(Token .serializer(), this )
449
+ val tempToken = response.body.toObject(Token .serializer(), this , json )
435
450
this .token = tempToken.copy(
436
451
refreshToken = tempToken.refreshToken ? : this .token.refreshToken
437
452
).apply { scopes = tempToken.scopes }
@@ -441,7 +456,8 @@ class SpotifyClientApi internal constructor(
441
456
} else throw SpotifyException .BadRequestException (
442
457
response.body.toObject(
443
458
AuthenticationError .serializer(),
444
- this
459
+ this ,
460
+ json
445
461
)
446
462
)
447
463
}
@@ -520,7 +536,7 @@ fun getAuthUrlFull(vararg scopes: SpotifyScope, clientId: String, redirectUri: S
520
536
if (scopes.isEmpty()) " " else " &scope=${scopes.joinToString(" %20" ) { it.uri }} "
521
537
}
522
538
523
- suspend fun getCredentialedToken (clientId : String , clientSecret : String , api : SpotifyApi <* , * >? ): Token {
539
+ suspend fun getCredentialedToken (clientId : String , clientSecret : String , api : SpotifyApi <* , * >? , json : Json ): Token {
524
540
val response = executeTokenRequest(
525
541
HttpConnection (
526
542
" https://accounts.spotify.com/api/token" ,
@@ -533,9 +549,9 @@ suspend fun getCredentialedToken(clientId: String, clientSecret: String, api: Sp
533
549
), clientId, clientSecret
534
550
)
535
551
536
- if (response.responseCode / 200 == 1 ) return response.body.toObject(Token .serializer(), null )
552
+ if (response.responseCode / 200 == 1 ) return response.body.toObject(Token .serializer(), null , json )
537
553
538
- throw SpotifyException .BadRequestException (response.body.toObject(AuthenticationError .serializer(), null ))
554
+ throw SpotifyException .BadRequestException (response.body.toObject(AuthenticationError .serializer(), null , json ))
539
555
}
540
556
541
557
internal suspend fun executeTokenRequest (
0 commit comments