Replace Pair<HttpUrl, Document> with LoginModuleResult
This commit is contained in:
parent
660e075cce
commit
2cadf274f1
6 changed files with 42 additions and 19 deletions
|
@ -14,6 +14,7 @@ import io.github.wulkanowy.sdk.scrapper.exception.VulcanClientError
|
|||
import io.github.wulkanowy.sdk.scrapper.exception.VulcanServerError
|
||||
import io.github.wulkanowy.sdk.scrapper.getModuleHeadersFromDocument
|
||||
import io.github.wulkanowy.sdk.scrapper.isAnyMappingAvailable
|
||||
import io.github.wulkanowy.sdk.scrapper.login.LoginModuleResult
|
||||
import io.github.wulkanowy.sdk.scrapper.login.LoginResult
|
||||
import io.github.wulkanowy.sdk.scrapper.login.ModuleHeaders
|
||||
import io.github.wulkanowy.sdk.scrapper.login.NotLoggedInException
|
||||
|
@ -52,7 +53,7 @@ internal class AutoLoginInterceptor(
|
|||
private val cookieJarCabinet: CookieJarCabinet,
|
||||
private val emptyCookieJarIntercept: Boolean = false,
|
||||
private val notLoggedInCallback: suspend () -> LoginResult,
|
||||
private val fetchModuleCookies: (UrlGenerator.Site) -> Pair<HttpUrl, Document>,
|
||||
private val fetchModuleCookies: (UrlGenerator.Site) -> LoginModuleResult,
|
||||
private val json: Json,
|
||||
private val headersByHost: MutableMap<String, ModuleHeaders?> = mutableMapOf(),
|
||||
private val loginLock: ReentrantLock = ReentrantLock(true),
|
||||
|
@ -135,7 +136,7 @@ internal class AutoLoginInterceptor(
|
|||
}
|
||||
}
|
||||
|
||||
private fun getModuleCookies(site: UrlGenerator.Site): Result<Pair<HttpUrl, Document>> {
|
||||
private fun getModuleCookies(site: UrlGenerator.Site): Result<LoginModuleResult> {
|
||||
return runCatching { fetchModuleCookies(site) }
|
||||
.onFailure { logger.error("Error in $site login", it) }
|
||||
.onSuccess { (url, doc) -> saveModuleHeaders(doc, url) }
|
||||
|
|
|
@ -18,10 +18,8 @@ import io.github.wulkanowy.sdk.scrapper.isCurrentLoginHasEduOne
|
|||
import io.github.wulkanowy.sdk.scrapper.register.HomePageResponse
|
||||
import io.github.wulkanowy.sdk.scrapper.repository.AccountRepository.Companion.SELECTOR_ADFS
|
||||
import io.github.wulkanowy.sdk.scrapper.service.LoginService
|
||||
import okhttp3.HttpUrl
|
||||
import okhttp3.HttpUrl.Companion.toHttpUrl
|
||||
import org.jsoup.Jsoup
|
||||
import org.jsoup.nodes.Document
|
||||
import org.jsoup.nodes.Element
|
||||
import org.slf4j.LoggerFactory
|
||||
import pl.droidsonroids.jspoon.Jspoon
|
||||
|
@ -119,7 +117,7 @@ internal class LoginHelper(
|
|||
}
|
||||
}
|
||||
|
||||
fun loginModule(site: UrlGenerator.Site): Pair<HttpUrl, Document> {
|
||||
fun loginModule(site: UrlGenerator.Site): LoginModuleResult {
|
||||
val moduleUrl = urlGenerator.generate(site) + "LoginEndpoint.aspx"
|
||||
val startHtml = api.getModuleStart(moduleUrl).execute().handleErrors().body().orEmpty()
|
||||
val startDoc = Jsoup.parse(startHtml)
|
||||
|
@ -140,11 +138,17 @@ internal class LoginHelper(
|
|||
throw IOException("Unknown module start page: ${certResponseDoc.title()}")
|
||||
} else {
|
||||
logger.debug("{} cookies fetch successfully!", site)
|
||||
return moduleUrl.toHttpUrl() to Jsoup.parse(certResponseHtml)
|
||||
return LoginModuleResult(
|
||||
moduleUrl = moduleUrl.toHttpUrl(),
|
||||
document = Jsoup.parse(certResponseHtml),
|
||||
)
|
||||
}
|
||||
} else {
|
||||
logger.debug("{} cookies already fetched!", site)
|
||||
return moduleUrl.toHttpUrl() to startDoc
|
||||
return LoginModuleResult(
|
||||
moduleUrl = moduleUrl.toHttpUrl(),
|
||||
document = startDoc,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
package io.github.wulkanowy.sdk.scrapper.login
|
||||
|
||||
import okhttp3.HttpUrl
|
||||
import org.jsoup.nodes.Document
|
||||
|
||||
internal data class LoginModuleResult(
|
||||
val moduleUrl: HttpUrl,
|
||||
val document: Document,
|
||||
)
|
|
@ -14,6 +14,7 @@ import io.github.wulkanowy.sdk.scrapper.isCurrentLoginHasEduOne
|
|||
import io.github.wulkanowy.sdk.scrapper.login.CertificateResponse
|
||||
import io.github.wulkanowy.sdk.scrapper.login.InvalidSymbolException
|
||||
import io.github.wulkanowy.sdk.scrapper.login.LoginHelper
|
||||
import io.github.wulkanowy.sdk.scrapper.login.LoginModuleResult
|
||||
import io.github.wulkanowy.sdk.scrapper.login.NotLoggedInException
|
||||
import io.github.wulkanowy.sdk.scrapper.login.UrlGenerator
|
||||
import io.github.wulkanowy.sdk.scrapper.mapModuleUrl
|
||||
|
@ -35,6 +36,7 @@ import io.github.wulkanowy.sdk.scrapper.service.StudentService
|
|||
import io.github.wulkanowy.sdk.scrapper.service.SymbolService
|
||||
import okhttp3.HttpUrl.Companion.toHttpUrl
|
||||
import org.jsoup.Jsoup
|
||||
import org.jsoup.nodes.Document
|
||||
import org.jsoup.nodes.Element
|
||||
import org.jsoup.parser.Parser
|
||||
import org.jsoup.select.Elements
|
||||
|
@ -176,14 +178,17 @@ internal class RegisterRepository(
|
|||
isEduOne -> UrlGenerator.Site.STUDENT_PLUS
|
||||
else -> UrlGenerator.Site.STUDENT
|
||||
}
|
||||
loginModule(site)
|
||||
loginHelper.loginModule(site)
|
||||
}
|
||||
|
||||
val registerStudents = runCatching {
|
||||
when {
|
||||
isEduOne -> {
|
||||
val (baseStudentPlus, homepage) = loginResult.getOrThrow()
|
||||
getEduOneDiaries(baseStudentPlus = baseStudentPlus, homepage = homepage)
|
||||
getEduOneDiaries(
|
||||
baseStudentPlus = baseStudentPlus.toString(),
|
||||
homepage = homepage,
|
||||
)
|
||||
}
|
||||
|
||||
else -> getStudentsFromOldModule(
|
||||
|
@ -226,12 +231,12 @@ internal class RegisterRepository(
|
|||
}
|
||||
|
||||
private suspend fun getStudentsFromOldModule(
|
||||
loginResult: Pair<String, String>,
|
||||
loginResult: LoginModuleResult,
|
||||
unitId: Int?,
|
||||
): List<RegisterStudent> {
|
||||
val (_, startPage) = loginResult
|
||||
val isParent = isStudentFromParentAccount(startPage)
|
||||
val diaries = getStudentDiaries(startPage)
|
||||
val isParent = isStudentFromParentAccount(startPage.toString())
|
||||
val diaries = getStudentDiaries(startPage.toString())
|
||||
return diaries.getStudentsFromDiaries(
|
||||
isParent = isParent,
|
||||
isEduOne = false,
|
||||
|
@ -240,7 +245,7 @@ internal class RegisterRepository(
|
|||
?: error("Can't find componentUnitId in student diaries"),
|
||||
).map {
|
||||
it.copy(
|
||||
schoolName = getScriptParam("organizationName", startPage),
|
||||
schoolName = getScriptParam("organizationName", startPage.toString()),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -342,8 +347,8 @@ internal class RegisterRepository(
|
|||
return userCache?.isParent
|
||||
}
|
||||
|
||||
private suspend fun getEduOneDiaries(baseStudentPlus: String, homepage: String): List<RegisterStudent> {
|
||||
val moduleHeaders = getModuleHeadersFromDocument(Jsoup.parse(homepage))
|
||||
private suspend fun getEduOneDiaries(baseStudentPlus: String, homepage: Document): List<RegisterStudent> {
|
||||
val moduleHeaders = getModuleHeadersFromDocument(homepage)
|
||||
|
||||
val contextUrl = (baseStudentPlus + "api/Context").toHttpUrl()
|
||||
val contextVToken = contextUrl.getMatchedVToken(StudentPlusModuleHost, moduleHeaders)
|
||||
|
|
|
@ -5,6 +5,7 @@ import io.github.wulkanowy.sdk.scrapper.interceptor.AutoLoginInterceptor
|
|||
import io.github.wulkanowy.sdk.scrapper.interceptor.ErrorInterceptor
|
||||
import io.github.wulkanowy.sdk.scrapper.interceptor.HttpErrorInterceptor
|
||||
import io.github.wulkanowy.sdk.scrapper.login.LoginHelper
|
||||
import io.github.wulkanowy.sdk.scrapper.login.LoginModuleResult
|
||||
import io.github.wulkanowy.sdk.scrapper.login.LoginResult
|
||||
import io.github.wulkanowy.sdk.scrapper.login.UrlGenerator
|
||||
import io.github.wulkanowy.sdk.scrapper.repository.StudentPlusRepository
|
||||
|
@ -158,7 +159,7 @@ abstract class BaseLocalTest : BaseTest() {
|
|||
).login("jan@fakelog.cf", "jan123")
|
||||
}
|
||||
},
|
||||
fetchModuleCookies = { _ -> "http://localhost".toHttpUrl() to Document("") },
|
||||
fetchModuleCookies = { _ -> LoginModuleResult("http://localhost".toHttpUrl(), Document("")) },
|
||||
json = json,
|
||||
)
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ import io.github.wulkanowy.sdk.scrapper.BaseLocalTest
|
|||
import io.github.wulkanowy.sdk.scrapper.CookieJarCabinet
|
||||
import io.github.wulkanowy.sdk.scrapper.Scrapper
|
||||
import io.github.wulkanowy.sdk.scrapper.login.LoginHelper
|
||||
import io.github.wulkanowy.sdk.scrapper.login.LoginModuleResult
|
||||
import io.github.wulkanowy.sdk.scrapper.login.LoginResult
|
||||
import io.github.wulkanowy.sdk.scrapper.login.LoginTest
|
||||
import io.github.wulkanowy.sdk.scrapper.login.UrlGenerator
|
||||
|
@ -17,7 +18,6 @@ import kotlinx.coroutines.awaitAll
|
|||
import kotlinx.coroutines.runBlocking
|
||||
import kotlinx.coroutines.supervisorScope
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import okhttp3.HttpUrl
|
||||
import okhttp3.HttpUrl.Companion.toHttpUrl
|
||||
import org.jsoup.Jsoup
|
||||
import org.jsoup.nodes.Document
|
||||
|
@ -152,7 +152,10 @@ class AutoLoginInterceptorTest : BaseLocalTest() {
|
|||
UrlGenerator.Site.MESSAGES -> "wiadomosciplus"
|
||||
else -> error("Not supported here")
|
||||
}
|
||||
"https://uonetplus-$subdomain.localhost".toHttpUrl() to Jsoup.parse(html)
|
||||
LoginModuleResult(
|
||||
moduleUrl = "https://uonetplus-$subdomain.localhost".toHttpUrl(),
|
||||
document = Jsoup.parse(html),
|
||||
)
|
||||
},
|
||||
)
|
||||
studentService.getUserCache()
|
||||
|
@ -184,7 +187,7 @@ class AutoLoginInterceptorTest : BaseLocalTest() {
|
|||
|
||||
private fun getService(
|
||||
checkJar: Boolean = false,
|
||||
fetchModuleCookies: (UrlGenerator.Site) -> Pair<HttpUrl, Document> = { _ -> "http://localhost".toHttpUrl() to Document("") },
|
||||
fetchModuleCookies: (UrlGenerator.Site) -> LoginModuleResult = { _ -> LoginModuleResult("http://localhost".toHttpUrl(), Document("")) },
|
||||
notLoggedInCallback: suspend () -> LoginResult = { loginHelper.login("", "") },
|
||||
): StudentService {
|
||||
val interceptor = AutoLoginInterceptor(
|
||||
|
|
Loading…
Reference in a new issue