aboutsummaryrefslogtreecommitdiff
path: root/shared/src
diff options
context:
space:
mode:
Diffstat (limited to 'shared/src')
-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
2 files changed, 62 insertions, 26 deletions
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<?>>