diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/Constants.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/Constants.kt index 51cedea1..576b4912 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/Constants.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/Constants.kt @@ -38,6 +38,7 @@ const val LIBRUS_API_CLIENT_ID_JST = "49" const val LIBRUS_JST_DEMO_CODE = "68656A21" const val LIBRUS_JST_DEMO_PIN = "1290" +const val LIBRUS_SYNERGIA_URL = "https://synergia.librus.pl/" /** https://synergia.librus.pl/loguj/token/TOKEN/przenies */ const val LIBRUS_SYNERGIA_TOKEN_LOGIN_URL = "https://synergia.librus.pl/loguj/token/TOKEN/przenies" diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/Errors.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/Errors.kt index bad56ff6..b6fcc05d 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/Errors.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/Errors.kt @@ -125,6 +125,7 @@ const val EXCEPTION_LOGIN_LIBRUS_API_TOKEN = 901 const val EXCEPTION_LOGIN_LIBRUS_PORTAL_TOKEN = 902 const val EXCEPTION_LIBRUS_PORTAL_SYNERGIA_TOKEN = 903 const val EXCEPTION_LIBRUS_API_REQUEST = 904 -const val EXCEPTION_MOBIDZIENNIK_WEB_REQUEST = 905 -const val EXCEPTION_VULCAN_API_REQUEST = 906 +const val EXCEPTION_LIBRUS_SYNERGIA_REQUEST = 905 +const val EXCEPTION_MOBIDZIENNIK_WEB_REQUEST = 906 +const val EXCEPTION_VULCAN_API_REQUEST = 907 const val EXCEPTION_NOTIFY_AND_SYNC = 910 diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/LibrusSynergia.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/LibrusSynergia.kt new file mode 100644 index 00000000..9b449ce0 --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/LibrusSynergia.kt @@ -0,0 +1,77 @@ +/* + * Copyright (c) Kacper Ziubryniewicz 2019-10-21. + */ + +package pl.szczodrzynski.edziennik.api.v2.librus.data + +import im.wangchao.mhttp.Request +import im.wangchao.mhttp.Response +import im.wangchao.mhttp.callback.TextCallbackHandler +import okhttp3.Cookie +import pl.szczodrzynski.edziennik.api.v2.* +import pl.szczodrzynski.edziennik.api.v2.librus.DataLibrus +import pl.szczodrzynski.edziennik.api.v2.models.ApiError +import pl.szczodrzynski.edziennik.utils.Utils.d +import java.lang.Exception + +open class LibrusSynergia(open val data: DataLibrus) { + companion object { + const val TAG = "LibrusSynergia" + } + + val profileId + get() = data.profile?.id ?: -1 + + val profile + get() = data.profile + + fun apiGet(tag: String, endpoint: String, method: Int = GET, + parameters: Map = emptyMap(), onSuccess: (text: String) -> Unit) { + d(tag, "Request: Librus/Synergia - $LIBRUS_SYNERGIA_URL$endpoint") + + val callback = object : TextCallbackHandler() { + override fun onSuccess(text: String?, response: Response?) { + if (text.isNullOrEmpty()) { + data.error(ApiError(TAG, ERROR_RESPONSE_EMPTY) + .withResponse(response)) + return + } + + // TODO: Error handling + + try { + onSuccess(text) + } catch (e: Exception) { + data.error(ApiError(tag, EXCEPTION_LIBRUS_SYNERGIA_REQUEST) + .withResponse(response) + .withThrowable(e) + .withApiResponse(text)) + } + } + } + + data.app.cookieJar.saveFromResponse(null, listOf( + Cookie.Builder() + .name("DZIENNIKSID") + .value(data.synergiaSessionId!!) + .domain("synergia.librus.pl") + .secure().httpOnly().build() + )) + + Request.builder() + .url("$LIBRUS_SYNERGIA_URL$endpoint") + .userAgent(LIBRUS_USER_AGENT) + .apply { + when (method) { + GET -> get() + POST -> post() + } + parameters.map { (name, value) -> + addParameter(name, value) + } + } + .callback(callback) + .build() + .enqueue() + } +}