From 432b2212504151202607386312c8d2b61856ccc2 Mon Sep 17 00:00:00 2001 From: Abdourahamane Boinaidi Date: Thu, 14 Nov 2024 09:47:02 +0100 Subject: [PATCH] feat: Handle transfer with password --- .../managers/TransferManager.kt | 2 +- .../network/repositories/TransferRepository.kt | 4 +++- .../network/requests/TransferRequest.kt | 15 +++++++++++++-- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/STCore/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/managers/TransferManager.kt b/STCore/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/managers/TransferManager.kt index b5803209..311fbc1c 100644 --- a/STCore/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/managers/TransferManager.kt +++ b/STCore/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/managers/TransferManager.kt @@ -150,7 +150,7 @@ class TransferManager internal constructor( ) suspend fun addTransferByLinkUUID(linkUUID: String, uploadSession: UploadSession?): Unit = withContext(Dispatchers.IO) { runCatching { - addTransfer(transferRepository.getTransferByLinkUUID(linkUUID).data, TransferDirection.SENT) + addTransfer(transferRepository.getTransferByLinkUUID(linkUUID, uploadSession?.password).data, TransferDirection.SENT) }.onFailure { exception -> when { uploadSession == null -> return@withContext diff --git a/STNetwork/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/network/repositories/TransferRepository.kt b/STNetwork/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/network/repositories/TransferRepository.kt index 9e78d582..1e2e0833 100644 --- a/STNetwork/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/network/repositories/TransferRepository.kt +++ b/STNetwork/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/network/repositories/TransferRepository.kt @@ -47,7 +47,9 @@ class TransferRepository internal constructor(private val transferRequest: Trans NetworkException::class, UnknownException::class, ) - suspend fun getTransferByLinkUUID(linkUUID: String): ApiResponse = transferRequest.getTransfer(linkUUID) + suspend fun getTransferByLinkUUID(linkUUID: String, password: String? = null): ApiResponse { + return transferRequest.getTransfer(linkUUID, password) + } @Throws( CancellationException::class, diff --git a/STNetwork/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/network/requests/TransferRequest.kt b/STNetwork/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/network/requests/TransferRequest.kt index e25b63fd..35946e4e 100644 --- a/STNetwork/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/network/requests/TransferRequest.kt +++ b/STNetwork/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/network/requests/TransferRequest.kt @@ -21,11 +21,22 @@ import com.infomaniak.multiplatform_swisstransfer.network.models.ApiResponse import com.infomaniak.multiplatform_swisstransfer.network.models.transfer.TransferApi import com.infomaniak.multiplatform_swisstransfer.network.utils.ApiRoutes import io.ktor.client.HttpClient +import io.ktor.http.HttpHeaders import kotlinx.serialization.json.Json +import kotlin.io.encoding.Base64 +import kotlin.io.encoding.ExperimentalEncodingApi internal class TransferRequest(json: Json, httpClient: HttpClient) : BaseRequest(json, httpClient) { - suspend fun getTransfer(linkUUID: String): ApiResponse { - return get(url = createUrl(ApiRoutes.getTransfer(linkUUID))) + @OptIn(ExperimentalEncodingApi::class) + suspend fun getTransfer(linkUUID: String, password: String? = null): ApiResponse { + return get( + url = createUrl(ApiRoutes.getTransfer(linkUUID)), + appendHeaders = { + if (password?.isNotEmpty() == true) { + append(HttpHeaders.Authorization, Base64.Default.encode(password.encodeToByteArray())) + } + } + ) } }