Make OkHttp coroutine calls always throw exceptions on errors

This commit is contained in:
arkon 2020-12-24 15:36:57 -05:00
parent c0cef58e39
commit 1268caf3e0
2 changed files with 9 additions and 9 deletions

View File

@ -44,7 +44,7 @@ class MyAnimeListApi(private val client: OkHttpClient, interceptor: MyAnimeListI
.add("code_verifier", codeVerifier)
.add("grant_type", "authorization_code")
.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()
json.decodeFromString(responseBody)
}
@ -57,7 +57,7 @@ class MyAnimeListApi(private val client: OkHttpClient, interceptor: MyAnimeListI
.url("$baseApiUrl/users/@me")
.get()
.build()
authClient.newCall(request).await(assertSuccess = true).use {
authClient.newCall(request).await().use {
val responseBody = it.body?.string().orEmpty()
val response = json.decodeFromString<JsonObject>(responseBody)
response["name"]!!.jsonPrimitive.content
@ -70,7 +70,7 @@ class MyAnimeListApi(private val client: OkHttpClient, interceptor: MyAnimeListI
val url = "$baseApiUrl/manga".toUri().buildUpon()
.appendQueryParameter("q", query)
.build()
authClient.newCall(GET(url.toString())).await(assertSuccess = true).use {
authClient.newCall(GET(url.toString())).await().use {
val responseBody = it.body?.string().orEmpty()
val response = json.decodeFromString<JsonObject>(responseBody)
response["data"]!!.jsonArray
@ -91,7 +91,7 @@ class MyAnimeListApi(private val client: OkHttpClient, interceptor: MyAnimeListI
.appendPath(id.toString())
.appendQueryParameter("fields", "id,title,synopsis,num_chapters,main_picture,status,media_type,start_date")
.build()
authClient.newCall(GET(url.toString())).await(assertSuccess = true).use {
authClient.newCall(GET(url.toString())).await().use {
val responseBody = it.body?.string().orEmpty()
val response = json.decodeFromString<JsonObject>(responseBody)
val obj = response.jsonObject
@ -124,7 +124,7 @@ class MyAnimeListApi(private val client: OkHttpClient, interceptor: MyAnimeListI
.url(mangaUrl(track.media_id).toString())
.put(formBody)
.build()
authClient.newCall(request).await(assertSuccess = true).use {
authClient.newCall(request).await().use {
parseMangaItem(it, track)
}
}
@ -140,7 +140,7 @@ class MyAnimeListApi(private val client: OkHttpClient, interceptor: MyAnimeListI
.url(mangaUrl(track.media_id).toString())
.put(formBody)
.build()
authClient.newCall(request).await(assertSuccess = true).use {
authClient.newCall(request).await().use {
parseMangaItem(it, track)
}
}
@ -158,7 +158,7 @@ class MyAnimeListApi(private val client: OkHttpClient, interceptor: MyAnimeListI
.url(mangaUrl(track.media_id).toString())
.put(formBody)
.build()
authClient.newCall(request).await(assertSuccess = true).use {
authClient.newCall(request).await().use {
parseMangaItem(it, track)
}
}

View File

@ -52,12 +52,12 @@ fun Call.asObservable(): Observable<Response> {
}
// 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 ->
enqueue(
object : Callback {
override fun onResponse(call: Call, response: Response) {
if (assertSuccess && !response.isSuccessful) {
if (!response.isSuccessful) {
continuation.resumeWithException(Exception("HTTP error ${response.code}"))
return
}