Skip to content
This repository has been archived by the owner on Jun 22, 2024. It is now read-only.

Commit

Permalink
tweak: Move classes, Remove method
Browse files Browse the repository at this point in the history
  • Loading branch information
sya-ri committed Aug 12, 2022
1 parent cd881b0 commit 1bb6a62
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 96 deletions.
19 changes: 4 additions & 15 deletions src/commonMain/kotlin/dev/s7a/gofile/GofileClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -67,22 +67,11 @@ class GofileClient(private val client: HttpClient) {
* Get the best server available to receive files.
*
* `https://api.gofile.io/getServer`
*
* @see getServerName
*/
suspend fun getServer(): Result<GofileResponse.GetServer> {
suspend fun getServer(): Result<GofileGetServerResponse> {
return request(GofileRequest.GetServer)
}

/**
* Get the best server name available to receive files.
*
* @see getServer
*/
suspend fun getServerName(): String? {
return getServer().getOrNull()?.server
}

/**
* Upload one file on a specific server.
* If you specify a folderId, the file will be added to this folder.
Expand All @@ -102,7 +91,7 @@ class GofileClient(private val client: HttpClient) {
* When using the folderId, you must pass the account token.
* @param server Server to upload to. If you specify null, it will use the best available.
*/
suspend fun uploadFile(fileName: String, fileContent: ByteArray, contentType: String, token: String? = null, folderId: String? = null, server: String? = null): Result<GofileResponse.UploadFile> {
suspend fun uploadFile(fileName: String, fileContent: ByteArray, contentType: String, token: String? = null, folderId: String? = null, server: String? = null): Result<GofileUploadFileResponse> {
val serverName = server ?: getServer().fold(onSuccess = { it.server }, onFailure = { return Result.failure(it) })
return request(GofileRequest.UploadFile(fileName, fileContent, contentType, token, folderId, serverName))
}
Expand All @@ -128,7 +117,7 @@ class GofileClient(private val client: HttpClient) {
* @param folderName The name of the created folder.
* @param token The access token of an account. Can be retrieved from the profile page.
*/
suspend fun createFolder(parentFolderId: String, folderName: String, token: String): Result<GofileResponse.CreateFolder> {
suspend fun createFolder(parentFolderId: String, folderName: String, token: String): Result<GofileCreateFolderResponse> {
return request(GofileRequest.CreateFolder(parentFolderId, folderName, token))
}

Expand Down Expand Up @@ -202,7 +191,7 @@ class GofileClient(private val client: HttpClient) {
*
* @param token The access token of an account. Can be retrieved from the profile page.
*/
suspend fun getAccountDetails(token: String): Result<GofileResponse.GetAccountDetails> {
suspend fun getAccountDetails(token: String): Result<GofileGetAccountDetailsResponse> {
return request(GofileRequest.GetAccountDetails(token))
}
}
23 changes: 23 additions & 0 deletions src/commonMain/kotlin/dev/s7a/gofile/GofileCreateFolderResponse.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package dev.s7a.gofile

import kotlinx.serialization.Serializable

/**
* @property id An id of the created folder.
* @property type Content type.
* @property name A name of the created folder.
* @property parentFolder The parent folder id of the created folder.
* @property createTime Creation time.
* @property childs Files and folders the folder.
* @property code A code to open the folder.
*/
@Serializable
data class GofileCreateFolderResponse(
val id: String,
val type: GofileContentType,
val name: String,
val parentFolder: String,
val createTime: Long,
val childs: List<String>,
val code: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package dev.s7a.gofile

import kotlinx.serialization.Serializable

/**
* **The statistics are updated every 24 hours.**
*
* @property token The access token of an account.
* @property email The email of an account.
* @property tier The tier of an account.
* @property tierAmount Dollars paid monthly.
* @property rootFolder The root folder id.
* @property filesCount A number of files.
* @property filesCountLimit Limit of [filesCount].
* @property totalSize A size of all files.
* @property totalSizeLimit Limit of [totalSize].
* @property total30DDLTraffic DDL traffic increases when someone downloads your content through a direct link. It is counted for the last 30 days. Downloads from the website are unlimited.
* @property total30DDLTrafficLimit Limit of [total30DDLTraffic].
*/
@Serializable
data class GofileGetAccountDetailsResponse(
val token: String,
val email: String,
val tier: GofileTier,
val tierAmount: Int? = null,
val rootFolder: String,
val filesCount: Int,
val filesCountLimit: Int?,
val totalSize: Double,
val totalSizeLimit: Double?,
val total30DDLTraffic: Double,
val total30DDLTrafficLimit: Double?
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package dev.s7a.gofile

import kotlinx.serialization.Serializable

/**
* @property server The best server available to receive files.
*/
@Serializable
data class GofileGetServerResponse(val server: String)
2 changes: 1 addition & 1 deletion src/commonMain/kotlin/dev/s7a/gofile/GofileRequest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import io.ktor.http.Parameters
/**
* Request types of Gofile.io.
*/
sealed interface GofileRequest {
internal sealed interface GofileRequest {
/**
* Http method.
*/
Expand Down
78 changes: 1 addition & 77 deletions src/commonMain/kotlin/dev/s7a/gofile/GofileResponse.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import kotlinx.serialization.json.JsonObject
* Response types of Gofile.io.
*/
@Serializable(with = GofileResponseSerializer::class)
sealed class GofileResponse<out T> {
internal sealed class GofileResponse<out T> {
/**
* Gofile.io returns "ok" status.
*/
Expand All @@ -17,80 +17,4 @@ sealed class GofileResponse<out T> {
* Gofile.io returns some error.
*/
data class Error(val status: String, val data: JsonObject? = null) : GofileResponse<Nothing>()

/**
* @property server The best server available to receive files.
*/
@Serializable
data class GetServer(val server: String)

/**
* @property downloadPage A url to download the uploaded file.
* @property code A code to download the uploaded file.
* @property parentFolder The parent folder id of the uploaded file.
* @property fileId A id of the uploaded file.
* @property fileName A name of the upload file.
* @property md5 A checksum of the uploaded file.
* @property guestToken If you don't specify a token in the request, Gofile.io will create a guest token.
*/
@Serializable
data class UploadFile(
val downloadPage: String,
val code: String,
val parentFolder: String,
val fileId: String,
val fileName: String,
val md5: String,
val guestToken: String? = null
)

/**
* @property id An id of the created folder.
* @property type Content type.
* @property name A name of the created folder.
* @property parentFolder The parent folder id of the created folder.
* @property createTime Creation time.
* @property childs Files and folders the folder.
* @property code A code to open the folder.
*/
@Serializable
data class CreateFolder(
val id: String,
val type: GofileContentType,
val name: String,
val parentFolder: String,
val createTime: Long,
val childs: List<String>,
val code: String
)

/**
* **The statistics are updated every 24 hours.**
*
* @property token The access token of an account.
* @property email The email of an account.
* @property tier The tier of an account.
* @property tierAmount Dollars paid monthly.
* @property rootFolder The root folder id.
* @property filesCount A number of files.
* @property filesCountLimit Limit of [filesCount].
* @property totalSize A size of all files.
* @property totalSizeLimit Limit of [totalSize].
* @property total30DDLTraffic DDL traffic increases when someone downloads your content through a direct link. It is counted for the last 30 days. Downloads from the website are unlimited.
* @property total30DDLTrafficLimit Limit of [total30DDLTraffic].
*/
@Serializable
data class GetAccountDetails(
val token: String,
val email: String,
val tier: GofileTier,
val tierAmount: Int? = null,
val rootFolder: String,
val filesCount: Int,
val filesCountLimit: Int?,
val totalSize: Double,
val totalSizeLimit: Double?,
val total30DDLTraffic: Double,
val total30DDLTrafficLimit: Double?
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import kotlinx.serialization.json.put
/**
* Serializer for [GofileResponse].
*/
class GofileResponseSerializer<T>(private val dataSerializer: KSerializer<T>) : KSerializer<GofileResponse<T>> {
internal class GofileResponseSerializer<T>(private val dataSerializer: KSerializer<T>) : KSerializer<GofileResponse<T>> {
@OptIn(InternalSerializationApi::class, ExperimentalSerializationApi::class)
override val descriptor = buildSerialDescriptor("GofileResponse", PolymorphicKind.SEALED) {
element(
Expand Down
23 changes: 23 additions & 0 deletions src/commonMain/kotlin/dev/s7a/gofile/GofileUploadFileResponse.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package dev.s7a.gofile

import kotlinx.serialization.Serializable

/**
* @property downloadPage A url to download the uploaded file.
* @property code A code to download the uploaded file.
* @property parentFolder The parent folder id of the uploaded file.
* @property fileId A id of the uploaded file.
* @property fileName A name of the upload file.
* @property md5 A checksum of the uploaded file.
* @property guestToken If you don't specify a token in the request, Gofile.io will create a guest token.
*/
@Serializable
data class GofileUploadFileResponse(
val downloadPage: String,
val code: String,
val parentFolder: String,
val fileId: String,
val fileName: String,
val md5: String,
val guestToken: String? = null
)
4 changes: 2 additions & 2 deletions src/commonTest/kotlin/Tests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Tests {
headers = headersOf(HttpHeaders.ContentType, "application/json")
)
}
assertEquals("store1", GofileClient(mockEngine).getServerName())
assertEquals("store1", GofileClient(mockEngine).getServer().getOrNull()?.server)
}
}

Expand Down Expand Up @@ -70,7 +70,7 @@ class Tests {
val mockEngine = MockEngine {
respondError(HttpStatusCode.InternalServerError)
}
assertNull(GofileClient(mockEngine).getServerName())
assertNull(GofileClient(mockEngine).getServer().getOrNull())
}
}

Expand Down

0 comments on commit 1bb6a62

Please sign in to comment.