commonMain.com.adamratzman.spotify.utils.Utils.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of spotify-api-kotlin-jvm Show documentation
Show all versions of spotify-api-kotlin-jvm Show documentation
A Kotlin wrapper for the Spotify Web API.
/* Spotify Web API, Kotlin Wrapper; MIT License, 2017-2021; Original author: Adam Ratzman */
package com.adamratzman.spotify.utils
import com.adamratzman.spotify.SpotifyException
import com.adamratzman.spotify.models.ResultEnum
import kotlinx.serialization.json.JsonElement
/**
* The current time in milliseconds since UNIX epoch.
*/
public expect fun getCurrentTimeMs(): Long
internal fun jsonMap(vararg pairs: Pair) = pairs.toMap().toMutableMap()
internal suspend inline fun catch(crossinline function: suspend () -> T): T? {
return try {
function()
} catch (e: SpotifyException.BadRequestException) {
if (e.statusCode !in listOf(400, 404)) throw e
// we should only ignore the exception if it's 400 or 404. Otherwise, it's a larger issue
null
}
}
internal fun Array.match(identifier: String) =
firstOrNull { it.retrieveIdentifier().toString().equals(identifier, true) }
internal expect fun formatDate(format: String, date: Long): String