Skip to content

Commit 60ad384

Browse files
committed
clean up SpotifyAPI classes
Signed-off-by: Adam Ratzman <adam@adamratzman.com>
1 parent 3c45b0d commit 60ad384

File tree

2 files changed

+46
-36
lines changed

2 files changed

+46
-36
lines changed

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

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -38,26 +38,17 @@ class SpotifyApiBuilderJava(val clientId: String, val clientSecret: String) {
3838
var authorizationCode: String? = null
3939
var tokenString: String? = null
4040
var token: Token? = null
41+
var useCache: Boolean = true
4142

42-
fun redirectUri(redirectUri: String?): SpotifyApiBuilderJava {
43-
this.redirectUri = redirectUri
44-
return this
45-
}
43+
fun useCache(useCache: Boolean) = apply { this.useCache = useCache }
4644

47-
fun authorizationCode(authorizationCode: String?): SpotifyApiBuilderJava {
48-
this.authorizationCode = authorizationCode
49-
return this
50-
}
45+
fun redirectUri(redirectUri: String?) = apply { this.redirectUri = redirectUri }
5146

52-
fun tokenString(tokenString: String?): SpotifyApiBuilderJava {
53-
this.tokenString = tokenString
54-
return this
55-
}
47+
fun authorizationCode(authorizationCode: String?) = apply { this.authorizationCode = authorizationCode }
5648

57-
fun token(token: Token?): SpotifyApiBuilderJava {
58-
this.token = token
59-
return this
60-
}
49+
fun tokenString(tokenString: String?) = apply { this.tokenString = tokenString }
50+
51+
fun token(token: Token?) = apply { this.token = token }
6152

6253
fun buildCredentialed() = spotifyApi {
6354
credentials {
@@ -120,6 +111,7 @@ class SpotifyUserAuthorizationBuilder(
120111
class SpotifyApiBuilder {
121112
private var credentials: SpotifyCredentials = SpotifyCredentials(null, null, null)
122113
private var authentication = SpotifyUserAuthorizationBuilder()
114+
var useCache: Boolean = true
123115

124116
fun credentials(block: SpotifyCredentialsBuilder.() -> Unit) {
125117
credentials = SpotifyCredentialsBuilder().apply(block).build()
@@ -140,6 +132,8 @@ class SpotifyApiBuilder {
140132
return getAuthUrlFull(*scopes, clientId = credentials.clientId!!, redirectUri = credentials.redirectUri!!)
141133
}
142134

135+
fun buildCredentialedAsync(consumer: (SpotifyAPI) -> Unit) = Runnable { consumer(buildCredentialed()) }.run()
136+
143137
fun buildCredentialed(): SpotifyAPI {
144138
val clientId = credentials.clientId
145139
val clientSecret = credentials.clientSecret
@@ -148,7 +142,7 @@ class SpotifyApiBuilder {
148142
}
149143
return when {
150144
authentication.token != null -> {
151-
SpotifyAppAPI(clientId ?: "not-set", clientSecret ?: "not-set", authentication.token!!)
145+
SpotifyAppAPI(clientId ?: "not-set", clientSecret ?: "not-set", authentication.token!!, useCache)
152146
}
153147
authentication.tokenString != null -> {
154148
SpotifyAppAPI(
@@ -157,20 +151,24 @@ class SpotifyApiBuilder {
157151
Token(
158152
authentication.tokenString!!, "client_credentials",
159153
60000, null, null
160-
)
154+
),
155+
useCache
161156
)
162157
}
163158
else -> try {
164159
if (clientId == null || clientSecret == null) throw IllegalArgumentException("Illegal credentials provided")
165160
val token = getCredentialedToken(clientId, clientSecret)
166161
?: throw IllegalArgumentException("Invalid credentials provided")
167-
SpotifyAppAPI(clientId, clientSecret, token)
162+
SpotifyAppAPI(clientId, clientSecret, token, useCache)
168163
} catch (e: Exception) {
169164
throw SpotifyException("Invalid credentials provided in the login process", e)
170165
}
171166
}
172167
}
173168

169+
fun buildClientAsync(consumer: (SpotifyClientAPI) -> Unit, automaticRefresh: Boolean = false) =
170+
Runnable { consumer(buildClient(automaticRefresh)) }.run()
171+
174172
fun buildClient(automaticRefresh: Boolean = false): SpotifyClientAPI =
175173
buildClient(
176174
authentication.authorizationCode, authentication.tokenString,
@@ -210,9 +208,15 @@ class SpotifyApiBuilder {
210208
method = HttpRequestMethod.POST,
211209
body = "grant_type=authorization_code&code=$authorizationCode&redirect_uri=$redirectUri",
212210
contentType = "application/x-www-form-urlencoded"
213-
).execute(HttpHeader("Authorization", "Basic ${"$clientId:$clientSecret".byteEncode()}")).body.toObject(Gson(), Token::class.java),
211+
).execute(
212+
HttpHeader(
213+
"Authorization",
214+
"Basic ${"$clientId:$clientSecret".byteEncode()}"
215+
)
216+
).body.toObject(Gson(), Token::class.java),
214217
automaticRefresh,
215-
redirectUri ?: throw IllegalArgumentException()
218+
redirectUri ?: throw IllegalArgumentException(),
219+
useCache
216220
)
217221
} catch (e: Exception) {
218222
throw SpotifyException("Invalid credentials provided in the login process", e)
@@ -222,23 +226,28 @@ class SpotifyApiBuilder {
222226
clientSecret ?: "not-set",
223227
token,
224228
automaticRefresh,
225-
redirectUri ?: "not-set"
229+
redirectUri ?: "not-set",
230+
useCache
226231
)
227232
tokenString != null -> SpotifyClientAPI(
228233
clientId ?: "not-set", clientSecret ?: "not-set", Token(
229234
tokenString, "client_credentials", 1000,
230235
null, null
231-
), false, redirectUri ?: "not-set"
236+
), false, redirectUri ?: "not-set",
237+
useCache
232238
)
233239
else -> throw IllegalArgumentException(
234240
"At least one of: authorizationCode, tokenString, or token must be provided " +
235-
"to build a SpotifyClientAPI object"
241+
"to build a SpotifyClientAPI object"
236242
)
237243
}
238244
}
239245
}
240246

241-
abstract class SpotifyAPI internal constructor(val clientId: String, val clientSecret: String, var token: Token) {
247+
abstract class SpotifyAPI internal constructor(
248+
val clientId: String, val clientSecret: String,
249+
var token: Token, var useCache: Boolean
250+
) {
242251
internal var expireTime = System.currentTimeMillis() + token.expires_in * 1000
243252
internal val executor = Executors.newScheduledThreadPool(2)
244253
internal val gson = GsonBuilder().setLenient().create()!!
@@ -257,8 +266,6 @@ abstract class SpotifyAPI internal constructor(val clientId: String, val clientS
257266
abstract fun refreshToken()
258267
abstract fun clearCache()
259268

260-
var useCache: Boolean = true
261-
262269
init {
263270
executor.scheduleAtFixedRate(::clearCache, 10, 10, TimeUnit.MINUTES)
264271
}
@@ -276,8 +283,8 @@ abstract class SpotifyAPI internal constructor(val clientId: String, val clientS
276283
}
277284
}
278285

279-
class SpotifyAppAPI internal constructor(clientId: String, clientSecret: String, token: Token) :
280-
SpotifyAPI(clientId, clientSecret, token) {
286+
class SpotifyAppAPI internal constructor(clientId: String, clientSecret: String, token: Token, useCache: Boolean) :
287+
SpotifyAPI(clientId, clientSecret, token, useCache) {
281288
override val search: SearchAPI = SearchAPI(this)
282289
override val albums: AlbumAPI = AlbumAPI(this)
283290
override val browse: BrowseAPI = BrowseAPI(this)
@@ -316,8 +323,9 @@ class SpotifyClientAPI internal constructor(
316323
clientSecret: String,
317324
token: Token,
318325
automaticRefresh: Boolean = false,
319-
var redirectUri: String
320-
) : SpotifyAPI(clientId, clientSecret, token) {
326+
var redirectUri: String,
327+
useCache: Boolean
328+
) : SpotifyAPI(clientId, clientSecret, token, useCache) {
321329
override val search: SearchAPI = SearchAPI(this)
322330
override val albums: AlbumAPI = AlbumAPI(this)
323331
override val browse: BrowseAPI = BrowseAPI(this)
@@ -399,9 +407,9 @@ class SpotifyClientAPI internal constructor(
399407

400408
private fun getAuthUrlFull(vararg scopes: SpotifyScope, clientId: String, redirectUri: String): String {
401409
return "https://accounts.spotify.com/authorize/?client_id=$clientId" +
402-
"&response_type=code" +
403-
"&redirect_uri=$redirectUri" +
404-
if (scopes.isEmpty()) "" else "&scope=${scopes.joinToString("%20") { it.uri }}"
410+
"&response_type=code" +
411+
"&redirect_uri=$redirectUri" +
412+
if (scopes.isEmpty()) "" else "&scope=${scopes.joinToString("%20") { it.uri }}"
405413
}
406414

407415
private fun getCredentialedToken(clientId: String, clientSecret: String) = Gson().fromJson(

src/test/kotlin/com/adamratzman/spotify/utilities/UtilityTests.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,14 @@ class UtilityTests : Spek({
4242

4343
it("App API valid parameters") {
4444
assertDoesNotThrow {
45-
spotifyApi {
45+
val api = spotifyApi {
4646
credentials {
4747
clientId = System.getProperty("clientId")
4848
clientSecret = System.getProperty("clientSecret")
4949
}
50-
}.buildCredentialed()
50+
}
51+
api.buildCredentialed()
52+
api.buildCredentialedAsync { }
5153
}
5254
}
5355
}

0 commit comments

Comments
 (0)