aboutsummaryrefslogtreecommitdiff
path: root/shared
diff options
context:
space:
mode:
authorIsidro Henoch <imhenoch@protonmail.com>2021-12-06 02:33:20 -0600
committerIsidro Henoch <imhenoch@protonmail.com>2021-12-06 02:33:20 -0600
commit4972774bb3d2b96a47b17c89d4bc6f9aeac27e25 (patch)
tree6a12ad7014afd141f774198385d8ec81810256f5 /shared
parent93c204bcd190b242f1dea49e52f28c795e4d0b92 (diff)
downloadetbsa-trackermap-mobile-4972774bb3d2b96a47b17c89d4bc6f9aeac27e25.tar.gz
etbsa-trackermap-mobile-4972774bb3d2b96a47b17c89d4bc6f9aeac27e25.tar.bz2
etbsa-trackermap-mobile-4972774bb3d2b96a47b17c89d4bc6f9aeac27e25.zip
WIP: Adds the token persistance
Diffstat (limited to 'shared')
-rw-r--r--shared/build.gradle.kts4
-rw-r--r--shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/SessionApi.kt75
-rw-r--r--shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/infrastructure/ApiClient.kt13
3 files changed, 66 insertions, 26 deletions
diff --git a/shared/build.gradle.kts b/shared/build.gradle.kts
index 4c6e870..6853a0c 100644
--- a/shared/build.gradle.kts
+++ b/shared/build.gradle.kts
@@ -6,6 +6,7 @@ plugins {
kotlin {
val ktor_version = "1.6.6"
+ val settings_version = "0.8.1"
android()
@@ -28,6 +29,9 @@ kotlin {
implementation("io.ktor:ktor-client-serialization:$ktor_version")
implementation("ch.qos.logback:logback-classic:1.2.6")
+ implementation("com.russhwolf:multiplatform-settings:$settings_version")
+ implementation("com.russhwolf:multiplatform-settings-no-arg:$settings_version")
+
implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.3.1")
}
}
diff --git a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/SessionApi.kt b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/SessionApi.kt
index f80135d..4344de0 100644
--- a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/SessionApi.kt
+++ b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/SessionApi.kt
@@ -11,40 +11,45 @@
*/
package mx.trackermap.TrackerMap.client.apis
-import io.ktor.client.request.forms.FormDataContent
-import io.ktor.http.Parameters
+import com.russhwolf.settings.Settings
import mx.trackermap.TrackerMap.client.models.User
-
import mx.trackermap.TrackerMap.client.infrastructure.*
+val ACCESS_TOKEN_KEY = "access_token"
+
class SessionApi(basePath: kotlin.String = "https://demo.traccar.org/api") : ApiClient(basePath) {
/**
* Close the Session
- *
+ *
* @return void
*/
suspend fun sessionDelete(): Unit {
-
+
val localVariableConfig = RequestConfig(
- RequestMethod.DELETE,
- "/session"
+ RequestMethod.DELETE,
+ "/session"
)
val response = request<Any?>(
- localVariableConfig
+ localVariableConfig
)
return when (response.responseType) {
ResponseType.Success -> Unit
ResponseType.Informational -> TODO()
ResponseType.Redirection -> TODO()
- ResponseType.ClientError -> throw ClientException((response as ClientError<*>).body as? String ?: "Client error")
- ResponseType.ServerError -> throw ServerException((response as ServerError<*>).message ?: "Server error")
+ ResponseType.ClientError -> throw ClientException(
+ (response as ClientError<*>).body as? String ?: "Client error"
+ )
+ ResponseType.ServerError -> throw ServerException(
+ (response as ServerError<*>).message ?: "Server error"
+ )
}
}
+
/**
* Fetch Session information
- *
+ *
* @param token (optional)
* @return User
*/
@@ -52,47 +57,65 @@ class SessionApi(basePath: kotlin.String = "https://demo.traccar.org/api") : Api
suspend fun sessionGet(token: kotlin.String? = null): User {
val localVariableQuery: MultiValueMap = mapOf("token" to listOf("$token"))
val localVariableConfig = RequestConfig(
- RequestMethod.GET,
- "/session", query = localVariableQuery
+ RequestMethod.GET,
+ "/session", query = localVariableQuery
)
val response = request<User>(
- localVariableConfig
+ localVariableConfig
)
return when (response.responseType) {
ResponseType.Success -> (response as Success<*>).data as User
ResponseType.Informational -> TODO()
ResponseType.Redirection -> TODO()
- ResponseType.ClientError -> throw ClientException((response as ClientError<*>).body as? String ?: "Client error")
- ResponseType.ServerError -> throw ServerException((response as ServerError<*>).message ?: "Server error")
+ ResponseType.ClientError -> throw ClientException(
+ (response as ClientError<*>).body as? String ?: "Client error"
+ )
+ ResponseType.ServerError -> throw ServerException(
+ (response as ServerError<*>).message ?: "Server error"
+ )
}
}
+
/**
* Create a new Session
- *
- * @param email
- * @param password
+ *
+ * @param email
+ * @param password
* @return User
*/
@Suppress("UNCHECKED_CAST")
suspend fun sessionPost(email: String, password: String): User {
val localVariableBody = mapOf("email" to email, "password" to password)
-
+
val localVariableHeaders = mapOf("Content-Type" to "application/x-www-form-urlencoded")
val localVariableConfig = RequestConfig(
- RequestMethod.POST,
- "/session", headers = localVariableHeaders
+ RequestMethod.POST,
+ "/session", headers = localVariableHeaders
)
val response = request<User>(
- localVariableConfig, localVariableBody
+ localVariableConfig, localVariableBody
)
return when (response.responseType) {
- ResponseType.Success -> (response as Success<*>).data as User
+ ResponseType.Success -> {
+ val cookie = response.headers
+ .values
+ .flatten()
+ .find { it.contains("JSESSIONID") }!!
+ .replace("; Path=/", "")
+ val settings = Settings()
+ settings.putString(ACCESS_TOKEN_KEY, cookie)
+ (response as Success<*>).data as User
+ }
ResponseType.Informational -> TODO()
ResponseType.Redirection -> TODO()
- ResponseType.ClientError -> throw ClientException((response as ClientError<*>).body as? String ?: "Client error")
- ResponseType.ServerError -> throw ServerException((response as ServerError<*>).message ?: "Server error")
+ ResponseType.ClientError -> throw ClientException(
+ (response as ClientError<*>).body as? String ?: "Client error"
+ )
+ ResponseType.ServerError -> throw ServerException(
+ (response as ServerError<*>).message ?: "Server error"
+ )
}
}
}
diff --git a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/infrastructure/ApiClient.kt b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/infrastructure/ApiClient.kt
index 91d0aa2..4ec7b46 100644
--- a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/infrastructure/ApiClient.kt
+++ b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/infrastructure/ApiClient.kt
@@ -1,5 +1,7 @@
package mx.trackermap.TrackerMap.client.infrastructure
+import com.russhwolf.settings.Settings
+import com.russhwolf.settings.string
import io.ktor.client.*
import io.ktor.client.call.*
import io.ktor.client.engine.cio.*
@@ -14,6 +16,7 @@ import io.ktor.client.request.forms.FormDataContent
import io.ktor.client.statement.*
import io.ktor.http.*
import io.ktor.util.*
+import mx.trackermap.TrackerMap.client.apis.ACCESS_TOKEN_KEY
import kotlinx.serialization.json.Json as KotlinJson
open class ApiClient(val baseUrl: String) {
@@ -52,6 +55,13 @@ open class ApiClient(val baseUrl: String) {
)
}
+ var token: String = ""
+
+ init {
+ val settings = Settings()
+ token = settings.getString(ACCESS_TOKEN_KEY, "")
+ }
+
protected inline fun <reified T> fillRequest(
requestBuilder: HttpRequestBuilder,
content: T,
@@ -155,6 +165,9 @@ open class ApiClient(val baseUrl: String) {
}
}
+ if (token.isNotEmpty()) {
+ request.headers["Cookie"] = token
+ }
val response: HttpResponse = client.request(request)
// TODO: handle specific mapping types. e.g. Map<int, Class<?>>