diff options
Diffstat (limited to 'shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/infrastructure')
3 files changed, 51 insertions, 26 deletions
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 dd61893..e9865e8 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 @@ -17,7 +17,6 @@ */ package mx.trackermap.TrackerMap.client.infrastructure -import com.russhwolf.settings.Settings import io.ktor.client.* import io.ktor.client.call.* import io.ktor.client.features.* @@ -32,12 +31,10 @@ 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 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" @@ -47,7 +44,7 @@ open class ApiClient( protected const val ApiFormURLType = "application/x-www-form-urlencoded" protected const val ApiXmlMediaType = "application/xml" - val client: HttpClient = HttpClient { + val client: HttpClient = HttpClientProvider().getHttpClient().config { install(HttpTimeout) { connectTimeoutMillis = 20_000 requestTimeoutMillis = 20_000 @@ -56,12 +53,13 @@ open class ApiClient( serializer = KotlinxSerializer( KotlinJson { ignoreUnknownKeys = true + useAlternativeNames = false } ) } install(Logging) { logger = Logger.DEFAULT - level = LogLevel.ALL + level = LogLevel.INFO } } @@ -70,21 +68,6 @@ open class ApiClient( ApiContentType to ApiJsonMediaType, ApiAccept to ApiJsonMediaType ) - - val jsonHeaders: Map<String, String> = - mapOf( - ApiContentType to ApiJsonMediaType, - ApiAccept to ApiJsonMediaType - ) - } - - 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( @@ -130,7 +113,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.") } @@ -147,11 +130,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.") } @@ -190,8 +173,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/HttpClientProvider.kt b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/infrastructure/HttpClientProvider.kt new file mode 100644 index 0000000..873722f --- /dev/null +++ b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/infrastructure/HttpClientProvider.kt @@ -0,0 +1,7 @@ +package mx.trackermap.TrackerMap.client.infrastructure + +import io.ktor.client.* + +expect class HttpClientProvider() { + fun getHttpClient(): HttpClient +}
\ No newline at end of file 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..caf2da1 --- /dev/null +++ b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/infrastructure/SessionManager.kt @@ -0,0 +1,35 @@ +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.Injectable + +class SessionManager( + private val defaultBaseUrl: String +): Injectable { + + companion object { + const val SERVER_URL_KEY = "server_url" + const val ACCESS_TOKEN_KEY = "access_token" + } + + 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 + } + + fun clearSession() { + settings.remove(ACCESS_TOKEN_KEY) + settings.remove(SERVER_URL_KEY) + } +}
\ No newline at end of file |