@@ -11,14 +11,14 @@ import com.adamratzman.spotify.models.ErrorResponse
11
11
import com.adamratzman.spotify.models.SpotifyRatelimitedException
12
12
import com.adamratzman.spotify.models.serialization.nonstrictJson
13
13
import com.adamratzman.spotify.models.serialization.toObject
14
+ import com.soywiz.korio.dynamic.KDynamic.Companion.toLong
14
15
import io.ktor.client.HttpClient
15
- import io.ktor.client.plugins .ResponseException
16
+ import io.ktor.client.features .ResponseException
16
17
import io.ktor.client.request.HttpRequestBuilder
17
18
import io.ktor.client.request.header
18
19
import io.ktor.client.request.request
19
- import io.ktor.client.request.setBody
20
20
import io.ktor.client.request.url
21
- import io.ktor.client.statement.bodyAsText
21
+ import io.ktor.client.statement.readText
22
22
import io.ktor.http.ContentType
23
23
import io.ktor.http.HttpMethod
24
24
import io.ktor.http.content.ByteArrayContent
@@ -62,21 +62,19 @@ public class HttpConnection constructor(
62
62
url(this @HttpConnection.url)
63
63
method = this @HttpConnection.method.externalMethod
64
64
65
- setBody(
66
- when (this @HttpConnection.method) {
67
- HttpRequestMethod .DELETE -> {
68
- bodyString.toByteArrayContent() ? : body
69
- }
70
- HttpRequestMethod .PUT , HttpRequestMethod .POST -> {
71
- val contentString = if (contentType == ContentType .Application .FormUrlEncoded ) {
72
- bodyMap?.map { " ${it.key} =${it.value} " }?.joinToString(" &" ) ? : bodyString
73
- } else bodyString
65
+ body = when (this @HttpConnection.method) {
66
+ HttpRequestMethod .DELETE -> {
67
+ bodyString.toByteArrayContent() ? : body
68
+ }
69
+ HttpRequestMethod .PUT , HttpRequestMethod .POST -> {
70
+ val contentString = if (contentType == ContentType .Application .FormUrlEncoded ) {
71
+ bodyMap?.map { " ${it.key} =${it.value} " }?.joinToString(" &" ) ? : bodyString
72
+ } else bodyString
74
73
75
- contentString.toByteArrayContent() ? : ByteArrayContent (" " .toByteArray(), contentType)
76
- }
77
- else -> body
74
+ contentString.toByteArrayContent() ? : ByteArrayContent (" " .toByteArray(), contentType)
78
75
}
79
- )
76
+ else -> body
77
+ }
80
78
81
79
// let additionalHeaders overwrite headers
82
80
val allHeaders = if (additionalHeaders == null ) this @HttpConnection.headers
@@ -94,7 +92,7 @@ public class HttpConnection constructor(
94
92
val httpRequest = buildRequest(additionalHeaders)
95
93
if (api?.spotifyApiOptions?.enableDebugMode == true ) println (" DEBUG MODE: Request: $this " )
96
94
try {
97
- return httpClient.request(httpRequest).let { response ->
95
+ return httpClient.request< io.ktor.client.statement. HttpResponse > (httpRequest).let { response ->
98
96
val respCode = response.status.value
99
97
100
98
if (respCode in 500 .. 599 && (retryIfInternalServerErrorLeft == null || retryIfInternalServerErrorLeft == - 1 || retryIfInternalServerErrorLeft > 0 )) {
@@ -118,7 +116,7 @@ public class HttpConnection constructor(
118
116
} else throw SpotifyRatelimitedException (ratelimit)
119
117
}
120
118
121
- val body: String = response.bodyAsText ()
119
+ val body: String = response.readText ()
122
120
if (api?.spotifyApiOptions?.enableDebugMode == true ) println (" DEBUG MODE: $body " )
123
121
124
122
if (respCode == 401 && body.contains(" access token" ) && api?.spotifyApiOptions?.automaticRefresh == true ) {
@@ -147,7 +145,7 @@ public class HttpConnection constructor(
147
145
} catch (e: CancellationException ) {
148
146
throw e
149
147
} catch (e: ResponseException ) {
150
- val errorBody = e.response.bodyAsText ()
148
+ val errorBody = e.response.readText ()
151
149
if (api?.spotifyApiOptions?.enableDebugMode == true ) println (" DEBUG MODE: $errorBody " )
152
150
try {
153
151
val respCode = e.response.status.value
0 commit comments