Skip to content

Commit 1dabdb8

Browse files
committed
add public constructors for SpotifyAppAPI and SpotifyClientAPI #116
1 parent 8226ae6 commit 1dabdb8

File tree

2 files changed

+152
-99
lines changed

2 files changed

+152
-99
lines changed

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

Lines changed: 50 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ data class SpotifyUserAuthorization(
214214
* @property testTokenValidity After API creation, test whether the token is valid by performing a lightweight request
215215
* @property enableAllUtilities Whether to enable all provided utilities
216216
*/
217-
class SpotifyUtilitiesBuilder(
217+
class SpotifyApiOptionsBuilder(
218218
var useCache: Boolean = true,
219219
var cacheLimit: Int? = 200,
220220
var automaticRefresh: Boolean = true,
@@ -225,7 +225,7 @@ class SpotifyUtilitiesBuilder(
225225
) {
226226
fun build() =
227227
if (enableAllUtilities)
228-
SpotifyUtilities(
228+
SpotifyApiOptions(
229229
true,
230230
200,
231231
automaticRefresh = true,
@@ -234,7 +234,7 @@ class SpotifyUtilitiesBuilder(
234234
testTokenValidity = true
235235
)
236236
else
237-
SpotifyUtilities(
237+
SpotifyApiOptions(
238238
useCache,
239239
cacheLimit,
240240
automaticRefresh,
@@ -244,7 +244,7 @@ class SpotifyUtilitiesBuilder(
244244
)
245245
}
246246

247-
data class SpotifyUtilities(
247+
data class SpotifyApiOptions(
248248
val useCache: Boolean,
249249
val cacheLimit: Int?,
250250
val automaticRefresh: Boolean,
@@ -253,6 +253,9 @@ data class SpotifyUtilities(
253253
val testTokenValidity: Boolean
254254
)
255255

256+
typealias SpotifyUtilities = SpotifyApiOptions
257+
typealias SpotifyUtilitiesBuilder = SpotifyApiOptionsBuilder
258+
256259
/**
257260
* Spotify API mutable parameters
258261
*
@@ -264,7 +267,7 @@ data class SpotifyUtilities(
264267
class SpotifyApiBuilderDsl {
265268
private var credentials: SpotifyCredentials = SpotifyCredentialsBuilder().build()
266269
private var authentication: SpotifyUserAuthorization = SpotifyUserAuthorizationBuilder().build()
267-
private var utilities: SpotifyUtilities = SpotifyUtilitiesBuilder().build()
270+
private var utilities: SpotifyApiOptions = SpotifyApiOptionsBuilder().build()
268271

269272
/**
270273
* A block in which Spotify application credentials (accessible via the Spotify [dashboard](https://developer.spotify.com/dashboard/applications))
@@ -285,22 +288,22 @@ class SpotifyApiBuilderDsl {
285288
/**
286289
* Allows you to override default values for caching, token refresh, and logging
287290
*/
288-
fun utilities(block: SpotifyUtilitiesBuilder.() -> Unit) {
289-
utilities = SpotifyUtilitiesBuilder().apply(block).build()
291+
fun utilities(block: SpotifyApiOptionsBuilder.() -> Unit) {
292+
utilities = SpotifyApiOptionsBuilder().apply(block).build()
290293
}
291294

292295
/**
293296
* Allows you to override default values for caching, token refresh, and logging
294297
*/
295-
fun config(block: SpotifyUtilitiesBuilder.() -> Unit) {
296-
utilities = SpotifyUtilitiesBuilder().apply(block).build()
298+
fun config(block: SpotifyApiOptionsBuilder.() -> Unit) {
299+
utilities = SpotifyApiOptionsBuilder().apply(block).build()
297300
}
298301

299302
/**
300303
* Allows you to override default values for caching, token refresh, and logging
301304
*/
302-
fun options(block: SpotifyUtilitiesBuilder.() -> Unit) {
303-
utilities = SpotifyUtilitiesBuilder().apply(block).build()
305+
fun options(block: SpotifyApiOptionsBuilder.() -> Unit) {
306+
utilities = SpotifyApiOptionsBuilder().apply(block).build()
304307
}
305308

306309
/**
@@ -443,49 +446,49 @@ class SpotifyApiBuilderDsl {
443446
), clientId, clientSecret)
444447

445448
SpotifyClientAPI(
446-
clientId,
447-
clientSecret,
448-
response.body.toObject(null),
449-
utilities.automaticRefresh,
450-
redirectUri,
451-
utilities.useCache,
452-
utilities.cacheLimit,
453-
utilities.retryWhenRateLimited,
454-
utilities.enableLogger,
455-
utilities.testTokenValidity
456-
)
457-
} catch (e: Exception) {
458-
throw SpotifyException("Invalid credentials provided in the login process", e)
459-
}
460-
token != null -> SpotifyClientAPI(
461-
clientId ?: "not-set",
462-
clientSecret ?: "not-set",
463-
token,
464-
utilities.automaticRefresh,
465-
redirectUri ?: "not-set",
449+
clientId,
450+
clientSecret,
451+
redirectUri,
452+
response.body.toObject(null),
466453
utilities.useCache,
467454
utilities.cacheLimit,
455+
utilities.automaticRefresh,
468456
utilities.retryWhenRateLimited,
469457
utilities.enableLogger,
470458
utilities.testTokenValidity
459+
)
460+
} catch (e: Exception) {
461+
throw SpotifyException("Invalid credentials provided in the login process", e)
462+
}
463+
token != null -> SpotifyClientAPI(
464+
clientId ?: "not-set",
465+
clientSecret ?: "not-set",
466+
redirectUri ?: "not-set",
467+
token,
468+
utilities.useCache,
469+
utilities.cacheLimit,
470+
utilities.automaticRefresh,
471+
utilities.retryWhenRateLimited,
472+
utilities.enableLogger,
473+
utilities.testTokenValidity
471474
)
472475
tokenString != null -> SpotifyClientAPI(
473-
clientId ?: "not-set",
474-
clientSecret ?: "not-set",
475-
Token(
476-
tokenString,
477-
"client_credentials",
478-
1000,
479-
null,
480-
null
481-
),
482-
false,
483-
redirectUri ?: "not-set",
484-
utilities.useCache,
485-
utilities.cacheLimit,
486-
utilities.retryWhenRateLimited,
487-
utilities.enableLogger,
488-
utilities.testTokenValidity
476+
clientId ?: "not-set",
477+
clientSecret ?: "not-set",
478+
redirectUri ?: "not-set",
479+
Token(
480+
tokenString,
481+
"client_credentials",
482+
1000,
483+
null,
484+
null
485+
),
486+
utilities.useCache,
487+
utilities.cacheLimit,
488+
false,
489+
utilities.retryWhenRateLimited,
490+
utilities.enableLogger,
491+
utilities.testTokenValidity
489492
)
490493
else -> throw IllegalArgumentException(
491494
"At least one of: authorizationCode, tokenString, or token must be provided " +

0 commit comments

Comments
 (0)