Skip to content

Commit 1b57e14

Browse files
committed
fix requires
1 parent 0d33ec7 commit 1b57e14

File tree

2 files changed

+32
-28
lines changed

2 files changed

+32
-28
lines changed

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

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ fun spotifyClientApi(block: SpotifyClientApiBuilder.() -> Unit) = SpotifyClientA
2121
* Spotify API builder
2222
*/
2323
class SpotifyApiBuilder(
24-
private var clientId: String,
25-
private var clientSecret: String,
24+
private var clientId: String?,
25+
private var clientSecret: String?,
2626
private var redirectUri: String?
2727
) {
2828
var authorization: SpotifyUserAuthorization = SpotifyUserAuthorizationBuilder().build()
@@ -239,9 +239,11 @@ class SpotifyClientApiBuilder(
239239
val clientSecret = credentials.clientSecret
240240
val redirectUri = credentials.redirectUri
241241

242-
require(clientId != null && clientSecret != null && redirectUri != null) { "You need to specify a valid clientId, clientSecret, and redirectUri in the credentials block!" }
242+
require((clientId != null && clientSecret != null && redirectUri != null) || authorization.token != null || authorization.tokenString != null) { "You need to specify a valid clientId, clientSecret, and redirectUri in the credentials block!" }
243243
return when {
244244
authorization.authorizationCode != null -> try {
245+
require(clientId != null && clientSecret != null && redirectUri != null) { "You need to specify a valid clientId, clientSecret, and redirectUri in the credentials block!" }
246+
245247
val response = executeTokenRequest(
246248
HttpConnection(
247249
"https://accounts.spotify.com/api/token",
@@ -274,9 +276,9 @@ class SpotifyClientApiBuilder(
274276
throw SpotifyException("Invalid credentials provided in the login process", e)
275277
}
276278
authorization.token != null -> SpotifyClientApi(
277-
clientId ?: "",
278-
clientSecret ?: "",
279-
redirectUri ?: "",
279+
clientId,
280+
clientSecret,
281+
redirectUri,
280282
authorization.token!!,
281283
options.useCache,
282284
options.cacheLimit,
@@ -286,9 +288,9 @@ class SpotifyClientApiBuilder(
286288
options.testTokenValidity
287289
)
288290
authorization.tokenString != null -> SpotifyClientApi(
289-
clientId ?: "",
290-
clientSecret ?: "",
291-
redirectUri ?: "",
291+
clientId,
292+
clientSecret,
293+
redirectUri,
292294
Token(
293295
authorization.tokenString!!,
294296
"client_credentials",
@@ -358,8 +360,8 @@ class SpotifyAppApiBuilder(
358360
return when {
359361
authorization.token != null -> {
360362
SpotifyAppApi(
361-
clientId ?: "not-set",
362-
clientSecret ?: "not-set",
363+
clientId,
364+
clientSecret,
363365
authorization.token!!,
364366
options.useCache,
365367
options.cacheLimit,
@@ -371,8 +373,8 @@ class SpotifyAppApiBuilder(
371373
}
372374
authorization.tokenString != null -> {
373375
SpotifyAppApi(
374-
clientId ?: "not-set",
375-
clientSecret ?: "not-set",
376+
clientId,
377+
clientSecret,
376378
Token(
377379
authorization.tokenString!!, "client_credentials",
378380
60000, null, null

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

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ internal const val base = "https://api.spotify.com/v1"
5353
*
5454
*/
5555
abstract class SpotifyApi internal constructor(
56-
val clientId: String,
57-
val clientSecret: String,
56+
val clientId: String?,
57+
val clientSecret: String?,
5858
var token: Token,
5959
useCache: Boolean,
6060
var cacheLimit: Int?,
@@ -149,6 +149,7 @@ abstract class SpotifyApi internal constructor(
149149
* @return Authorization URL that can be used in a browser
150150
*/
151151
fun getAuthorizationUrl(vararg scopes: SpotifyScope, redirectUri: String): String {
152+
require(clientId != null)
152153
return getAuthUrlFull(*scopes, clientId = clientId, redirectUri = redirectUri)
153154
}
154155

@@ -181,8 +182,8 @@ abstract class SpotifyApi internal constructor(
181182
* client authentication
182183
*/
183184
class SpotifyAppApi internal constructor(
184-
clientId: String,
185-
clientSecret: String,
185+
clientId: String?,
186+
clientSecret: String?,
186187
token: Token,
187188
useCache: Boolean,
188189
cacheLimit: Int?,
@@ -240,14 +241,12 @@ class SpotifyAppApi internal constructor(
240241
override val following: FollowingApi = FollowingApi(this)
241242

242243
override fun refreshToken(): Token {
243-
if (clientId != "not-set" && clientSecret != "not-set") {
244-
val currentToken = this.token
244+
require(clientId != null && clientSecret != null) { "Either the client id or the client secret is not set" }
245+
val currentToken = this.token
245246

246-
token = getCredentialedToken(clientId, clientSecret, this)
247+
token = getCredentialedToken(clientId, clientSecret, this)
247248

248-
return currentToken
249-
}
250-
throw BadRequestException("Either the client id or the client secret is not set")
249+
return currentToken
251250
}
252251

253252
override val endpoints: List<SpotifyEndpoint>
@@ -283,9 +282,9 @@ class SpotifyAppApi internal constructor(
283282
* managed through the scopes exposed in [token]
284283
*/
285284
class SpotifyClientApi internal constructor(
286-
clientId: String,
287-
clientSecret: String,
288-
var redirectUri: String,
285+
clientId: String?,
286+
clientSecret: String?,
287+
var redirectUri: String?,
289288
token: Token,
290289
useCache: Boolean,
291290
cacheLimit: Int?,
@@ -392,6 +391,8 @@ class SpotifyClientApi internal constructor(
392391
}
393392

394393
override fun refreshToken(): Token {
394+
require(clientId != null && clientSecret != null) { "Either the client id or the client secret is not set" }
395+
395396
val currentToken = this.token
396397

397398
val response = executeTokenRequest(
@@ -413,7 +414,7 @@ class SpotifyClientApi internal constructor(
413414
val tempToken = response.body.toObject(Token.serializer(), this)
414415
this.token = tempToken.copy(
415416
refreshToken = tempToken.refreshToken ?: this.token.refreshToken
416-
).apply { scopes = tempToken.scopes }
417+
).apply { scopes = tempToken.scopes }
417418

418419
logger.logInfo("Successfully refreshed the Spotify token")
419420
return currentToken
@@ -464,7 +465,8 @@ class SpotifyClientApi internal constructor(
464465
* @return Authorization URL that can be used in a browser
465466
*/
466467
fun getAuthorizationUrl(vararg scopes: SpotifyScope): String {
467-
return getAuthUrlFull(*scopes, clientId = clientId, redirectUri = redirectUri)
468+
require(clientId != null && clientSecret != null) { "Either the client id or the client secret is not set" }
469+
return redirectUri?.let { getAuthUrlFull(*scopes, clientId = clientId, redirectUri = it) } ?: throw IllegalArgumentException("The redirect uri must be set")
468470
}
469471

470472
/**

0 commit comments

Comments
 (0)