@@ -37,8 +37,8 @@ import com.adamratzman.spotify.models.serialization.nonstrictJson
3737import com.adamratzman.spotify.models.serialization.toObject
3838import com.adamratzman.spotify.utils.asList
3939import com.adamratzman.spotify.utils.base64ByteEncode
40- import kotlin.jvm.JvmOverloads
4140import kotlinx.serialization.json.Json
41+ import kotlin.jvm.JvmOverloads
4242
4343/* *
4444 * Represents an instance of the Spotify API client, with common
@@ -212,6 +212,20 @@ public sealed class SpotifyApi<T : SpotifyApi<T, B>, B : ISpotifyApiBuilder<T, B
212212 }
213213 }
214214
215+ /* *
216+ * Tests whether the current [token] is actually valid. By default, an endpoint is called *once* to verify
217+ * validity.
218+ *
219+ * @param makeTestRequest Whether to also make an endpoint request to verify authentication.
220+ *
221+ * @return [TokenValidityResponse] containing whether this token is valid, and if not, an Exception explaining why
222+ */
223+ @JvmOverloads
224+ public fun isTokenValidRestAction (makeTestRequest : Boolean = true): SpotifyRestAction <TokenValidityResponse > =
225+ SpotifyRestAction {
226+ isTokenValid(makeTestRequest)
227+ }
228+
215229 /* *
216230 * If the method used to create the [token] supports token refresh and
217231 * the information in [token] is accurate, attempt to refresh the token
@@ -224,7 +238,18 @@ public sealed class SpotifyApi<T : SpotifyApi<T, B>, B : ISpotifyApiBuilder<T, B
224238 this @SpotifyApi.token = this
225239 spotifyApiOptions.onTokenRefresh?.invoke(this @SpotifyApi)
226240 spotifyApiOptions.afterTokenRefresh?.invoke(this @SpotifyApi)
227- } ? : throw SpotifyException .ReAuthenticationNeededException (IllegalStateException (" The refreshTokenProducer is null." ))
241+ }
242+ ? : throw SpotifyException .ReAuthenticationNeededException (IllegalStateException (" The refreshTokenProducer is null." ))
243+
244+ /* *
245+ * If the method used to create the [token] supports token refresh and
246+ * the information in [token] is accurate, attempt to refresh the token
247+ *
248+ * @return The old access token if refresh was successful
249+ * @throws BadRequestException if refresh fails
250+ * @throws IllegalStateException if [SpotifyApiOptions.refreshTokenProducer] is null
251+ */
252+ public fun refreshTokenRestAction (): SpotifyRestAction <Token > = SpotifyRestAction { refreshToken() }
228253
229254 public companion object {
230255 internal suspend fun testTokenValidity (api : GenericSpotifyApi ) {
@@ -327,6 +352,22 @@ public sealed class SpotifyApi<T : SpotifyApi<T, B>, B : ISpotifyApiBuilder<T, B
327352
328353 throw BadRequestException (response.body.toObject(AuthenticationError .serializer(), null , json))
329354 }
355+
356+ /* *
357+ *
358+ * Get an application token (can only access public methods) that can be used to instantiate a new [SpotifyAppApi]
359+ *
360+ * @param clientId Spotify [client id](https://developer.spotify.com/documentation/general/guides/app-settings/)
361+ * @param clientSecret Spotify [client secret](https://developer.spotify.com/documentation/general/guides/app-settings/)
362+ * @param api The Spotify Api instance, or null if one doesn't exist yet
363+ * @param json The json instance that will deserialize the response.
364+ */
365+ public fun getCredentialedTokenRestAction (
366+ clientId : String ,
367+ clientSecret : String ,
368+ api : GenericSpotifyApi ? ,
369+ json : Json = api?.spotifyApiOptions?.json ? : Json .Default
370+ ): SpotifyRestAction <Token > = SpotifyRestAction { getCredentialedToken(clientId, clientSecret, api, json) }
330371 }
331372}
332373
@@ -514,6 +555,11 @@ public open class SpotifyClientApi(
514555 public suspend fun getUserId (): String =
515556 if (::userIdBacking.isInitialized) userIdBacking else initiatizeUserIdBacking()
516557
558+ /* *
559+ * The Spotify user id to which the api instance is connected
560+ */
561+ public fun getUserIdRestAction (): SpotifyRestAction <String > = SpotifyRestAction { getUserId() }
562+
517563 /* *
518564 * Stop all automatic functions like refreshToken or clearCache and shut down the scheduled
519565 * executor
@@ -574,6 +620,12 @@ public open class SpotifyClientApi(
574620 */
575621 public suspend fun hasScope (scope : SpotifyScope ): Boolean? = hasScopes(scope)
576622
623+ /* *
624+ * Whether the current access token allows access to scope [scope]
625+ */
626+ public fun hasScopeRestAction (scope : SpotifyScope ): SpotifyRestAction <Boolean ?> =
627+ SpotifyRestAction { hasScope(scope) }
628+
577629 /* *
578630 * Whether the current access token allows access to all of the provided scopes
579631 */
@@ -583,13 +635,21 @@ public open class SpotifyClientApi(
583635 token.scopes?.contains(scope) == true &&
584636 scopes.all { token.scopes?.contains(it) == true }
585637
638+ /* *
639+ * Whether the current access token allows access to all of the provided scopes
640+ */
641+ public fun hasScopesRestAction (scope : SpotifyScope , vararg scopes : SpotifyScope ): SpotifyRestAction <Boolean ?> =
642+ SpotifyRestAction {
643+ hasScopes(scope, * scopes)
644+ }
645+
586646 public companion object {
587647 private val defaultClientApiTokenRefreshProducer: suspend (GenericSpotifyApi ) -> Token = { api ->
588648 api as SpotifyClientApi
589649
590650 require(api.clientId != null ) { " The client id is not set" }
591651
592- refreshSpotifyClientToken(api.clientId, api.clientSecret, api.token.refreshToken, api.usesPkceAuth)
652+ refreshSpotifyClientToken(api.clientId, api.clientSecret, api.token.refreshToken, api.usesPkceAuth)
593653 }
594654 }
595655}
@@ -707,3 +767,21 @@ public suspend fun refreshSpotifyClientToken(
707767 )
708768 )
709769}
770+
771+ /* *
772+ * Refresh a Spotify client token
773+ *
774+ * @param clientId The Spotify application client id.
775+ * @param clientSecret The Spotify application client secret (not needed for PKCE).
776+ * @param refreshToken The refresh token.
777+ * @param usesPkceAuth Whether this token was created using PKCE auth or not.
778+ */
779+ public fun refreshSpotifyClientTokenRestAction (
780+ clientId : String ,
781+ clientSecret : String? ,
782+ refreshToken : String? ,
783+ usesPkceAuth : Boolean
784+ ): SpotifyRestAction <Token > =
785+ SpotifyRestAction { refreshSpotifyClientToken(clientId, clientSecret, refreshToken, usesPkceAuth) }
786+
787+
0 commit comments