From 01be183b7502cd7258d9023b09b4c3703c9f1b83 Mon Sep 17 00:00:00 2001 From: qhy040404 Date: Mon, 26 Jun 2023 16:32:49 +0800 Subject: [PATCH] Delay 200ms to avoid reaching max requests --- .../libraryonetap/constant/URLManager.kt | 2 +- .../libraryonetap/ui/tools/GradesActivity.kt | 2 +- .../libraryonetap/utils/web/Requests.kt | 19 +++++++++++++------ 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/com/qhy040404/libraryonetap/constant/URLManager.kt b/app/src/main/java/com/qhy040404/libraryonetap/constant/URLManager.kt index ecfc7352..d5f0cd52 100644 --- a/app/src/main/java/com/qhy040404/libraryonetap/constant/URLManager.kt +++ b/app/src/main/java/com/qhy040404/libraryonetap/constant/URLManager.kt @@ -49,7 +49,7 @@ object URLManager { const val VOLTIME_POST_URL = "https://www.dutbit.com/apivue/voltime/" const val VOLTIME_LATEST_URL = "https://www.dutbit.com/apivue/voltime/last-date" - const val EDU_TOP_DOMAIN = "http://jxgl.dlut.edu.cn" + const val EDU_DOMAIN = "http://jxgl.dlut.edu.cn" const val EDU_LOGIN_SSO_URL = "https://sso.dlut.edu.cn/cas/login?service=http%3A%2F%2Fjxgl.dlut.edu.cn%2Fstudent%2Fucas-sso%2Flogin" const val EDU_CHECK_URL = "http://jxgl.dlut.edu.cn/student/ws/student/home-page/students" diff --git a/app/src/main/java/com/qhy040404/libraryonetap/ui/tools/GradesActivity.kt b/app/src/main/java/com/qhy040404/libraryonetap/ui/tools/GradesActivity.kt index f52c14e9..edb197f0 100644 --- a/app/src/main/java/com/qhy040404/libraryonetap/ui/tools/GradesActivity.kt +++ b/app/src/main/java/com/qhy040404/libraryonetap/ui/tools/GradesActivity.kt @@ -142,7 +142,7 @@ class GradesActivity : BaseEduActivity(), MenuProvider { Requests.get( URLManager.getEduEvaluationTaskUrl(this.id), mapOf( - "Authorization" to CookieJarImpl.loadForRequest(URLManager.EDU_TOP_DOMAIN.toHttpUrl()) + "Authorization" to CookieJarImpl.loadForRequest(URLManager.EDU_DOMAIN.toHttpUrl()) .find { it.name == "student_evaluation_token" }?.value.orEmpty() ).toHeaders() ) diff --git a/app/src/main/java/com/qhy040404/libraryonetap/utils/web/Requests.kt b/app/src/main/java/com/qhy040404/libraryonetap/utils/web/Requests.kt index 7f438f0c..a59255fd 100644 --- a/app/src/main/java/com/qhy040404/libraryonetap/utils/web/Requests.kt +++ b/app/src/main/java/com/qhy040404/libraryonetap/utils/web/Requests.kt @@ -15,6 +15,7 @@ import com.qhy040404.libraryonetap.utils.extensions.toJson import com.qhy040404.libraryonetap.utils.lazy.resettableLazy import com.qhy040404.libraryonetap.utils.lazy.resettableManager import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.delay import kotlinx.coroutines.runBlocking import okhttp3.Cookie import okhttp3.Headers @@ -27,7 +28,6 @@ import okhttp3.RequestBody.Companion.toRequestBody import org.json.JSONObject import java.net.SocketTimeoutException import java.net.UnknownHostException -import java.util.concurrent.TimeUnit @Suppress("SpellCheckingInspection") object Requests { @@ -37,13 +37,12 @@ object Requests { val netLazyMgr = resettableManager() val client by resettableLazy(netLazyMgr) { OkHttpClient.Builder() - .connectTimeout(10, TimeUnit.SECONDS) - .readTimeout(15, TimeUnit.SECONDS) - .writeTimeout(15, TimeUnit.SECONDS) .cookieJar(CookieJarImpl) .build() } + private val EDU_HOST = URLManager.EDU_DOMAIN.toHttpUrl().host + fun get( url: String, headers: Headers = mapOf().toHeaders(), @@ -66,6 +65,9 @@ object Requests { .build() try { runBlocking(Dispatchers.IO) { + if (url.contains(EDU_HOST)) { + delay(200L) + } client.newCall(request).execute() }.use { response -> if (getUrl) { @@ -142,6 +144,9 @@ object Requests { .build() try { runBlocking(Dispatchers.IO) { + if (url.contains(EDU_HOST)) { + delay(200L) + } client.newCall(request).execute() }.use { response -> if (getUrl) { @@ -315,7 +320,9 @@ object Requests { URLManager.EDU_CHECK_URL, shouldHas = "person" ).also { - initEduEval() + if (it) { + initEduEval() + } eduInitialized = it return it } @@ -331,7 +338,7 @@ object Requests { ).let { JSONObject(it).optJSONObject("data")!!.optString("token") } - val url = URLManager.EDU_TOP_DOMAIN.toHttpUrl() + val url = URLManager.EDU_DOMAIN.toHttpUrl() CookieJarImpl.saveFromResponse( url, listOf(