mirror of
https://github.com/tachiyomiorg/tachiyomi.git
synced 2024-11-16 18:09:16 +01:00
Make OkHttp coroutine calls always throw exceptions on errors
This commit is contained in:
parent
c0cef58e39
commit
1268caf3e0
@ -44,7 +44,7 @@ class MyAnimeListApi(private val client: OkHttpClient, interceptor: MyAnimeListI
|
|||||||
.add("code_verifier", codeVerifier)
|
.add("code_verifier", codeVerifier)
|
||||||
.add("grant_type", "authorization_code")
|
.add("grant_type", "authorization_code")
|
||||||
.build()
|
.build()
|
||||||
client.newCall(POST("$baseOAuthUrl/token", body = formBody)).await(assertSuccess = true).use {
|
client.newCall(POST("$baseOAuthUrl/token", body = formBody)).await().use {
|
||||||
val responseBody = it.body?.string().orEmpty()
|
val responseBody = it.body?.string().orEmpty()
|
||||||
json.decodeFromString(responseBody)
|
json.decodeFromString(responseBody)
|
||||||
}
|
}
|
||||||
@ -57,7 +57,7 @@ class MyAnimeListApi(private val client: OkHttpClient, interceptor: MyAnimeListI
|
|||||||
.url("$baseApiUrl/users/@me")
|
.url("$baseApiUrl/users/@me")
|
||||||
.get()
|
.get()
|
||||||
.build()
|
.build()
|
||||||
authClient.newCall(request).await(assertSuccess = true).use {
|
authClient.newCall(request).await().use {
|
||||||
val responseBody = it.body?.string().orEmpty()
|
val responseBody = it.body?.string().orEmpty()
|
||||||
val response = json.decodeFromString<JsonObject>(responseBody)
|
val response = json.decodeFromString<JsonObject>(responseBody)
|
||||||
response["name"]!!.jsonPrimitive.content
|
response["name"]!!.jsonPrimitive.content
|
||||||
@ -70,7 +70,7 @@ class MyAnimeListApi(private val client: OkHttpClient, interceptor: MyAnimeListI
|
|||||||
val url = "$baseApiUrl/manga".toUri().buildUpon()
|
val url = "$baseApiUrl/manga".toUri().buildUpon()
|
||||||
.appendQueryParameter("q", query)
|
.appendQueryParameter("q", query)
|
||||||
.build()
|
.build()
|
||||||
authClient.newCall(GET(url.toString())).await(assertSuccess = true).use {
|
authClient.newCall(GET(url.toString())).await().use {
|
||||||
val responseBody = it.body?.string().orEmpty()
|
val responseBody = it.body?.string().orEmpty()
|
||||||
val response = json.decodeFromString<JsonObject>(responseBody)
|
val response = json.decodeFromString<JsonObject>(responseBody)
|
||||||
response["data"]!!.jsonArray
|
response["data"]!!.jsonArray
|
||||||
@ -91,7 +91,7 @@ class MyAnimeListApi(private val client: OkHttpClient, interceptor: MyAnimeListI
|
|||||||
.appendPath(id.toString())
|
.appendPath(id.toString())
|
||||||
.appendQueryParameter("fields", "id,title,synopsis,num_chapters,main_picture,status,media_type,start_date")
|
.appendQueryParameter("fields", "id,title,synopsis,num_chapters,main_picture,status,media_type,start_date")
|
||||||
.build()
|
.build()
|
||||||
authClient.newCall(GET(url.toString())).await(assertSuccess = true).use {
|
authClient.newCall(GET(url.toString())).await().use {
|
||||||
val responseBody = it.body?.string().orEmpty()
|
val responseBody = it.body?.string().orEmpty()
|
||||||
val response = json.decodeFromString<JsonObject>(responseBody)
|
val response = json.decodeFromString<JsonObject>(responseBody)
|
||||||
val obj = response.jsonObject
|
val obj = response.jsonObject
|
||||||
@ -124,7 +124,7 @@ class MyAnimeListApi(private val client: OkHttpClient, interceptor: MyAnimeListI
|
|||||||
.url(mangaUrl(track.media_id).toString())
|
.url(mangaUrl(track.media_id).toString())
|
||||||
.put(formBody)
|
.put(formBody)
|
||||||
.build()
|
.build()
|
||||||
authClient.newCall(request).await(assertSuccess = true).use {
|
authClient.newCall(request).await().use {
|
||||||
parseMangaItem(it, track)
|
parseMangaItem(it, track)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -140,7 +140,7 @@ class MyAnimeListApi(private val client: OkHttpClient, interceptor: MyAnimeListI
|
|||||||
.url(mangaUrl(track.media_id).toString())
|
.url(mangaUrl(track.media_id).toString())
|
||||||
.put(formBody)
|
.put(formBody)
|
||||||
.build()
|
.build()
|
||||||
authClient.newCall(request).await(assertSuccess = true).use {
|
authClient.newCall(request).await().use {
|
||||||
parseMangaItem(it, track)
|
parseMangaItem(it, track)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -158,7 +158,7 @@ class MyAnimeListApi(private val client: OkHttpClient, interceptor: MyAnimeListI
|
|||||||
.url(mangaUrl(track.media_id).toString())
|
.url(mangaUrl(track.media_id).toString())
|
||||||
.put(formBody)
|
.put(formBody)
|
||||||
.build()
|
.build()
|
||||||
authClient.newCall(request).await(assertSuccess = true).use {
|
authClient.newCall(request).await().use {
|
||||||
parseMangaItem(it, track)
|
parseMangaItem(it, track)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -52,12 +52,12 @@ fun Call.asObservable(): Observable<Response> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Based on https://github.com/gildor/kotlin-coroutines-okhttp
|
// Based on https://github.com/gildor/kotlin-coroutines-okhttp
|
||||||
suspend fun Call.await(assertSuccess: Boolean = false): Response {
|
suspend fun Call.await(): Response {
|
||||||
return suspendCancellableCoroutine { continuation ->
|
return suspendCancellableCoroutine { continuation ->
|
||||||
enqueue(
|
enqueue(
|
||||||
object : Callback {
|
object : Callback {
|
||||||
override fun onResponse(call: Call, response: Response) {
|
override fun onResponse(call: Call, response: Response) {
|
||||||
if (assertSuccess && !response.isSuccessful) {
|
if (!response.isSuccessful) {
|
||||||
continuation.resumeWithException(Exception("HTTP error ${response.code}"))
|
continuation.resumeWithException(Exception("HTTP error ${response.code}"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user