Skip to content

Commit

Permalink
Delay 200ms to avoid reaching max requests
Browse files Browse the repository at this point in the history
  • Loading branch information
qhy040404 committed Jun 26, 2023
1 parent 1c058db commit 01be183
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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<String, String>().toHeaders(),
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -315,7 +320,9 @@ object Requests {
URLManager.EDU_CHECK_URL,
shouldHas = "person"
).also {
initEduEval()
if (it) {
initEduEval()
}
eduInitialized = it
return it
}
Expand All @@ -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(
Expand Down

0 comments on commit 01be183

Please sign in to comment.