@@ -9,17 +9,17 @@ import io.ktor.client.HttpClient
99import io.ktor.client.features.ResponseException
1010import io.ktor.client.request.HttpRequestBuilder
1111import io.ktor.client.request.header
12+ import io.ktor.client.request.request
1213import io.ktor.client.request.url
13- import io.ktor.client.response .readText
14+ import io.ktor.client.statement .readText
1415import io.ktor.client.utils.EmptyContent
1516import io.ktor.http.ContentType
1617import io.ktor.http.HttpMethod
1718import io.ktor.http.content.ByteArrayContent
1819import io.ktor.http.content.TextContent
20+ import io.ktor.utils.io.core.toByteArray
1921import kotlinx.coroutines.CancellationException
2022import kotlinx.coroutines.delay
21- import kotlinx.io.core.toByteArray
22- import kotlinx.io.core.use
2323
2424enum class HttpRequestMethod (internal val externalMethod : HttpMethod ) {
2525 GET (HttpMethod .Get ),
@@ -33,13 +33,13 @@ internal data class HttpHeader(val key: String, val value: String)
3333internal data class HttpResponse (val responseCode : Int , val body : String , val headers : List <HttpHeader >)
3434
3535internal 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 \n Retrying.." ,
99- null
96+ false ,
97+ " Received 502 (Invalid response) for URL $url and $this \n Retrying.." ,
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 \n Not 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 ) {
0 commit comments