Skip to content

Commit eca5011

Browse files
committed
don't immediate instantiate the user id in SpotifyClientApi
1 parent ec4671d commit eca5011

File tree

1 file changed

+84
-78
lines changed

1 file changed

+84
-78
lines changed

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

Lines changed: 84 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import com.adamratzman.spotify.endpoints.client.ClientPersonalizationApi
99
import com.adamratzman.spotify.endpoints.client.ClientPlayerApi
1010
import com.adamratzman.spotify.endpoints.client.ClientPlaylistApi
1111
import com.adamratzman.spotify.endpoints.client.ClientProfileApi
12+
import com.adamratzman.spotify.endpoints.client.ClientSearchApi
1213
import com.adamratzman.spotify.endpoints.public.AlbumApi
1314
import com.adamratzman.spotify.endpoints.public.ArtistApi
1415
import com.adamratzman.spotify.endpoints.public.BrowseApi
@@ -31,10 +32,10 @@ import com.adamratzman.spotify.models.TokenValidityResponse
3132
import com.adamratzman.spotify.models.serialization.toObject
3233
import com.adamratzman.spotify.utils.asList
3334
import com.adamratzman.spotify.utils.runBlocking
34-
import kotlinx.coroutines.Dispatchers
35-
import kotlinx.serialization.json.Json
3635
import kotlin.coroutines.CoroutineContext
3736
import kotlin.jvm.JvmOverloads
37+
import kotlinx.coroutines.Dispatchers
38+
import kotlinx.serialization.json.Json
3839

3940
/**
4041
* Base url for Spotify web api calls
@@ -66,19 +67,19 @@ internal const val base = "https://api.spotify.com/v1"
6667
*
6768
*/
6869
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
8283
) {
8384
var useCache = useCache
8485
set(value) {
@@ -207,8 +208,8 @@ sealed class SpotifyApi<T : SpotifyApi<T, B>, B : ISpotifyApiBuilder<T, B>>(
207208

208209
@JvmOverloads
209210
suspend fun suspendIsTokenValid(
210-
makeTestRequest: Boolean = true,
211-
context: CoroutineContext = Dispatchers.Default
211+
makeTestRequest: Boolean = true,
212+
context: CoroutineContext = Dispatchers.Default
212213
): TokenValidityResponse {
213214
if (token.shouldRefresh()) return TokenValidityResponse(
214215
false,
@@ -288,19 +289,19 @@ sealed class SpotifyApi<T : SpotifyApi<T, B>, B : ISpotifyApiBuilder<T, B>>(
288289
* client authentication
289290
*/
290291
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
304305
) : SpotifyApi<SpotifyAppApi, SpotifyAppApiBuilder>(
305306
clientId,
306307
clientSecret,
@@ -317,10 +318,10 @@ class SpotifyAppApi internal constructor(
317318
json
318319
) {
319320
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()
324325
) : this(
325326
clientId,
326327
clientSecret,
@@ -400,20 +401,20 @@ class SpotifyAppApi internal constructor(
400401
* managed through the scopes exposed in [token]
401402
*/
402403
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
417418
) : SpotifyApi<SpotifyClientApi, SpotifyClientApiBuilder>(
418419
clientId,
419420
clientSecret,
@@ -430,11 +431,11 @@ open class SpotifyClientApi internal constructor(
430431
json
431432
) {
432433
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()
438439
) : this(
439440
clientId,
440441
clientSecret,
@@ -452,12 +453,13 @@ open class SpotifyClientApi internal constructor(
452453
options.json
453454
)
454455

455-
override val search: SearchApi = SearchApi(this)
456456
override val albums: AlbumApi = AlbumApi(this)
457457
override val browse: BrowseApi = BrowseApi(this)
458458
override val artists: ArtistApi = ArtistApi(this)
459459
override val tracks: TrackApi = TrackApi(this)
460460

461+
override val search: ClientSearchApi = ClientSearchApi(this)
462+
461463
/**
462464
* Provides access to [endpoints](https://developer.spotify.com/documentation/web-api/reference/episodes/) for retrieving
463465
* information about one or more episodes from the Spotify catalog.
@@ -467,7 +469,6 @@ open class SpotifyClientApi internal constructor(
467469
@SpotifyExperimentalHttpApi
468470
val episodes: EpisodeApi = EpisodeApi(this)
469471

470-
471472
/**
472473
* Provides access to [endpoints](https://developer.spotify.com/documentation/web-api/reference/shows/) for retrieving
473474
* information about one or more shows from the Spotify catalog.
@@ -520,14 +521,19 @@ open class SpotifyClientApi internal constructor(
520521
*/
521522
val player: ClientPlayerApi = ClientPlayerApi(this)
522523

524+
525+
private lateinit var userIdBacking: String
526+
527+
private fun initiatizeUserIdBacking(): String {
528+
userIdBacking = users.getClientProfile().complete().id
529+
return userIdBacking
530+
}
531+
523532
/**
524533
* The Spotify user id to which the api instance is connected
525534
*/
526-
val userId: String
535+
val userId: String get() = if (::userIdBacking.isInitialized) userIdBacking else initiatizeUserIdBacking()
527536

528-
init {
529-
userId = users.getClientProfile().complete().id
530-
}
531537

532538
/**
533539
* Stop all automatic functions like refreshToken or clearCache and shut down the scheduled
@@ -560,8 +566,9 @@ open class SpotifyClientApi internal constructor(
560566
if (response.responseCode / 200 == 1) {
561567
val tempToken = response.body.toObject(Token.serializer(), this, json)
562568
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+
)
565572

566573
logger.logInfo("Successfully refreshed the Spotify token")
567574
return currentToken
@@ -636,25 +643,24 @@ open class SpotifyClientApi internal constructor(
636643
scopes.all { token.scopes?.contains(it) == true }
637644
}
638645

639-
640646
/**
641647
* An API instance created through implicit grant flow, with access to private information
642648
* managed through the scopes exposed in [token]. [token] is not refreshable and is only accessible for limited time.
643649
*/
644650
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
658664
) : SpotifyClientApi(
659665
clientId,
660666
clientSecret,
@@ -696,9 +702,9 @@ typealias SpotifyAppAPI = SpotifyAppApi
696702
suspend fun getCredentialedToken(clientId: String, clientSecret: String, api: SpotifyApi<*, *>?, json: Json): Token = SpotifyApi.getCredentialedToken(clientId, clientSecret, api, json)
697703

698704
internal suspend fun executeTokenRequest(
699-
httpConnection: HttpConnection,
700-
clientId: String,
701-
clientSecret: String
705+
httpConnection: HttpConnection,
706+
clientId: String,
707+
clientSecret: String
702708
): HttpResponse {
703709
return httpConnection.execute(
704710
listOf(

0 commit comments

Comments
 (0)