Skip to content

Commit d50cc64

Browse files
committed
Use modern enum entries call vs values(), update test settings, coerce input values for total
1 parent c7a2dbc commit d50cc64

File tree

15 files changed

+29
-22
lines changed

15 files changed

+29
-22
lines changed

src/commonJvmLikeTest/kotlin/com/adamratzman/spotify/CommonImpl.kt

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ private suspend fun buildSpotifyApiInternal(): GenericSpotifyApi? {
3838

3939
val optionsCreator: (SpotifyApiOptions.() -> Unit) = {
4040
this.enableDebugMode = logHttp
41+
retryOnInternalServerErrorTimes = 0
4142
}
4243

4344
return when {

src/commonMain/kotlin/com.adamratzman.spotify/endpoints/pub/EpisodeApi.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.adamratzman.spotify.endpoints.pub
33

44
import com.adamratzman.spotify.GenericSpotifyApi
55
import com.adamratzman.spotify.SpotifyAppApi
6+
import com.adamratzman.spotify.SpotifyClientApi
67
import com.adamratzman.spotify.SpotifyException.BadRequestException
78
import com.adamratzman.spotify.SpotifyScope
89
import com.adamratzman.spotify.http.SpotifyEndpoint
@@ -60,7 +61,7 @@ public open class EpisodeApi(api: GenericSpotifyApi) : SpotifyEndpoint(api) {
6061
* Users can view the country that is associated with their account in the account settings. Required for [SpotifyAppApi], but **you may use [Market.FROM_TOKEN] to get the user market**
6162
*
6263
* @return List of possibly-null [Episode] objects.
63-
* @throws BadRequestException If any invalid show id is provided
64+
* @throws BadRequestException If any invalid show id is provided, if this is a [SpotifyClientApi]
6465
*/
6566
public suspend fun getEpisodes(vararg ids: String, market: Market): List<Episode?> {
6667
checkBulkRequesting(50, ids.size)

src/commonMain/kotlin/com.adamratzman.spotify/endpoints/pub/SearchApi.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ public open class SearchApi(api: GenericSpotifyApi) : SpotifyEndpoint(api) {
338338
market: Market,
339339
language: Language? = null
340340
): SpotifySearchResult =
341-
search(query, filters = filters, searchTypes = SearchType.values(), limit = limit, offset = offset, market = market)
341+
search(query, filters = filters, searchTypes = SearchType.entries.toTypedArray(), limit = limit, offset = offset, market = market, language = language)
342342

343343
protected fun build(
344344
query: String,

src/commonMain/kotlin/com.adamratzman.spotify/models/Albums.kt

+4-4
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,14 @@ public data class SimpleAlbum(
5454

5555
val albumType: AlbumResultType
5656
get() = albumTypeString.let { _ ->
57-
AlbumResultType.values().first { it.id.equals(albumTypeString, true) }
57+
AlbumResultType.entries.first { it.id.equals(albumTypeString, true) }
5858
}
5959

6060
val releaseDate: ReleaseDate? get() = releaseDateString?.let { getReleaseDate(releaseDateString) }
6161

6262
val albumGroup: AlbumResultType?
6363
get() = albumGroupString?.let { _ ->
64-
AlbumResultType.values().find { it.id == albumGroupString }
64+
AlbumResultType.entries.find { it.id == albumGroupString }
6565
}
6666

6767
/**
@@ -157,7 +157,7 @@ public data class Album(
157157

158158
val externalIds: List<ExternalId> get() = externalIdsString.map { ExternalId(it.key, it.value) }
159159

160-
val albumType: AlbumResultType get() = AlbumResultType.values().first { it.id == albumTypeString }
160+
val albumType: AlbumResultType get() = AlbumResultType.entries.first { it.id == albumTypeString }
161161

162162
val releaseDate: ReleaseDate get() = getReleaseDate(releaseDateString)
163163

@@ -181,7 +181,7 @@ public data class SpotifyCopyright(
181181
.removePrefix("(C)")
182182
.trim()
183183

184-
val type: CopyrightType get() = CopyrightType.values().match(typeString)!!
184+
val type: CopyrightType get() = CopyrightType.entries.toTypedArray().match(typeString)!!
185185
}
186186

187187
@Serializable

src/commonMain/kotlin/com.adamratzman.spotify/models/Authentication.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public data class Token(
3333
val expiresAt: Long get() = getCurrentTimeMs() + expiresIn * 1000
3434

3535
val scopes: List<SpotifyScope>? get() = scopeString?.let { str ->
36-
str.split(" ").mapNotNull { scope -> SpotifyScope.values().find { it.uri.equals(scope, true) } }
36+
str.split(" ").mapNotNull { scope -> SpotifyScope.entries.find { it.uri.equals(scope, true) } }
3737
}
3838

3939
public fun shouldRefresh(): Boolean = getCurrentTimeMs() > expiresAt

src/commonMain/kotlin/com.adamratzman.spotify/models/Player.kt

+5-5
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public data class SpotifyContext(
2424
val uri: ContextUri,
2525
@SerialName("type") val typeString: String
2626
) {
27-
val type: SpotifyContextType get() = SpotifyContextType.values().match(typeString)!!
27+
val type: SpotifyContextType get() = SpotifyContextType.entries.toTypedArray().match(typeString)!!
2828
val externalUrls: List<ExternalUrl> get() = getExternalUrls(externalUrlsString)
2929
}
3030

@@ -64,7 +64,7 @@ public data class Device(
6464
@SerialName("type") val typeString: String,
6565
@SerialName("volume_percent") val volumePercent: Int
6666
) : IdentifiableNullable() {
67-
val type: DeviceType get() = DeviceType.values().first { it.identifier.equals(typeString, true) }
67+
val type: DeviceType get() = DeviceType.entries.first { it.identifier.equals(typeString, true) }
6868

6969
override val href: String? = null
7070
}
@@ -118,7 +118,7 @@ public data class CurrentlyPlayingContext(
118118
val context: SpotifyContext? = null
119119
) {
120120
val repeatState: ClientPlayerApi.PlayerRepeatState
121-
get() = ClientPlayerApi.PlayerRepeatState.values().match(repeatStateString)!!
121+
get() = ClientPlayerApi.PlayerRepeatState.entries.toTypedArray().match(repeatStateString)!!
122122
}
123123

124124
/**
@@ -146,7 +146,7 @@ public data class CurrentlyPlayingObject(
146146
val actions: PlaybackActions
147147
) {
148148
val currentlyPlayingType: CurrentlyPlayingType
149-
get() = CurrentlyPlayingType.values().match(currentlyPlayingTypeString)!!
149+
get() = CurrentlyPlayingType.entries.toTypedArray().match(currentlyPlayingTypeString)!!
150150
}
151151

152152
/**
@@ -162,7 +162,7 @@ public data class PlaybackActions(
162162
val disallows: List<DisallowablePlaybackAction>
163163
get() = disallowsString.map {
164164
DisallowablePlaybackAction(
165-
PlaybackAction.values().match(it.key)!!,
165+
PlaybackAction.entries.toTypedArray().match(it.key)!!,
166166
it.value ?: false
167167
)
168168
}

src/commonMain/kotlin/com.adamratzman.spotify/models/Playlist.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public data class Playlist(
150150
@Serializable
151151
public data class PlaylistTrackInfo(
152152
val href: String,
153-
val total: Int
153+
val total: Int? = null
154154
)
155155

156156
@Serializable

src/commonMain/kotlin/com.adamratzman.spotify/models/serialization/SerializationUtils.kt

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ internal val nonstrictJson =
2323
ignoreUnknownKeys = true
2424
allowSpecialFloatingPointValues = true
2525
useArrayPolymorphism = true
26+
coerceInputValues = true
2627
}
2728

2829
// Parse function that catches on parse exception

src/commonMain/kotlin/com.adamratzman.spotify/utils/Locale.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -806,7 +806,7 @@ public enum class Locale(public val language: Language, public val country: Mark
806806

807807
public companion object {
808808
public fun from(language: Language, country: Market): Locale? {
809-
return values().find { locale -> locale.language == language && locale.country == country }
809+
return entries.find { locale -> locale.language == language && locale.country == country }
810810
}
811811
}
812812
}

src/commonMain/kotlin/com.adamratzman.spotify/utils/Utils.kt

+3-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ internal suspend inline fun <T> catch(catchInternalServerError: Boolean = false,
2121
return try {
2222
function()
2323
} catch (e: SpotifyException.BadRequestException) {
24-
if (e.statusCode !in listOf(400, 404) && (e.statusCode !in 500..599) || catchInternalServerError) throw e
24+
if (e.statusCode !in listOf(400, 404)) throw e
25+
else if (e.statusCode in 500..599 && catchInternalServerError) throw e
26+
2527
// we should only ignore the exception if it's 400 or 404. Otherwise, it's a larger issue
2628
null
2729
}

src/commonTest/kotlin/com.adamratzman/spotify/AbstractTest.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ abstract class AbstractTest<T : GenericSpotifyApi> {
3737
return if (apiInitialized) {
3838
true
3939
} else {
40-
println("Api is not initialized. buildSpotifyApi returns ${buildSpotifyApi(testClassQualifiedName, "n/a")}")
40+
println("Api is not initialized or does not match the expected type. buildSpotifyApi returns ${buildSpotifyApi(testClassQualifiedName, "n/a")}")
4141
false
4242
}
4343
}

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ class ClientEpisodeApiTest : AbstractTest<SpotifyClientApi>() {
3030
buildApi<SpotifyClientApi>(::testGetEpisodes.name)
3131
if (!isApiInitialized()) return@runTestOnDefaultDispatcher
3232

33-
assertEquals(listOf(null, null), api.episodes.getEpisodes("hi", "dad"))
34-
assertEquals(null, api.episodes.getEpisodes("1cfOhXP4GQCd5ZFHoSF8gg", "j")[1])
33+
assertFailsWith<BadRequestException> { api.episodes.getEpisodes("hi", "dad") }
34+
assertFailsWith<BadRequestException> { api.episodes.getEpisodes("1cfOhXP4GQCd5ZFHoSF8gg", "j")[1] }
35+
3536
assertEquals(
3637
listOf("The Great Inflation (Classic)"),
3738
api.episodes.getEpisodes("3lMZTE81Pbrp0U12WZe27l").map { it?.name }

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ class ClientPlaylistApiTest : AbstractTest<SpotifyClientApi>() {
8585

8686
api.spotifyApiOptions.allowBulkRequests = true
8787

88-
suspend fun calculatePlaylistSize() = api.playlists.getClientPlaylist(createdPlaylist!!.id)!!.tracks.total
89-
val sizeBefore = calculatePlaylistSize()
88+
suspend fun calculatePlaylistSize(): Int? = api.playlists.getClientPlaylist(createdPlaylist!!.id)!!.tracks.total
89+
val sizeBefore = calculatePlaylistSize() ?: 0
9090
api.playlists.addPlayablesToClientPlaylist(createdPlaylist!!.id, playables = tracks.toTypedArray())
9191
assertEquals(sizeBefore + tracks.size, calculatePlaylistSize())
9292
api.playlists.removePlayablesFromClientPlaylist(createdPlaylist!!.id, playables = tracks.toTypedArray())
@@ -98,6 +98,7 @@ class ClientPlaylistApiTest : AbstractTest<SpotifyClientApi>() {
9898
}
9999

100100
@Test
101+
@Ignore // ignored because Spotify currently broke the ability to change `public` field
101102
fun testEditPlaylists(): TestResult = runTestOnDefaultDispatcher {
102103
buildApi<SpotifyClientApi>(::testEditPlaylists.name)
103104
if (!isApiInitialized()) return@runTestOnDefaultDispatcher

src/commonTest/kotlin/com.adamratzman/spotify/pub/SearchApiTest.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class SearchApiTest : AbstractTest<GenericSpotifyApi>() {
2323
fun testSearchMultiple(): TestResult = runTestOnDefaultDispatcher {
2424
buildApi(::testSearchMultiple.name)
2525

26-
val query = api.search.search("lo", *SearchApi.SearchType.values(), market = Market.US)
26+
val query = api.search.search("lo", *SearchApi.SearchType.entries.toTypedArray(), market = Market.US)
2727
assertTrue(
2828
query.albums?.items?.isNotEmpty() == true && query.tracks?.items?.isNotEmpty() == true && query.artists?.items?.isNotEmpty() == true &&
2929
query.playlists?.items?.isNotEmpty() == true && query.shows?.items?.isNotEmpty() == true && query.episodes?.items?.isNotEmpty() == true

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class PkceTest {
2727

2828
println(
2929
getSpotifyPkceAuthorizationUrl(
30-
*SpotifyScope.values(),
30+
*SpotifyScope.entries.toTypedArray(),
3131
clientId = clientId,
3232
redirectUri = serverRedirectUri,
3333
codeChallenge = getSpotifyPkceCodeChallenge(pkceCodeVerifier),

0 commit comments

Comments
 (0)