Skip to content

Commit

Permalink
core: Add api requests
Browse files Browse the repository at this point in the history
  • Loading branch information
sirambd committed Jul 2, 2024
1 parent 3b36d5b commit 91219e4
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 0 deletions.
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())
}
}
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)
}
}
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
}
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
}

0 comments on commit 91219e4

Please sign in to comment.