优化代码

pull/540/head
gedoor 4 years ago
parent 707f19552e
commit 3d65cd8a8f
  1. 20
      app/src/main/java/io/legado/app/help/http/HttpHelper.kt
  2. 5
      app/src/main/java/io/legado/app/lib/webdav/WebDav.kt
  3. 27
      app/src/main/java/io/legado/app/utils/OkHttpExtensions.kt

@ -3,13 +3,11 @@ package io.legado.app.help.http
import io.legado.app.utils.msg import io.legado.app.utils.msg
import kotlinx.coroutines.suspendCancellableCoroutine import kotlinx.coroutines.suspendCancellableCoroutine
import okhttp3.* import okhttp3.*
import java.io.IOException
import java.net.InetSocketAddress import java.net.InetSocketAddress
import java.net.Proxy import java.net.Proxy
import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
import kotlin.coroutines.resume import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
@Suppress("unused") @Suppress("unused")
object HttpHelper { object HttpHelper {
@ -42,24 +40,6 @@ object HttpHelper {
builder.build() builder.build()
} }
suspend fun awaitResponse(request: Request): Response = suspendCancellableCoroutine { block ->
val call = client.newCall(request)
block.invokeOnCancellation {
call.cancel()
}
call.enqueue(object : Callback {
override fun onFailure(call: Call, e: IOException) {
block.resumeWithException(e)
}
override fun onResponse(call: Call, response: Response) {
block.resume(response)
}
})
}
/** /**
* 缓存代理okHttp * 缓存代理okHttp
*/ */

@ -1,6 +1,7 @@
package io.legado.app.lib.webdav package io.legado.app.lib.webdav
import io.legado.app.help.http.HttpHelper import io.legado.app.help.http.HttpHelper
import io.legado.app.utils.await
import okhttp3.* import okhttp3.*
import okhttp3.MediaType.Companion.toMediaType import okhttp3.MediaType.Companion.toMediaType
import okhttp3.RequestBody.Companion.asRequestBody import okhttp3.RequestBody.Companion.asRequestBody
@ -119,7 +120,7 @@ class WebDav(urlStr: String) {
request.header("Authorization", Credentials.basic(it.user, it.pass)) request.header("Authorization", Credentials.basic(it.user, it.pass))
} }
request.header("Depth", if (depth < 0) "infinity" else depth.toString()) request.header("Depth", if (depth < 0) "infinity" else depth.toString())
return HttpHelper.awaitResponse(request.build()) return HttpHelper.client.newCall(request.build()).await()
} }
return null return null
} }
@ -233,7 +234,7 @@ class WebDav(urlStr: String) {
HttpAuth.auth?.let { HttpAuth.auth?.let {
requestBuilder.header("Authorization", Credentials.basic(it.user, it.pass)) requestBuilder.header("Authorization", Credentials.basic(it.user, it.pass))
} }
val response = HttpHelper.awaitResponse(requestBuilder.build()) val response = HttpHelper.client.newCall(requestBuilder.build()).await()
return response.isSuccessful return response.isSuccessful
} }

@ -0,0 +1,27 @@
package io.legado.app.utils
import kotlinx.coroutines.suspendCancellableCoroutine
import okhttp3.Call
import okhttp3.Callback
import okhttp3.Response
import java.io.IOException
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
suspend fun Call.await(): Response = suspendCancellableCoroutine { block ->
block.invokeOnCancellation {
cancel()
}
enqueue(object : Callback {
override fun onFailure(call: Call, e: IOException) {
block.resumeWithException(e)
}
override fun onResponse(call: Call, response: Response) {
block.resume(response)
}
})
}
Loading…
Cancel
Save