mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2026-03-11 12:52:23 +00:00
generate docs for a lot of API (test try)
This commit is contained in:
@@ -4,10 +4,25 @@ import io.ktor.client.call.body
|
||||
import io.ktor.client.statement.HttpResponse
|
||||
import io.ktor.http.HttpStatusCode
|
||||
|
||||
/**
|
||||
* Returns the response body as type [T] if the [statusFilter] condition is met, otherwise returns null.
|
||||
* By default, the filter checks if the status code is [HttpStatusCode.OK].
|
||||
*
|
||||
* @param T The type to deserialize the response body to
|
||||
* @param statusFilter A predicate to determine if the body should be retrieved. Defaults to checking for OK status
|
||||
* @return The deserialized body of type [T], or null if the filter condition is not met
|
||||
*/
|
||||
suspend inline fun <reified T : Any> HttpResponse.bodyOrNull(
|
||||
statusFilter: (HttpResponse) -> Boolean = { it.status == HttpStatusCode.OK }
|
||||
) = takeIf(statusFilter) ?.body<T>()
|
||||
|
||||
/**
|
||||
* Returns the response body as type [T] if the status code is not [HttpStatusCode.NoContent], otherwise returns null.
|
||||
* This is useful for handling responses that may return 204 No Content.
|
||||
*
|
||||
* @param T The type to deserialize the response body to
|
||||
* @return The deserialized body of type [T], or null if the status is No Content
|
||||
*/
|
||||
suspend inline fun <reified T : Any> HttpResponse.bodyOrNullOnNoContent() = bodyOrNull<T> {
|
||||
it.status != HttpStatusCode.NoContent
|
||||
}
|
||||
|
||||
@@ -4,6 +4,13 @@ import io.ktor.client.plugins.ClientRequestException
|
||||
import io.ktor.client.statement.HttpResponse
|
||||
import io.ktor.http.isSuccess
|
||||
|
||||
/**
|
||||
* Throws a [ClientRequestException] if this [HttpResponse] does not have a successful status code.
|
||||
* A status code is considered successful if it's in the 2xx range.
|
||||
*
|
||||
* @param unsuccessMessage A lambda that provides the error message to use if the response is unsuccessful
|
||||
* @throws ClientRequestException if the response status is not successful
|
||||
*/
|
||||
inline fun HttpResponse.throwOnUnsuccess(
|
||||
unsuccessMessage: () -> String
|
||||
) {
|
||||
|
||||
@@ -5,6 +5,12 @@ import dev.inmo.micro_utils.common.filesize
|
||||
import dev.inmo.micro_utils.ktor.common.input
|
||||
import io.ktor.client.request.forms.InputProvider
|
||||
|
||||
/**
|
||||
* Creates a Ktor [InputProvider] from this multiplatform file for use in HTTP client requests.
|
||||
* The input provider knows the file size and can create input streams on demand.
|
||||
*
|
||||
* @return An [InputProvider] for reading this file in HTTP requests
|
||||
*/
|
||||
fun MPPFile.inputProvider(): InputProvider = InputProvider(filesize) {
|
||||
input()
|
||||
}
|
||||
|
||||
@@ -1,3 +1,9 @@
|
||||
package dev.inmo.micro_utils.ktor.client
|
||||
|
||||
/**
|
||||
* A callback function type for tracking upload progress.
|
||||
*
|
||||
* @param uploaded The number of bytes uploaded so far
|
||||
* @param count The total number of bytes to be uploaded
|
||||
*/
|
||||
typealias OnUploadCallback = suspend (uploaded: Long, count: Long) -> Unit
|
||||
|
||||
@@ -5,6 +5,15 @@ import dev.inmo.micro_utils.ktor.common.*
|
||||
import io.ktor.client.HttpClient
|
||||
import io.ktor.client.content.*
|
||||
|
||||
/**
|
||||
* Uploads a file to a temporary storage on the server.
|
||||
* The server should provide an endpoint that accepts multipart uploads and returns a [TemporalFileId].
|
||||
*
|
||||
* @param fullTempUploadDraftPath The full URL path to the temporary upload endpoint
|
||||
* @param file The file to upload
|
||||
* @param onUpload Progress callback invoked during upload
|
||||
* @return A [TemporalFileId] that can be used to reference the uploaded file
|
||||
*/
|
||||
expect suspend fun HttpClient.tempUpload(
|
||||
fullTempUploadDraftPath: String,
|
||||
file: MPPFile,
|
||||
|
||||
@@ -10,6 +10,14 @@ import kotlinx.serialization.DeserializationStrategy
|
||||
import kotlinx.serialization.StringFormat
|
||||
import kotlinx.serialization.json.Json
|
||||
|
||||
/**
|
||||
* Information about a file to upload in a multipart request.
|
||||
* This allows uploading from custom sources beyond regular files.
|
||||
*
|
||||
* @param fileName The name of the file
|
||||
* @param mimeType The MIME type of the file
|
||||
* @param inputAllocator A lambda that provides input streams for reading the file data
|
||||
*/
|
||||
data class UniUploadFileInfo(
|
||||
val fileName: FileName,
|
||||
val mimeType: String,
|
||||
|
||||
Reference in New Issue
Block a user