aboutsummaryrefslogtreecommitdiff
path: root/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/infrastructure/ApiClient.kt
diff options
context:
space:
mode:
Diffstat (limited to 'shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/infrastructure/ApiClient.kt')
-rw-r--r--shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/infrastructure/ApiClient.kt36
1 files changed, 29 insertions, 7 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 606fef0..dd61893 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,10 +1,26 @@
+/**
+ * TrackerMap
+ * Copyright (C) 2021-2022 Iván Ávalos <avalos@disroot.org>, Henoch Ojeda <imhenoch@protonmail.com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
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.*
+import io.ktor.client.features.*
import io.ktor.client.features.json.*
import io.ktor.client.features.json.serializer.KotlinxSerializer
import io.ktor.client.features.logging.DEFAULT
@@ -17,9 +33,12 @@ 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(val baseUrl: String) {
+open class ApiClient(
+ defaultBaseUrl: String = "",
+) {
companion object {
protected const val ApiContentType = "Content-Type"
protected const val ApiAccept = "Accept"
@@ -28,7 +47,11 @@ open class ApiClient(val baseUrl: String) {
protected const val ApiFormURLType = "application/x-www-form-urlencoded"
protected const val ApiXmlMediaType = "application/xml"
- val client: HttpClient = HttpClient(CIO) {
+ val client: HttpClient = HttpClient {
+ install(HttpTimeout) {
+ connectTimeoutMillis = 20_000
+ requestTimeoutMillis = 20_000
+ }
install(JsonFeature) {
serializer = KotlinxSerializer(
KotlinJson {
@@ -40,9 +63,6 @@ open class ApiClient(val baseUrl: String) {
logger = Logger.DEFAULT
level = LogLevel.ALL
}
- engine {
- requestTimeout = 20_000
- }
}
val defaultHeaders: Map<String, String> =
@@ -58,10 +78,12 @@ open class ApiClient(val baseUrl: String) {
)
}
+ var baseUrl: String = ""
var token: String = ""
init {
val settings = Settings()
+ baseUrl = settings.getString(SERVER_URL_KEY, defaultBaseUrl)
token = settings.getString(ACCESS_TOKEN_KEY, "")
}