2
2
package com.adamratzman.spotify
3
3
4
4
import com.adamratzman.spotify.http.HttpConnection
5
- import com.adamratzman.spotify.http.HttpHeader
6
5
import com.adamratzman.spotify.http.HttpRequestMethod
7
- import com.adamratzman.spotify.http.byteEncode
8
6
import com.adamratzman.spotify.models.Token
9
7
import com.adamratzman.spotify.models.serialization.toObject
10
8
@@ -14,10 +12,8 @@ import com.adamratzman.spotify.models.serialization.toObject
14
12
*/
15
13
fun spotifyApi (block : SpotifyApiBuilderDsl .() -> Unit ) = SpotifyApiBuilderDsl ().apply (block)
16
14
17
- // Java-friendly builder
18
-
19
15
/* *
20
- * A builder in the style of traditional Java builders
16
+ * Spotify traditional Java style API builder
21
17
*/
22
18
class SpotifyApiBuilder (
23
19
val clientId : String ,
@@ -26,7 +22,8 @@ class SpotifyApiBuilder(
26
22
var authorizationCode : String? = null ,
27
23
var tokenString : String? = null ,
28
24
var token : Token ? = null ,
29
- var useCache : Boolean = true
25
+ var useCache : Boolean = true ,
26
+ var automaticRefresh : Boolean = true
30
27
) {
31
28
/* *
32
29
* Instantiate the builder with the application [clientId] and [clientSecret]
@@ -48,22 +45,22 @@ class SpotifyApiBuilder(
48
45
* Instantiate the builder with the application [clientId], [clientSecret], application
49
46
* [redirectUri], and an [authorizationCode]
50
47
*/
51
- constructor (clientId: String , clientSecret: String , redirectUri: String? , authorizationCode: String , useCache: Boolean )
52
- : this (clientId, clientSecret, redirectUri, authorizationCode, null , useCache = useCache)
48
+ constructor (clientId: String , clientSecret: String , redirectUri: String? , authorizationCode: String , useCache: Boolean ) :
49
+ this (clientId, clientSecret, redirectUri, authorizationCode, null , useCache = useCache)
53
50
54
51
/* *
55
52
* Instantiate the builder with the application [clientId], [clientSecret], application
56
53
* [redirectUri], and an access token string ([tokenString])
57
54
*/
58
- constructor (clientId: String , clientSecret: String , redirectUri: String? , tokenString: String )
59
- : this (clientId, clientSecret, redirectUri, null , tokenString)
55
+ constructor (clientId: String , clientSecret: String , redirectUri: String? , tokenString: String ) :
56
+ this (clientId, clientSecret, redirectUri, null , tokenString)
60
57
61
58
/* *
62
59
* Instantiate the builder with the application [clientId], [clientSecret], application
63
60
* [redirectUri], and a [token]
64
61
*/
65
- constructor (clientId: String , clientSecret: String , redirectUri: String? , token: Token , useCache: Boolean )
66
- : this (clientId, clientSecret, redirectUri, null , null , token, useCache)
62
+ constructor (clientId: String , clientSecret: String , redirectUri: String? , token: Token , useCache: Boolean ) :
63
+ this (clientId, clientSecret, redirectUri, null , null , token, useCache)
67
64
68
65
/* *
69
66
* Set whether to cache requests. Default: true
@@ -90,6 +87,11 @@ class SpotifyApiBuilder(
90
87
*/
91
88
fun token (token : Token ? ) = apply { this .token = token }
92
89
90
+ /* *
91
+ * Enable or disable automatic refresh of the Spotify access token
92
+ */
93
+ fun automaticRefresh (automaticRefresh : Boolean ) = apply { this .automaticRefresh = automaticRefresh }
94
+
93
95
/* fun build(type: AuthorizationType, automaticRefresh: Boolean = true) {
94
96
95
97
}*/
@@ -106,9 +108,11 @@ class SpotifyApiBuilder(
106
108
token = this @SpotifyApiBuilder.token
107
109
tokenString = this @SpotifyApiBuilder.tokenString
108
110
}
111
+
112
+ automaticRefresh = this @SpotifyApiBuilder.automaticRefresh
109
113
}.buildCredentialed()
110
114
111
- fun buildClient (automaticRefresh : Boolean = true ) = spotifyApi {
115
+ fun buildClient () = spotifyApi {
112
116
credentials {
113
117
clientId = this @SpotifyApiBuilder.clientId
114
118
clientSecret = this @SpotifyApiBuilder.clientSecret
@@ -119,7 +123,9 @@ class SpotifyApiBuilder(
119
123
tokenString = this @SpotifyApiBuilder.tokenString
120
124
token = this @SpotifyApiBuilder.token
121
125
}
122
- }.buildClient(automaticRefresh)
126
+
127
+ automaticRefresh = this @SpotifyApiBuilder.automaticRefresh
128
+ }.buildClient()
123
129
}
124
130
125
131
/* *
@@ -159,6 +165,7 @@ class SpotifyApiBuilderDsl {
159
165
private var credentials: SpotifyCredentials = SpotifyCredentials (null , null , null )
160
166
private var authentication = SpotifyUserAuthorizationBuilder ()
161
167
var useCache: Boolean = true
168
+ var automaticRefresh: Boolean = true
162
169
163
170
fun credentials (block : SpotifyCredentialsBuilder .() -> Unit ) {
164
171
credentials = SpotifyCredentialsBuilder ().apply (block).build()
@@ -181,7 +188,7 @@ class SpotifyApiBuilderDsl {
181
188
182
189
fun buildCredentialedAsync (consumer : (SpotifyAPI ) -> Unit ) = Runnable { consumer(buildCredentialed()) }.run ()
183
190
184
- fun buildCredentialed (automaticRefresh : Boolean = true ): SpotifyAPI {
191
+ fun buildCredentialed (): SpotifyAPI {
185
192
val clientId = credentials.clientId
186
193
val clientSecret = credentials.clientSecret
187
194
if ((clientId == null || clientSecret == null ) && (authentication.token == null && authentication.tokenString == null )) {
@@ -190,7 +197,7 @@ class SpotifyApiBuilderDsl {
190
197
return when {
191
198
authentication.token != null -> {
192
199
SpotifyAppAPI (clientId ? : " not-set" , clientSecret
193
- ? : " not-set" , authentication.token!! , useCache, automaticRefresh )
200
+ ? : " not-set" , authentication.token!! , useCache, false )
194
201
}
195
202
authentication.tokenString != null -> {
196
203
SpotifyAppAPI (
@@ -206,22 +213,21 @@ class SpotifyApiBuilderDsl {
206
213
}
207
214
else -> try {
208
215
if (clientId == null || clientSecret == null ) throw IllegalArgumentException (" Illegal credentials provided" )
209
- val token = getCredentialedToken(clientId, clientSecret)
210
- ? : throw IllegalArgumentException (" Invalid credentials provided" )
216
+ val token = getCredentialedToken(clientId, clientSecret, null )
211
217
SpotifyAppAPI (clientId, clientSecret, token, useCache, automaticRefresh)
212
218
} catch (e: Exception ) {
213
219
throw SpotifyException (" Invalid credentials provided in the login process" , e)
214
220
}
215
221
}
216
222
}
217
223
218
- fun buildClientAsync (consumer : (SpotifyClientAPI ) -> Unit , automaticRefresh : Boolean = false ) =
219
- Runnable { consumer(buildClient(automaticRefresh )) }.run ()
224
+ fun buildClientAsync (consumer : (SpotifyClientAPI ) -> Unit ) =
225
+ Runnable { consumer(buildClient()) }.run ()
220
226
221
- fun buildClient (automaticRefresh : Boolean = false ): SpotifyClientAPI =
227
+ fun buildClient (): SpotifyClientAPI =
222
228
buildClient(
223
229
authentication.authorizationCode, authentication.tokenString,
224
- authentication.token, automaticRefresh
230
+ authentication.token
225
231
)
226
232
227
233
/* *
@@ -237,8 +243,7 @@ class SpotifyApiBuilderDsl {
237
243
private fun buildClient (
238
244
authorizationCode : String? = null,
239
245
tokenString : String? = null,
240
- token : Token ? = null,
241
- automaticRefresh : Boolean = false
246
+ token : Token ? = null
242
247
): SpotifyClientAPI {
243
248
val clientId = credentials.clientId
244
249
val clientSecret = credentials.clientSecret
@@ -249,21 +254,22 @@ class SpotifyApiBuilderDsl {
249
254
}
250
255
return when {
251
256
authorizationCode != null -> try {
257
+ clientId ? : throw IllegalArgumentException ()
258
+ clientSecret ? : throw IllegalArgumentException ()
259
+ redirectUri ? : IllegalArgumentException ()
260
+
261
+ val response = executeTokenRequest(HttpConnection (
262
+ url = " https://accounts.spotify.com/api/token" ,
263
+ method = HttpRequestMethod .POST ,
264
+ body = " grant_type=authorization_code&code=$authorizationCode &redirect_uri=$redirectUri " ,
265
+ contentType = " application/x-www-form-urlencoded" ,
266
+ api = null
267
+ ), clientId, clientSecret)
268
+
252
269
SpotifyClientAPI (
253
- clientId ? : throw IllegalArgumentException (),
254
- clientSecret ? : throw IllegalArgumentException (),
255
- HttpConnection (
256
- url = " https://accounts.spotify.com/api/token" ,
257
- method = HttpRequestMethod .POST ,
258
- body = " grant_type=authorization_code&code=$authorizationCode &redirect_uri=$redirectUri " ,
259
- contentType = " application/x-www-form-urlencoded" ,
260
- api = null
261
- ).execute(
262
- HttpHeader (
263
- " Authorization" ,
264
- " Basic ${" $clientId :$clientSecret " .byteEncode()} "
265
- )
266
- ).body.toObject(null ),
270
+ clientId,
271
+ clientSecret,
272
+ response.body.toObject(null ),
267
273
automaticRefresh,
268
274
redirectUri ? : throw IllegalArgumentException (),
269
275
useCache
@@ -280,10 +286,17 @@ class SpotifyApiBuilderDsl {
280
286
useCache
281
287
)
282
288
tokenString != null -> SpotifyClientAPI (
283
- clientId ? : " not-set" , clientSecret ? : " not-set" , Token (
284
- tokenString, " client_credentials" , 1000 ,
285
- null , null
286
- ), false , redirectUri ? : " not-set" ,
289
+ clientId ? : " not-set" ,
290
+ clientSecret ? : " not-set" ,
291
+ Token (
292
+ tokenString,
293
+ " client_credentials" ,
294
+ 1000 ,
295
+ null ,
296
+ null
297
+ ),
298
+ false ,
299
+ redirectUri ? : " not-set" ,
287
300
useCache
288
301
)
289
302
else -> throw IllegalArgumentException (
0 commit comments