[APIv2/Vulcan] Add Vulcan first login

This commit is contained in:
Kacper Ziubryniewicz 2019-10-20 00:00:50 +02:00
parent 92fb83ccf9
commit dd34e7d008
6 changed files with 133 additions and 27 deletions

View File

@ -152,22 +152,23 @@ class DataVulcan(app: App, profile: Profile?, loginStore: LoginStore) : Data(app
val apiUrl: String?
get() {
return when (apiToken?.substring(0, 3)) {
"3S1" -> "https://lekcjaplus.vulcan.net.pl/$symbol/"
"TA1" -> "https://uonetplus-komunikacja.umt.tarnow.pl/$symbol/"
"OP1" -> "https://uonetplus-komunikacja.eszkola.opolskie.pl/$symbol/"
"RZ1" -> "https://uonetplus-komunikacja.resman.pl/$symbol/"
"GD1" -> "https://uonetplus-komunikacja.edu.gdansk.pl/$symbol/"
"KA1" -> "https://uonetplus-komunikacja.mcuw.katowice.eu/$symbol/"
"KA2" -> "https://uonetplus-komunikacja-test.mcuw.katowice.eu/$symbol/"
"P03" -> "https://efeb-komunikacja-pro-efebmobile.pro.vulcan.pl/$symbol/"
"P01" -> "http://efeb-komunikacja.pro-hudson.win.vulcan.pl/$symbol/"
"P02" -> "http://efeb-komunikacja.pro-hudsonrc.win.vulcan.pl/$symbol/"
"P90" -> "http://efeb-komunikacja-pro-mwujakowska.neo.win.vulcan.pl/$symbol/"
"FK1", "FS1" -> "http://api.fakelog.cf/$symbol/"
"SZ9" -> "http://vulcan.szkolny.eu/$symbol/"
val url = when (apiToken?.substring(0, 3)) {
"3S1" -> "https://lekcjaplus.vulcan.net.pl/"
"TA1" -> "https://uonetplus-komunikacja.umt.tarnow.pl/"
"OP1" -> "https://uonetplus-komunikacja.eszkola.opolskie.pl/"
"RZ1" -> "https://uonetplus-komunikacja.resman.pl/"
"GD1" -> "https://uonetplus-komunikacja.edu.gdansk.pl/"
"KA1" -> "https://uonetplus-komunikacja.mcuw.katowice.eu/"
"KA2" -> "https://uonetplus-komunikacja-test.mcuw.katowice.eu/"
"P03" -> "https://efeb-komunikacja-pro-efebmobile.pro.vulcan.pl/"
"P01" -> "http://efeb-komunikacja.pro-hudson.win.vulcan.pl/"
"P02" -> "http://efeb-komunikacja.pro-hudsonrc.win.vulcan.pl/"
"P90" -> "http://efeb-komunikacja-pro-mwujakowska.neo.win.vulcan.pl/"
"FK1", "FS1" -> "http://api.fakelog.cf/"
"SZ9" -> "http://vulcan.szkolny.eu/"
else -> null
}
return if (url != null) "$url$symbol/" else null
}
val fullApiUrl: String?

View File

@ -12,6 +12,7 @@ import pl.szczodrzynski.edziennik.api.v2.interfaces.EdziennikInterface
import pl.szczodrzynski.edziennik.api.v2.models.ApiError
import pl.szczodrzynski.edziennik.api.v2.prepare
import pl.szczodrzynski.edziennik.api.v2.vulcan.data.VulcanData
import pl.szczodrzynski.edziennik.api.v2.vulcan.firstlogin.VulcanFirstLogin
import pl.szczodrzynski.edziennik.api.v2.vulcan.login.VulcanLogin
import pl.szczodrzynski.edziennik.api.v2.vulcanLoginMethods
import pl.szczodrzynski.edziennik.data.db.modules.login.LoginStore
@ -64,7 +65,9 @@ class Vulcan(val app: App, val profile: Profile?, val loginStore: LoginStore, va
}
override fun firstLogin() {
// TODO
VulcanFirstLogin(data) {
completed()
}
}
override fun cancel() {
@ -102,4 +105,4 @@ class Vulcan(val app: App, val profile: Profile?, val loginStore: LoginStore, va
}
}
}
}
}

View File

@ -27,8 +27,11 @@ open class VulcanApi(open val data: DataVulcan) {
val profile
get() = data.profile
fun apiGet(tag: String, endpoint: String, method: Int = POST, payload: JsonObject? = null, onSuccess: (json: JsonObject) -> Unit) {
d(tag, "Request: Librus/Api - ${data.fullApiUrl}/$endpoint")
fun apiGet(tag: String, endpoint: String, method: Int = POST, payload: JsonObject? = null,
baseUrl: Boolean = false, onSuccess: (json: JsonObject, response: Response?) -> Unit) {
val url = "${if (baseUrl) data.apiUrl else data.fullApiUrl}$endpoint"
d(tag, "Request: Librus/Api - $url")
val finalPayload = JsonObject()
finalPayload.addProperty("IdUczen", data.studentId)
@ -57,7 +60,7 @@ open class VulcanApi(open val data: DataVulcan) {
}
try {
onSuccess(json)
onSuccess(json, response)
} catch (e: Exception) {
data.error(ApiError(tag, EXCEPTION_VULCAN_API_REQUEST)
.withResponse(response)
@ -74,7 +77,7 @@ open class VulcanApi(open val data: DataVulcan) {
}
Request.builder()
.url("${data.fullApiUrl}$endpoint")
.url(url)
.userAgent(VULCAN_API_USER_AGENT)
.addHeader("RequestCertificateKey", data.apiCertificateKey)
.addHeader("RequestSignatureValue",

View File

@ -17,7 +17,7 @@ class VulcanApiGrades(override val data: DataVulcan, val onSuccess: () -> Unit)
}
init {
apiGet(TAG, VULCAN_API_ENDPOINT_GRADES) { json ->
apiGet(TAG, VULCAN_API_ENDPOINT_GRADES) { json, _ ->
val grades = json.getJsonArray("Data")
data.setSyncNext(ENDPOINT_VULCAN_API_GRADES, SYNC_ALWAYS)

View File

@ -0,0 +1,104 @@
/*
* Copyright (c) Kacper Ziubryniewicz 2019-10-19
*/
package pl.szczodrzynski.edziennik.api.v2.vulcan.firstlogin
import org.greenrobot.eventbus.EventBus
import pl.szczodrzynski.edziennik.api.v2.ERROR_NO_STUDENTS_IN_ACCOUNT
import pl.szczodrzynski.edziennik.api.v2.VULCAN_API_ENDPOINT_STUDENT_LIST
import pl.szczodrzynski.edziennik.api.v2.events.FirstLoginFinishedEvent
import pl.szczodrzynski.edziennik.api.v2.models.ApiError
import pl.szczodrzynski.edziennik.api.v2.vulcan.DataVulcan
import pl.szczodrzynski.edziennik.api.v2.vulcan.data.VulcanApi
import pl.szczodrzynski.edziennik.api.v2.vulcan.login.VulcanLoginApi
import pl.szczodrzynski.edziennik.data.db.modules.profiles.Profile
import pl.szczodrzynski.edziennik.getInt
import pl.szczodrzynski.edziennik.getJsonArray
import pl.szczodrzynski.edziennik.getString
import pl.szczodrzynski.edziennik.utils.models.Date
class VulcanFirstLogin(val data: DataVulcan, val onSuccess: () -> Unit) {
companion object {
const val TAG = "VulcanFirstLogin"
}
private val api = VulcanApi(data)
private val profileList = mutableListOf<Profile>()
init {
VulcanLoginApi(data) {
api.apiGet(TAG, VULCAN_API_ENDPOINT_STUDENT_LIST, baseUrl = true) { json, response ->
val students = json.getJsonArray("Data")
if (students == null || students.size() < 1) {
data.error(ApiError(TAG, ERROR_NO_STUDENTS_IN_ACCOUNT)
.withResponse(response)
.withApiResponse(json))
return@apiGet
}
students.forEach { studentEl ->
val student = studentEl.asJsonObject
val studentId = student.getInt("Id") ?: return@forEach
val studentLoginId = student.getInt("UzytkownikLoginId") ?: return@forEach
val studentClassId = student.getInt("IdOddzial") ?: return@forEach
val studentClassNumber = student.getString("OkresPoziom")
val studentClassSymbol = student.getString("OddzialSymbol")
val studentClassName = "$studentClassNumber$studentClassSymbol"
val studentSemesterId = student.getInt("IdOkresKlasyfikacyjny")
?: return@forEach
val studentFirstName = student.getString("Imie")
val studentLastName = student.getString("Nazwisko")
val studentNameLong = "$studentFirstName $studentLastName"
val studentNameShort = "$studentFirstName ${studentLastName?.get(0)}."
val userName = student.getString("UzytkownikNazwa") ?: ""
val userLogin = student.getString("UzytkownikLogin") ?: ""
val schoolSymbol = student.getString("JednostkaSprawozdawczaSymbol")
?: return@forEach
val schoolName = "${data.symbol}_$schoolSymbol"
val currentSemesterStartDate = student.getInt("OkresDataOd") ?: return@forEach
val currentSemesterEndDate = (student.getInt("OkresDataDo")
?: return@forEach) + 86400
val studentSemesterNumber = student.getInt("OkresNumer") ?: return@forEach
val newProfile = Profile()
newProfile.empty = true
newProfile.putStudentData("studentId", studentId)
newProfile.putStudentData("studentLoginId", studentLoginId)
newProfile.putStudentData("studentClassId", studentClassId)
newProfile.putStudentData("studentClassName", studentClassName)
newProfile.putStudentData("studentSemesterId", studentSemesterId)
newProfile.putStudentData("userName", userName)
newProfile.putStudentData("schoolSymbol", schoolSymbol)
newProfile.putStudentData("schoolName", schoolName)
newProfile.putStudentData("currentSemesterEndDate", currentSemesterEndDate)
newProfile.putStudentData("studentSemesterNumber", studentSemesterNumber)
when (studentSemesterNumber) {
1 -> {
newProfile.dateSemester1Start = Date.fromMillis((currentSemesterStartDate * 1000).toLong())
newProfile.dateSemester2Start = Date.fromMillis((currentSemesterEndDate * 1000).toLong())
}
2 -> {
newProfile.dateSemester2Start = Date.fromMillis((currentSemesterStartDate * 1000).toLong())
newProfile.dateYearEnd = Date.fromMillis((currentSemesterEndDate * 1000).toLong())
}
}
newProfile.studentNameLong = studentNameLong
newProfile.studentNameShort = studentNameShort
newProfile.name = studentNameLong
newProfile.subname = userLogin
profileList.add(newProfile)
}
EventBus.getDefault().post(FirstLoginFinishedEvent(profileList, data.loginStore))
onSuccess()
}
}
}
}

View File

@ -24,12 +24,7 @@ class VulcanLoginApi(val data: DataVulcan, val onSuccess: () -> Unit) {
}
init { run {
if (data.profile == null) {
data.error(ApiError(TAG, ERROR_PROFILE_MISSING))
return@run
}
if (data.isApiLoginValid()) {
if (data.profile != null && data.isApiLoginValid()) {
onSuccess()
}
else {