From 550f9f0fec3b717dcf05deed1bde1cae7f4ee072 Mon Sep 17 00:00:00 2001 From: Abdourahamane Boinaidi Date: Tue, 9 Jul 2024 11:43:08 +0200 Subject: [PATCH] network: Use a default httpclient on BaseRequest --- .../network/requests/BaseRequest.kt | 18 +++++++++--------- .../network/requests/TransferRequest.kt | 5 ++--- .../network/requests/UploadRequest.kt | 2 +- 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/Network/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/network/requests/BaseRequest.kt b/Network/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/network/requests/BaseRequest.kt index ca9c4816..3cadea88 100644 --- a/Network/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/network/requests/BaseRequest.kt +++ b/Network/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/network/requests/BaseRequest.kt @@ -31,7 +31,7 @@ import io.ktor.http.URLBuilder import io.ktor.http.Url import kotlinx.serialization.json.Json -internal open class BaseRequest(protected val json: Json) { +internal open class BaseRequest(protected val json: Json, protected val httpClient: HttpClient) { protected fun createUrl(path: String, vararg queries: Pair): Url { val baseUrl = Url(UrlConstants.baseUrl + path) @@ -40,20 +40,20 @@ internal open class BaseRequest(protected val json: Json) { }.build() } - protected suspend inline fun get(client: HttpClient, url: Url): R { - return client.get(url) {}.decode() + protected suspend inline fun get(url: Url, httpClient: HttpClient = this.httpClient): R { + return httpClient.get(url) {}.decode() } - protected suspend inline fun post(client: HttpClient, url: Url, data: Any?): R { - return client.post(url) { setBody(data) }.decode() + protected suspend inline fun post(url: Url, data: Any?, httpClient: HttpClient = this.httpClient): R { + return httpClient.post(url) { setBody(data) }.decode() } - protected suspend inline fun put(client: HttpClient, url: Url, data: Any?): R { - return client.put(url) { setBody(data) }.decode() + protected suspend inline fun put(url: Url, data: Any?, httpClient: HttpClient = this.httpClient): R { + return httpClient.put(url) { setBody(data) }.decode() } - protected suspend inline fun delete(client: HttpClient, url: Url): R { - return client.delete(url) {}.decode() + protected suspend inline fun delete(url: Url, httpClient: HttpClient = this.httpClient): R { + return httpClient.delete(url) {}.decode() } protected suspend inline fun HttpResponse.decode(): R { diff --git a/Network/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/network/requests/TransferRequest.kt b/Network/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/network/requests/TransferRequest.kt index 7220f69f..2f45c3fb 100644 --- a/Network/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/network/requests/TransferRequest.kt +++ b/Network/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/network/requests/TransferRequest.kt @@ -24,10 +24,9 @@ import com.infomaniak.multiplatform_swisstransfer.network.utils.UrlConstants import io.ktor.client.HttpClient import kotlinx.serialization.json.Json -internal class TransferRequest internal constructor(json: Json, private val httpClient: HttpClient) : BaseRequest(json) { +internal class TransferRequest constructor(json: Json, httpClient: HttpClient) : BaseRequest(json, httpClient) { suspend fun getTransfer(linkUUID: String): ApiResponse { - val url = createUrl("${UrlConstants.links}/$linkUUID") - return get(httpClient, url) + return get(url = createUrl("${UrlConstants.links}/$linkUUID")) } } diff --git a/Network/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/network/requests/UploadRequest.kt b/Network/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/network/requests/UploadRequest.kt index d3a90f98..9807cb3f 100644 --- a/Network/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/network/requests/UploadRequest.kt +++ b/Network/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/network/requests/UploadRequest.kt @@ -21,7 +21,7 @@ package com.infomaniak.multiplatform_swisstransfer.network.requests import io.ktor.client.HttpClient import kotlinx.serialization.json.Json -internal class UploadRequest internal constructor(json: Json, private val httpClient: HttpClient) : BaseRequest(json) { +internal class UploadRequest internal constructor(json: Json, httpClient: HttpClient) : BaseRequest(json, httpClient) { // TODO: implement method here }