Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add download files api + refactor #16

Merged
merged 4 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ import com.infomaniak.multiplatform_swisstransfer.network.exceptions.*
import com.infomaniak.multiplatform_swisstransfer.network.exceptions.ContainerErrorsException.Companion.toContainerErrorsException
import com.infomaniak.multiplatform_swisstransfer.network.exceptions.EmailValidationException.Companion.toEmailValidationException
import com.infomaniak.multiplatform_swisstransfer.network.models.upload.AuthorEmailToken
import com.infomaniak.multiplatform_swisstransfer.network.models.upload.UploadCompleteResponse
import com.infomaniak.multiplatform_swisstransfer.network.models.upload.InitUploadResponseApi
import com.infomaniak.multiplatform_swisstransfer.network.models.upload.request.InitUploadBody
import com.infomaniak.multiplatform_swisstransfer.network.models.upload.UploadCompleteResponse
import com.infomaniak.multiplatform_swisstransfer.network.models.upload.request.FinishUploadBody
import com.infomaniak.multiplatform_swisstransfer.network.models.upload.request.InitUploadBody
import com.infomaniak.multiplatform_swisstransfer.network.models.upload.request.ResendEmailCodeBody
import com.infomaniak.multiplatform_swisstransfer.network.models.upload.request.VerifyEmailCodeBody
import com.infomaniak.multiplatform_swisstransfer.network.requests.UploadRequest
Expand Down Expand Up @@ -103,13 +103,14 @@ class UploadRepository internal constructor(private val uploadRequest: UploadReq
UnknownException::class,
)
suspend fun uploadChunk(
uploadHost: String,
containerUUID: String,
fileUUID: String,
chunkIndex: Int,
lastChunk: Boolean,
data: ByteArray,
): Boolean {
return uploadRequest.uploadChunk(containerUUID, fileUUID, chunkIndex, lastChunk, data)
return uploadRequest.uploadChunk(uploadHost, containerUUID, fileUUID, chunkIndex, lastChunk, data)
}

@Throws(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ import com.infomaniak.multiplatform_swisstransfer.network.utils.ApiRoutes
import io.ktor.client.HttpClient
import kotlinx.serialization.json.Json

internal class TransferRequest constructor(json: Json, httpClient: HttpClient) : BaseRequest(json, httpClient) {
internal class TransferRequest(json: Json, httpClient: HttpClient) : BaseRequest(json, httpClient) {

suspend fun getTransfer(linkUUID: String): ApiResponse<TransferApi> {
return get(url = createUrl("${ApiRoutes.links}/$linkUUID"))
return get(url = createUrl(ApiRoutes.getTransfer(linkUUID)))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,20 @@
package com.infomaniak.multiplatform_swisstransfer.network.requests

import com.infomaniak.multiplatform_swisstransfer.network.models.upload.AuthorEmailToken
import com.infomaniak.multiplatform_swisstransfer.network.models.upload.UploadCompleteResponse
import com.infomaniak.multiplatform_swisstransfer.network.models.upload.InitUploadResponseApi
import com.infomaniak.multiplatform_swisstransfer.network.models.upload.UploadCompleteResponse
import com.infomaniak.multiplatform_swisstransfer.network.models.upload.request.FinishUploadBody
import com.infomaniak.multiplatform_swisstransfer.network.models.upload.request.InitUploadBody
import com.infomaniak.multiplatform_swisstransfer.network.models.upload.request.VerifyEmailCodeBody
import com.infomaniak.multiplatform_swisstransfer.network.models.upload.request.ResendEmailCodeBody
import com.infomaniak.multiplatform_swisstransfer.network.models.upload.request.FinishUploadBody
import com.infomaniak.multiplatform_swisstransfer.network.models.upload.request.VerifyEmailCodeBody
import com.infomaniak.multiplatform_swisstransfer.network.utils.ApiRoutes
import io.ktor.client.HttpClient
import io.ktor.client.request.post
import io.ktor.client.request.setBody
import io.ktor.http.isSuccess
import kotlinx.serialization.json.Json

internal class UploadRequest internal constructor(json: Json, httpClient: HttpClient) : BaseRequest(json, httpClient) {
internal class UploadRequest(json: Json, httpClient: HttpClient) : BaseRequest(json, httpClient) {

suspend fun initUpload(initUploadBody: InitUploadBody): InitUploadResponseApi {
return post(url = createUrl(ApiRoutes.initUpload), initUploadBody)
Expand All @@ -50,14 +50,15 @@ internal class UploadRequest internal constructor(json: Json, httpClient: HttpCl
}

suspend fun uploadChunk(
uploadHost: String,
containerUUID: String,
fileUUID: String,
chunkIndex: Int,
lastChunk: Boolean,
data: ByteArray,
): Boolean {
val httpResponse = httpClient.post(
url = createUrl(ApiRoutes.uploadChunk(containerUUID, fileUUID, chunkIndex, lastChunk))
urlString = ApiRoutes.uploadChunk(uploadHost, containerUUID, fileUUID, chunkIndex, lastChunk)
) {
setBody(data)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,27 @@ internal object ApiRoutes {
const val baseUrl = "https://www.swisstransfer.com/api/"

//region Transfer
const val links = "links"
fun getTransfer(linkUUID: String): String {
return "links/$linkUUID"
}

fun downloadFiles(downloadHost: String, linkUUID: String): String {
return "https://$downloadHost/api/download/$linkUUID"
}

fun downloadFile(downloadHost: String, linkUUID: String, fileUUID: String?): String {
return "${downloadFiles(downloadHost, linkUUID)}/$fileUUID"
}
//endRegion

//region Upload
private const val uploadChunk = "uploadChunk"

const val initUpload = "containers"
const val verifyEmailCode = "emails-validation"
const val resendEmailCode = "$verifyEmailCode/resend"
const val finishUpload = "uploadComplete"

fun uploadChunk(containerUUID: String, fileUUID: String, chunkIndex: Int, lastChunk: Boolean): String {
return "$uploadChunk/$containerUUID/$fileUUID/$chunkIndex/${lastChunk.int()}"
fun uploadChunk(uploadHost: String, containerUUID: String, fileUUID: String, chunkIndex: Int, lastChunk: Boolean): String {
return "https://$uploadHost/api/uploadChunk/$containerUUID/$fileUUID/$chunkIndex/${lastChunk.int()}"
}
//endregion

Expand Down
Loading