-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
...monMain/kotlin/com/infomaniak/multiplatform_swisstransfer/network/requests/BaseRequest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
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<String, String>): Url { | ||
val baseUrl = Url(UrlConstants.baseUrl + path) | ||
return URLBuilder(baseUrl).apply { | ||
queries.forEach { parameters.append(it.first, it.second) } | ||
}.build() | ||
} | ||
|
||
protected suspend inline fun <reified R> get(client: HttpClient, url: Url): R { | ||
return client.get(url) {}.decode<R>() | ||
} | ||
|
||
protected suspend inline fun <reified R> post(client: HttpClient, url: Url, data: Any?): R { | ||
return client.post(url) { setBody(data) }.decode<R>() | ||
} | ||
|
||
protected suspend inline fun <reified R> put(client: HttpClient, url: Url, data: Any?): R { | ||
return client.put(url) { setBody(data) }.decode<R>() | ||
} | ||
|
||
protected suspend inline fun <reified R> delete(client: HttpClient, url: Url): R { | ||
return client.delete(url) {}.decode<R>() | ||
} | ||
|
||
protected suspend inline fun <reified R> HttpResponse.decode(): R { | ||
return json.decodeFromString<R>(bodyAsText()) | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...ain/kotlin/com/infomaniak/multiplatform_swisstransfer/network/requests/TransferRequest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
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<TransferApi> { | ||
val url = createUrl("${UrlConstants.links}/$linkUUID") | ||
return get(httpClient, url) | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...nMain/kotlin/com/infomaniak/multiplatform_swisstransfer/network/requests/UploadRequest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
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 | ||
} |
31 changes: 31 additions & 0 deletions
31
...ommonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/network/utils/UrlConstants.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
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 | ||
} |