Skip to content

Commit a25c9c9

Browse files
committed
set defaultLimit
1 parent 3e49d5e commit a25c9c9

File tree

15 files changed

+206
-167
lines changed

15 files changed

+206
-167
lines changed

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

Lines changed: 132 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ class SpotifyApiBuilder(
6363
*/
6464
fun testTokenValidity(testTokenValidity: Boolean) = apply { this.options.testTokenValidity = testTokenValidity }
6565

66+
/**
67+
* Allows you to set the default amount of objects to retrieve in one request
68+
*/
69+
fun defaultLimit(defaultLimit: Int) = apply { this.options.defaultLimit = defaultLimit }
70+
6671
/**
6772
* Set the application client id
6873
*/
@@ -92,7 +97,7 @@ class SpotifyApiBuilder(
9297
* Set a returned [authorization code](https://developer.spotify.com/documentation/general/guides/authorization-guide/)
9398
*/
9499
fun authorizationCode(authorizationCode: String?) =
95-
apply { this.authorization.authorizationCode = authorizationCode }
100+
apply { this.authorization.authorizationCode = authorizationCode }
96101

97102
/**
98103
* If you only have an access token, the api can be instantiated with it
@@ -113,7 +118,7 @@ class SpotifyApiBuilder(
113118
* Set whether to block the current thread and wait until the API can retry the request
114119
*/
115120
fun retryWhenRateLimited(retryWhenRateLimited: Boolean) =
116-
apply { this.options.retryWhenRateLimited = retryWhenRateLimited }
121+
apply { this.options.retryWhenRateLimited = retryWhenRateLimited }
117122

118123
/**
119124
* Set whether to enable to the exception logger
@@ -323,74 +328,77 @@ class SpotifyClientApiBuilder(
323328
require(clientId != null && clientSecret != null && redirectUri != null) { "You need to specify a valid clientId, clientSecret, and redirectUri in the credentials block!" }
324329

325330
val response = executeTokenRequest(
326-
HttpConnection(
327-
"https://accounts.spotify.com/api/token",
328-
HttpRequestMethod.POST,
329-
mapOf(
330-
"grant_type" to "authorization_code",
331-
"code" to authorization.authorizationCode,
332-
"redirect_uri" to redirectUri
333-
),
334-
null,
335-
"application/x-www-form-urlencoded",
336-
listOf(),
337-
null
338-
), clientId, clientSecret
331+
HttpConnection(
332+
"https://accounts.spotify.com/api/token",
333+
HttpRequestMethod.POST,
334+
mapOf(
335+
"grant_type" to "authorization_code",
336+
"code" to authorization.authorizationCode,
337+
"redirect_uri" to redirectUri
338+
),
339+
null,
340+
"application/x-www-form-urlencoded",
341+
listOf(),
342+
null
343+
), clientId, clientSecret
339344
)
340345

341346
SpotifyClientApi(
347+
clientId,
348+
clientSecret,
349+
redirectUri,
350+
response.body.toObject(Token.serializer(), null, options.json),
351+
options.useCache,
352+
options.cacheLimit,
353+
options.automaticRefresh,
354+
options.retryWhenRateLimited,
355+
options.enableLogger,
356+
options.testTokenValidity,
357+
options.defaultLimit,
358+
options.json
359+
)
360+
} catch (e: CancellationException) {
361+
throw e
362+
} catch (e: Exception) {
363+
throw SpotifyException.AuthenticationException("Invalid credentials provided in the login process (clientId=$clientId, clientSecret=$clientSecret, authCode=${authorization.authorizationCode})", e)
364+
}
365+
authorization.token != null -> SpotifyClientApi(
342366
clientId,
343367
clientSecret,
344368
redirectUri,
345-
response.body.toObject(Token.serializer(), null, options.json),
369+
authorization.token!!,
346370
options.useCache,
347371
options.cacheLimit,
348372
options.automaticRefresh,
349373
options.retryWhenRateLimited,
350374
options.enableLogger,
351375
options.testTokenValidity,
376+
options.defaultLimit,
352377
options.json
353-
)
354-
} catch (e: CancellationException) {
355-
throw e
356-
} catch (e: Exception) {
357-
throw SpotifyException.AuthenticationException("Invalid credentials provided in the login process (clientId=$clientId, clientSecret=$clientSecret, authCode=${authorization.authorizationCode})", e)
358-
}
359-
authorization.token != null -> SpotifyClientApi(
360-
clientId,
361-
clientSecret,
362-
redirectUri,
363-
authorization.token!!,
364-
options.useCache,
365-
options.cacheLimit,
366-
options.automaticRefresh,
367-
options.retryWhenRateLimited,
368-
options.enableLogger,
369-
options.testTokenValidity,
370-
options.json
371378
)
372379
authorization.tokenString != null -> SpotifyClientApi(
373-
clientId,
374-
clientSecret,
375-
redirectUri,
376-
Token(
377-
authorization.tokenString!!,
378-
"client_credentials",
379-
1000,
380-
null,
381-
null
382-
),
383-
options.useCache,
384-
options.cacheLimit,
385-
false,
386-
options.retryWhenRateLimited,
387-
options.enableLogger,
388-
options.testTokenValidity,
389-
options.json
380+
clientId,
381+
clientSecret,
382+
redirectUri,
383+
Token(
384+
authorization.tokenString!!,
385+
"client_credentials",
386+
1000,
387+
null,
388+
null
389+
),
390+
options.useCache,
391+
options.cacheLimit,
392+
false,
393+
options.retryWhenRateLimited,
394+
options.enableLogger,
395+
options.testTokenValidity,
396+
options.defaultLimit,
397+
options.json
390398
)
391399
else -> throw IllegalArgumentException(
392-
"At least one of: authorizationCode, tokenString, or token must be provided " +
393-
"to build a SpotifyClientApi object"
400+
"At least one of: authorizationCode, tokenString, or token must be provided " +
401+
"to build a SpotifyClientApi object"
394402
)
395403
}
396404
}
@@ -418,62 +426,66 @@ class SpotifyAppApiBuilder(
418426
require((clientId != null && clientSecret != null) || authorization.token != null || authorization.tokenString != null) { "You didn't specify a client id or client secret in the credentials block!" }
419427
return when {
420428
authorization.token != null -> SpotifyAppApi(
421-
clientId,
422-
clientSecret,
423-
authorization.token!!,
424-
options.useCache,
425-
options.cacheLimit,
426-
false,
427-
options.retryWhenRateLimited,
428-
options.enableLogger,
429-
options.testTokenValidity,
430-
options.json
431-
)
432-
authorization.tokenString != null -> {
433-
SpotifyAppApi(
434429
clientId,
435430
clientSecret,
436-
Token(
437-
authorization.tokenString!!, "client_credentials",
438-
60000, null, null
439-
),
431+
authorization.token!!,
440432
options.useCache,
441433
options.cacheLimit,
442434
false,
443435
options.retryWhenRateLimited,
444436
options.enableLogger,
445437
options.testTokenValidity,
438+
options.defaultLimit,
446439
options.json
440+
)
441+
authorization.tokenString != null -> {
442+
SpotifyAppApi(
443+
clientId,
444+
clientSecret,
445+
Token(
446+
authorization.tokenString!!, "client_credentials",
447+
60000, null, null
448+
),
449+
options.useCache,
450+
options.cacheLimit,
451+
false,
452+
options.retryWhenRateLimited,
453+
options.enableLogger,
454+
options.testTokenValidity,
455+
options.defaultLimit,
456+
options.json
447457
)
448458
}
449459
authorization.refreshTokenString != null -> {
450460
SpotifyAppApi(
451-
clientId,
452-
clientSecret,
453-
Token("", "", 0, authorization.refreshTokenString!!),
454-
options.useCache,
455-
options.cacheLimit,
456-
false,
457-
options.retryWhenRateLimited,
458-
options.enableLogger,
459-
options.testTokenValidity,
460-
options.json
461+
clientId,
462+
clientSecret,
463+
Token("", "", 0, authorization.refreshTokenString!!),
464+
options.useCache,
465+
options.cacheLimit,
466+
false,
467+
options.retryWhenRateLimited,
468+
options.enableLogger,
469+
options.testTokenValidity,
470+
options.defaultLimit,
471+
options.json
461472
)
462473
}
463474
else -> try {
464475
require(clientId != null && clientSecret != null) { "Illegal credentials provided" }
465476
val token = getCredentialedToken(clientId, clientSecret, null, options.json)
466477
SpotifyAppApi(
467-
clientId,
468-
clientSecret,
469-
token,
470-
options.useCache,
471-
options.cacheLimit,
472-
false,
473-
options.retryWhenRateLimited,
474-
options.enableLogger,
475-
options.testTokenValidity,
476-
options.json
478+
clientId,
479+
clientSecret,
480+
token,
481+
options.useCache,
482+
options.cacheLimit,
483+
false,
484+
options.retryWhenRateLimited,
485+
options.enableLogger,
486+
options.testTokenValidity,
487+
options.defaultLimit,
488+
options.json
477489
)
478490
} catch (e: CancellationException) {
479491
throw e
@@ -497,8 +509,8 @@ class SpotifyCredentialsBuilder {
497509
var redirectUri: String? = null
498510

499511
fun build() =
500-
if (clientId?.isNotEmpty() == false || clientSecret?.isNotEmpty() == false) throw IllegalArgumentException("clientId or clientSecret is empty")
501-
else SpotifyCredentials(clientId, clientSecret, redirectUri)
512+
if (clientId?.isNotEmpty() == false || clientSecret?.isNotEmpty() == false) throw IllegalArgumentException("clientId or clientSecret is empty")
513+
else SpotifyCredentials(clientId, clientSecret, redirectUri)
502514
}
503515

504516
data class SpotifyCredentials(val clientId: String?, val clientSecret: String?, val redirectUri: String?)
@@ -549,6 +561,8 @@ data class SpotifyUserAuthorization(
549561
* @property retryWhenRateLimited Set whether to block the current thread and wait until the API can retry the request
550562
* @property enableLogger Set whether to enable to the exception logger
551563
* @property testTokenValidity After API creation, test whether the token is valid by performing a lightweight request
564+
* @property defaultLimit The default amount of objects to retrieve in one request
565+
* @property json The Json serializer/deserializer instance
552566
* @property enableAllOptions Whether to enable all provided utilities
553567
*/
554568
class SpotifyApiOptionsBuilder(
@@ -559,29 +573,32 @@ class SpotifyApiOptionsBuilder(
559573
var enableLogger: Boolean = true,
560574
var testTokenValidity: Boolean = false,
561575
var enableAllOptions: Boolean = false,
576+
val defaultLimit: Int = 50,
562577
var json: Json = stableJson
563578
) {
564579
fun build() =
565-
if (enableAllOptions)
566-
SpotifyApiOptions(
567-
true,
568-
200,
569-
automaticRefresh = false,
570-
retryWhenRateLimited = true,
571-
enableLogger = true,
572-
testTokenValidity = true,
573-
json = json
574-
)
575-
else
576-
SpotifyApiOptions(
577-
useCache,
578-
cacheLimit,
579-
automaticRefresh,
580-
retryWhenRateLimited,
581-
enableLogger,
582-
testTokenValidity,
583-
json
584-
)
580+
if (enableAllOptions)
581+
SpotifyApiOptions(
582+
true,
583+
200,
584+
automaticRefresh = false,
585+
retryWhenRateLimited = true,
586+
enableLogger = true,
587+
testTokenValidity = true,
588+
defaultLimit = 50,
589+
json = json
590+
)
591+
else
592+
SpotifyApiOptions(
593+
useCache,
594+
cacheLimit,
595+
automaticRefresh,
596+
retryWhenRateLimited,
597+
enableLogger,
598+
testTokenValidity,
599+
defaultLimit,
600+
json
601+
)
585602
}
586603

587604
/**
@@ -593,7 +610,8 @@ class SpotifyApiOptionsBuilder(
593610
* @property retryWhenRateLimited Set whether to block the current thread and wait until the API can retry the request
594611
* @property enableLogger Set whether to enable to the exception logger
595612
* @property testTokenValidity After API creation, test whether the token is valid by performing a lightweight request
596-
* @property enableAllOptions Whether to enable all provided utilities
613+
* @property defaultLimit The default amount of objects to retrieve in one request
614+
* @property json The Json serializer/deserializer instance
597615
*/
598616

599617
data class SpotifyApiOptions(
@@ -603,6 +621,7 @@ data class SpotifyApiOptions(
603621
var retryWhenRateLimited: Boolean,
604622
var enableLogger: Boolean,
605623
var testTokenValidity: Boolean,
624+
var defaultLimit: Int,
606625
var json: Json
607626
)
608627

0 commit comments

Comments
 (0)