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") editor = diskCache.edit(key) ?: throw IOException("Unable to edit key")
// Get OutputStream and write image with Okio. // Get OutputStream and write image with Okio.
response.body!!.source().saveTo(editor.newOutputStream(0)) response.body.source().saveTo(editor.newOutputStream(0))
diskCache.flush() diskCache.flush()
editor.commit() editor.commit()
} finally { } finally {
response.body?.close() response.body.close()
editor?.abortUnlessCommitted() editor?.abortUnlessCommitted()
} }
} }

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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