@@ -9,6 +9,7 @@ import com.adamratzman.spotify.endpoints.client.ClientPersonalizationApi
9
9
import com.adamratzman.spotify.endpoints.client.ClientPlayerApi
10
10
import com.adamratzman.spotify.endpoints.client.ClientPlaylistApi
11
11
import com.adamratzman.spotify.endpoints.client.ClientProfileApi
12
+ import com.adamratzman.spotify.endpoints.client.ClientSearchApi
12
13
import com.adamratzman.spotify.endpoints.public.AlbumApi
13
14
import com.adamratzman.spotify.endpoints.public.ArtistApi
14
15
import com.adamratzman.spotify.endpoints.public.BrowseApi
@@ -31,10 +32,10 @@ import com.adamratzman.spotify.models.TokenValidityResponse
31
32
import com.adamratzman.spotify.models.serialization.toObject
32
33
import com.adamratzman.spotify.utils.asList
33
34
import com.adamratzman.spotify.utils.runBlocking
34
- import kotlinx.coroutines.Dispatchers
35
- import kotlinx.serialization.json.Json
36
35
import kotlin.coroutines.CoroutineContext
37
36
import kotlin.jvm.JvmOverloads
37
+ import kotlinx.coroutines.Dispatchers
38
+ import kotlinx.serialization.json.Json
38
39
39
40
/* *
40
41
* Base url for Spotify web api calls
@@ -66,19 +67,19 @@ internal const val base = "https://api.spotify.com/v1"
66
67
*
67
68
*/
68
69
sealed class SpotifyApi <T : SpotifyApi <T , B >, B : ISpotifyApiBuilder <T , B >>(
69
- val clientId : String? ,
70
- val clientSecret : String? ,
71
- var token : Token ,
72
- useCache : Boolean ,
73
- var cacheLimit : Int? ,
74
- var automaticRefresh : Boolean ,
75
- var retryWhenRateLimited : Boolean ,
76
- enableLogger : Boolean ,
77
- testTokenValidity : Boolean ,
78
- var defaultLimit : Int ,
79
- var allowBulkRequests : Boolean ,
80
- var requestTimeoutMillis : Long? ,
81
- var json : Json
70
+ val clientId : String? ,
71
+ val clientSecret : String? ,
72
+ var token : Token ,
73
+ useCache : Boolean ,
74
+ var cacheLimit : Int? ,
75
+ var automaticRefresh : Boolean ,
76
+ var retryWhenRateLimited : Boolean ,
77
+ enableLogger : Boolean ,
78
+ testTokenValidity : Boolean ,
79
+ var defaultLimit : Int ,
80
+ var allowBulkRequests : Boolean ,
81
+ var requestTimeoutMillis : Long? ,
82
+ var json : Json
82
83
) {
83
84
var useCache = useCache
84
85
set(value) {
@@ -207,8 +208,8 @@ sealed class SpotifyApi<T : SpotifyApi<T, B>, B : ISpotifyApiBuilder<T, B>>(
207
208
208
209
@JvmOverloads
209
210
suspend fun suspendIsTokenValid (
210
- makeTestRequest : Boolean = true,
211
- context : CoroutineContext = Dispatchers .Default
211
+ makeTestRequest : Boolean = true,
212
+ context : CoroutineContext = Dispatchers .Default
212
213
): TokenValidityResponse {
213
214
if (token.shouldRefresh()) return TokenValidityResponse (
214
215
false ,
@@ -288,19 +289,19 @@ sealed class SpotifyApi<T : SpotifyApi<T, B>, B : ISpotifyApiBuilder<T, B>>(
288
289
* client authentication
289
290
*/
290
291
class SpotifyAppApi internal constructor(
291
- clientId : String? ,
292
- clientSecret : String? ,
293
- token : Token ,
294
- useCache : Boolean ,
295
- cacheLimit : Int? ,
296
- automaticRefresh : Boolean ,
297
- retryWhenRateLimited : Boolean ,
298
- enableLogger : Boolean ,
299
- testTokenValidity : Boolean ,
300
- defaultLimit : Int ,
301
- allowBulkRequests : Boolean ,
302
- requestTimeoutMillis : Long? ,
303
- json : Json
292
+ clientId : String? ,
293
+ clientSecret : String? ,
294
+ token : Token ,
295
+ useCache : Boolean ,
296
+ cacheLimit : Int? ,
297
+ automaticRefresh : Boolean ,
298
+ retryWhenRateLimited : Boolean ,
299
+ enableLogger : Boolean ,
300
+ testTokenValidity : Boolean ,
301
+ defaultLimit : Int ,
302
+ allowBulkRequests : Boolean ,
303
+ requestTimeoutMillis : Long? ,
304
+ json : Json
304
305
) : SpotifyApi<SpotifyAppApi, SpotifyAppApiBuilder>(
305
306
clientId,
306
307
clientSecret,
@@ -317,10 +318,10 @@ class SpotifyAppApi internal constructor(
317
318
json
318
319
) {
319
320
constructor (
320
- clientId: String ,
321
- clientSecret: String ,
322
- token: Token ,
323
- options: SpotifyApiOptions = SpotifyApiOptionsBuilder ().build()
321
+ clientId: String ,
322
+ clientSecret: String ,
323
+ token: Token ,
324
+ options: SpotifyApiOptions = SpotifyApiOptionsBuilder ().build()
324
325
) : this (
325
326
clientId,
326
327
clientSecret,
@@ -400,20 +401,20 @@ class SpotifyAppApi internal constructor(
400
401
* managed through the scopes exposed in [token]
401
402
*/
402
403
open class SpotifyClientApi internal constructor(
403
- clientId : String? ,
404
- clientSecret : String? ,
405
- var redirectUri : String? ,
406
- token : Token ,
407
- useCache : Boolean ,
408
- cacheLimit : Int? ,
409
- automaticRefresh : Boolean ,
410
- retryWhenRateLimited : Boolean ,
411
- enableLogger : Boolean ,
412
- testTokenValidity : Boolean ,
413
- defaultLimit : Int ,
414
- allowBulkRequests : Boolean ,
415
- requestTimeoutMillis : Long? ,
416
- json : Json
404
+ clientId : String? ,
405
+ clientSecret : String? ,
406
+ var redirectUri : String? ,
407
+ token : Token ,
408
+ useCache : Boolean ,
409
+ cacheLimit : Int? ,
410
+ automaticRefresh : Boolean ,
411
+ retryWhenRateLimited : Boolean ,
412
+ enableLogger : Boolean ,
413
+ testTokenValidity : Boolean ,
414
+ defaultLimit : Int ,
415
+ allowBulkRequests : Boolean ,
416
+ requestTimeoutMillis : Long? ,
417
+ json : Json
417
418
) : SpotifyApi<SpotifyClientApi, SpotifyClientApiBuilder>(
418
419
clientId,
419
420
clientSecret,
@@ -430,11 +431,11 @@ open class SpotifyClientApi internal constructor(
430
431
json
431
432
) {
432
433
constructor (
433
- clientId: String ,
434
- clientSecret: String ,
435
- redirectUri: String ,
436
- token: Token ,
437
- options: SpotifyApiOptions = SpotifyApiOptionsBuilder ().build()
434
+ clientId: String ,
435
+ clientSecret: String ,
436
+ redirectUri: String ,
437
+ token: Token ,
438
+ options: SpotifyApiOptions = SpotifyApiOptionsBuilder ().build()
438
439
) : this (
439
440
clientId,
440
441
clientSecret,
@@ -452,12 +453,13 @@ open class SpotifyClientApi internal constructor(
452
453
options.json
453
454
)
454
455
455
- override val search: SearchApi = SearchApi (this )
456
456
override val albums: AlbumApi = AlbumApi (this )
457
457
override val browse: BrowseApi = BrowseApi (this )
458
458
override val artists: ArtistApi = ArtistApi (this )
459
459
override val tracks: TrackApi = TrackApi (this )
460
460
461
+ override val search: ClientSearchApi = ClientSearchApi (this )
462
+
461
463
/* *
462
464
* Provides access to [endpoints](https://developer.spotify.com/documentation/web-api/reference/episodes/) for retrieving
463
465
* information about one or more episodes from the Spotify catalog.
@@ -467,7 +469,6 @@ open class SpotifyClientApi internal constructor(
467
469
@SpotifyExperimentalHttpApi
468
470
val episodes: EpisodeApi = EpisodeApi (this )
469
471
470
-
471
472
/* *
472
473
* Provides access to [endpoints](https://developer.spotify.com/documentation/web-api/reference/shows/) for retrieving
473
474
* information about one or more shows from the Spotify catalog.
@@ -520,14 +521,19 @@ open class SpotifyClientApi internal constructor(
520
521
*/
521
522
val player: ClientPlayerApi = ClientPlayerApi (this )
522
523
524
+
525
+ private lateinit var userIdBacking: String
526
+
527
+ private fun initiatizeUserIdBacking (): String {
528
+ userIdBacking = users.getClientProfile().complete().id
529
+ return userIdBacking
530
+ }
531
+
523
532
/* *
524
533
* The Spotify user id to which the api instance is connected
525
534
*/
526
- val userId: String
535
+ val userId: String get() = if (::userIdBacking.isInitialized) userIdBacking else initiatizeUserIdBacking()
527
536
528
- init {
529
- userId = users.getClientProfile().complete().id
530
- }
531
537
532
538
/* *
533
539
* Stop all automatic functions like refreshToken or clearCache and shut down the scheduled
@@ -560,8 +566,9 @@ open class SpotifyClientApi internal constructor(
560
566
if (response.responseCode / 200 == 1 ) {
561
567
val tempToken = response.body.toObject(Token .serializer(), this , json)
562
568
this .token = tempToken.copy(
563
- refreshToken = tempToken.refreshToken ? : this .token.refreshToken
564
- ).apply { scopes = tempToken.scopes }
569
+ refreshToken = tempToken.refreshToken ? : this .token.refreshToken,
570
+ scopeString = tempToken.scopeString
571
+ )
565
572
566
573
logger.logInfo(" Successfully refreshed the Spotify token" )
567
574
return currentToken
@@ -636,25 +643,24 @@ open class SpotifyClientApi internal constructor(
636
643
scopes.all { token.scopes?.contains(it) == true }
637
644
}
638
645
639
-
640
646
/* *
641
647
* An API instance created through implicit grant flow, with access to private information
642
648
* managed through the scopes exposed in [token]. [token] is not refreshable and is only accessible for limited time.
643
649
*/
644
650
class SpotifyImplicitGrantApi (
645
- clientId : String? ,
646
- clientSecret : String? ,
647
- redirectUri : String? ,
648
- token : Token ,
649
- useCache : Boolean ,
650
- cacheLimit : Int? ,
651
- retryWhenRateLimited : Boolean ,
652
- enableLogger : Boolean ,
653
- testTokenValidity : Boolean ,
654
- defaultLimit : Int ,
655
- allowBulkRequests : Boolean ,
656
- requestTimeoutMillis : Long? ,
657
- json : Json
651
+ clientId : String? ,
652
+ clientSecret : String? ,
653
+ redirectUri : String? ,
654
+ token : Token ,
655
+ useCache : Boolean ,
656
+ cacheLimit : Int? ,
657
+ retryWhenRateLimited : Boolean ,
658
+ enableLogger : Boolean ,
659
+ testTokenValidity : Boolean ,
660
+ defaultLimit : Int ,
661
+ allowBulkRequests : Boolean ,
662
+ requestTimeoutMillis : Long? ,
663
+ json : Json
658
664
) : SpotifyClientApi(
659
665
clientId,
660
666
clientSecret,
@@ -696,9 +702,9 @@ typealias SpotifyAppAPI = SpotifyAppApi
696
702
suspend fun getCredentialedToken (clientId : String , clientSecret : String , api : SpotifyApi <* , * >? , json : Json ): Token = SpotifyApi .getCredentialedToken(clientId, clientSecret, api, json)
697
703
698
704
internal suspend fun executeTokenRequest (
699
- httpConnection : HttpConnection ,
700
- clientId : String ,
701
- clientSecret : String
705
+ httpConnection : HttpConnection ,
706
+ clientId : String ,
707
+ clientSecret : String
702
708
): HttpResponse {
703
709
return httpConnection.execute(
704
710
listOf (
0 commit comments