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: Upload api #15

Merged
merged 21 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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 @@ -18,7 +18,7 @@

package com.infomaniak.multiplatform_swisstransfer.common.interfaces.upload

interface UploadContainerResponse<C : UploadContainer> {
interface InitUploadResponse<C : UploadContainer> {
var container: C
var uploadHost: String
var filesUUID: List<String>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class SwissTransferInjection {
/**
* Loads the default user account and initializes Realm transfers for the default user ID defined in the constants.
*/
@Throws(Exception::class)
@Throws(IllegalArgumentException::class, IllegalStateException::class)
fun loadDefaultAccount() {
realmProvider.loadRealmTransfers(Constants.DEFAULT_USER_ID)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@

package com.infomaniak.multiplatform_swisstransfer.network.exceptions

class ApiException(val errorCode: Int, errorMessage: String) : Exception(errorMessage)
open class ApiException(val errorCode: Int, errorMessage: String) : Exception(errorMessage)
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* 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.exceptions

/**
* A sealed class representing various container error exceptions that extend [ApiException].
* This class is used to handle API errors that occur when creating a container through the API.
* Each specific error exception is associated with an HTTP status code and a descriptive error message.
*
* @constructor Creates a [ContainerErrorsException] with the given status code and error message.
*
* @param statusCode The HTTP status code for the error.
* @param errorMessage A descriptive error message for the error.
*/
sealed class ContainerErrorsException(val statusCode: Int, errorMessage: String) : ApiException(statusCode, errorMessage) {

/**
* Exception indicating that email address validation is required.
* This corresponds to an HTTP 401 Unauthorized status.
*/
class EmailValidationRequired : ContainerErrorsException(401, "Email address validation required")

/**
* Exception indicating that the domain was automatically blocked for security reasons.
* This corresponds to an HTTP 403 Forbidden status.
*/
class DomainBlockedException : ContainerErrorsException(403, "The domain was automatically blocked for security reasons")

/**
* Exception indicating that the daily transfer limit has been reached.
* This corresponds to an HTTP 404 Not Found status.
*/
class DailyTransferLimitReachedException : ContainerErrorsException(404, "Daily transfer limit reached")

/**
* Exception indicating that the provided captcha is not valid.
* This corresponds to an HTTP 422 Unprocessable Entity status.
*/
class CaptchaNotValidException : ContainerErrorsException(422, "Captcha not valid")

/**
* Exception indicating that too many codes have been generated.
* This corresponds to an HTTP 429 Too Many Requests status.
*/
class TooManyCodesGeneratedException : ContainerErrorsException(429, "Too many codes generated")

internal companion object {
/**
* Extension function to convert an instance of [UnknownApiException] to a more specific exception
* based on its HTTP status code.
*
* This function maps the status codes to specific exceptions as follows:
* - 401: [EmailValidationRequired]
* - 403: [DomainBlockedException]
* - 404: [DailyTransferLimitReachedException]
* - 422: [CaptchaNotValidException]
* - 429: [TooManyCodesGeneratedException]
* - Other status codes: The original [UnknownApiException] instance
*
* @receiver An instance of [UnknownApiException].
* @return An instance of [Exception] which can be one of the specific exceptions mentioned above,
* or the original [UnknownApiException] if the status code does not match any predefined values.
*/
fun UnknownApiException.toContainerErrorsException(): Exception {
return when (statusCode) {
401 -> EmailValidationRequired()
403 -> DomainBlockedException()
404 -> DailyTransferLimitReachedException()
422 -> CaptchaNotValidException()
429 -> TooManyCodesGeneratedException()
else -> this
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* 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.exceptions

/**
* A sealed class representing exceptions related to email validation that extend [ApiException].
* This class is used to handle specific errors that occur during the email validation process.
*
* @property statusCode The HTTP status code associated with the error.
* @constructor Creates an [EmailValidationException] with the given status code.
*
* @param statusCode The HTTP status code for the error.
*/
sealed class EmailValidationException(statusCode: Int) : ApiException(statusCode, "") {

/**
* Exception indicating that the provided password is invalid during email validation.
* This corresponds to an HTTP 401 Unauthorized status.
*/
class InvalidPasswordException : EmailValidationException(401)

internal companion object {
/**
* Extension function to convert an instance of [UnknownApiException] to a specific
* [EmailValidationException] based on its HTTP status code.
*
* This function maps the status codes to specific exceptions as follows:
* - 401: [InvalidPasswordException]
* - Other status codes: The original [UnknownApiException] instance
*
* @receiver An instance of [UnknownApiException].
* @return An instance of [EmailValidationException] which can be [InvalidPasswordException]
* or the original [UnknownApiException] if the status code does not match any predefined values.
*/
fun UnknownApiException.toEmailValidationException() = when (statusCode) {
401 -> InvalidPasswordException()
else -> this
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,11 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.infomaniak.multiplatform_swisstransfer.network.utils
package com.infomaniak.multiplatform_swisstransfer.network.models.upload

internal object UrlConstants {
const val baseUrl = "https://www.swisstransfer.com/api/"
import kotlinx.serialization.Serializable

//region Transfer
const val links = "links"
//endRegion

//region Upload
const val uploadComplete = "uploadComplete"
//endregion
}
@Serializable
data class AuthorEmailToken(
val token: String,
)
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@

package com.infomaniak.multiplatform_swisstransfer.network.models.upload

import com.infomaniak.multiplatform_swisstransfer.common.interfaces.upload.UploadContainerResponse
import com.infomaniak.multiplatform_swisstransfer.common.interfaces.upload.InitUploadResponse

class UploadContainerResponseApi : UploadContainerResponse<UploadContainerApi> {
class InitUploadResponseApi : InitUploadResponse<UploadContainerApi> {
override var container: UploadContainerApi = UploadContainerApi()
override var uploadHost: String = ""
override var filesUUID: List<String> = emptyList()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,25 @@

package com.infomaniak.multiplatform_swisstransfer.network.models.upload

import com.infomaniak.multiplatform_swisstransfer.common.interfaces.upload.UploadCompleteResponse
import com.infomaniak.multiplatform_swisstransfer.network.serializers.DateToTimestampSerializer
import com.infomaniak.multiplatform_swisstransfer.network.serializers.IntToBooleanSerializer
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
class UploadCompleteResponseApi : UploadCompleteResponse {
override var linkUUID: String = ""
override var containerUUID: String = ""
override var userEmail: String? = null
override var downloadCounterCredit: Long = 0

class UploadCompleteResponse(
var linkUUID: String = "",
var containerUUID: String = "",
var userEmail: String? = null,
var downloadCounterCredit: Long = 0,
@SerialName("createdDate")
@Serializable(DateToTimestampSerializer::class)
override var createdDateTimestamp: Long = 0

var createdDateTimestamp: Long = 0,
@SerialName("expiredDate")
@Serializable(DateToTimestampSerializer::class)
override var expiredDateTimestamp: Long = 0

var expiredDateTimestamp: Long = 0,
@Serializable(with = IntToBooleanSerializer::class)
override var isDownloadOnetime: Boolean = false

var isDownloadOnetime: Boolean = false,
@Serializable(with = IntToBooleanSerializer::class)
override var isMailSent: Boolean = false
}
var isMailSent: Boolean = false,
)
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class UploadComplete(
data class FinishUploadBody(
@SerialName("UUID")
val containerUUID: String = "",
val lang: String = "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ package com.infomaniak.multiplatform_swisstransfer.network.models.upload.request
import kotlinx.serialization.Serializable

@Serializable
class UploadRequest(
class InitUploadBody(
val duration: String = "",
val authorEmail: String = "",
val password: String = "",
val message: String = "",
val sizeOfUpload: Long = 0,
val numberOfDownload: Long = 0,
val numberOfFile: Long = 0,
val numberOfDownload: Int = 0,
val numberOfFile: Int = 0,
val recaptcha: String = "",
val recaptchaVersion: Long = 0,
val lang: String = "",
Expand Down
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.models.upload.request

import kotlinx.serialization.Serializable

@Serializable
data class ResendEmailCodeBody(
val address: String,
val lang: String, // example "fr_FR"
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* 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.models.upload.request

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class VerifyEmailCodeBody(
val code: String,
@SerialName("address")
val email: String,
)
Loading
Loading