Replace try-catch with use{} in parse functions

master
Koitharu 1 year ago
parent 9ec62f0be0
commit 45b843fadc
Signed by: Koitharu
GPG Key ID: 676DEE768C17A9D7

@ -15,4 +15,5 @@ public object ErrorMessages {
public const val FILTER_BOTH_STATES_GENRES_NOT_SUPPORTED: String = public const val FILTER_BOTH_STATES_GENRES_NOT_SUPPORTED: String =
"Filtering by both genres and states is not supported by this source" "Filtering by both genres and states is not supported by this source"
public const val SEARCH_NOT_SUPPORTED: String = "Search is not supported by this source" public const val SEARCH_NOT_SUPPORTED: String = "Search is not supported by this source"
public const val RESPONSE_NULL_BODY: String = "Response has no body"
} }

@ -4,11 +4,11 @@ package org.koitharu.kotatsu.parsers.util
import okhttp3.Response import okhttp3.Response
import okhttp3.ResponseBody import okhttp3.ResponseBody
import okhttp3.internal.closeQuietly
import org.json.JSONArray import org.json.JSONArray
import org.json.JSONObject import org.json.JSONObject
import org.jsoup.Jsoup import org.jsoup.Jsoup
import org.jsoup.nodes.Document import org.jsoup.nodes.Document
import org.koitharu.kotatsu.parsers.ErrorMessages
import org.koitharu.kotatsu.parsers.InternalParsersApi import org.koitharu.kotatsu.parsers.InternalParsersApi
import java.text.DateFormat import java.text.DateFormat
@ -21,12 +21,10 @@ internal const val SCHEME_HTTPS = "https"
* @see [parseJsonArray] * @see [parseJsonArray]
*/ */
// TODO suspend // TODO suspend
public fun Response.parseHtml(): Document = try { public fun Response.parseHtml(): Document = use { response ->
val body = requireBody() val body = response.requireBody()
val charset = body.contentType()?.charset()?.name() val charset = body.contentType()?.charset()?.name()
Jsoup.parse(body.byteStream(), charset, request.url.toString()) Jsoup.parse(body.byteStream(), charset, response.request.url.toString())
} finally {
closeQuietly()
} }
/** /**
@ -34,10 +32,8 @@ public fun Response.parseHtml(): Document = try {
* @see [parseJsonArray] * @see [parseJsonArray]
* @see [parseHtml] * @see [parseHtml]
*/ */
public fun Response.parseJson(): JSONObject = try { public fun Response.parseJson(): JSONObject = use { response ->
JSONObject(requireBody().string()) JSONObject(response.requireBody().string())
} finally {
closeQuietly()
} }
/** /**
@ -45,22 +41,16 @@ public fun Response.parseJson(): JSONObject = try {
* @see [parseJson] * @see [parseJson]
* @see [parseHtml] * @see [parseHtml]
*/ */
public fun Response.parseJsonArray(): JSONArray = try { public fun Response.parseJsonArray(): JSONArray = use { response ->
JSONArray(requireBody().string()) JSONArray(response.requireBody().string())
} finally {
closeQuietly()
} }
public fun Response.parseRaw(): String = try { public fun Response.parseRaw(): String = use { response ->
requireBody().string() response.requireBody().string()
} finally {
closeQuietly()
} }
public fun Response.parseBytes(): ByteArray = try { public fun Response.parseBytes(): ByteArray = use { response ->
requireBody().bytes() response.requireBody().bytes()
} finally {
closeQuietly()
} }
/** /**
@ -110,4 +100,6 @@ public fun DateFormat.tryParse(str: String?): Long = if (str.isNullOrEmpty()) {
}.getOrDefault(0L) }.getOrDefault(0L)
} }
public fun Response.requireBody(): ResponseBody = requireNotNull(body) { "Response body is null" } public fun Response.requireBody(): ResponseBody = requireNotNull(body) {
ErrorMessages.RESPONSE_NULL_BODY
}

Loading…
Cancel
Save