|
|
@ -4,7 +4,9 @@ import io.legado.app.constant.AppConst |
|
|
|
import io.legado.app.help.AppConfig |
|
|
|
import io.legado.app.help.AppConfig |
|
|
|
import io.legado.app.utils.EncodingDetect |
|
|
|
import io.legado.app.utils.EncodingDetect |
|
|
|
import io.legado.app.utils.UTF8BOMFighter |
|
|
|
import io.legado.app.utils.UTF8BOMFighter |
|
|
|
|
|
|
|
import kotlinx.coroutines.Dispatchers.IO |
|
|
|
import kotlinx.coroutines.suspendCancellableCoroutine |
|
|
|
import kotlinx.coroutines.suspendCancellableCoroutine |
|
|
|
|
|
|
|
import kotlinx.coroutines.withContext |
|
|
|
import okhttp3.* |
|
|
|
import okhttp3.* |
|
|
|
import okhttp3.HttpUrl.Companion.toHttpUrl |
|
|
|
import okhttp3.HttpUrl.Companion.toHttpUrl |
|
|
|
import okhttp3.MediaType.Companion.toMediaType |
|
|
|
import okhttp3.MediaType.Companion.toMediaType |
|
|
@ -18,34 +20,38 @@ suspend fun OkHttpClient.newCall( |
|
|
|
retry: Int = 0, |
|
|
|
retry: Int = 0, |
|
|
|
builder: Request.Builder.() -> Unit |
|
|
|
builder: Request.Builder.() -> Unit |
|
|
|
): ResponseBody { |
|
|
|
): ResponseBody { |
|
|
|
|
|
|
|
return withContext(IO) { |
|
|
|
val requestBuilder = Request.Builder() |
|
|
|
val requestBuilder = Request.Builder() |
|
|
|
requestBuilder.header(AppConst.UA_NAME, AppConfig.userAgent) |
|
|
|
requestBuilder.header(AppConst.UA_NAME, AppConfig.userAgent) |
|
|
|
requestBuilder.apply(builder) |
|
|
|
requestBuilder.apply(builder) |
|
|
|
var response: Response? = null |
|
|
|
var response: Response? = null |
|
|
|
for (i in 0..retry) { |
|
|
|
for (i in 0..retry) { |
|
|
|
response = this.newCall(requestBuilder.build()).await() |
|
|
|
response = this@newCall.newCall(requestBuilder.build()).await() |
|
|
|
if (response.isSuccessful) { |
|
|
|
if (response.isSuccessful) { |
|
|
|
return response.body!! |
|
|
|
return@withContext response.body!! |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
return response!!.body ?: throw IOException(response.message) |
|
|
|
return@withContext response!!.body ?: throw IOException(response.message) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
suspend fun OkHttpClient.newCallStrResponse( |
|
|
|
suspend fun OkHttpClient.newCallStrResponse( |
|
|
|
retry: Int = 0, |
|
|
|
retry: Int = 0, |
|
|
|
builder: Request.Builder.() -> Unit |
|
|
|
builder: Request.Builder.() -> Unit |
|
|
|
): StrResponse { |
|
|
|
): StrResponse { |
|
|
|
|
|
|
|
return withContext(IO) { |
|
|
|
val requestBuilder = Request.Builder() |
|
|
|
val requestBuilder = Request.Builder() |
|
|
|
requestBuilder.header(AppConst.UA_NAME, AppConfig.userAgent) |
|
|
|
requestBuilder.header(AppConst.UA_NAME, AppConfig.userAgent) |
|
|
|
requestBuilder.apply(builder) |
|
|
|
requestBuilder.apply(builder) |
|
|
|
var response: Response? = null |
|
|
|
var response: Response? = null |
|
|
|
for (i in 0..retry) { |
|
|
|
for (i in 0..retry) { |
|
|
|
response = this.newCall(requestBuilder.build()).await() |
|
|
|
response = this@newCallStrResponse.newCall(requestBuilder.build()).await() |
|
|
|
if (response.isSuccessful) { |
|
|
|
if (response.isSuccessful) { |
|
|
|
return StrResponse(response, response.body!!.text()) |
|
|
|
return@withContext StrResponse(response, response.body!!.text()) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return@withContext StrResponse(response!!, response.body?.text() ?: response.message) |
|
|
|
} |
|
|
|
} |
|
|
|
return StrResponse(response!!, response.body?.text() ?: response.message) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
suspend fun Call.await(): Response = suspendCancellableCoroutine { block -> |
|
|
|
suspend fun Call.await(): Response = suspendCancellableCoroutine { block -> |
|
|
|