Skip to content

Commit 5e9210a

Browse files
committed
remove testTokenValidity SpotifyApi param
parse auth errors occuring during other requests, lint bump readme version
1 parent 2d29e60 commit 5e9210a

File tree

9 files changed

+33
-29
lines changed

9 files changed

+33
-29
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.12'
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ buildscript {
3131
}
3232

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

3636
tasks.withType<Test> {
3737
this.testLogging {

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: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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,
@@ -826,7 +825,6 @@ class SpotifyClientApiBuilder(
826825
options.automaticRefresh,
827826
options.retryWhenRateLimited,
828827
options.enableLogger,
829-
options.testTokenValidity,
830828
options.defaultLimit,
831829
options.allowBulkRequests,
832830
options.requestTimeoutMillis,
@@ -870,7 +868,6 @@ class SpotifyClientApiBuilder(
870868
options.automaticRefresh,
871869
options.retryWhenRateLimited,
872870
options.enableLogger,
873-
options.testTokenValidity,
874871
options.defaultLimit,
875872
options.allowBulkRequests,
876873
options.requestTimeoutMillis,
@@ -895,7 +892,6 @@ class SpotifyClientApiBuilder(
895892
options.automaticRefresh,
896893
options.retryWhenRateLimited,
897894
options.enableLogger,
898-
options.testTokenValidity,
899895
options.defaultLimit,
900896
options.allowBulkRequests,
901897
options.requestTimeoutMillis,
@@ -921,7 +917,6 @@ class SpotifyClientApiBuilder(
921917
false,
922918
options.retryWhenRateLimited,
923919
options.enableLogger,
924-
options.testTokenValidity,
925920
options.defaultLimit,
926921
options.allowBulkRequests,
927922
options.requestTimeoutMillis,
@@ -974,7 +969,6 @@ class SpotifyAppApiBuilder(
974969
options.automaticRefresh,
975970
options.retryWhenRateLimited,
976971
options.enableLogger,
977-
options.testTokenValidity,
978972
options.defaultLimit,
979973
options.allowBulkRequests,
980974
options.requestTimeoutMillis,
@@ -996,7 +990,6 @@ class SpotifyAppApiBuilder(
996990
options.automaticRefresh,
997991
options.retryWhenRateLimited,
998992
options.enableLogger,
999-
options.testTokenValidity,
1000993
options.defaultLimit,
1001994
options.allowBulkRequests,
1002995
options.requestTimeoutMillis,
@@ -1016,7 +1009,6 @@ class SpotifyAppApiBuilder(
10161009
options.automaticRefresh,
10171010
options.retryWhenRateLimited,
10181011
options.enableLogger,
1019-
options.testTokenValidity,
10201012
options.defaultLimit,
10211013
options.allowBulkRequests,
10221014
options.requestTimeoutMillis,
@@ -1038,7 +1030,6 @@ class SpotifyAppApiBuilder(
10381030
options.automaticRefresh,
10391031
options.retryWhenRateLimited,
10401032
options.enableLogger,
1041-
options.testTokenValidity,
10421033
options.defaultLimit,
10431034
options.allowBulkRequests,
10441035
options.requestTimeoutMillis,

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

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ sealed class SpotifyApi<T : SpotifyApi<T, B>, B : ISpotifyApiBuilder<T, B>>(
7676
var automaticRefresh: Boolean,
7777
var retryWhenRateLimited: Boolean,
7878
enableLogger: Boolean,
79-
testTokenValidity: Boolean,
8079
var defaultLimit: Int,
8180
var allowBulkRequests: Boolean,
8281
var requestTimeoutMillis: Long?,
@@ -370,7 +369,6 @@ class SpotifyAppApi internal constructor(
370369
automaticRefresh: Boolean,
371370
retryWhenRateLimited: Boolean,
372371
enableLogger: Boolean,
373-
testTokenValidity: Boolean,
374372
defaultLimit: Int,
375373
allowBulkRequests: Boolean,
376374
requestTimeoutMillis: Long?,
@@ -387,7 +385,6 @@ class SpotifyAppApi internal constructor(
387385
automaticRefresh,
388386
retryWhenRateLimited,
389387
enableLogger,
390-
testTokenValidity,
391388
defaultLimit,
392389
allowBulkRequests,
393390
requestTimeoutMillis,
@@ -410,7 +407,6 @@ class SpotifyAppApi internal constructor(
410407
options.automaticRefresh,
411408
options.retryWhenRateLimited,
412409
options.enableLogger,
413-
options.testTokenValidity,
414410
options.defaultLimit,
415411
options.allowBulkRequests,
416412
options.requestTimeoutMillis,
@@ -491,7 +487,6 @@ open class SpotifyClientApi(
491487
automaticRefresh: Boolean,
492488
retryWhenRateLimited: Boolean,
493489
enableLogger: Boolean,
494-
testTokenValidity: Boolean,
495490
defaultLimit: Int,
496491
allowBulkRequests: Boolean,
497492
requestTimeoutMillis: Long?,
@@ -509,7 +504,6 @@ open class SpotifyClientApi(
509504
automaticRefresh,
510505
retryWhenRateLimited,
511506
enableLogger,
512-
testTokenValidity,
513507
defaultLimit,
514508
allowBulkRequests,
515509
requestTimeoutMillis,
@@ -535,7 +529,6 @@ open class SpotifyClientApi(
535529
options.automaticRefresh,
536530
options.retryWhenRateLimited,
537531
options.enableLogger,
538-
options.testTokenValidity,
539532
options.defaultLimit,
540533
options.allowBulkRequests,
541534
options.requestTimeoutMillis,
@@ -763,7 +756,6 @@ class SpotifyImplicitGrantApi(
763756
cacheLimit: Int?,
764757
retryWhenRateLimited: Boolean,
765758
enableLogger: Boolean,
766-
testTokenValidity: Boolean,
767759
defaultLimit: Int,
768760
allowBulkRequests: Boolean,
769761
requestTimeoutMillis: Long?,
@@ -779,7 +771,6 @@ class SpotifyImplicitGrantApi(
779771
false,
780772
retryWhenRateLimited,
781773
enableLogger,
782-
testTokenValidity,
783774
defaultLimit,
784775
allowBulkRequests,
785776
requestTimeoutMillis,

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ sealed class SpotifyException(message: String, cause: Throwable? = null) : Excep
3939

4040
class ParseException(message: String, cause: Throwable? = null) : SpotifyException(message, cause)
4141

42-
class AuthenticationException(message: String, cause: Throwable? = null) : SpotifyException(message, cause)
42+
class AuthenticationException(message: String, cause: Throwable? = null) : SpotifyException(message, cause) {
43+
constructor(authenticationError: AuthenticationError) :
44+
this("Authentication error: ${authenticationError.error}. Description: ${authenticationError.description}")
45+
}
4346

4447
class TimeoutException(message: String, cause: Throwable? = null) : SpotifyException(message, cause)
4548
}

src/commonMain/kotlin/com.adamratzman.spotify/http/HttpConnection.kt

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
package com.adamratzman.spotify.http
33

44
import com.adamratzman.spotify.GenericSpotifyApi
5-
import com.adamratzman.spotify.SpotifyException
5+
import com.adamratzman.spotify.SpotifyException.AuthenticationException
6+
import com.adamratzman.spotify.SpotifyException.BadRequestException
7+
import com.adamratzman.spotify.SpotifyException.ParseException
8+
import com.adamratzman.spotify.models.AuthenticationError
69
import com.adamratzman.spotify.models.ErrorResponse
710
import com.adamratzman.spotify.models.SpotifyRatelimitedException
811
import com.adamratzman.spotify.models.serialization.nonstrictJson
@@ -119,7 +122,6 @@ class HttpConnection constructor(
119122
}
120123

121124
val body = response.readText()
122-
123125
if (respCode == 401 && body.contains("access token") &&
124126
api != null && api.automaticRefresh
125127
) {
@@ -144,8 +146,13 @@ class HttpConnection constructor(
144146
throw e
145147
} catch (e: ResponseException) {
146148
val errorBody = e.response.readText()
147-
val error = errorBody.toObject(ErrorResponse.serializer(), api, api?.json ?: nonstrictJson).error
148-
throw SpotifyException.BadRequestException(error.copy(reason = (error.reason ?: "") + " URL: $url"))
149+
try {
150+
val error = errorBody.toObject(ErrorResponse.serializer(), api, api?.json ?: nonstrictJson).error
151+
throw BadRequestException(error.copy(reason = (error.reason ?: "") + " URL: $url"))
152+
} catch (ignored: ParseException) {
153+
val error = errorBody.toObject(AuthenticationError.serializer(), api, api?.json ?: nonstrictJson)
154+
throw AuthenticationException(error)
155+
}
149156
}
150157
}
151158

src/commonTest/kotlin/com.adamratzman/spotify/priv/EpisodeApiTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class EpisodeApiTest : Spek({
2828
assertFailsWith<BadRequestException> { t.getEpisodes("1cfOhXP4GQCd5ZFHoSF8gg", "j").complete().map { it?.name } }
2929
}
3030
it("known episodes") {
31-
assertEquals(listOf("The 'Murder Hornets' And The Honey Bees"), t.getEpisodes( "4IhgnOc8rwMW70agMWVVfh").complete().map { it?.name })
31+
assertEquals(listOf("The 'Murder Hornets' And The Honey Bees"), t.getEpisodes("4IhgnOc8rwMW70agMWVVfh").complete().map { it?.name })
3232
}
3333
}
3434
}

src/jvmTest/kotlin/com/adamratzman/spotify/PkceTest.kt

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
/* Spotify Web API, Kotlin Wrapper; MIT License, 2017-2020; Original author: Adam Ratzman */
22
package com.adamratzman.spotify
33

4+
import com.adamratzman.spotify.SpotifyException.AuthenticationException
45
import kotlin.random.Random
6+
import kotlin.test.assertFailsWith
57
import org.spekframework.spek2.Spek
68
import org.spekframework.spek2.style.specification.describe
79
import spark.Spark.exception
@@ -45,10 +47,21 @@ class PkceTest : Spek({
4547
testTokenValidity = true
4648
)
4749
).build()
48-
println(api.token)
50+
val token = api.token.copy(expiresIn = -1)
4951
api.refreshToken()
50-
println(api.token)
52+
// test that using same token will fail with auth exception
53+
54+
assertFailsWith<AuthenticationException> {
55+
spotifyClientPkceApi(
56+
_clientId,
57+
serverRedirectUri,
58+
token,
59+
pkceCodeVerifier
60+
).build().library.getSavedTracks().complete()
61+
}
62+
5163
val username = api.users.getClientProfile().complete().displayName
64+
5265
stop = true
5366
"Successfully authenticated $username with PKCE and refreshed the token."
5467
} else "err."

0 commit comments

Comments
 (0)