Skip to content

Commit 323b3cf

Browse files
committed
Move httpResponseSubscriber invocation to after handling response
1 parent e047800 commit 323b3cf

File tree

3 files changed

+31
-29
lines changed

3 files changed

+31
-29
lines changed

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

+12-7
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,7 @@ import com.adamratzman.spotify.models.serialization.toObject
1212
import com.adamratzman.spotify.utils.ConcurrentHashMap
1313
import com.adamratzman.spotify.utils.getCurrentTimeMs
1414
import io.ktor.http.HttpStatusCode
15-
import kotlinx.coroutines.CancellationException
16-
import kotlinx.coroutines.async
17-
import kotlinx.coroutines.awaitAll
18-
import kotlinx.coroutines.coroutineScope
19-
import kotlinx.coroutines.withTimeout
15+
import kotlinx.coroutines.*
2016
import kotlinx.serialization.Serializable
2117
import kotlinx.serialization.Transient
2218
import kotlin.math.ceil
@@ -129,20 +125,29 @@ public abstract class SpotifyEndpoint(public val api: GenericSpotifyApi) {
129125
try {
130126
return withTimeout(api.spotifyApiOptions.requestTimeoutMillis ?: (100 * 1000L)) {
131127
try {
132-
val document = createConnection(url, body, method, contentType).execute(
128+
val httpRequest = createConnection(url, body, method, contentType)
129+
val document = httpRequest.execute(
133130
additionalHeaders = cacheState?.eTag?.let {
134131
listOf(HttpHeader("If-None-Match", it))
135132
},
136133
retryIfInternalServerErrorLeft = api.spotifyApiOptions.retryOnInternalServerErrorTimes
137134
)
138135

139-
handleResponse(document, cacheState, spotifyRequest, retry202) ?: run {
136+
val response = handleResponse(document, cacheState, spotifyRequest, retry202) ?: run {
140137
if (retryOnNull) {
141138
execute<ReturnType>(url, body, method, false, contentType, retryOnNull)
142139
} else {
143140
null
144141
}
145142
}
143+
144+
api.spotifyApiOptions.httpResponseSubscriber?.let { subscriber ->
145+
launch(currentCoroutineContext()) {
146+
subscriber(httpRequest, document)
147+
}
148+
}
149+
150+
response
146151
} catch (e: BadRequestException) {
147152
if (e.statusCode == 401 && !attemptedRefresh) {
148153
api.refreshToken()

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

-6
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,6 @@ public class HttpRequest constructor(
165165
}
166166
)
167167

168-
api?.spotifyApiOptions?.httpResponseSubscriber?.let { subscriber ->
169-
launch(currentCoroutineContext()) {
170-
subscriber(this, httpResponseToReturn)
171-
}
172-
}
173-
174168
return httpResponseToReturn
175169
}
176170
} catch (e: CancellationException) {

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

+19-16
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,11 @@
33

44
package com.adamratzman.spotify.utilities
55

6-
import com.adamratzman.spotify.GenericSpotifyApi
7-
import com.adamratzman.spotify.SpotifyClientApi
8-
import com.adamratzman.spotify.SpotifyScope
9-
import com.adamratzman.spotify.SpotifyUserAuthorization
10-
import com.adamratzman.spotify.buildSpotifyApi
11-
import com.adamratzman.spotify.getSpotifyPkceCodeChallenge
12-
import com.adamratzman.spotify.getTestClientId
13-
import com.adamratzman.spotify.getTestClientSecret
14-
import com.adamratzman.spotify.runTestOnDefaultDispatcher
15-
import com.adamratzman.spotify.spotifyAppApi
16-
import com.adamratzman.spotify.spotifyClientApi
17-
import io.ktor.util.PlatformUtils
6+
import com.adamratzman.spotify.*
7+
import io.ktor.util.*
188
import kotlinx.coroutines.ExperimentalCoroutinesApi
199
import kotlinx.coroutines.test.TestResult
20-
import kotlin.test.Test
21-
import kotlin.test.assertEquals
22-
import kotlin.test.assertFailsWith
23-
import kotlin.test.assertTrue
10+
import kotlin.test.*
2411

2512
class UtilityTests {
2613
var api: GenericSpotifyApi? = null
@@ -140,4 +127,20 @@ class UtilityTests {
140127
) { requiredScopes = listOf(SpotifyScope.PlaylistReadPrivate) }.build()
141128
}
142129
}
130+
131+
@Test
132+
fun testResponseSubscriber(): TestResult = runTestOnDefaultDispatcher {
133+
buildSpotifyApi(this::class.simpleName!!, ::testPagingObjectGetAllItems.name)?.let { api = it }
134+
val options = api!!.spotifyApiOptions
135+
val oldSubscriber = options.httpResponseSubscriber
136+
options.httpResponseSubscriber = { request, response ->
137+
assertNotNull(
138+
api!!.getCache().entries.singleOrNull { it.key.url == request.url }
139+
)
140+
oldSubscriber?.invoke(request, response)
141+
}
142+
143+
api!!.tracks.getTrack("6DrcMKnfMByc3RhhIvEw0F")
144+
options.httpResponseSubscriber = oldSubscriber
145+
}
143146
}

0 commit comments

Comments
 (0)