Skip to content

Commit 923ac68

Browse files
committed
introduce java-friendly, less verbose builder
1 parent e02b554 commit 923ac68

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,57 @@ import java.util.concurrent.TimeUnit
1313

1414
internal val base = "https://api.spotify.com/v1"
1515

16+
// Kotlin DSL builder
1617
fun spotifyApi(block: SpotifyApiBuilder.() -> Unit) = SpotifyApiBuilder().apply(block)
1718

19+
// Java-friendly builder
20+
class SpotifyApiBuilderJava(val clientId: String, val clientSecret: String) {
21+
var redirectUri: String? = null
22+
var authorizationCode: String? = null
23+
var tokenString: String? = null
24+
var token: Token? = null
25+
26+
fun redirectUri(redirectUri: String?): SpotifyApiBuilderJava {
27+
this.redirectUri = redirectUri
28+
return this
29+
}
30+
31+
fun authorizationCode(authorizationCode: String?): SpotifyApiBuilderJava {
32+
this.authorizationCode = authorizationCode
33+
return this
34+
}
35+
36+
fun tokenString(tokenString: String?): SpotifyApiBuilderJava {
37+
this.tokenString = tokenString
38+
return this
39+
}
40+
41+
fun token(token: Token?): SpotifyApiBuilderJava {
42+
this.token = token
43+
return this
44+
}
45+
46+
fun buildCredentialed() = spotifyApi {
47+
credentials {
48+
clientId = this@SpotifyApiBuilderJava.clientId
49+
clientSecret = this@SpotifyApiBuilderJava.clientSecret
50+
}
51+
}.buildCredentialed()
52+
53+
fun buildClient(automaticRefresh: Boolean=false) = spotifyApi {
54+
credentials{
55+
clientId = this@SpotifyApiBuilderJava.clientId
56+
clientSecret = this@SpotifyApiBuilderJava.clientSecret
57+
redirectUri = this@SpotifyApiBuilderJava.redirectUri
58+
}
59+
clientAuthentication {
60+
authorizationCode = this@SpotifyApiBuilderJava.authorizationCode
61+
tokenString = this@SpotifyApiBuilderJava.tokenString
62+
token = this@SpotifyApiBuilderJava.token
63+
}
64+
}.buildClient(automaticRefresh)
65+
}
66+
1867
/**
1968
* @property clientId the client id of your Spotify application
2069
* @property clientSecret the client secret of your Spotify application

src/test/kotlin/com/adamratzman/spotify/Common.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ val api = when {
1919
redirectUri = System.getProperty("spotifyRedirectUri")
2020
}
2121
clientAuthentication {
22-
tokenString = System.getProperty("spotifyTokenString")
22+
authorizationCode = System.getProperty("spotifyAuthCode")
2323
}
2424
}.buildClient()
2525
}

0 commit comments

Comments
 (0)