aboutsummaryrefslogtreecommitdiff
path: root/shared/src/commonMain
diff options
context:
space:
mode:
authorIván Ávalos <avalos@disroot.org>2022-01-25 22:49:10 -0600
committerIván Ávalos <avalos@disroot.org>2022-01-25 22:49:10 -0600
commitb4785c42aa026a561674257955a7c59b8c964e6f (patch)
tree1c4e3e9f74e92a222e9f0cffae0457befe39741e /shared/src/commonMain
parent4a85207614840d340167000e4d66649fdd462901 (diff)
downloadetbsa-trackermap-mobile-b4785c42aa026a561674257955a7c59b8c964e6f.tar.gz
etbsa-trackermap-mobile-b4785c42aa026a561674257955a7c59b8c964e6f.tar.bz2
etbsa-trackermap-mobile-b4785c42aa026a561674257955a7c59b8c964e6f.zip
Refactored session handling into a shared SessionManager to prevent mutability and get it run in Native (untested!)
Diffstat (limited to 'shared/src/commonMain')
-rw-r--r--shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/AttributesApi.kt2
-rw-r--r--shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/CalendarsApi.kt2
-rw-r--r--shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/CommandsApi.kt2
-rw-r--r--shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/DevicesApi.kt2
-rw-r--r--shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/DriversApi.kt2
-rw-r--r--shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/EventsApi.kt2
-rw-r--r--shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/GeofencesApi.kt2
-rw-r--r--shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/GroupsApi.kt2
-rw-r--r--shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/MaintenanceApi.kt2
-rw-r--r--shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/NotificationsApi.kt2
-rw-r--r--shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/PermissionsApi.kt2
-rw-r--r--shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/PositionsApi.kt2
-rw-r--r--shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/ReportsApi.kt2
-rw-r--r--shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/ServerApi.kt2
-rw-r--r--shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/SessionApi.kt4
-rw-r--r--shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/StatisticsApi.kt2
-rw-r--r--shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/UsersApi.kt2
-rw-r--r--shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/infrastructure/ApiClient.kt21
-rw-r--r--shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/infrastructure/SessionManager.kt27
-rw-r--r--shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/SessionController.kt4
20 files changed, 52 insertions, 36 deletions
diff --git a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/AttributesApi.kt b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/AttributesApi.kt
index 2a67fd0..c324f4c 100644
--- a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/AttributesApi.kt
+++ b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/AttributesApi.kt
@@ -15,7 +15,7 @@ import mx.trackermap.TrackerMap.client.models.Attribute
import mx.trackermap.TrackerMap.client.infrastructure.*
-class AttributesApi(defaultBaseUrl: String) : ApiClient(defaultBaseUrl) {
+class AttributesApi(sessionManager: SessionManager) : ApiClient(sessionManager) {
/**
* Fetch a list of Attributes
diff --git a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/CalendarsApi.kt b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/CalendarsApi.kt
index 8dd2da8..7a9bd38 100644
--- a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/CalendarsApi.kt
+++ b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/CalendarsApi.kt
@@ -15,7 +15,7 @@ import mx.trackermap.TrackerMap.client.models.Calendar
import mx.trackermap.TrackerMap.client.infrastructure.*
-class CalendarsApi(defaultBaseUrl: String) : ApiClient(defaultBaseUrl) {
+class CalendarsApi(sessionManager: SessionManager) : ApiClient(sessionManager) {
/**
* Fetch a list of Calendars
diff --git a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/CommandsApi.kt b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/CommandsApi.kt
index 8ee7436..ffdb699 100644
--- a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/CommandsApi.kt
+++ b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/CommandsApi.kt
@@ -16,7 +16,7 @@ import mx.trackermap.TrackerMap.client.models.CommandType
import mx.trackermap.TrackerMap.client.infrastructure.*
-class CommandsApi(defaultBaseUrl: String) : ApiClient(defaultBaseUrl) {
+class CommandsApi(sessionManager: SessionManager) : ApiClient(sessionManager) {
/**
* Fetch a list of Saved Commands
diff --git a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/DevicesApi.kt b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/DevicesApi.kt
index 74d105f..eff69bd 100644
--- a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/DevicesApi.kt
+++ b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/DevicesApi.kt
@@ -16,7 +16,7 @@ import mx.trackermap.TrackerMap.client.models.DeviceAccumulators
import mx.trackermap.TrackerMap.client.infrastructure.*
-class DevicesApi(defaultBaseUrl: String) : ApiClient(defaultBaseUrl) {
+class DevicesApi(sessionManager: SessionManager) : ApiClient(sessionManager) {
/**
* Fetch a list of Devices
diff --git a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/DriversApi.kt b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/DriversApi.kt
index a9f47b8..f050bbe 100644
--- a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/DriversApi.kt
+++ b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/DriversApi.kt
@@ -15,7 +15,7 @@ import mx.trackermap.TrackerMap.client.models.Driver
import mx.trackermap.TrackerMap.client.infrastructure.*
-class DriversApi(defaultBaseUrl: String) : ApiClient(defaultBaseUrl) {
+class DriversApi(sessionManager: SessionManager) : ApiClient(sessionManager) {
/**
* Fetch a list of Drivers
diff --git a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/EventsApi.kt b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/EventsApi.kt
index 360d6e1..572fdd0 100644
--- a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/EventsApi.kt
+++ b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/EventsApi.kt
@@ -15,7 +15,7 @@ import mx.trackermap.TrackerMap.client.models.Event
import mx.trackermap.TrackerMap.client.infrastructure.*
-class EventsApi(defaultBaseUrl: String) : ApiClient(defaultBaseUrl) {
+class EventsApi(sessionManager: SessionManager) : ApiClient(sessionManager) {
/**
*
diff --git a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/GeofencesApi.kt b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/GeofencesApi.kt
index d7174e5..25996fe 100644
--- a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/GeofencesApi.kt
+++ b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/GeofencesApi.kt
@@ -15,7 +15,7 @@ import mx.trackermap.TrackerMap.client.models.Geofence
import mx.trackermap.TrackerMap.client.infrastructure.*
-class GeofencesApi(defaultBaseUrl: String) : ApiClient(defaultBaseUrl) {
+class GeofencesApi(sessionManager: SessionManager) : ApiClient(sessionManager) {
/**
* Fetch a list of Geofences
diff --git a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/GroupsApi.kt b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/GroupsApi.kt
index 620a25e..032b7fc 100644
--- a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/GroupsApi.kt
+++ b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/GroupsApi.kt
@@ -15,7 +15,7 @@ import mx.trackermap.TrackerMap.client.models.Group
import mx.trackermap.TrackerMap.client.infrastructure.*
-class GroupsApi(defaultBaseUrl: String) : ApiClient(defaultBaseUrl) {
+class GroupsApi(sessionManager: SessionManager) : ApiClient(sessionManager) {
/**
* Fetch a list of Groups
diff --git a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/MaintenanceApi.kt b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/MaintenanceApi.kt
index 5b10e9e..6dd10ec 100644
--- a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/MaintenanceApi.kt
+++ b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/MaintenanceApi.kt
@@ -15,7 +15,7 @@ import mx.trackermap.TrackerMap.client.models.Maintenance
import mx.trackermap.TrackerMap.client.infrastructure.*
-class MaintenanceApi(defaultBaseUrl: String) : ApiClient(defaultBaseUrl) {
+class MaintenanceApi(sessionManager: SessionManager) : ApiClient(sessionManager) {
/**
* Fetch a list of Maintenance
diff --git a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/NotificationsApi.kt b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/NotificationsApi.kt
index 290a14d..c27a532 100644
--- a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/NotificationsApi.kt
+++ b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/NotificationsApi.kt
@@ -16,7 +16,7 @@ import mx.trackermap.TrackerMap.client.models.NotificationType
import mx.trackermap.TrackerMap.client.infrastructure.*
-class NotificationsApi(defaultBaseUrl: String) : ApiClient(defaultBaseUrl) {
+class NotificationsApi(sessionManager: SessionManager) : ApiClient(sessionManager) {
/**
* Fetch a list of Notifications
diff --git a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/PermissionsApi.kt b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/PermissionsApi.kt
index 33aab48..0fb7651 100644
--- a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/PermissionsApi.kt
+++ b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/PermissionsApi.kt
@@ -15,7 +15,7 @@ import mx.trackermap.TrackerMap.client.models.Permission
import mx.trackermap.TrackerMap.client.infrastructure.*
-class PermissionsApi(defaultBaseUrl: String) : ApiClient(defaultBaseUrl) {
+class PermissionsApi(sessionManager: SessionManager) : ApiClient(sessionManager) {
/**
* Unlink an Object from another Object
diff --git a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/PositionsApi.kt b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/PositionsApi.kt
index 61285e9..3373448 100644
--- a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/PositionsApi.kt
+++ b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/PositionsApi.kt
@@ -15,7 +15,7 @@ import mx.trackermap.TrackerMap.client.models.Position
import mx.trackermap.TrackerMap.client.infrastructure.*
-class PositionsApi(defaultBaseUrl: String) : ApiClient(defaultBaseUrl) {
+class PositionsApi(sessionManager: SessionManager) : ApiClient(sessionManager) {
/**
* Fetches a list of Positions
diff --git a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/ReportsApi.kt b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/ReportsApi.kt
index eff40ad..67bff68 100644
--- a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/ReportsApi.kt
+++ b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/ReportsApi.kt
@@ -21,7 +21,7 @@ import mx.trackermap.TrackerMap.client.models.ReportTrips
import mx.trackermap.TrackerMap.client.infrastructure.*
-class ReportsApi(defaultBaseUrl: String) : ApiClient(defaultBaseUrl) {
+class ReportsApi(sessionManager: SessionManager) : ApiClient(sessionManager) {
/**
* Fetch a list of Events within the time period for the Devices or Groups
diff --git a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/ServerApi.kt b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/ServerApi.kt
index bf65dfe..ea7b911 100644
--- a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/ServerApi.kt
+++ b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/ServerApi.kt
@@ -15,7 +15,7 @@ import mx.trackermap.TrackerMap.client.models.Server
import mx.trackermap.TrackerMap.client.infrastructure.*
-class ServerApi(defaultBaseUrl: String) : ApiClient(defaultBaseUrl) {
+class ServerApi(sessionManager: SessionManager) : ApiClient(sessionManager) {
/**
* Fetch Server information
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 3f90c4c..a5a893e 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
@@ -18,7 +18,7 @@ import mx.trackermap.TrackerMap.client.infrastructure.*
const val SERVER_URL_KEY = "server_url"
const val ACCESS_TOKEN_KEY = "access_token"
-class SessionApi(defaultBaseUrl: String) : ApiClient(defaultBaseUrl) {
+class SessionApi(sessionManager: SessionManager) : ApiClient(sessionManager) {
/**
* Close the Session
@@ -107,7 +107,7 @@ class SessionApi(defaultBaseUrl: String) : ApiClient(defaultBaseUrl) {
.flatten()
.find { it.contains("JSESSIONID") }!!
.replace("; Path=/", "")
- this.token = cookie
+ this.sessionManager.token = cookie
val settings = Settings()
settings.putString(ACCESS_TOKEN_KEY, cookie)
(response as Success<*>).data as User
diff --git a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/StatisticsApi.kt b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/StatisticsApi.kt
index 296e589..1c7c5f5 100644
--- a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/StatisticsApi.kt
+++ b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/StatisticsApi.kt
@@ -16,7 +16,7 @@ import mx.trackermap.TrackerMap.client.models.Statistics
import mx.trackermap.TrackerMap.client.infrastructure.*
-class StatisticsApi(defaultBaseUrl: String) : ApiClient(defaultBaseUrl) {
+class StatisticsApi(sessionManager: SessionManager) : ApiClient(sessionManager) {
/**
* Fetch server Statistics
diff --git a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/UsersApi.kt b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/UsersApi.kt
index 51f2138..21e4bd3 100644
--- a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/UsersApi.kt
+++ b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/UsersApi.kt
@@ -15,7 +15,7 @@ import mx.trackermap.TrackerMap.client.models.User
import mx.trackermap.TrackerMap.client.infrastructure.*
-class UsersApi(defaultBaseUrl: String) : ApiClient(defaultBaseUrl) {
+class UsersApi(sessionManager: SessionManager) : ApiClient(sessionManager) {
/**
* Fetch a list of Users
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 0638dc9..385ece4 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
@@ -37,7 +37,7 @@ import mx.trackermap.TrackerMap.client.apis.SERVER_URL_KEY
import kotlinx.serialization.json.Json as KotlinJson
open class ApiClient(
- defaultBaseUrl: String = "",
+ val sessionManager: SessionManager
) {
companion object {
protected const val ApiContentType = "Content-Type"
@@ -79,15 +79,6 @@ open class ApiClient(
)
}
- var baseUrl: String = ""
- var token: String = ""
-
- init {
- val settings = Settings()
- baseUrl = settings.getString(SERVER_URL_KEY, defaultBaseUrl)
- token = settings.getString(ACCESS_TOKEN_KEY, "")
- }
-
protected inline fun <reified T> fillRequest(
requestBuilder: HttpRequestBuilder,
content: T,
@@ -131,7 +122,7 @@ open class ApiClient(
): ApiInfrastructureResponse<T?> {
val httpUrl: Url
try {
- httpUrl = Url(baseUrl)
+ httpUrl = Url(sessionManager.baseUrl)
} catch (e: URLDecodeException) {
throw IllegalStateException("baseUrl is invalid.")
}
@@ -148,11 +139,11 @@ open class ApiClient(
val url = urlBuilder.build()
val headers = defaultHeaders + requestConfig.headers
- if (headers[ApiContentType] ?: "" == "") {
+ if ((headers[ApiContentType] ?: "") == "") {
throw IllegalStateException("Missing Content-Type header. This is required.")
}
- if (headers[ApiAccept] ?: "" == "") {
+ if ((headers[ApiAccept] ?: "") == "") {
throw IllegalStateException("Missing Accept header. This is required.")
}
@@ -191,8 +182,8 @@ open class ApiClient(
}
}
- if (token.isNotEmpty()) {
- request.headers["Cookie"] = token
+ if (sessionManager.token.isNotEmpty()) {
+ request.headers["Cookie"] = sessionManager.token
}
val response: HttpResponse = client.request(request)
diff --git a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/infrastructure/SessionManager.kt b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/infrastructure/SessionManager.kt
new file mode 100644
index 0000000..306533c
--- /dev/null
+++ b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/infrastructure/SessionManager.kt
@@ -0,0 +1,27 @@
+package mx.trackermap.TrackerMap.client.infrastructure
+
+import com.russhwolf.settings.Settings
+import com.russhwolf.settings.get
+import com.russhwolf.settings.set
+import mx.trackermap.TrackerMap.client.apis.ACCESS_TOKEN_KEY
+import mx.trackermap.TrackerMap.client.apis.SERVER_URL_KEY
+
+class SessionManager(
+ private val defaultBaseUrl: String
+) {
+
+ private val settings = Settings()
+
+ var baseUrl: String
+ get() = settings[SERVER_URL_KEY] ?: defaultBaseUrl
+ set(baseUrl) {
+ settings[SERVER_URL_KEY] = baseUrl
+ }
+
+ var token: String
+ get() = settings[ACCESS_TOKEN_KEY] ?: ""
+ set(token) {
+ settings[ACCESS_TOKEN_KEY] = token
+ }
+
+} \ No newline at end of file
diff --git a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/SessionController.kt b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/SessionController.kt
index afa6dc2..2f196f1 100644
--- a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/SessionController.kt
+++ b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/SessionController.kt
@@ -95,11 +95,9 @@ class SessionController(
loginStateFlow.value = LoginState.Loading
GlobalScope.launch {
try {
- sessionApi.baseUrl = url
- usersApi.baseUrl = sessionApi.baseUrl
+ sessionApi.sessionManager.baseUrl = url
val session = sessionApi.sessionPost(email, password)
userFlow.value = session
- usersApi.token = sessionApi.token
token?.let { registerFcmToken(it) }
loginStateFlow.value = LoginState.Success
} catch (e: Exception) {