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) override fun login(username: String, password: String) = login(password)
fun login(authCode: String): Completable { fun login(authCode: String): Completable {
return try { return Completable.fromCallable {
val oauth = runBlocking { api.getAccessToken(authCode) } val oauth = runBlocking { api.getAccessToken(authCode) }
interceptor.setAuth(oauth) interceptor.setAuth(oauth)
val username = runBlocking { api.getCurrentUser() } val username = runBlocking { api.getCurrentUser() }
saveCredentials(username, oauth.access_token) 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() { override fun logout() {

View File

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