@@ -58,6 +58,7 @@ fun getSpotifyAuthorizationUrl(
58
58
* @param state This provides protection against attacks such as cross-site request forgery.
59
59
* @param codeChallenge In order to generate the code challenge, your app should hash the code verifier using the SHA256 algorithm.
60
60
* Then, base64url encode the hash that you generated.
61
+ *
61
62
*/
62
63
fun getPkceAuthorizationUrl (
63
64
vararg scopes : SpotifyScope ,
@@ -106,8 +107,12 @@ expect fun getSpotifyPkceCodeChallenge(codeVerifier: String): String
106
107
*
107
108
* @return [SpotifyImplicitGrantApi] that can immediately begin making calls
108
109
*/
109
- fun spotifyImplicitGrantApi (clientId : String? , redirectUri : String? , token : Token , options : SpotifyApiOptions = SpotifyApiOptionsBuilder ().build()) =
110
- SpotifyImplicitGrantApi (
110
+ fun spotifyImplicitGrantApi (
111
+ clientId : String? ,
112
+ redirectUri : String? ,
113
+ token : Token ,
114
+ options : SpotifyApiOptionsBuilder = SpotifyApiOptionsBuilder ()
115
+ ) = SpotifyImplicitGrantApi (
111
116
clientId,
112
117
null ,
113
118
redirectUri,
@@ -142,10 +147,17 @@ fun spotifyImplicitGrantApi(clientId: String?, redirectUri: String?, token: Toke
142
147
*
143
148
* @param clientId Spotify [client id](https://developer.spotify.com/documentation/general/guides/app-settings/)
144
149
* @param clientSecret Spotify [client secret](https://developer.spotify.com/documentation/general/guides/app-settings/)
150
+ * @param options Override default API options such as the cache limit
145
151
*
146
152
* @return Configurable [SpotifyAppApiBuilder] that, when built, creates a new [SpotifyAppApi]
147
153
*/
148
- fun spotifyAppApi (clientId : String , clientSecret : String ) = spotifyAppApi(clientId, clientSecret) {}
154
+ fun spotifyAppApi (
155
+ clientId : String ,
156
+ clientSecret : String ,
157
+ options : SpotifyApiOptionsBuilder ? = null
158
+ ) = spotifyAppApi(clientId, clientSecret) {
159
+ options?.let { this .options = it.build() }
160
+ }
149
161
150
162
/* *
151
163
* Instantiate a new [SpotifyAppApiBuilder] using a Spotify [clientId] and [clientSecret], with the ability to configure
@@ -157,8 +169,11 @@ fun spotifyAppApi(clientId: String, clientSecret: String) = spotifyAppApi(client
157
169
*
158
170
* @return Configurable [SpotifyAppApiBuilder] that, when built, creates a new [SpotifyAppApi]
159
171
*/
160
- fun spotifyAppApi (clientId : String , clientSecret : String , block : SpotifyAppApiBuilder .() -> Unit = {}) =
161
- SpotifyAppApiBuilder ().apply (block).apply {
172
+ fun spotifyAppApi (
173
+ clientId : String ,
174
+ clientSecret : String ,
175
+ block : SpotifyAppApiBuilder .() -> Unit
176
+ ) = SpotifyAppApiBuilder ().apply (block).apply {
162
177
credentials {
163
178
this .clientId = clientId
164
179
this .clientSecret = clientSecret
@@ -171,19 +186,26 @@ fun spotifyAppApi(clientId: String, clientSecret: String, block: SpotifyAppApiBu
171
186
* @param clientId Spotify [client id](https://developer.spotify.com/documentation/general/guides/app-settings/)
172
187
* @param clientSecret Spotify [client secret](https://developer.spotify.com/documentation/general/guides/app-settings/)
173
188
* @param apiToken The access token that will be associated with the built API instance
189
+ * @param options Override default API options such as the cache limit
174
190
*
175
191
* @return Configurable [SpotifyAppApiBuilder] that, when built, creates a new [SpotifyAppApi]
176
192
*/
177
- fun spotifyAppApi (clientId : String? , clientSecret : String? , apiToken : Token ) =
178
- SpotifyAppApiBuilder ().apply {
179
- credentials {
180
- this .clientId = clientId
181
- this .clientSecret = clientSecret
182
- }
183
- authentication {
184
- token = apiToken
185
- }
186
- }
193
+ fun spotifyAppApi (
194
+ clientId : String? ,
195
+ clientSecret : String? ,
196
+ apiToken : Token ,
197
+ options : SpotifyApiOptionsBuilder ? = null
198
+ ) = SpotifyAppApiBuilder ().apply {
199
+ credentials {
200
+ this .clientId = clientId
201
+ this .clientSecret = clientSecret
202
+ }
203
+ authentication {
204
+ token = apiToken
205
+ }
206
+
207
+ options?.let { this .options = it.build() }
208
+ }
187
209
188
210
/* *
189
211
* Instantiate a new [SpotifyAppApiBuilder] by providing a builder initialization [block].
@@ -244,13 +266,15 @@ fun spotifyClientApi(
244
266
* @param clientSecret Spotify [client secret](https://developer.spotify.com/documentation/general/guides/app-settings/)
245
267
* @param redirectUri Spotify [redirect uri](https://developer.spotify.com/documentation/general/guides/app-settings/)
246
268
* @param apiToken The access token that will be associated with the built API instance
269
+ * @param options Override default API options such as the cache limit
247
270
*
248
271
* @return Configurable [SpotifyClientApiBuilder] that, when built, creates a new [SpotifyClientApi]
249
272
*/
250
273
fun spotifyClientApi (
251
274
clientId : String? ,
252
275
clientSecret : String? ,
253
276
redirectUri : String? ,
277
+ options : SpotifyApiOptionsBuilder ? = null,
254
278
apiToken : Token
255
279
) = SpotifyClientApiBuilder ().apply {
256
280
credentials {
@@ -262,6 +286,8 @@ fun spotifyClientApi(
262
286
authentication {
263
287
token = apiToken
264
288
}
289
+
290
+ options?.let { this .options = it.build() }
265
291
}
266
292
267
293
/* *
@@ -272,14 +298,16 @@ fun spotifyClientApi(
272
298
* @param clientSecret Spotify [client secret](https://developer.spotify.com/documentation/general/guides/app-settings/)
273
299
* @param redirectUri Spotify [redirect uri](https://developer.spotify.com/documentation/general/guides/app-settings/)
274
300
* @param authCode The authorization code retrieved after OAuth authorization
301
+ * @param options Override default API options such as the cache limit
275
302
*
276
303
* @return Configurable [SpotifyClientApiBuilder] that, when built, creates a new [SpotifyClientApi]
277
304
*/
278
305
fun spotifyClientApi (
279
306
clientId : String? ,
280
307
clientSecret : String? ,
281
308
redirectUri : String? ,
282
- authCode : String
309
+ authCode : String ,
310
+ options : SpotifyApiOptionsBuilder ? = null
283
311
) = SpotifyClientApiBuilder ().apply {
284
312
credentials {
285
313
this .clientId = clientId
@@ -290,6 +318,8 @@ fun spotifyClientApi(
290
318
authentication {
291
319
authorizationCode = authCode
292
320
}
321
+
322
+ options?.let { this .options = it.build() }
293
323
}
294
324
295
325
/* *
@@ -313,15 +343,15 @@ fun spotifyClientApi(
313
343
clientSecret : String ,
314
344
redirectUri : String ,
315
345
authorization : SpotifyUserAuthorization ,
316
- options : SpotifyApiOptions ? = null,
346
+ options : SpotifyApiOptionsBuilder ? = null,
317
347
block : SpotifyClientApiBuilder .() -> Unit = {}
318
348
) = SpotifyClientApiBuilder ().apply (block).apply {
319
349
credentials {
320
350
this .clientId = clientId
321
351
this .clientSecret = clientSecret
322
352
this .redirectUri = redirectUri
323
353
}
324
- options?.let { this .options = options }
354
+ options?.let { this .options = options.build() }
325
355
this .authorization = authorization
326
356
}
327
357
@@ -359,36 +389,7 @@ fun spotifyClientApi(block: SpotifyClientApiBuilder.() -> Unit) = SpotifyClientA
359
389
* @param redirectUri Spotify [redirect uri](https://developer.spotify.com/documentation/general/guides/app-settings/)
360
390
* @param authCode The authorization code retrieved after OAuth authorization
361
391
* @param codeVerifier Your code verifier plaintext.
362
- *
363
- * @return Configurable [SpotifyClientApiBuilder] that, when built, creates a new [SpotifyClientApi]
364
- */
365
- fun spotifyClientPkceApi (
366
- clientId : String? ,
367
- redirectUri : String? ,
368
- authCode : String ,
369
- codeVerifier : String
370
- ) = SpotifyClientApiBuilder ().apply {
371
- credentials {
372
- this .clientId = clientId
373
- this .redirectUri = redirectUri
374
- }
375
-
376
- authentication {
377
- authorizationCode = authCode
378
- pkceCodeVerifier = codeVerifier
379
- }
380
- }
381
-
382
- /* *
383
- * Instantiate a new [SpotifyClientApiBuilder] using an [authCode] and [codeVerifier]. This is for **PKCE authorization**.
384
- *
385
- *
386
- * @param clientId Spotify [client id](https://developer.spotify.com/documentation/general/guides/app-settings/)
387
- * @param clientSecret Spotify [client secret](https://developer.spotify.com/documentation/general/guides/app-settings/)
388
- * @param redirectUri Spotify [redirect uri](https://developer.spotify.com/documentation/general/guides/app-settings/)
389
- * @param authCode The authorization code retrieved after OAuth authorization
390
- * @param codeVerifier Your code verifier plaintext.
391
- * @param block Api settings block
392
+ * @param options Override default API options such as the cache limit
392
393
*
393
394
* @return Configurable [SpotifyClientApiBuilder] that, when built, creates a new [SpotifyClientApi]
394
395
*/
@@ -397,7 +398,7 @@ fun spotifyClientPkceApi(
397
398
redirectUri : String? ,
398
399
authCode : String ,
399
400
codeVerifier : String ,
400
- block : SpotifyClientApiBuilder .() -> Unit = {}
401
+ options : SpotifyApiOptionsBuilder ? = null
401
402
) = SpotifyClientApiBuilder ().apply {
402
403
credentials {
403
404
this .clientId = clientId
@@ -408,7 +409,9 @@ fun spotifyClientPkceApi(
408
409
authorizationCode = authCode
409
410
pkceCodeVerifier = codeVerifier
410
411
}
411
- }.apply (block)
412
+
413
+ options?.let { this .options = it.build() }
414
+ }
412
415
413
416
/* *
414
417
* Spotify API builder
@@ -649,6 +652,11 @@ interface ISpotifyApiBuilder<T : SpotifyApi<T, B>, B : ISpotifyApiBuilder<T, B>>
649
652
*/
650
653
fun options (options : SpotifyApiOptions ) = apply { this .options = options }
651
654
655
+ /* *
656
+ * Allows you to override default values for caching, token refresh, and logging
657
+ */
658
+ fun options (options : SpotifyApiOptionsBuilder ) = apply { this .options = options.build() }
659
+
652
660
/* *
653
661
* Build the [T] by provided information
654
662
*/
@@ -751,7 +759,8 @@ class SpotifyClientApiBuilder(
751
759
options.requestTimeoutMillis,
752
760
options.json,
753
761
options.refreshTokenProducer,
754
- false
762
+ false ,
763
+ options.onTokenRefresh
755
764
)
756
765
} catch (e: CancellationException ) {
757
766
throw e
@@ -793,7 +802,8 @@ class SpotifyClientApiBuilder(
793
802
options.requestTimeoutMillis,
794
803
options.json,
795
804
options.refreshTokenProducer,
796
- true
805
+ true ,
806
+ options.onTokenRefresh
797
807
)
798
808
} catch (e: CancellationException ) {
799
809
throw e
@@ -816,7 +826,8 @@ class SpotifyClientApiBuilder(
816
826
options.requestTimeoutMillis,
817
827
options.json,
818
828
options.refreshTokenProducer,
819
- false
829
+ false ,
830
+ options.onTokenRefresh
820
831
)
821
832
authorization.tokenString != null -> SpotifyClientApi (
822
833
clientId,
@@ -840,7 +851,8 @@ class SpotifyClientApiBuilder(
840
851
options.requestTimeoutMillis,
841
852
options.json,
842
853
options.refreshTokenProducer,
843
- false
854
+ false ,
855
+ options.onTokenRefresh
844
856
)
845
857
else -> throw IllegalArgumentException (
846
858
" At least one of: authorizationCode, tokenString, or token must be provided " +
@@ -885,7 +897,8 @@ class SpotifyAppApiBuilder(
885
897
options.allowBulkRequests,
886
898
options.requestTimeoutMillis,
887
899
options.json,
888
- options.refreshTokenProducer
900
+ options.refreshTokenProducer,
901
+ options.onTokenRefresh
889
902
)
890
903
authorization.tokenString != null -> {
891
904
SpotifyAppApi (
@@ -905,7 +918,8 @@ class SpotifyAppApiBuilder(
905
918
options.allowBulkRequests,
906
919
options.requestTimeoutMillis,
907
920
options.json,
908
- options.refreshTokenProducer
921
+ options.refreshTokenProducer,
922
+ options.onTokenRefresh
909
923
)
910
924
}
911
925
authorization.refreshTokenString != null -> {
@@ -923,7 +937,8 @@ class SpotifyAppApiBuilder(
923
937
options.allowBulkRequests,
924
938
options.requestTimeoutMillis,
925
939
options.json,
926
- options.refreshTokenProducer
940
+ options.refreshTokenProducer,
941
+ options.onTokenRefresh
927
942
)
928
943
}
929
944
else -> try {
@@ -943,7 +958,8 @@ class SpotifyAppApiBuilder(
943
958
options.allowBulkRequests,
944
959
options.requestTimeoutMillis,
945
960
options.json,
946
- options.refreshTokenProducer
961
+ options.refreshTokenProducer,
962
+ options.onTokenRefresh
947
963
)
948
964
} catch (e: CancellationException ) {
949
965
throw e
@@ -1028,6 +1044,7 @@ data class SpotifyUserAuthorization(
1028
1044
* @property allowBulkRequests Allow splitting too-large requests into smaller, allowable api requests
1029
1045
* @property requestTimeoutMillis The maximum time, in milliseconds, before terminating an http request
1030
1046
* @property refreshTokenProducer Provide if you want to use your own logic when refreshing a Spotify token
1047
+ * @property onTokenRefresh Provide if you want to act on token refresh event
1031
1048
*
1032
1049
*/
1033
1050
class SpotifyApiOptionsBuilder (
@@ -1042,7 +1059,8 @@ class SpotifyApiOptionsBuilder(
1042
1059
var allowBulkRequests : Boolean = true ,
1043
1060
var requestTimeoutMillis : Long? = null ,
1044
1061
var json : Json = nonstrictJson,
1045
- var refreshTokenProducer : (suspend (SpotifyApi <* , * >) -> Token )? = null
1062
+ var refreshTokenProducer : (suspend (GenericSpotifyApi ) -> Token )? = null ,
1063
+ var onTokenRefresh : (suspend (GenericSpotifyApi ) -> Unit )? = null
1046
1064
) {
1047
1065
fun build () =
1048
1066
if (enableAllOptions)
@@ -1057,7 +1075,8 @@ class SpotifyApiOptionsBuilder(
1057
1075
allowBulkRequests = true ,
1058
1076
requestTimeoutMillis = requestTimeoutMillis,
1059
1077
json = json,
1060
- refreshTokenProducer = refreshTokenProducer
1078
+ refreshTokenProducer = refreshTokenProducer,
1079
+ onTokenRefresh = onTokenRefresh
1061
1080
)
1062
1081
else
1063
1082
SpotifyApiOptions (
@@ -1071,7 +1090,8 @@ class SpotifyApiOptionsBuilder(
1071
1090
allowBulkRequests,
1072
1091
requestTimeoutMillis,
1073
1092
json,
1074
- refreshTokenProducer
1093
+ refreshTokenProducer,
1094
+ onTokenRefresh
1075
1095
)
1076
1096
}
1077
1097
@@ -1089,6 +1109,7 @@ class SpotifyApiOptionsBuilder(
1089
1109
* @property allowBulkRequests Allow splitting too-large requests into smaller, allowable api requests. Default: true
1090
1110
* @property requestTimeoutMillis The maximum time, in milliseconds, before terminating an http request. Default: 100000ms
1091
1111
* @property refreshTokenProducer Provide if you want to use your own logic when refreshing a Spotify token.
1112
+ * @property onTokenRefresh Provide if you want to act on token refresh event
1092
1113
*
1093
1114
*/
1094
1115
@@ -1103,7 +1124,8 @@ data class SpotifyApiOptions(
1103
1124
var allowBulkRequests : Boolean ,
1104
1125
var requestTimeoutMillis : Long? ,
1105
1126
var json : Json ,
1106
- var refreshTokenProducer : (suspend (SpotifyApi <* , * >) -> Token )?
1127
+ var refreshTokenProducer : (suspend (SpotifyApi <* , * >) -> Token )? ,
1128
+ var onTokenRefresh : (suspend (SpotifyApi <* , * >) -> Unit )?
1107
1129
)
1108
1130
1109
1131
@Deprecated(" Name has been replaced by `options`" , ReplaceWith (" SpotifyApiOptions" ))
0 commit comments