Surface MAL HTTP errors properly

This commit is contained in:
arkon 2020-12-24 15:09:10 -05:00
parent d363d205c3
commit c0cef58e39
2 changed files with 14 additions and 13 deletions

View File

@ -91,17 +91,18 @@ class MyAnimeList(private val context: Context, id: Int) : TrackService(id) {
override fun login(username: String, password: String) = login(password)
fun login(authCode: String): Completable {
return try {
return Completable.fromCallable {
val oauth = runBlocking { api.getAccessToken(authCode) }
interceptor.setAuth(oauth)
val username = runBlocking { api.getCurrentUser() }
saveCredentials(username, oauth.access_token)
return Completable.complete()
} catch (e: Exception) {
Timber.e(e)
logout()
Completable.error(e)
}
.doOnError {
Timber.e(it)
logout()
}
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
}
override fun logout() {

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().use {
client.newCall(POST("$baseOAuthUrl/token", body = formBody)).await(assertSuccess = true).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().use {
authClient.newCall(request).await(assertSuccess = true).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().use {
authClient.newCall(GET(url.toString())).await(assertSuccess = true).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().use {
authClient.newCall(GET(url.toString())).await(assertSuccess = true).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().use {
authClient.newCall(request).await(assertSuccess = true).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().use {
authClient.newCall(request).await(assertSuccess = true).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().use {
authClient.newCall(request).await(assertSuccess = true).use {
parseMangaItem(it, track)
}
}