[APIv2/Librus] Refactor structure

This commit is contained in:
Kuba Szczodrzyński 2019-10-05 12:28:45 +02:00
parent fabacfdcca
commit bb0a366ef1
9 changed files with 30 additions and 113 deletions

View File

@ -4,10 +4,10 @@
package pl.szczodrzynski.edziennik.api.v2 package pl.szczodrzynski.edziennik.api.v2
import pl.szczodrzynski.edziennik.api.v2.librus.login.LoginLibrusPortal import pl.szczodrzynski.edziennik.api.v2.librus.login.LibrusLoginPortal
import pl.szczodrzynski.edziennik.api.v2.librus.login.LoginLibrusApi import pl.szczodrzynski.edziennik.api.v2.librus.login.LibrusLoginApi
import pl.szczodrzynski.edziennik.api.v2.librus.login.LoginLibrusMessages import pl.szczodrzynski.edziennik.api.v2.librus.login.LibrusLoginMessages
import pl.szczodrzynski.edziennik.api.v2.librus.login.LoginLibrusSynergia import pl.szczodrzynski.edziennik.api.v2.librus.login.LibrusLoginSynergia
import pl.szczodrzynski.edziennik.api.v2.models.LoginMethod import pl.szczodrzynski.edziennik.api.v2.models.LoginMethod
val SYNERGIA_API_ENABLED = "true".toBoolean() val SYNERGIA_API_ENABLED = "true".toBoolean()
@ -39,13 +39,13 @@ const val LOGIN_METHOD_VULCAN_WEB = 100
const val LOGIN_METHOD_VULCAN_API = 200 const val LOGIN_METHOD_VULCAN_API = 200
val librusLoginMethods = listOf( val librusLoginMethods = listOf(
LoginMethod(LOGIN_TYPE_LIBRUS, LOGIN_METHOD_LIBRUS_PORTAL, LoginLibrusPortal::class.java) LoginMethod(LOGIN_TYPE_LIBRUS, LOGIN_METHOD_LIBRUS_PORTAL, LibrusLoginPortal::class.java)
.withIsPossible { _, loginStore -> .withIsPossible { _, loginStore ->
loginStore.mode == LOGIN_MODE_LIBRUS_EMAIL loginStore.mode == LOGIN_MODE_LIBRUS_EMAIL
} }
.withRequiredLoginMethod { _, _ -> LOGIN_METHOD_NOT_NEEDED }, .withRequiredLoginMethod { _, _ -> LOGIN_METHOD_NOT_NEEDED },
LoginMethod(LOGIN_TYPE_LIBRUS, LOGIN_METHOD_LIBRUS_API, LoginLibrusApi::class.java) LoginMethod(LOGIN_TYPE_LIBRUS, LOGIN_METHOD_LIBRUS_API, LibrusLoginApi::class.java)
.withIsPossible { _, loginStore -> .withIsPossible { _, loginStore ->
loginStore.mode != LOGIN_MODE_LIBRUS_SYNERGIA || SYNERGIA_API_ENABLED loginStore.mode != LOGIN_MODE_LIBRUS_SYNERGIA || SYNERGIA_API_ENABLED
} }
@ -53,13 +53,13 @@ val librusLoginMethods = listOf(
if (loginStore.mode == LOGIN_MODE_LIBRUS_EMAIL) LOGIN_METHOD_LIBRUS_PORTAL else LOGIN_METHOD_NOT_NEEDED if (loginStore.mode == LOGIN_MODE_LIBRUS_EMAIL) LOGIN_METHOD_LIBRUS_PORTAL else LOGIN_METHOD_NOT_NEEDED
}, },
LoginMethod(LOGIN_TYPE_LIBRUS, LOGIN_METHOD_LIBRUS_SYNERGIA, LoginLibrusSynergia::class.java) LoginMethod(LOGIN_TYPE_LIBRUS, LOGIN_METHOD_LIBRUS_SYNERGIA, LibrusLoginSynergia::class.java)
.withIsPossible { _, _ -> true } .withIsPossible { _, _ -> true }
.withRequiredLoginMethod { profile, _ -> .withRequiredLoginMethod { profile, _ ->
if (profile?.hasStudentData("accountPassword") == false) LOGIN_METHOD_LIBRUS_API else LOGIN_METHOD_NOT_NEEDED if (profile?.hasStudentData("accountPassword") == false) LOGIN_METHOD_LIBRUS_API else LOGIN_METHOD_NOT_NEEDED
}, },
LoginMethod(LOGIN_TYPE_LIBRUS, LOGIN_METHOD_LIBRUS_MESSAGES, LoginLibrusMessages::class.java) LoginMethod(LOGIN_TYPE_LIBRUS, LOGIN_METHOD_LIBRUS_MESSAGES, LibrusLoginMessages::class.java)
.withIsPossible { _, _ -> true } .withIsPossible { _, _ -> true }
.withRequiredLoginMethod { profile, _ -> .withRequiredLoginMethod { profile, _ ->
if (profile?.hasStudentData("accountPassword") == false) LOGIN_METHOD_LIBRUS_SYNERGIA else LOGIN_METHOD_NOT_NEEDED if (profile?.hasStudentData("accountPassword") == false) LOGIN_METHOD_LIBRUS_SYNERGIA else LOGIN_METHOD_NOT_NEEDED

View File

@ -12,6 +12,8 @@ import pl.szczodrzynski.edziennik.api.v2.endpoints
import pl.szczodrzynski.edziennik.api.v2.interfaces.EdziennikCallback import pl.szczodrzynski.edziennik.api.v2.interfaces.EdziennikCallback
import pl.szczodrzynski.edziennik.api.v2.interfaces.EdziennikInterface import pl.szczodrzynski.edziennik.api.v2.interfaces.EdziennikInterface
import pl.szczodrzynski.edziennik.api.v2.librus.data.DataLibrus import pl.szczodrzynski.edziennik.api.v2.librus.data.DataLibrus
import pl.szczodrzynski.edziennik.api.v2.librus.data.LibrusData
import pl.szczodrzynski.edziennik.api.v2.librus.login.LibrusLogin
import pl.szczodrzynski.edziennik.api.v2.librusLoginMethods import pl.szczodrzynski.edziennik.api.v2.librusLoginMethods
import pl.szczodrzynski.edziennik.api.v2.models.ApiError import pl.szczodrzynski.edziennik.api.v2.models.ApiError
import pl.szczodrzynski.edziennik.api.v2.models.Endpoint import pl.szczodrzynski.edziennik.api.v2.models.Endpoint
@ -122,7 +124,7 @@ class Librus(val app: App, val profile: Profile?, val loginStore: LoginStore, va
Log.d(TAG, "Endpoint IDs: ${data.targetEndpointIds}") Log.d(TAG, "Endpoint IDs: ${data.targetEndpointIds}")
LibrusLogin(data) { LibrusLogin(data) {
LibrusEndpoints(data) { LibrusData(data) {
completed() completed()
} }
} }

View File

@ -1,16 +1,17 @@
/* /*
* Copyright (c) Kuba Szczodrzyński 2019-10-1. * Copyright (c) Kuba Szczodrzyński 2019-10-5.
*/ */
package pl.szczodrzynski.edziennik.api.v2.librus package pl.szczodrzynski.edziennik.api.v2.librus.data
import pl.szczodrzynski.edziennik.R import pl.szczodrzynski.edziennik.R
import pl.szczodrzynski.edziennik.api.v2.ENDPOINT_LIBRUS_API_ME import pl.szczodrzynski.edziennik.api.v2.ENDPOINT_LIBRUS_API_ME
import pl.szczodrzynski.edziennik.api.v2.librus.data.DataLibrus import pl.szczodrzynski.edziennik.api.v2.ENDPOINT_LIBRUS_API_SCHOOLS
import pl.szczodrzynski.edziennik.api.v2.librus.data.api.LibrusApiMe import pl.szczodrzynski.edziennik.api.v2.librus.data.api.LibrusApiMe
import pl.szczodrzynski.edziennik.api.v2.librus.data.api.LibrusApiSchools
import pl.szczodrzynski.edziennik.utils.Utils import pl.szczodrzynski.edziennik.utils.Utils
class LibrusEndpoints(val data: DataLibrus, val onSuccess: () -> Unit) { class LibrusData(val data: DataLibrus, val onSuccess: () -> Unit) {
companion object { companion object {
private const val TAG = "LibrusEndpoints" private const val TAG = "LibrusEndpoints"
} }
@ -42,6 +43,10 @@ class LibrusEndpoints(val data: DataLibrus, val onSuccess: () -> Unit) {
data.startProgress(R.string.edziennik_progress_endpoint_student_info) data.startProgress(R.string.edziennik_progress_endpoint_student_info)
LibrusApiMe(data) { onSuccess() } LibrusApiMe(data) { onSuccess() }
} }
ENDPOINT_LIBRUS_API_SCHOOLS -> {
data.startProgress(R.string.edziennik_progress_endpoint_school_info)
LibrusApiSchools(data) { onSuccess() }
}
else -> onSuccess() else -> onSuccess()
} }
} }

View File

@ -1,8 +1,8 @@
/* /*
* Copyright (c) Kuba Szczodrzyński 2019-10-1. * Copyright (c) Kuba Szczodrzyński 2019-10-5.
*/ */
package pl.szczodrzynski.edziennik.api.v2.librus package pl.szczodrzynski.edziennik.api.v2.librus.login
import pl.szczodrzynski.edziennik.R import pl.szczodrzynski.edziennik.R
import pl.szczodrzynski.edziennik.api.v2.LOGIN_METHOD_LIBRUS_API import pl.szczodrzynski.edziennik.api.v2.LOGIN_METHOD_LIBRUS_API
@ -10,10 +10,6 @@ import pl.szczodrzynski.edziennik.api.v2.LOGIN_METHOD_LIBRUS_MESSAGES
import pl.szczodrzynski.edziennik.api.v2.LOGIN_METHOD_LIBRUS_PORTAL import pl.szczodrzynski.edziennik.api.v2.LOGIN_METHOD_LIBRUS_PORTAL
import pl.szczodrzynski.edziennik.api.v2.LOGIN_METHOD_LIBRUS_SYNERGIA import pl.szczodrzynski.edziennik.api.v2.LOGIN_METHOD_LIBRUS_SYNERGIA
import pl.szczodrzynski.edziennik.api.v2.librus.data.DataLibrus import pl.szczodrzynski.edziennik.api.v2.librus.data.DataLibrus
import pl.szczodrzynski.edziennik.api.v2.librus.login.LoginLibrusApi
import pl.szczodrzynski.edziennik.api.v2.librus.login.LoginLibrusMessages
import pl.szczodrzynski.edziennik.api.v2.librus.login.LoginLibrusPortal
import pl.szczodrzynski.edziennik.api.v2.librus.login.LoginLibrusSynergia
import pl.szczodrzynski.edziennik.utils.Utils import pl.szczodrzynski.edziennik.utils.Utils
class LibrusLogin(val data: DataLibrus, val onSuccess: () -> Unit) { class LibrusLogin(val data: DataLibrus, val onSuccess: () -> Unit) {
@ -53,19 +49,19 @@ class LibrusLogin(val data: DataLibrus, val onSuccess: () -> Unit) {
when (loginMethodId) { when (loginMethodId) {
LOGIN_METHOD_LIBRUS_PORTAL -> { LOGIN_METHOD_LIBRUS_PORTAL -> {
data.startProgress(R.string.edziennik_progress_login_librus_portal) data.startProgress(R.string.edziennik_progress_login_librus_portal)
LoginLibrusPortal(data) { onSuccess(loginMethodId) } LibrusLoginPortal(data) { onSuccess(loginMethodId) }
} }
LOGIN_METHOD_LIBRUS_API -> { LOGIN_METHOD_LIBRUS_API -> {
data.startProgress(R.string.edziennik_progress_login_librus_api) data.startProgress(R.string.edziennik_progress_login_librus_api)
LoginLibrusApi(data) { onSuccess(loginMethodId) } LibrusLoginApi(data) { onSuccess(loginMethodId) }
} }
LOGIN_METHOD_LIBRUS_SYNERGIA -> { LOGIN_METHOD_LIBRUS_SYNERGIA -> {
data.startProgress(R.string.edziennik_progress_login_librus_synergia) data.startProgress(R.string.edziennik_progress_login_librus_synergia)
LoginLibrusSynergia(data) { onSuccess(loginMethodId) } LibrusLoginSynergia(data) { onSuccess(loginMethodId) }
} }
LOGIN_METHOD_LIBRUS_MESSAGES -> { LOGIN_METHOD_LIBRUS_MESSAGES -> {
data.startProgress(R.string.edziennik_progress_login_librus_messages) data.startProgress(R.string.edziennik_progress_login_librus_messages)
LoginLibrusMessages(data) { onSuccess(loginMethodId) } LibrusLoginMessages(data) { onSuccess(loginMethodId) }
} }
} }
} }

View File

@ -15,7 +15,7 @@ import pl.szczodrzynski.edziennik.api.v2.librus.data.DataLibrus
import pl.szczodrzynski.edziennik.api.v2.models.ApiError import pl.szczodrzynski.edziennik.api.v2.models.ApiError
import java.net.HttpURLConnection.* import java.net.HttpURLConnection.*
class LoginLibrusApi { class LibrusLoginApi {
companion object { companion object {
private const val TAG = "LoginLibrusApi" private const val TAG = "LoginLibrusApi"
} }

View File

@ -16,7 +16,7 @@ import pl.szczodrzynski.edziennik.api.v2.models.ApiError
import pl.szczodrzynski.edziennik.currentTimeUnix import pl.szczodrzynski.edziennik.currentTimeUnix
import pl.szczodrzynski.edziennik.getUnixDate import pl.szczodrzynski.edziennik.getUnixDate
class LoginLibrusMessages(val data: DataLibrus, val onSuccess: () -> Unit) { class LibrusLoginMessages(val data: DataLibrus, val onSuccess: () -> Unit) {
companion object { companion object {
private const val TAG = "LoginLibrusMessages" private const val TAG = "LoginLibrusMessages"
} }

View File

@ -15,7 +15,7 @@ import java.net.HttpURLConnection.HTTP_UNAUTHORIZED
import java.util.ArrayList import java.util.ArrayList
import java.util.regex.Pattern import java.util.regex.Pattern
class LoginLibrusPortal(val data: DataLibrus, val onSuccess: () -> Unit) { class LibrusLoginPortal(val data: DataLibrus, val onSuccess: () -> Unit) {
companion object { companion object {
private const val TAG = "LoginLibrusPortal" private const val TAG = "LoginLibrusPortal"
} }

View File

@ -17,7 +17,7 @@ import pl.szczodrzynski.edziennik.getString
import pl.szczodrzynski.edziennik.getUnixDate import pl.szczodrzynski.edziennik.getUnixDate
import java.net.HttpURLConnection import java.net.HttpURLConnection
class LoginLibrusSynergia(val data: DataLibrus, val onSuccess: () -> Unit) { class LibrusLoginSynergia(val data: DataLibrus, val onSuccess: () -> Unit) {
companion object { companion object {
private const val TAG = "LoginLibrusSynergia" private const val TAG = "LoginLibrusSynergia"
} }

View File

@ -1,86 +0,0 @@
/*
* Copyright (c) Kuba Szczodrzyński 2019-9-21.
*/
package pl.szczodrzynski.edziennik.api.v2.librus.login
import pl.szczodrzynski.edziennik.R
import pl.szczodrzynski.edziennik.api.v2.*
import pl.szczodrzynski.edziennik.api.v2.librus.data.DataLibrus
import pl.szczodrzynski.edziennik.api.v2.models.LoginMethod
import pl.szczodrzynski.edziennik.utils.Utils.d
import kotlin.math.log
class LoginLibrus(val data: DataLibrus, vararg loginMethodIds: Int, val onSuccess: () -> Unit) {
companion object {
private const val TAG = "LoginLibrus"
}
private var loginMethodList = mutableListOf<Int>()
init {
for (loginMethodId in loginMethodIds) {
var requiredLoginMethod: Int? = loginMethodId
while (requiredLoginMethod != LOGIN_METHOD_NOT_NEEDED) {
librusLoginMethods.singleOrNull { it.loginMethodId == requiredLoginMethod }?.let { loginMethod ->
if (requiredLoginMethod != null)
loginMethodList.add(requiredLoginMethod!!)
requiredLoginMethod = loginMethod.requiredLoginMethod(data.profile, data.loginStore)
}
}
}
loginMethodList = loginMethodList.toHashSet().toMutableList()
loginMethodList.sort()
//data.satisfyLoginMethods()
nextLoginMethod()
}
private fun nextLoginMethod() {
if (loginMethodList.isEmpty()) {
onSuccess()
return
}
useLoginMethod(loginMethodList.removeAt(0)) {
nextLoginMethod()
}
}
private fun useLoginMethod(loginMethodId: Int, onSuccess: () -> Unit) {
if (data.loginMethods.contains(loginMethodId)) {
onSuccess()
return
}
d(TAG, "Using login method $loginMethodId")
when (loginMethodId) {
LOGIN_METHOD_LIBRUS_PORTAL -> {
data.startProgress(R.string.edziennik_progress_login_librus_portal)
LoginLibrusPortal(data) {
data.loginMethods.add(loginMethodId)
onSuccess()
}
}
LOGIN_METHOD_LIBRUS_API -> {
data.startProgress(R.string.edziennik_progress_login_librus_api)
LoginLibrusApi(data) {
data.loginMethods.add(loginMethodId)
onSuccess()
}
}
LOGIN_METHOD_LIBRUS_SYNERGIA -> {
data.startProgress(R.string.edziennik_progress_login_librus_synergia)
LoginLibrusSynergia(data) {
data.loginMethods.add(loginMethodId)
onSuccess()
}
}
LOGIN_METHOD_LIBRUS_MESSAGES -> {
data.startProgress(R.string.edziennik_progress_login_librus_messages)
LoginLibrusMessages(data) {
data.loginMethods.add(loginMethodId)
onSuccess()
}
}
}
}
}