Skip to content

Commit 45c4499

Browse files
committed
bump spek, ktor, coroutine, international dependency versions
1 parent 0cd6935 commit 45c4499

File tree

7 files changed

+62
-46
lines changed

7 files changed

+62
-46
lines changed

build.gradle.kts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
21
import org.jetbrains.dokka.gradle.DokkaTask
32
import org.jetbrains.kotlin.gradle.dsl.KotlinJsCompile
4-
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
53

64
plugins {
75
`maven-publish`
@@ -35,10 +33,10 @@ kotlin {
3533

3634
targets {
3735
sourceSets {
38-
val coroutineVersion = "1.3.2"
36+
val coroutineVersion = "1.3.3"
3937
val serializationVersion = "0.14.0"
40-
val spekVersion = "2.0.8"
41-
val ktorVersion = "1.2.6"
38+
val spekVersion = "2.0.9"
39+
val ktorVersion = "1.3.0-rc2"
4240

4341
val commonMain by getting {
4442
dependencies {
@@ -63,7 +61,6 @@ kotlin {
6361
}
6462

6563
dependencies {
66-
implementation("com.neovisionaries:nv-i18n:1.26")
6764
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutineVersion")
6865
implementation("org.jetbrains.kotlinx:kotlinx-serialization-runtime:$serializationVersion")
6966
implementation("io.ktor:ktor-client-apache:$ktorVersion")
@@ -75,7 +72,7 @@ kotlin {
7572
dependencies {
7673
implementation(kotlin("test"))
7774
implementation(kotlin("test-junit"))
78-
implementation("org.junit.jupiter:junit-jupiter:5.5.2")
75+
implementation("org.junit.jupiter:junit-jupiter:5.6.0-M1")
7976
implementation("org.spekframework.spek2:spek-dsl-jvm:$spekVersion")
8077
runtimeOnly("org.spekframework.spek2:spek-runner-junit5:$spekVersion")
8178
runtimeOnly(kotlin("reflect"))
@@ -84,7 +81,7 @@ kotlin {
8481

8582
val jsMain by getting {
8683
dependencies {
87-
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core-js:1.3.2")
84+
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core-js:$coroutineVersion")
8885
implementation("org.jetbrains.kotlinx:kotlinx-serialization-runtime-js:$serializationVersion")
8986
implementation("io.ktor:ktor-client-js:$ktorVersion")
9087
compileOnly(kotlin("stdlib-js"))

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

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@ import io.ktor.client.HttpClient
99
import io.ktor.client.features.ResponseException
1010
import io.ktor.client.request.HttpRequestBuilder
1111
import io.ktor.client.request.header
12+
import io.ktor.client.request.request
1213
import io.ktor.client.request.url
13-
import io.ktor.client.response.readText
14+
import io.ktor.client.statement.readText
1415
import io.ktor.client.utils.EmptyContent
1516
import io.ktor.http.ContentType
1617
import io.ktor.http.HttpMethod
1718
import io.ktor.http.content.ByteArrayContent
1819
import io.ktor.http.content.TextContent
20+
import io.ktor.utils.io.core.toByteArray
1921
import kotlinx.coroutines.CancellationException
2022
import kotlinx.coroutines.delay
21-
import kotlinx.io.core.toByteArray
22-
import kotlinx.io.core.use
2323

2424
enum class HttpRequestMethod(internal val externalMethod: HttpMethod) {
2525
GET(HttpMethod.Get),
@@ -33,13 +33,13 @@ internal data class HttpHeader(val key: String, val value: String)
3333
internal data class HttpResponse(val responseCode: Int, val body: String, val headers: List<HttpHeader>)
3434

3535
internal class HttpConnection constructor(
36-
private val url: String,
37-
private val method: HttpRequestMethod,
38-
private val bodyMap: Map<*, *>?,
39-
private val bodyString: String?,
40-
contentType: String?,
41-
private val headers: List<HttpHeader> = listOf(),
42-
val api: SpotifyApi<*, *>? = null
36+
private val url: String,
37+
private val method: HttpRequestMethod,
38+
private val bodyMap: Map<*, *>?,
39+
private val bodyString: String?,
40+
contentType: String?,
41+
private val headers: List<HttpHeader> = listOf(),
42+
val api: SpotifyApi<*, *>? = null
4343
) {
4444
private val contentType: ContentType = contentType?.let { ContentType.parse(it) } ?: ContentType.Application.Json
4545

@@ -82,45 +82,44 @@ internal class HttpConnection constructor(
8282
}
8383

8484
internal suspend fun execute(
85-
additionalHeaders: List<HttpHeader>? = null,
86-
retryIf502: Boolean = true
85+
additionalHeaders: List<HttpHeader>? = null,
86+
retryIf502: Boolean = true
8787
): HttpResponse {
8888
val httpRequest = buildRequest(additionalHeaders)
8989

9090
try {
91-
return client.execute(httpRequest).use {
92-
val resp = it.response
93-
val respCode = resp.status.value
91+
return client.request<io.ktor.client.statement.HttpResponse>(httpRequest).let { response ->
92+
val respCode = response.status.value
9493

9594
if (respCode == 502 && retryIf502) {
9695
api?.logger?.logError(
97-
false,
98-
"Received 502 (Invalid response) for URL $url and $this\nRetrying..",
99-
null
96+
false,
97+
"Received 502 (Invalid response) for URL $url and $this\nRetrying..",
98+
null
10099
)
101-
return@use execute(additionalHeaders, retryIf502 = false)
100+
return@let execute(additionalHeaders, retryIf502 = false)
102101
} else if (respCode == 502 && !retryIf502) {
103102
api?.logger?.logWarning("Recieved 502 (Invalid response) for URL $url and $this\nNot retrying")
104103
}
105104

106105
if (respCode == 429) {
107-
val ratelimit = resp.headers["Retry-After"]!!.toLong() + 1L
106+
val ratelimit = response.headers["Retry-After"]!!.toLong() + 1L
108107
if (api?.retryWhenRateLimited == true) {
109108
api.logger.logError(
110-
false,
111-
"The request ($url) was ratelimited for $ratelimit seconds at ${getCurrentTimeMs()}",
112-
null
109+
false,
110+
"The request ($url) was ratelimited for $ratelimit seconds at ${getCurrentTimeMs()}",
111+
null
113112
)
114113

115114
delay(ratelimit * 1000)
116-
return@use execute(additionalHeaders, retryIf502 = retryIf502)
115+
return@let execute(additionalHeaders, retryIf502 = retryIf502)
117116
} else throw SpotifyRatelimitedException(ratelimit)
118117
}
119118

120-
val body = resp.readText()
119+
val body = response.readText()
121120

122121
if (respCode == 401 && body.contains("access token") &&
123-
api != null && api.automaticRefresh
122+
api != null && api.automaticRefresh
124123
) {
125124
api.suspendRefreshToken()
126125
val newAdditionalHeaders = additionalHeaders?.toMutableList() ?: mutableListOf()
@@ -129,14 +128,14 @@ internal class HttpConnection constructor(
129128
}
130129

131130
return HttpResponse(
132-
responseCode = respCode,
133-
body = body,
134-
headers = resp.headers.entries().map { (key, value) ->
135-
HttpHeader(
136-
key,
137-
value.getOrNull(0) ?: "null"
138-
)
139-
}
131+
responseCode = respCode,
132+
body = body,
133+
headers = response.headers.entries().map { (key, value) ->
134+
HttpHeader(
135+
key,
136+
value.getOrNull(0) ?: "null"
137+
)
138+
}
140139
)
141140
}
142141
} catch (e: CancellationException) {

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

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

4-
import kotlinx.io.core.String
5-
import kotlinx.io.core.toByteArray
4+
import io.ktor.utils.io.core.String
5+
import io.ktor.utils.io.core.toByteArray
66

77
private val BASE64_ALPHABET: String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
88
private val BASE64_MASK: Byte = 0x3f

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,12 @@ val api = when {
4040
}
4141

4242
expect fun getEnvironmentVariable(name: String): String?
43+
44+
expect fun Exception.stackTrace()
45+
46+
fun block(code: () -> Unit) = try {
47+
code
48+
} catch (e: Exception) {
49+
e.stackTrace()
50+
throw e
51+
}

src/commonTest/kotlin/com.adamratzman/spotify/utilities/UtilityTests.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.adamratzman.spotify.utilities
33

44
import com.adamratzman.spotify.SpotifyClientAPI
55
import com.adamratzman.spotify.api
6+
import com.adamratzman.spotify.block
67
import com.adamratzman.spotify.getEnvironmentVariable
78
import com.adamratzman.spotify.spotifyAppApi
89
import com.adamratzman.spotify.spotifyClientApi
@@ -49,8 +50,11 @@ class UtilityTests : Spek({
4950
clientSecret = getEnvironmentVariable("SPOTIFY_CLIENT_SECRET")
5051
}
5152
}
52-
api.build()
53-
api.buildAsyncAt(GlobalScope) { }
53+
54+
block {
55+
api.build()
56+
api.buildAsyncAt(GlobalScope) { }
57+
}
5458
}
5559

5660
it("Refresh on invalid token") {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ actual fun getEnvironmentVariable(name: String): String? {
88
return null
99
}
1010

11+
actual fun Exception.stackTrace() = println(this)
12+
1113
@Test
1214
fun test() {
1315
println("asdf")

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,8 @@ package com.adamratzman.spotify
44
actual fun getEnvironmentVariable(name: String): String? {
55
return System.getenv(name) ?: System.getProperty(name)
66
}
7+
8+
actual fun Exception.stackTrace() {
9+
println(this.stackTrace.joinToString("\n") { it.toString() })
10+
this.printStackTrace()
11+
}

0 commit comments

Comments
 (0)