Initial fetchCompat okhttp layer
This commit is contained in:
1 parent
02855b68e6
commit
b7c8afe7d2
2 files changed
+33
-10
No files matched your search
@@ -52,6 +52,7 @@ import com.hippo.ehviewer.dailycheck.showEventNotification
|
|||||||
import com.hippo.ehviewer.dailycheck.today
|
import com.hippo.ehviewer.dailycheck.today
|
||||||
import com.hippo.ehviewer.util.AppConfig
|
import com.hippo.ehviewer.util.AppConfig
|
||||||
import com.hippo.ehviewer.util.StatusCodeException
|
import com.hippo.ehviewer.util.StatusCodeException
|
||||||
|
import com.hippo.ehviewer.util.isCronetSupported
|
||||||
import io.ktor.utils.io.pool.DirectByteBufferPool
|
import io.ktor.utils.io.pool.DirectByteBufferPool
|
||||||
import kotlinx.coroutines.CancellationException
|
import kotlinx.coroutines.CancellationException
|
||||||
import kotlinx.coroutines.delay
|
import kotlinx.coroutines.delay
|
||||||
@@ -133,16 +134,31 @@ val httpContentPool = DirectByteBufferPool(16, 16384)
|
|||||||
|
|
||||||
suspend inline fun <T> fetchCompat(url: String, referer: String? = null, crossinline parser: (ByteBuffer) -> T): T {
|
suspend inline fun <T> fetchCompat(url: String, referer: String? = null, crossinline parser: (ByteBuffer) -> T): T {
|
||||||
val ret: T
|
val ret: T
|
||||||
cronetRequest(url, referer).execute {
|
if (isCronetSupported) {
|
||||||
val buffer = httpContentPool.borrow()
|
cronetRequest(url, referer).execute {
|
||||||
try {
|
val buffer = httpContentPool.borrow()
|
||||||
awaitBodyFully {
|
try {
|
||||||
buffer.put(it)
|
awaitBodyFully {
|
||||||
|
buffer.put(it)
|
||||||
|
}
|
||||||
|
buffer.flip()
|
||||||
|
ret = parser(buffer)
|
||||||
|
} finally {
|
||||||
|
httpContentPool.recycle(buffer)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
ehRequest(url, referer).execute {
|
||||||
|
val buffer = httpContentPool.borrow()
|
||||||
|
try {
|
||||||
|
while (true) {
|
||||||
|
if (body.source().read(buffer) == -1) break
|
||||||
|
}
|
||||||
|
buffer.flip()
|
||||||
|
ret = parser(buffer)
|
||||||
|
} finally {
|
||||||
|
httpContentPool.recycle(buffer)
|
||||||
}
|
}
|
||||||
buffer.flip()
|
|
||||||
ret = parser(buffer)
|
|
||||||
} finally {
|
|
||||||
httpContentPool.recycle(buffer)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return ret
|
return ret
|
||||||
|
|||||||
@@ -38,6 +38,8 @@ import okhttp3.RequestBody.Companion.toRequestBody
|
|||||||
import okhttp3.Response
|
import okhttp3.Response
|
||||||
import okhttp3.executeAsync
|
import okhttp3.executeAsync
|
||||||
import okio.BufferedSource
|
import okio.BufferedSource
|
||||||
|
import kotlin.contracts.InvocationKind
|
||||||
|
import kotlin.contracts.contract
|
||||||
import kotlin.coroutines.resume
|
import kotlin.coroutines.resume
|
||||||
import kotlin.coroutines.resumeWithException
|
import kotlin.coroutines.resumeWithException
|
||||||
|
|
||||||
@@ -104,7 +106,12 @@ suspend inline fun <R> Call.usingCancellable(crossinline block: suspend Response
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend inline fun <R> Request.execute(block: Response.() -> R) = okHttpClient.newCall(this).executeAsync().use(block)
|
suspend inline fun <R> Request.execute(block: Response.() -> R): R {
|
||||||
|
contract {
|
||||||
|
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
|
||||||
|
}
|
||||||
|
return okHttpClient.newCall(this).executeAsync().use(block)
|
||||||
|
}
|
||||||
suspend inline fun <R> Request.executeNonCache(crossinline block: suspend Response.() -> R) = baseOkHttpClient.newCall(this).usingCancellable(block)
|
suspend inline fun <R> Request.executeNonCache(crossinline block: suspend Response.() -> R) = baseOkHttpClient.newCall(this).usingCancellable(block)
|
||||||
suspend inline fun <R> Request.executeNoRedirect(block: Response.() -> R) = noRedirectOkHttpClient.newCall(this).executeAsync().use(block)
|
suspend inline fun <R> Request.executeNoRedirect(block: Response.() -> R) = noRedirectOkHttpClient.newCall(this).executeAsync().use(block)
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user