Address some OkHttp nullability changes

This commit is contained in:
arkon 2022-09-03 10:38:56 -04:00
parent bbf5c86b46
commit 38950f7bc8
9 changed files with 15 additions and 19 deletions

View file

@ -164,12 +164,12 @@ class ChapterCache(private val context: Context) {
editor = diskCache.edit(key) ?: throw IOException("Unable to edit key")
// Get OutputStream and write image with Okio.
response.body!!.source().saveTo(editor.newOutputStream(0))
response.body.source().saveTo(editor.newOutputStream(0))
diskCache.flush()
editor.commit()
} finally {
response.body?.close()
response.body.close()
editor?.abortUnlessCommitted()
}
}

View file

@ -425,7 +425,7 @@ class Downloader(
.map { response ->
val file = tmpDir.createFile("$filename.tmp")
try {
response.body!!.source().saveTo(file.openOutputStream())
response.body.source().saveTo(file.openOutputStream())
val extension = getImageExtension(response, file)
file.renameTo("$filename.$extension")
} catch (e: Exception) {
@ -470,7 +470,7 @@ class Downloader(
*/
private fun getImageExtension(response: Response, file: UniFile): String {
// Read content type if available.
val mime = response.body?.contentType()?.run { if (type == "image") "image/$subtype" else null }
val mime = response.body.contentType()?.run { if (type == "image") "image/$subtype" else null }
// Else guess from the uri.
?: context.contentResolver.getType(file.uri)
// Else read magic numbers.

View file

@ -80,7 +80,7 @@ class BangumiApi(private val client: OkHttpClient, interceptor: BangumiIntercept
authClient.newCall(GET(url.toString()))
.await()
.use {
var responseBody = it.body?.string().orEmpty()
var responseBody = it.body.string()
if (responseBody.isEmpty()) {
throw Exception("Null Response")
}
@ -135,8 +135,8 @@ class BangumiApi(private val client: OkHttpClient, interceptor: BangumiIntercept
.build()
// TODO: get user readed chapter here
var response = authClient.newCall(requestUserRead).await()
var responseBody = response.body?.string().orEmpty()
val response = authClient.newCall(requestUserRead).await()
val responseBody = response.body.string()
if (responseBody.isEmpty()) {
throw Exception("Null Response")
}
@ -183,10 +183,6 @@ class BangumiApi(private val client: OkHttpClient, interceptor: BangumiIntercept
private const val redirectUrl = "tachiyomi://bangumi-auth"
private const val baseMangaUrl = "$apiUrl/mangas"
fun mangaUrl(remoteId: Int): String {
return "$baseMangaUrl/$remoteId"
}
fun authUrl(): Uri =
loginUrl.toUri().buildUpon()
.appendQueryParameter("client_id", clientId)

View file

@ -24,7 +24,7 @@ class BangumiInterceptor(val bangumi: Bangumi) : Interceptor {
if (currAuth.isExpired()) {
val response = chain.proceed(BangumiApi.refreshTokenRequest(currAuth.refresh_token!!))
if (response.isSuccessful) {
newAuth(json.decodeFromString<OAuth>(response.body!!.string()))
newAuth(json.decodeFromString<OAuth>(response.body.string()))
} else {
response.close()
}

View file

@ -26,7 +26,7 @@ class KitsuInterceptor(val kitsu: Kitsu) : Interceptor {
if (currAuth.isExpired()) {
val response = chain.proceed(KitsuApi.refreshTokenRequest(refreshToken))
if (response.isSuccessful) {
newAuth(json.decodeFromString(response.body!!.string()))
newAuth(json.decodeFromString(response.body.string()))
} else {
response.close()
}

View file

@ -26,7 +26,7 @@ class ShikimoriInterceptor(val shikimori: Shikimori) : Interceptor {
if (currAuth.isExpired()) {
val response = chain.proceed(ShikimoriApi.refreshTokenRequest(refreshToken))
if (response.isSuccessful) {
newAuth(json.decodeFromString<OAuth>(response.body!!.string()))
newAuth(json.decodeFromString<OAuth>(response.body.string()))
} else {
response.close()
}

View file

@ -127,7 +127,7 @@ class AppUpdateService : Service() {
val apkFile = File(externalCacheDir, "update.apk")
if (response.isSuccessful) {
response.body!!.source().saveTo(apkFile)
response.body.source().saveTo(apkFile)
} else {
response.close()
throw Exception("Unsuccessful response")

View file

@ -70,7 +70,7 @@ suspend fun Call.await(): Response {
}
continuation.resume(response) {
response.body?.closeQuietly()
response.body.closeQuietly()
}
}
@ -107,7 +107,7 @@ fun OkHttpClient.newCallWithProgress(request: Request, listener: ProgressListene
.addNetworkInterceptor { chain ->
val originalResponse = chain.proceed(chain.request())
originalResponse.newBuilder()
.body(ProgressResponseBody(originalResponse.body!!, listener))
.body(ProgressResponseBody(originalResponse.body, listener))
.build()
}
.build()
@ -119,7 +119,7 @@ inline fun <reified T> Response.parseAs(): T {
// Avoiding Injekt.get<Json>() due to compiler issues
val json = Injekt.getInstance<Json>(fullType<Json>().type)
this.use {
val responseBody = it.body?.string().orEmpty()
val responseBody = it.body.string()
return json.decodeFromString(responseBody)
}
}

View file

@ -22,5 +22,5 @@ fun Element.attrOrText(css: String): String {
* @param html the body of the response. Use only if the body was read before calling this method.
*/
fun Response.asJsoup(html: String? = null): Document {
return Jsoup.parse(html ?: body!!.string(), request.url.toString())
return Jsoup.parse(html ?: body.string(), request.url.toString())
}