Skip to content

Commit a01b229

Browse files
authored
Merge pull request #244 from adamint/dev
remove kotlinx-datetime dependency from all targets, add klock to native
2 parents aa800c0 + 6b32bb2 commit a01b229

File tree

16 files changed

+117
-55
lines changed

16 files changed

+117
-55
lines changed

build.gradle.kts

Lines changed: 37 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ System.getenv("signing.keyId")?.let { project.ext["signing.keyId"] = it }
3838
System.getenv("signing.password")?.let { project.ext["signing.password"] = it }
3939
System.getenv("signing.secretKeyRingFile")?.let { project.ext["signing.secretKeyRingFile"] = it }
4040

41-
4241
tasks.withType<Test> {
4342
this.testLogging {
4443
this.showStandardStreams = true
@@ -173,55 +172,22 @@ kotlin {
173172
listOf(jvm(), js()).map { it.name } + "kotlinMultiplatform"
174173

175174
publishing {
176-
publications {
177-
matching { it.name in publicationsFromMainHost }.all {
178-
val targetPublication = this@all
179-
tasks.withType<AbstractPublishToMaven>()
180-
.matching { it.publication == targetPublication }
181-
.configureEach { onlyIf { findProperty("isMainHost") == "true" } }
182-
}
183-
184-
val kotlinMultiplatform by getting(MavenPublication::class) {
185-
artifactId = "spotify-api-kotlin-core"
186-
setupPom(artifactId)
187-
}
188-
189-
/*val metadata by getting(MavenPublication::class) {
190-
artifactId = "spotify-api-kotlin-metadata"
191-
setupPom(artifactId)
192-
}*/
193-
}
194-
195-
repositories {
196-
maven {
197-
name = "nexus"
198-
val releasesRepoUrl = "https://oss.sonatype.org/service/local/staging/deploy/maven2/"
199-
val snapshotsRepoUrl = "https://oss.sonatype.org/content/repositories/snapshots/"
200-
url = uri(if (version.toString().endsWith("SNAPSHOT")) snapshotsRepoUrl else releasesRepoUrl)
201-
202-
credentials {
203-
val nexusUsername: String? = System.getenv("nexus.username") ?: project.extra["nexusUsername"] as? String
204-
val nexusPassword: String? = System.getenv("nexus.password") ?: project.extra["nexusPassword"] as? String
205-
username = nexusUsername
206-
password = nexusPassword
207-
}
208-
}
209-
}
175+
if ("local" !in (version as String)) registerPublishing()
210176
}
211177

212178
targets {
213179
sourceSets {
214180
val coroutineVersion = "1.4.2-native-mt"
215181
val serializationVersion = "1.0.1"
216182
val ktorVersion = "1.5.0"
217-
val kotlinxDatetimeVersion = "0.1.1"
183+
val klockVersion = "2.0.3"
218184

219185
val commonMain by getting {
220186
dependencies {
221187
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutineVersion")
222188
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:$serializationVersion")
223189
implementation("io.ktor:ktor-client-core:$ktorVersion")
224-
implementation("org.jetbrains.kotlinx:kotlinx-datetime:$kotlinxDatetimeVersion")
190+
implementation("com.soywiz.korlibs.klock:klock:$klockVersion")
225191
}
226192
}
227193

@@ -296,9 +262,7 @@ kotlin {
296262
}
297263

298264
val desktopTest by creating {
299-
dependencies {
300-
301-
}
265+
dependsOn(commonTest)
302266
}
303267

304268
val linuxX64Main by getting {
@@ -332,6 +296,10 @@ kotlin {
332296
}
333297
}
334298

299+
publishing {
300+
if ("local" in (version as String)) registerPublishing()
301+
}
302+
335303
signing {
336304
if (project.hasProperty("signing.keyId")
337305
&& project.hasProperty("signing.password")
@@ -410,3 +378,32 @@ fun MavenPublication.setupPom(publicationName: String) {
410378
}
411379
}
412380

381+
fun PublishingExtension.registerPublishing() {
382+
publications {
383+
val kotlinMultiplatform by getting(MavenPublication::class) {
384+
artifactId = "spotify-api-kotlin-core"
385+
setupPom(artifactId)
386+
}
387+
388+
/*val metadata by getting(MavenPublication::class) {
389+
artifactId = "spotify-api-kotlin-metadata"
390+
setupPom(artifactId)
391+
}*/
392+
}
393+
394+
repositories {
395+
maven {
396+
name = "nexus"
397+
val releasesRepoUrl = "https://oss.sonatype.org/service/local/staging/deploy/maven2/"
398+
val snapshotsRepoUrl = "https://oss.sonatype.org/content/repositories/snapshots/"
399+
url = uri(if (version.toString().endsWith("SNAPSHOT")) snapshotsRepoUrl else releasesRepoUrl)
400+
401+
credentials {
402+
val nexusUsername: String? = System.getenv("nexus.username") ?: if (project.extra.has("nexusUsername")) project.extra["nexusUsername"] as? String else null
403+
val nexusPassword: String? = System.getenv("nexus.password") ?: if (project.extra.has("nexusPassword")) project.extra["nexusPassword"] as? String else null
404+
username = nexusUsername
405+
password = nexusPassword
406+
}
407+
}
408+
}
409+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
/* Spotify Web API, Kotlin Wrapper; MIT License, 2017-2021; Original author: Adam Ratzman */
22
package com.adamratzman.spotify.utils
33

4+
import android.annotation.SuppressLint
5+
import java.text.SimpleDateFormat
6+
import java.util.Date
7+
48
/**
59
* The current time in milliseconds since UNIX epoch.
610
*/
711
public actual fun getCurrentTimeMs(): Long = System.currentTimeMillis()
12+
13+
@SuppressLint("SimpleDateFormat")
14+
internal actual fun formatDate(format: String, date: Long): String {
15+
return SimpleDateFormat(format).format(Date(date))
16+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ package com.adamratzman.spotify.utils
44
/**
55
* Actual platform that this program is run on.
66
*/
7-
public actual val platform: Platform = Platform.ANDROID
7+
public actual val currentApiPlatform: Platform = Platform.ANDROID

src/commonMain/kotlin/com.adamratzman.spotify/endpoints/public/BrowseApi.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public class BrowseApi(api: GenericSpotifyApi) : SpotifyEndpoint(api) {
101101
endpointBuilder("/browse/featured-playlists").with("limit", limit).with("offset", offset).with(
102102
"market",
103103
market?.name
104-
).with("locale", locale).with("timestamp", timestamp?.let { formatDate(it) }).toString()
104+
).with("locale", locale).with("timestamp", timestamp?.let { formatDate("yyyy-MM-dd'T'HH:mm:ss", it) }).toString()
105105
).toObject(FeaturedPlaylists.serializer(), api, json)
106106

107107
/**

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,29 @@ public class HttpConnection constructor(
144144
} catch (e: ResponseException) {
145145
val errorBody = e.response.readText()
146146
try {
147+
if (e.response.status.value == 429) {
148+
val ratelimit = e.response.headers["Retry-After"]!!.toLong() + 1L
149+
if (api?.spotifyApiOptions?.retryWhenRateLimited == true) {
150+
api.logger.logError(
151+
false,
152+
"The request ($url) was ratelimited for $ratelimit seconds at ${getCurrentTimeMs()}",
153+
null
154+
)
155+
156+
delay(ratelimit * 1000)
157+
return execute(additionalHeaders, retryIfInternalServerError = retryIfInternalServerError)
158+
} else throw SpotifyRatelimitedException(ratelimit)
159+
}
160+
161+
if (e.response.status.value == 401 && errorBody.contains("access token") &&
162+
api != null && api.spotifyApiOptions.automaticRefresh
163+
) {
164+
api.refreshToken()
165+
val newAdditionalHeaders = additionalHeaders?.toMutableList() ?: mutableListOf()
166+
newAdditionalHeaders.add(0, HttpHeader("Authorization", "Bearer ${api.token.accessToken}"))
167+
return execute(newAdditionalHeaders, retryIfInternalServerError)
168+
}
169+
147170
val error = errorBody.toObject(
148171
ErrorResponse.serializer(),
149172
api,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ public enum class Platform {
1111
NATIVE
1212
}
1313

14-
public expect val platform: Platform
14+
public expect val currentApiPlatform: Platform

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@ package com.adamratzman.spotify.utils
33

44
import com.adamratzman.spotify.SpotifyException
55
import com.adamratzman.spotify.models.ResultEnum
6-
import kotlinx.datetime.Instant
7-
import kotlinx.datetime.TimeZone
8-
import kotlinx.datetime.toLocalDateTime
96
import kotlinx.serialization.json.JsonElement
107

118
/**
@@ -28,5 +25,4 @@ internal suspend inline fun <T> catch(crossinline function: suspend () -> T): T?
2825
internal fun <T : ResultEnum> Array<T>.match(identifier: String) =
2926
firstOrNull { it.retrieveIdentifier().toString().equals(identifier, true) }
3027

31-
internal fun formatDate(date: Long): String =
32-
Instant.fromEpochMilliseconds(date).toLocalDateTime(TimeZone.currentSystemDefault()).toString()
28+
internal expect fun formatDate(format: String, date: Long): String

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import com.adamratzman.spotify.models.Playlist
1010
import com.adamratzman.spotify.models.SimplePlaylist
1111
import com.adamratzman.spotify.runBlockingTest
1212
import com.adamratzman.spotify.utils.Platform
13-
import com.adamratzman.spotify.utils.platform
13+
import com.adamratzman.spotify.utils.currentApiPlatform
1414
import kotlin.test.Test
1515
import kotlin.test.assertEquals
1616
import kotlin.test.assertTrue
@@ -99,7 +99,7 @@ class ClientPlaylistApiTest {
9999

100100
@Test
101101
fun testEditPlaylists() {
102-
if (platform != Platform.NATIVE) {
102+
if (currentApiPlatform != Platform.NATIVE) {
103103
runBlockingTest {
104104
if (!testPrereq()) return@runBlockingTest
105105

@@ -114,7 +114,7 @@ class ClientPlaylistApiTest {
114114
"7FjZU7XFs7P9jHI9Z0yRhK"
115115
)
116116

117-
if (platform !in listOf(Platform.ANDROID, Platform.NATIVE)) {
117+
if (currentApiPlatform !in listOf(Platform.ANDROID, Platform.NATIVE)) {
118118
api.playlists.uploadClientPlaylistCover(
119119
createdPlaylist.id,
120120
imageUrl = "https://developer.spotify.com/assets/WebAPI_intro.png"
@@ -150,7 +150,7 @@ class ClientPlaylistApiTest {
150150

151151
@Test
152152
fun testRemovePlaylistTracks() {
153-
if (platform != Platform.NATIVE) {
153+
if (currentApiPlatform != Platform.NATIVE) {
154154
runBlockingTest {
155155
if (!testPrereq()) return@runBlockingTest
156156

src/desktopMain/kotlin/com/adamratzman/spotify/utils/Platform.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ package com.adamratzman.spotify.utils
44
/**
55
* Actual platform that this program is run on.
66
*/
7-
public actual val platform: Platform = Platform.NATIVE
7+
public actual val currentApiPlatform: Platform = Platform.NATIVE

src/jsMain/kotlin/com/adamratzman/spotify/utils/Misc.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,7 @@ import kotlin.js.Date
99
public actual fun getCurrentTimeMs(): Long {
1010
return Date().getTime().toLong()
1111
}
12+
13+
internal actual fun formatDate(format: String, date: Long): String {
14+
return Date(date).toISOString()
15+
}

0 commit comments

Comments
 (0)