From 8efa8bc0d2039e2396fce20ca0d3ada738de132f Mon Sep 17 00:00:00 2001 From: Koitharu Date: Thu, 1 Feb 2024 10:31:23 +0200 Subject: [PATCH] Handle MAL errors in html --- .../kotatsu/scrobbling/mal/data/MALInterceptor.kt | 10 +++++++++- .../kotatsu/scrobbling/mal/data/MALRepository.kt | 3 ++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/scrobbling/mal/data/MALInterceptor.kt b/app/src/main/kotlin/org/koitharu/kotatsu/scrobbling/mal/data/MALInterceptor.kt index f023d5f75..fb0e0dcc1 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/scrobbling/mal/data/MALInterceptor.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/scrobbling/mal/data/MALInterceptor.kt @@ -2,10 +2,14 @@ package org.koitharu.kotatsu.scrobbling.mal.data import okhttp3.Interceptor import okhttp3.Response +import okio.IOException import org.koitharu.kotatsu.core.network.CommonHeaders +import org.koitharu.kotatsu.parsers.util.mimeType +import org.koitharu.kotatsu.parsers.util.parseHtml import org.koitharu.kotatsu.scrobbling.common.data.ScrobblerStorage private const val JSON = "application/json" +private const val HTML = "text/html" class MALInterceptor(private val storage: ScrobblerStorage) : Interceptor { @@ -19,7 +23,11 @@ class MALInterceptor(private val storage: ScrobblerStorage) : Interceptor { request.header(CommonHeaders.AUTHORIZATION, "Bearer $it") } } - return chain.proceed(request.build()) + val response = chain.proceed(request.build()) + if (response.mimeType == HTML) { + throw IOException(response.parseHtml().title()) + } + return response } } diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/scrobbling/mal/data/MALRepository.kt b/app/src/main/kotlin/org/koitharu/kotatsu/scrobbling/mal/data/MALRepository.kt index d2887c30c..f7364275c 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/scrobbling/mal/data/MALRepository.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/scrobbling/mal/data/MALRepository.kt @@ -151,7 +151,8 @@ class MALRepository @Inject constructor( override suspend fun updateRate(rateId: Int, mangaId: Long, rating: Float, status: String?, comment: String?) { val body = FormBody.Builder() .add("status", status.toString()) - .add("score", rating.toString()) + .add("score", rating.toInt().toString()) + .add("comments", comment.orEmpty()) val url = BASE_API_URL.toHttpUrl().newBuilder() .addPathSegment("manga") .addPathSegment(rateId.toString())