Extract UrlGenerator to separate file

This commit is contained in:
Mikołaj Pich 2022-12-22 01:10:21 +01:00
parent 55b2283c33
commit 42dbd67a39
7 changed files with 51 additions and 38 deletions

View file

@ -7,7 +7,7 @@ plugins {
} }
ext { ext {
PUBLISH_VERSION = '1.8.3' PUBLISH_VERSION = '1.8.4-SNAPSHOT'
SITE_URL = 'https://github.com/wulkanowy/sdk' SITE_URL = 'https://github.com/wulkanowy/sdk'
GIT_URL = 'https://github.com/wulkanowy/sdk.git' GIT_URL = 'https://github.com/wulkanowy/sdk.git'

View file

@ -0,0 +1,33 @@
package io.github.wulkanowy.sdk.scrapper.login
import java.net.URL
class UrlGenerator(
private val schema: String,
private val host: String,
var symbol: String,
var schoolId: String,
) {
constructor(url: URL, symbol: String, schoolId: String) : this(url.protocol, url.host, symbol, schoolId)
enum class Site {
BASE, LOGIN, HOME, SNP, STUDENT, MESSAGES
}
fun generate(type: Site): String {
if (type == Site.BASE) return "$schema://$host"
return "$schema://${getSubDomain(type)}.$host/$symbol/${if (type == Site.SNP || type == Site.STUDENT) "$schoolId/" else ""}"
}
private fun getSubDomain(type: Site): String {
return when (type) {
Site.LOGIN -> "cufs"
Site.HOME -> "uonetplus"
Site.SNP -> "uonetplus-opiekun"
Site.STUDENT -> "uonetplus-uczen"
Site.MESSAGES -> "uonetplus-wiadomosciplus"
else -> error("unknown")
}
}
}

View file

@ -13,6 +13,7 @@ import io.github.wulkanowy.sdk.scrapper.exception.InvalidCaptchaException
import io.github.wulkanowy.sdk.scrapper.exception.InvalidEmailException import io.github.wulkanowy.sdk.scrapper.exception.InvalidEmailException
import io.github.wulkanowy.sdk.scrapper.exception.NoAccountFoundException import io.github.wulkanowy.sdk.scrapper.exception.NoAccountFoundException
import io.github.wulkanowy.sdk.scrapper.exception.PasswordResetErrorException import io.github.wulkanowy.sdk.scrapper.exception.PasswordResetErrorException
import io.github.wulkanowy.sdk.scrapper.login.UrlGenerator
import io.github.wulkanowy.sdk.scrapper.service.AccountService import io.github.wulkanowy.sdk.scrapper.service.AccountService
import io.github.wulkanowy.sdk.scrapper.service.ServiceManager import io.github.wulkanowy.sdk.scrapper.service.ServiceManager
import java.net.URL import java.net.URL
@ -83,7 +84,7 @@ class AccountRepository(private val account: AccountService) {
} }
return if (unlockUrl.first == AUTO) { return if (unlockUrl.first == AUTO) {
val loginType = getLoginType(ServiceManager.UrlGenerator(url, symbol, "")) val loginType = getLoginType(UrlGenerator(url, symbol, ""))
loginType to when (loginType) { loginType to when (loginType) {
STANDARD -> "https://cufs.vulcan.net.pl/$symbol/AccountManage/UnlockAccount" STANDARD -> "https://cufs.vulcan.net.pl/$symbol/AccountManage/UnlockAccount"
ADFSLightScoped -> "https://adfslight.vulcan.net.pl/$symbol/AccountManage/UnlockAccountRequest" ADFSLightScoped -> "https://adfslight.vulcan.net.pl/$symbol/AccountManage/UnlockAccountRequest"
@ -92,8 +93,8 @@ class AccountRepository(private val account: AccountService) {
} else unlockUrl } else unlockUrl
} }
private suspend fun getLoginType(urlGenerator: ServiceManager.UrlGenerator): Scrapper.LoginType { private suspend fun getLoginType(urlGenerator: UrlGenerator): Scrapper.LoginType {
val page = account.getFormType(urlGenerator.generate(ServiceManager.UrlGenerator.Site.LOGIN) + "Account/LogOn").page val page = account.getFormType(urlGenerator.generate(UrlGenerator.Site.LOGIN) + "Account/LogOn").page
return when { return when {
page.select(SELECTOR_STANDARD).isNotEmpty() -> STANDARD page.select(SELECTOR_STANDARD).isNotEmpty() -> STANDARD

View file

@ -10,6 +10,7 @@ import io.github.wulkanowy.sdk.scrapper.interceptor.handleErrors
import io.github.wulkanowy.sdk.scrapper.login.AccountPermissionException import io.github.wulkanowy.sdk.scrapper.login.AccountPermissionException
import io.github.wulkanowy.sdk.scrapper.login.CertificateResponse import io.github.wulkanowy.sdk.scrapper.login.CertificateResponse
import io.github.wulkanowy.sdk.scrapper.login.LoginHelper import io.github.wulkanowy.sdk.scrapper.login.LoginHelper
import io.github.wulkanowy.sdk.scrapper.login.UrlGenerator
import io.github.wulkanowy.sdk.scrapper.register.AuthInfo import io.github.wulkanowy.sdk.scrapper.register.AuthInfo
import io.github.wulkanowy.sdk.scrapper.register.Diary import io.github.wulkanowy.sdk.scrapper.register.Diary
import io.github.wulkanowy.sdk.scrapper.register.Permission import io.github.wulkanowy.sdk.scrapper.register.Permission
@ -37,7 +38,7 @@ class RegisterRepository(
private val loginHelper: LoginHelper, private val loginHelper: LoginHelper,
private val register: RegisterService, private val register: RegisterService,
private val student: StudentService, private val student: StudentService,
private val url: ServiceManager.UrlGenerator private val url: UrlGenerator
) { ) {
companion object { companion object {
@ -94,8 +95,8 @@ class RegisterRepository(
return getLoginType(url.also { it.symbol = symbol }) return getLoginType(url.also { it.symbol = symbol })
} }
private suspend fun getLoginType(urlGenerator: ServiceManager.UrlGenerator): Scrapper.LoginType { private suspend fun getLoginType(urlGenerator: UrlGenerator): Scrapper.LoginType {
val page = register.getFormType(urlGenerator.generate(ServiceManager.UrlGenerator.Site.LOGIN) + "Account/LogOn").page val page = register.getFormType(urlGenerator.generate(UrlGenerator.Site.LOGIN) + "Account/LogOn").page
return when { return when {
page.select(SELECTOR_STANDARD).isNotEmpty() -> Scrapper.LoginType.STANDARD page.select(SELECTOR_STANDARD).isNotEmpty() -> Scrapper.LoginType.STANDARD
page.select(SELECTOR_ADFS).isNotEmpty() -> Scrapper.LoginType.ADFS page.select(SELECTOR_ADFS).isNotEmpty() -> Scrapper.LoginType.ADFS
@ -126,7 +127,7 @@ class RegisterRepository(
url.schoolId = unit.symbol url.schoolId = unit.symbol
val studentStartPage = try { val studentStartPage = try {
student.getStart(url.generate(ServiceManager.UrlGenerator.Site.STUDENT) + "Start") student.getStart(url.generate(UrlGenerator.Site.STUDENT) + "Start")
} catch (e: TemporarilyDisabledException) { } catch (e: TemporarilyDisabledException) {
logger.debug("Start page is unavailable", e) logger.debug("Start page is unavailable", e)
return listOf() return listOf()
@ -153,7 +154,7 @@ class RegisterRepository(
schoolName = getScriptParam("organizationName", studentStartPage, "${unit.name} ${unit.short}"), schoolName = getScriptParam("organizationName", studentStartPage, "${unit.name} ${unit.short}"),
className = diary.symbol.orEmpty(), className = diary.symbol.orEmpty(),
classId = classId, classId = classId,
baseUrl = url.generate(ServiceManager.UrlGenerator.Site.BASE), baseUrl = url.generate(UrlGenerator.Site.BASE),
loginType = loginType, loginType = loginType,
isParent = cache?.isParent == true, isParent = cache?.isParent == true,
semesters = diaries.toSemesters(diary.studentId, classId, authInfo?.loginId ?: diary.studentId), semesters = diaries.toSemesters(diary.studentId, classId, authInfo?.loginId ?: diary.studentId),
@ -165,14 +166,14 @@ class RegisterRepository(
} }
private suspend fun getStudentCache(startPage: String) = student.getUserCache( private suspend fun getStudentCache(startPage: String) = student.getUserCache(
url.generate(ServiceManager.UrlGenerator.Site.STUDENT) + "UczenCache.mvc/Get", url.generate(UrlGenerator.Site.STUDENT) + "UczenCache.mvc/Get",
getScriptParam("antiForgeryToken", startPage), getScriptParam("antiForgeryToken", startPage),
getScriptParam("appGuid", startPage), getScriptParam("appGuid", startPage),
getScriptParam("version", startPage) getScriptParam("version", startPage)
).data ).data
private suspend fun getStudentDiaries() = student private suspend fun getStudentDiaries() = student
.getSchoolInfo(url.generate(ServiceManager.UrlGenerator.Site.STUDENT) + "UczenDziennik.mvc/Get") .getSchoolInfo(url.generate(UrlGenerator.Site.STUDENT) + "UczenDziennik.mvc/Get")
.handleErrors() .handleErrors()
.data.orEmpty() .data.orEmpty()

View file

@ -12,6 +12,7 @@ import io.github.wulkanowy.sdk.scrapper.interceptor.HttpErrorInterceptor
import io.github.wulkanowy.sdk.scrapper.interceptor.StudentCookieInterceptor import io.github.wulkanowy.sdk.scrapper.interceptor.StudentCookieInterceptor
import io.github.wulkanowy.sdk.scrapper.interceptor.UserAgentInterceptor import io.github.wulkanowy.sdk.scrapper.interceptor.UserAgentInterceptor
import io.github.wulkanowy.sdk.scrapper.login.LoginHelper import io.github.wulkanowy.sdk.scrapper.login.LoginHelper
import io.github.wulkanowy.sdk.scrapper.login.UrlGenerator
import kotlinx.serialization.ExperimentalSerializationApi import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
import kotlinx.serialization.modules.SerializersModule import kotlinx.serialization.modules.SerializersModule
@ -215,29 +216,4 @@ class ServiceManager(
} }
} }
} }
class UrlGenerator(private val schema: String, private val host: String, var symbol: String, var schoolId: String) {
constructor(url: URL, symbol: String, schoolId: String) : this(url.protocol, url.host, symbol, schoolId)
enum class Site {
BASE, LOGIN, HOME, SNP, STUDENT, MESSAGES
}
fun generate(type: Site): String {
if (type == Site.BASE) return "$schema://$host"
return "$schema://${getSubDomain(type)}.$host/$symbol/${if (type == Site.SNP || type == Site.STUDENT) "$schoolId/" else ""}"
}
private fun getSubDomain(type: Site): String {
return when (type) {
Site.LOGIN -> "cufs"
Site.HOME -> "uonetplus"
Site.SNP -> "uonetplus-opiekun"
Site.STUDENT -> "uonetplus-uczen"
Site.MESSAGES -> "uonetplus-wiadomosciplus"
else -> error("unknown")
}
}
}
} }

View file

@ -4,6 +4,7 @@ import io.github.wulkanowy.sdk.scrapper.BaseLocalTest
import io.github.wulkanowy.sdk.scrapper.Scrapper import io.github.wulkanowy.sdk.scrapper.Scrapper
import io.github.wulkanowy.sdk.scrapper.login.LoginHelper import io.github.wulkanowy.sdk.scrapper.login.LoginHelper
import io.github.wulkanowy.sdk.scrapper.login.LoginTest import io.github.wulkanowy.sdk.scrapper.login.LoginTest
import io.github.wulkanowy.sdk.scrapper.login.UrlGenerator
import io.github.wulkanowy.sdk.scrapper.repository.RegisterRepository import io.github.wulkanowy.sdk.scrapper.repository.RegisterRepository
import io.github.wulkanowy.sdk.scrapper.service.LoginService import io.github.wulkanowy.sdk.scrapper.service.LoginService
import io.github.wulkanowy.sdk.scrapper.service.RegisterService import io.github.wulkanowy.sdk.scrapper.service.RegisterService
@ -52,7 +53,7 @@ class RegisterTest : BaseLocalTest() {
) )
), ),
student = getService(StudentService::class.java, "http://fakelog.localhost:3000", false), student = getService(StudentService::class.java, "http://fakelog.localhost:3000", false),
url = ServiceManager.UrlGenerator("http", "fakelog.localhost:3000", "default", "123") url = UrlGenerator("http", "fakelog.localhost:3000", "default", "123")
) )
} }

View file

@ -6,6 +6,7 @@ import io.github.wulkanowy.sdk.scrapper.exception.VulcanException
import io.github.wulkanowy.sdk.scrapper.interceptor.ErrorInterceptorTest import io.github.wulkanowy.sdk.scrapper.interceptor.ErrorInterceptorTest
import io.github.wulkanowy.sdk.scrapper.login.LoginHelper import io.github.wulkanowy.sdk.scrapper.login.LoginHelper
import io.github.wulkanowy.sdk.scrapper.login.LoginTest import io.github.wulkanowy.sdk.scrapper.login.LoginTest
import io.github.wulkanowy.sdk.scrapper.login.UrlGenerator
import io.github.wulkanowy.sdk.scrapper.register.RegisterTest import io.github.wulkanowy.sdk.scrapper.register.RegisterTest
import io.github.wulkanowy.sdk.scrapper.service.LoginService import io.github.wulkanowy.sdk.scrapper.service.LoginService
import io.github.wulkanowy.sdk.scrapper.service.RegisterService import io.github.wulkanowy.sdk.scrapper.service.RegisterService
@ -39,7 +40,7 @@ class RegisterRepositoryTest : BaseLocalTest() {
okHttp = getOkHttp(errorInterceptor = false, autoLoginInterceptorOn = false) okHttp = getOkHttp(errorInterceptor = false, autoLoginInterceptorOn = false)
), ),
student = getService(service = StudentService::class.java, html = false), student = getService(service = StudentService::class.java, html = false),
url = ServiceManager.UrlGenerator("http", "fakelog.localhost:3000", symbol, "") url = UrlGenerator("http", "fakelog.localhost:3000", symbol, "")
) )
} }