fix(kobo): proxy 401 errors on initialization

Co-authored-by: Gauthier Roebroeck <gauthier.roebroeck@gmail.com>
This commit is contained in:
Jason 2025-11-27 19:10:01 -08:00 committed by GitHub
parent 0f25453949
commit 3739951b36
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 21 additions and 24 deletions

View file

@ -30,28 +30,24 @@ class KoboProxy(
private val komgaSyncTokenGenerator: KomgaSyncTokenGenerator,
private val komgaSettingsProvider: KomgaSettingsProvider,
) {
private val koboApiClient: RestClient
private val koboApiClient: RestClient =
RestClient
.builder()
.uriBuilderFactory(
DefaultUriBuilderFactory("https://storeapi.kobo.com")
.apply {
this.encodingMode = DefaultUriBuilderFactory.EncodingMode.NONE
},
).requestFactory(
ClientHttpRequestFactoryBuilder.reactor().build(
ClientHttpRequestFactorySettings
.defaults()
.withReadTimeout(1.minutes.toJavaDuration())
.withConnectTimeout(1.minutes.toJavaDuration()),
),
).build()
init {
val uriBuilderFactory = DefaultUriBuilderFactory("https://storeapi.kobo.com")
uriBuilderFactory.setEncodingMode(DefaultUriBuilderFactory.EncodingMode.NONE)
koboApiClient =
RestClient
.builder()
.uriBuilderFactory(
uriBuilderFactory,
).requestFactory(
ClientHttpRequestFactoryBuilder.reactor().build(
ClientHttpRequestFactorySettings
.defaults()
.withReadTimeout(1.minutes.toJavaDuration())
.withConnectTimeout(1.minutes.toJavaDuration()),
),
).build()
}
private val pathRegex = """\/kobo\/[-\w]*(.*)""".toRegex()
private val pathRegex = """/kobo/[-\w]*(.*)""".toRegex()
private val headersOutInclude =
setOf(
@ -120,7 +116,7 @@ class KoboProxy(
}.apply { if (body != null) body(body) }
.retrieve()
.onStatus(HttpStatusCode::isError) { _, response ->
logger.debug { "Kobo response: $response" }
logger.debug { "Kobo response: ${response.statusCode}: ${response.body.bufferedReader().use { it.readText() }}" }
throw ResponseStatusException(response.statusCode, response.statusText)
}.toEntity<JsonNode>()

View file

@ -194,6 +194,7 @@ class KoboController(
try {
koboProxy.proxyCurrentRequest().body?.get("Resources")
} catch (e: Exception) {
if (e is ResponseStatusException && e.statusCode == HttpStatus.UNAUTHORIZED) throw e
logger.warn { "Failed to get response from Kobo /v1/initialization, fallback to noproxy" }
null
} ?: koboProxy.nativeKoboResources
@ -233,7 +234,7 @@ class KoboController(
): Any {
try {
return koboProxy.proxyCurrentRequest(body)
} catch (e: Exception) {
} catch (_: Exception) {
logger.warn { "Failed to get response from Kobo /v1/auth/device, fallback to noproxy" }
}
@ -395,7 +396,7 @@ class KoboController(
addAll(
// changed books are also passed as changed reading state because Kobo does not process ChangedEntitlement even if it contains a ReadingState
(booksChanged.content + changedReadingState.content).mapNotNull { book ->
readProgress[book.bookId]?.let { it ->
readProgress[book.bookId]?.let {
ChangedReadingStateDto(
WrappedReadingStateDto(
it.toDto(),