Skip to content

Commit b3ccda8

Browse files
authored
Merge pull request #220 from adamint/dev
pkce fixes
2 parents b85a52f + 5e9210a commit b3ccda8

File tree

21 files changed

+179
-160
lines changed

21 files changed

+179
-160
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ repositories {
3939
jcenter()
4040
}
4141
42-
compile group: 'com.adamratzman', name: 'spotify-api-kotlin-core', version: '3.2.11'
42+
compile group: 'com.adamratzman', name: 'spotify-api-kotlin-core', version: '3.2.13'
4343
```
4444

4545
Note that images and profiles are not supported on the Kotlin/JS target.

build.gradle.kts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ buildscript {
3131
}
3232

3333
group = "com.adamratzman"
34-
version = "3.2.11"
34+
version = "3.2.13"
3535

3636
tasks.withType<Test> {
3737
this.testLogging {
@@ -208,7 +208,7 @@ kotlin {
208208
api("net.sourceforge.streamsupport:android-retrofuture:1.7.2")
209209
api("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutineVersion")
210210
api("org.jetbrains.kotlinx:kotlinx-serialization-runtime:$serializationVersion")
211-
api("io.ktor:ktor-client-cio:$ktorVersion")
211+
api("io.ktor:ktor-client-okhttp:$ktorVersion")
212212
api("io.coil-kt:coil:0.11.0")
213213
implementation(kotlin("stdlib-jdk8"))
214214
}

gradle/wrapper/gradle-wrapper.jar

57.3 KB
Binary file not shown.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
distributionBase=GRADLE_USER_HOME
2+
distributionPath=wrapper/dists
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-bin.zip
4+
zipStoreBase=GRADLE_USER_HOME
5+
zipStorePath=wrapper/dists

src/androidMain/kotlin/com/adamratzman/spotify/utils/Concurrency.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import kotlinx.coroutines.delay
88
import kotlinx.coroutines.launch
99
import kotlinx.coroutines.runBlocking as kRunBlocking
1010

11-
actual inline fun <T> runBlocking(crossinline coroutineCode: suspend () -> T): T = kRunBlocking {
11+
actual inline fun <T> runBlockingMpp(crossinline coroutineCode: suspend () -> T): T = kRunBlocking {
1212
coroutineCode()
1313
}
1414

src/androidMain/kotlin/com/adamratzman/spotify/utils/IO.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import android.util.Base64
77
import java.io.ByteArrayOutputStream
88
import java.net.URL
99

10-
1110
internal actual fun encodeBufferedImageToBase64String(image: BufferedImage): String {
1211
val byteArrayOutputStream = ByteArrayOutputStream()
1312
image.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream)

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

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import com.adamratzman.spotify.http.HttpRequestMethod
77
import com.adamratzman.spotify.models.Token
88
import com.adamratzman.spotify.models.serialization.nonstrictJson
99
import com.adamratzman.spotify.models.serialization.toObject
10-
import com.adamratzman.spotify.utils.runBlocking
10+
import com.adamratzman.spotify.utils.runBlockingMpp
1111
import kotlinx.coroutines.CancellationException
1212
import kotlinx.coroutines.CoroutineScope
1313
import kotlinx.coroutines.Job
@@ -123,7 +123,6 @@ fun spotifyImplicitGrantApi(
123123
options.cacheLimit,
124124
options.retryWhenRateLimited,
125125
options.enableLogger,
126-
options.testTokenValidity,
127126
options.defaultLimit,
128127
options.allowBulkRequests,
129128
options.requestTimeoutMillis,
@@ -237,7 +236,6 @@ fun spotifyAppApi(
237236
*/
238237
fun spotifyAppApi(block: SpotifyAppApiBuilder.() -> Unit) = SpotifyAppApiBuilder().apply(block)
239238

240-
241239
// Client Api Builders
242240
/*
243241
____________________________
@@ -468,11 +466,11 @@ fun spotifyClientPkceApi(
468466
* @return Configurable [SpotifyClientApiBuilder] that, when built, creates a new [SpotifyClientApi]
469467
*/
470468
fun spotifyClientPkceApi(
471-
clientId: String?,
472-
redirectUri: String?,
473-
authCode: String,
474-
codeVerifier: String,
475-
options: SpotifyApiOptionsBuilder? = null
469+
clientId: String?,
470+
redirectUri: String?,
471+
authCode: String,
472+
codeVerifier: String,
473+
options: SpotifyApiOptionsBuilder? = null
476474
) = SpotifyClientApiBuilder().apply {
477475
credentials {
478476
this.clientId = clientId
@@ -487,7 +485,6 @@ fun spotifyClientPkceApi(
487485
options?.let { this.options = it.build() }
488486
}
489487

490-
491488
/**
492489
* Spotify API builder
493490
*/
@@ -740,7 +737,7 @@ interface ISpotifyApiBuilder<T : SpotifyApi<T, B>, B : ISpotifyApiBuilder<T, B>>
740737
/**
741738
* Build the [T] by provided information
742739
*/
743-
fun build(): T = runBlocking { suspendBuild() }
740+
fun build(): T = runBlockingMpp { suspendBuild() }
744741

745742
/**
746743
* Build the [T] by provided information
@@ -798,7 +795,7 @@ class SpotifyClientApiBuilder(
798795

799796
// either application credentials, or a token is required
800797
require((clientId != null && clientSecret != null && redirectUri != null) || (clientId != null && redirectUri != null && authorization.pkceCodeVerifier != null) || authorization.token != null || authorization.tokenString != null) { "You need to specify a valid clientId, clientSecret, and redirectUri in the credentials block!" }
801-
return when {
798+
val api = when {
802799
authorization.authorizationCode != null && authorization.pkceCodeVerifier == null -> try {
803800
require(clientId != null && clientSecret != null && redirectUri != null) { "You need to specify a valid clientId, clientSecret, and redirectUri in the credentials block!" }
804801

@@ -828,7 +825,6 @@ class SpotifyClientApiBuilder(
828825
options.automaticRefresh,
829826
options.retryWhenRateLimited,
830827
options.enableLogger,
831-
options.testTokenValidity,
832828
options.defaultLimit,
833829
options.allowBulkRequests,
834830
options.requestTimeoutMillis,
@@ -844,7 +840,7 @@ class SpotifyClientApiBuilder(
844840
throw SpotifyException.AuthenticationException("Invalid credentials provided in the login process (clientId=$clientId, clientSecret=$clientSecret, authCode=${authorization.authorizationCode})", e)
845841
}
846842
authorization.authorizationCode != null && authorization.pkceCodeVerifier != null -> try {
847-
require(clientId != null && redirectUri != null) { "You need to specify a valid clientId, clientSecret, and redirectUri in the credentials block!" }
843+
require(clientId != null && redirectUri != null) { "You need to specify a valid clientId and redirectUri in the credentials block!" }
848844

849845
val response = HttpConnection(
850846
"https://accounts.spotify.com/api/token",
@@ -872,7 +868,6 @@ class SpotifyClientApiBuilder(
872868
options.automaticRefresh,
873869
options.retryWhenRateLimited,
874870
options.enableLogger,
875-
options.testTokenValidity,
876871
options.defaultLimit,
877872
options.allowBulkRequests,
878873
options.requestTimeoutMillis,
@@ -897,7 +892,6 @@ class SpotifyClientApiBuilder(
897892
options.automaticRefresh,
898893
options.retryWhenRateLimited,
899894
options.enableLogger,
900-
options.testTokenValidity,
901895
options.defaultLimit,
902896
options.allowBulkRequests,
903897
options.requestTimeoutMillis,
@@ -923,7 +917,6 @@ class SpotifyClientApiBuilder(
923917
false,
924918
options.retryWhenRateLimited,
925919
options.enableLogger,
926-
options.testTokenValidity,
927920
options.defaultLimit,
928921
options.allowBulkRequests,
929922
options.requestTimeoutMillis,
@@ -938,6 +931,10 @@ class SpotifyClientApiBuilder(
938931
"to build a SpotifyClientApi object"
939932
)
940933
}
934+
935+
if (options.testTokenValidity) SpotifyApi.testTokenValidity(api)
936+
937+
return api
941938
}
942939
}
943940

@@ -961,7 +958,8 @@ class SpotifyAppApiBuilder(
961958
val clientId = credentials.clientId
962959
val clientSecret = credentials.clientSecret
963960
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!" }
964-
return when {
961+
962+
val api = when {
965963
authorization.token != null -> SpotifyAppApi(
966964
clientId,
967965
clientSecret,
@@ -971,7 +969,6 @@ class SpotifyAppApiBuilder(
971969
options.automaticRefresh,
972970
options.retryWhenRateLimited,
973971
options.enableLogger,
974-
options.testTokenValidity,
975972
options.defaultLimit,
976973
options.allowBulkRequests,
977974
options.requestTimeoutMillis,
@@ -993,7 +990,6 @@ class SpotifyAppApiBuilder(
993990
options.automaticRefresh,
994991
options.retryWhenRateLimited,
995992
options.enableLogger,
996-
options.testTokenValidity,
997993
options.defaultLimit,
998994
options.allowBulkRequests,
999995
options.requestTimeoutMillis,
@@ -1013,7 +1009,6 @@ class SpotifyAppApiBuilder(
10131009
options.automaticRefresh,
10141010
options.retryWhenRateLimited,
10151011
options.enableLogger,
1016-
options.testTokenValidity,
10171012
options.defaultLimit,
10181013
options.allowBulkRequests,
10191014
options.requestTimeoutMillis,
@@ -1035,7 +1030,6 @@ class SpotifyAppApiBuilder(
10351030
options.automaticRefresh,
10361031
options.retryWhenRateLimited,
10371032
options.enableLogger,
1038-
options.testTokenValidity,
10391033
options.defaultLimit,
10401034
options.allowBulkRequests,
10411035
options.requestTimeoutMillis,
@@ -1050,6 +1044,10 @@ class SpotifyAppApiBuilder(
10501044
throw SpotifyException.AuthenticationException("Invalid credentials provided in the login process (clientId=$clientId, clientSecret=$clientSecret)", e)
10511045
}
10521046
}
1047+
1048+
if (options.testTokenValidity) SpotifyApi.testTokenValidity(api)
1049+
1050+
return api
10531051
}
10541052
}
10551053

0 commit comments

Comments
 (0)