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 new file mode 100644 index 00000000..ca9c4816 --- /dev/null +++ b/Network/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/network/requests/BaseRequest.kt @@ -0,0 +1,62 @@ +/* + * Infomaniak SwissTransfer - Multiplatform + * Copyright (C) 2024 Infomaniak Network SA + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.infomaniak.multiplatform_swisstransfer.network.requests + +import com.infomaniak.multiplatform_swisstransfer.network.utils.UrlConstants +import io.ktor.client.HttpClient +import io.ktor.client.request.delete +import io.ktor.client.request.get +import io.ktor.client.request.post +import io.ktor.client.request.put +import io.ktor.client.request.setBody +import io.ktor.client.statement.HttpResponse +import io.ktor.client.statement.bodyAsText +import io.ktor.http.URLBuilder +import io.ktor.http.Url +import kotlinx.serialization.json.Json + +internal open class BaseRequest(protected val json: Json) { + + protected fun createUrl(path: String, vararg queries: Pair): Url { + val baseUrl = Url(UrlConstants.baseUrl + path) + return URLBuilder(baseUrl).apply { + queries.forEach { parameters.append(it.first, it.second) } + }.build() + } + + protected suspend inline fun get(client: HttpClient, url: Url): R { + return client.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 put(client: HttpClient, url: Url, data: Any?): R { + return client.put(url) { setBody(data) }.decode() + } + + protected suspend inline fun delete(client: HttpClient, url: Url): R { + return client.delete(url) {}.decode() + } + + protected suspend inline fun HttpResponse.decode(): R { + return json.decodeFromString(bodyAsText()) + } +} 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 new file mode 100644 index 00000000..7220f69f --- /dev/null +++ b/Network/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/network/requests/TransferRequest.kt @@ -0,0 +1,33 @@ +/* + * Infomaniak SwissTransfer - Multiplatform + * Copyright (C) 2024 Infomaniak Network SA + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.infomaniak.multiplatform_swisstransfer.network.requests + +import com.infomaniak.multiplatform_swisstransfer.network.models.ApiResponse +import com.infomaniak.multiplatform_swisstransfer.network.models.transfer.TransferApi +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) { + + suspend fun getTransfer(linkUUID: String): ApiResponse { + val url = createUrl("${UrlConstants.links}/$linkUUID") + return get(httpClient, url) + } +} 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 new file mode 100644 index 00000000..d3a90f98 --- /dev/null +++ b/Network/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/network/requests/UploadRequest.kt @@ -0,0 +1,27 @@ +/* + * Infomaniak SwissTransfer - Multiplatform + * Copyright (C) 2024 Infomaniak Network SA + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +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) { + + // TODO: implement method here +} diff --git a/Network/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/network/utils/UrlConstants.kt b/Network/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/network/utils/UrlConstants.kt new file mode 100644 index 00000000..5d674a6d --- /dev/null +++ b/Network/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/network/utils/UrlConstants.kt @@ -0,0 +1,31 @@ +/* + * Infomaniak SwissTransfer - Multiplatform + * Copyright (C) 2024 Infomaniak Network SA + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.infomaniak.multiplatform_swisstransfer.network.utils + +internal object UrlConstants { + const val baseUrl = "https://www.swisstransfer.com/api/" + + //region Transfer + const val links = "links" + //endRegion + + //region Upload + const val uploadComplete = "uploadComplete" + //endregion +}