fix: Fix "Stream already closed" error

This commit is contained in:
2025-03-20 16:03:29 +01:00
parent de3d318ae9
commit 88be221eb6
2 changed files with 2 additions and 24 deletions
@@ -38,10 +38,9 @@ class BahnApiService(
val time = LocalTime.of(hourAtDay.hour.value, 0) val time = LocalTime.of(hourAtDay.hour.value, 0)
val day = hourAtDay.date.format(dateFormatter) val day = hourAtDay.date.format(dateFormatter)
val hour = time.format(timeFormatter) val hour = time.format(timeFormatter)
val responseLogger = BahnResponseLogger()
val uri = "https://apis.deutschebahn.com/db-api-marketplace/apis/timetables/v1/plan/" + val uri = "https://apis.deutschebahn.com/db-api-marketplace/apis/timetables/v1/plan/" +
"${eva.value}/${day}/${hour}" "${eva.value}/${day}/${hour}"
val response = restClient.get() val body = restClient.get()
.uri(uri) .uri(uri)
.headers { .headers {
it.accept = mutableListOf(MediaType.APPLICATION_XML) it.accept = mutableListOf(MediaType.APPLICATION_XML)
@@ -50,9 +49,8 @@ class BahnApiService(
it.set("DB-Api-Key", properties.clientSecret) it.set("DB-Api-Key", properties.clientSecret)
} }
.retrieve() .retrieve()
.onStatus(responseLogger) .body(BahnTimetable::class.java)
log.debug { "Response from DB API, station [${eva.value}], date [$day], hour [$hour], requestURI [$uri]" } log.debug { "Response from DB API, station [${eva.value}], date [$day], hour [$hour], requestURI [$uri]" }
val body = response.body(BahnTimetable::class.java)
body!!.eva = eva body!!.eva = eva
return body return body
} }
@@ -1,20 +0,0 @@
package de.twomartens.timetable.bahnApi.service
import mu.KotlinLogging
import org.springframework.http.client.ClientHttpResponse
import org.springframework.web.client.ResponseErrorHandler
class BahnResponseLogger : ResponseErrorHandler {
override fun hasError(response: ClientHttpResponse): Boolean {
return true
}
override fun handleError(response: ClientHttpResponse) {
val body = response.body.bufferedReader().use { it.readText() }
log.debug { "Status code [${response.statusCode.value()}], status text [${response.statusText}], headers [${response.headers}], body [$body]" }
}
companion object {
private val log = KotlinLogging.logger {}
}
}