mirror of
https://github.com/2martens/tsw-timetable.git
synced 2026-10-17 07:57:55 +02:00
change: Add error handler and log error responses
This commit is contained in:
+13
-3
@@ -6,6 +6,7 @@ import de.twomartens.timetable.bahnApi.model.dto.BahnStations
|
||||
import de.twomartens.timetable.bahnApi.model.dto.BahnTimetable
|
||||
import de.twomartens.timetable.bahnApi.property.BahnApiProperties
|
||||
import de.twomartens.timetable.types.HourAtDay
|
||||
import mu.KotlinLogging
|
||||
import org.springframework.http.MediaType
|
||||
import org.springframework.stereotype.Service
|
||||
import org.springframework.web.client.RestClient
|
||||
@@ -35,9 +36,12 @@ class BahnApiService(
|
||||
val dateFormatter = DateTimeFormatter.ofPattern("yyMMdd")
|
||||
val timeFormatter = DateTimeFormatter.ofPattern("HH")
|
||||
val time = LocalTime.of(hourAtDay.hour.value, 0)
|
||||
val body = restClient.get()
|
||||
val day = hourAtDay.date.format(dateFormatter)
|
||||
val hour = time.format(timeFormatter)
|
||||
val bahnErrorHandler = BahnErrorHandler()
|
||||
val response = restClient.get()
|
||||
.uri("https://apis.deutschebahn.com/db-api-marketplace/apis/timetables/v1/plan/" +
|
||||
"${eva.value}/${hourAtDay.date.format(dateFormatter)}/${time.format(timeFormatter)}")
|
||||
"${eva.value}/${day}/${hour}")
|
||||
.headers {
|
||||
it.accept = mutableListOf(MediaType.APPLICATION_XML)
|
||||
it.contentType = MediaType.APPLICATION_XML
|
||||
@@ -45,8 +49,14 @@ class BahnApiService(
|
||||
it.set("DB-Api-Key", properties.clientSecret)
|
||||
}
|
||||
.retrieve()
|
||||
.body(BahnTimetable::class.java)
|
||||
.onStatus(bahnErrorHandler)
|
||||
log.debug { "Response from DB API, station [${eva.value}], date [$day], hour [$hour]" }
|
||||
val body = response.body(BahnTimetable::class.java)
|
||||
body!!.eva = eva
|
||||
return body
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val log = KotlinLogging.logger {}
|
||||
}
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package de.twomartens.timetable.bahnApi.service
|
||||
|
||||
import mu.KotlinLogging
|
||||
import org.springframework.http.client.ClientHttpResponse
|
||||
import org.springframework.web.client.ResponseErrorHandler
|
||||
|
||||
class BahnErrorHandler : ResponseErrorHandler {
|
||||
override fun hasError(response: ClientHttpResponse): Boolean {
|
||||
return response.statusCode.isError
|
||||
}
|
||||
|
||||
override fun handleError(response: ClientHttpResponse) {
|
||||
log.debug { "Error response: $response" }
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val log = KotlinLogging.logger {}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user