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