Complete AniList api integration #208
parent
94203785f1
commit
6ca6ec28ac
@ -0,0 +1,56 @@
|
||||
package org.koitharu.kotatsu.core.network
|
||||
|
||||
import android.util.Log
|
||||
import okhttp3.Interceptor
|
||||
import okhttp3.Response
|
||||
import okio.Buffer
|
||||
import org.koitharu.kotatsu.core.network.CommonHeaders.ACCEPT_ENCODING
|
||||
|
||||
class CurlLoggingInterceptor(
|
||||
private val curlOptions: String? = null
|
||||
) : Interceptor {
|
||||
|
||||
override fun intercept(chain: Interceptor.Chain): Response {
|
||||
val request = chain.request()
|
||||
var isCompressed = false
|
||||
|
||||
val curlCmd = StringBuilder()
|
||||
curlCmd.append("curl")
|
||||
if (curlOptions != null) {
|
||||
curlCmd.append(' ').append(curlOptions)
|
||||
}
|
||||
curlCmd.append(" -X ").append(request.method)
|
||||
|
||||
for ((name, value) in request.headers) {
|
||||
if (name.equals(ACCEPT_ENCODING, ignoreCase = true) && value.equals("gzip", ignoreCase = true)) {
|
||||
isCompressed = true
|
||||
}
|
||||
curlCmd.append(" -H \"").append(name).append(": ").append(value.escape()).append('\"')
|
||||
}
|
||||
|
||||
val body = request.body
|
||||
if (body != null) {
|
||||
val buffer = Buffer()
|
||||
body.writeTo(buffer)
|
||||
val charset = body.contentType()?.charset() ?: Charsets.UTF_8
|
||||
curlCmd.append(" --data-raw '")
|
||||
.append(buffer.readString(charset).replace("\n", "\\n"))
|
||||
.append("'")
|
||||
}
|
||||
if (isCompressed) {
|
||||
curlCmd.append(" --compressed")
|
||||
}
|
||||
curlCmd.append(" \"").append(request.url).append('"')
|
||||
|
||||
log("---cURL (" + request.url + ")")
|
||||
log(curlCmd.toString())
|
||||
|
||||
return chain.proceed(request)
|
||||
}
|
||||
|
||||
private fun String.escape() = replace("\"", "\\\"")
|
||||
|
||||
private fun log(msg: String) {
|
||||
Log.d("CURL", msg)
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,27 @@
|
||||
package org.koitharu.kotatsu.scrobbling.anilist.data
|
||||
|
||||
import org.koitharu.kotatsu.utils.ext.printStackTraceDebug
|
||||
|
||||
enum class ScoreFormat {
|
||||
|
||||
POINT_100, POINT_10_DECIMAL, POINT_10, POINT_5, POINT_3;
|
||||
|
||||
fun normalize(score: Float): Float = when (this) {
|
||||
POINT_100 -> score / 100f
|
||||
POINT_10_DECIMAL,
|
||||
POINT_10 -> score / 10f
|
||||
|
||||
POINT_5 -> score / 5f
|
||||
POINT_3 -> score / 3f
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
fun of(rawValue: String?): ScoreFormat {
|
||||
rawValue ?: return POINT_10_DECIMAL
|
||||
return runCatching { valueOf(rawValue) }
|
||||
.onFailure { it.printStackTraceDebug() }
|
||||
.getOrDefault(POINT_10_DECIMAL)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,11 @@
|
||||
package org.koitharu.kotatsu.core.network
|
||||
|
||||
import okhttp3.Interceptor
|
||||
import okhttp3.Response
|
||||
|
||||
class CurlLoggingInterceptor : Interceptor {
|
||||
|
||||
override fun intercept(chain: Interceptor.Chain): Response {
|
||||
return chain.proceed(chain.request()) // no-op implementation
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue