Skip to content

Commit

Permalink
Final touches
Browse files Browse the repository at this point in the history
- consistent `contentProvider` name
- optimize imports
- add @Suppress KDoc tags
  • Loading branch information
lukellmann committed Sep 10, 2022
1 parent 7026ad9 commit 48351b2
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 23 deletions.
1 change: 0 additions & 1 deletion core/src/test/kotlin/rest/RestTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import kotlinx.coroutines.flow.toList
import kotlinx.coroutines.runBlocking
import org.junit.jupiter.api.*
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable
import java.io.File
import kotlin.io.path.toPath
import kotlin.test.assertEquals
import kotlin.test.assertTrue
Expand Down
2 changes: 1 addition & 1 deletion rest/api/rest.api
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public final class dev/kord/rest/NamedFile {
public final fun component2 ()Lio/ktor/client/request/forms/ChannelProvider;
public final synthetic fun component2 ()Ljava/io/InputStream;
public final fun component3 ()Ljava/lang/String;
public final fun getChannelProvider ()Lio/ktor/client/request/forms/ChannelProvider;
public final fun getContentProvider ()Lio/ktor/client/request/forms/ChannelProvider;
public final fun getInputStream ()Ljava/io/InputStream;
public final fun getName ()Ljava/lang/String;
public final fun getUrl ()Ljava/lang/String;
Expand Down
16 changes: 10 additions & 6 deletions rest/src/main/kotlin/NamedFile.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package dev.kord.rest

import io.ktor.client.request.forms.*
import io.ktor.utils.io.*
import io.ktor.utils.io.jvm.javaio.*
import java.io.InputStream

public class NamedFile(public val name: String, public val channelProvider: ChannelProvider) {
public class NamedFile(public val name: String, public val contentProvider: ChannelProvider) {
/** @suppress */
@Deprecated(
"Use lazy ChannelProvider instead of InputStream. You should also make sure that the stream/channel is only " +
"opened inside the block of the ChannelProvider because it could otherwise be read multiple times " +
Expand All @@ -17,10 +17,14 @@ public class NamedFile(public val name: String, public val channelProvider: Chan
),
DeprecationLevel.WARNING,
)
public constructor(name: String, inputStream: InputStream) : this(name, ChannelProvider { inputStream.toByteReadChannel() })
public constructor(name: String, inputStream: InputStream) : this(
name,
ChannelProvider { inputStream.toByteReadChannel() },
)

public val url: String get() = "attachment://$name"

/** @suppress */
@Deprecated(
"Use ChannelProvider instead of InputStream",
ReplaceWith(
Expand All @@ -29,14 +33,14 @@ public class NamedFile(public val name: String, public val channelProvider: Chan
),
DeprecationLevel.WARNING,
)
public val inputStream: InputStream get() = channelProvider.block().toInputStream()
public val inputStream: InputStream get() = contentProvider.block().toInputStream()

public operator fun component1(): String = name
public operator fun component2(): ChannelProvider = channelProvider
public operator fun component2(): ChannelProvider = contentProvider
public operator fun component3(): String = url

@Deprecated("Binary compatibility", level = DeprecationLevel.HIDDEN)
@JvmName("component2")
@Suppress("DEPRECATION")
@Suppress("DEPRECATION", "FunctionName")
public fun _component2(): InputStream = inputStream
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import dev.kord.rest.builder.message.AllowedMentionsBuilder
import dev.kord.rest.builder.message.EmbedBuilder
import io.ktor.client.request.forms.*
import io.ktor.util.cio.*
import io.ktor.utils.io.*
import io.ktor.utils.io.jvm.javaio.toByteReadChannel
import io.ktor.utils.io.jvm.javaio.*
import java.io.InputStream
import java.nio.file.Path
import kotlin.contracts.InvocationKind
Expand Down Expand Up @@ -53,6 +52,8 @@ public sealed interface MessageCreateBuilder {

/**
* Adds a file with the [name] and [content] to the attachments.
*
* @suppress
*/
@Deprecated(
"Use lazy ChannelProvider instead of InputStream. You should also make sure that the stream/channel is only " +
Expand All @@ -69,10 +70,10 @@ public sealed interface MessageCreateBuilder {
addFile(name, ChannelProvider { content.toByteReadChannel() })

/**
* Adds a file with the [name] and [content] to the attachments.
* Adds a file with the [name] and [contentProvider] to the attachments.
*/
public fun addFile(name: String, content: ChannelProvider): NamedFile {
val namedFile = NamedFile(name, content)
public fun addFile(name: String, contentProvider: ChannelProvider): NamedFile {
val namedFile = NamedFile(name, contentProvider)
files += namedFile
return namedFile
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import dev.kord.rest.builder.message.AllowedMentionsBuilder
import dev.kord.rest.builder.message.EmbedBuilder
import io.ktor.client.request.forms.*
import io.ktor.util.cio.*
import io.ktor.utils.io.*
import io.ktor.utils.io.jvm.javaio.toByteReadChannel
import io.ktor.utils.io.jvm.javaio.*
import java.io.InputStream
import java.nio.file.Path
import kotlin.contracts.InvocationKind
Expand All @@ -35,6 +34,8 @@ public sealed interface MessageModifyBuilder {

/**
* Adds a file with the [name] and [content] to the attachments.
*
* @suppress
*/
@Deprecated(
"Use lazy ChannelProvider instead of InputStream. You should also make sure that the stream/channel is only " +
Expand All @@ -50,6 +51,9 @@ public sealed interface MessageModifyBuilder {
public fun addFile(name: String, content: InputStream): NamedFile =
addFile(name, ChannelProvider { content.toByteReadChannel() })

/**
* Adds a file with the given [path] to the attachments.
*/
public suspend fun addFile(path: Path): NamedFile =
addFile(path.fileName.toString(), ChannelProvider { path.readChannel() })

Expand Down
5 changes: 2 additions & 3 deletions rest/src/main/kotlin/request/Request.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import io.ktor.client.request.forms.*
import io.ktor.http.*
import io.ktor.http.content.*
import io.ktor.util.*
import io.ktor.utils.io.*
import kotlinx.serialization.SerializationStrategy
import kotlinx.serialization.json.Json

Expand Down Expand Up @@ -90,10 +89,10 @@ public class MultipartRequest<B : Any, R>(
body?.let {
append("payload_json", Json.encodeToString(it.strategy, it.body))
}
files.forEachIndexed { index, (fileName, channelProvider) ->
files.forEachIndexed { index, (fileName, contentProvider) ->
append(
"file$index",
channelProvider,
contentProvider,
headersOf(HttpHeaders.ContentDisposition, "filename=$fileName")
)
}
Expand Down
6 changes: 3 additions & 3 deletions rest/src/main/kotlin/request/RequestBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import dev.kord.rest.route.Route
import io.ktor.client.request.forms.*
import io.ktor.http.*
import io.ktor.util.cio.*
import io.ktor.utils.io.*
import kotlinx.serialization.SerializationStrategy
import java.io.InputStream
import java.nio.file.Path
Expand Down Expand Up @@ -60,6 +59,7 @@ public class RequestBuilder<T>(public val route: Route<T>, keySize: Int = 2) {
headers.append(key, value)
}

/** @suppress */
@Deprecated(
"Use lazy ChannelProvider instead of InputStream. You should also make sure that the stream/channel is only " +
"opened inside the block of the ChannelProvider because it could otherwise be read multiple times " +
Expand All @@ -80,8 +80,8 @@ public class RequestBuilder<T>(public val route: Route<T>, keySize: Int = 2) {
file(path.fileName.toString(), ChannelProvider { path.readChannel() })
}

public fun file(name: String, channelProvider: ChannelProvider) {
files.add(NamedFile(name, channelProvider))
public fun file(name: String, contentProvider: ChannelProvider) {
files.add(NamedFile(name, contentProvider))
}

public fun file(file: NamedFile) {
Expand Down
3 changes: 1 addition & 2 deletions rest/src/test/kotlin/request/MessageRequests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import io.ktor.client.*
import io.ktor.client.engine.mock.*
import io.ktor.client.request.forms.*
import io.ktor.util.cio.*
import io.ktor.utils.io.jvm.javaio.*
import kotlinx.coroutines.runBlocking
import kotlinx.datetime.Clock
import kotlinx.serialization.encodeToString
Expand Down Expand Up @@ -57,7 +56,7 @@ class MessageRequests {

val channelService = ChannelService(KtorRequestHandler(client = HttpClient(mockEngine), token = ""))

val fileChannel = ClassLoader.getSystemResource("images/kord.png")!!.toURI().toPath().readChannel()
val fileChannel = ClassLoader.getSystemResource("images/kord.png").toURI().toPath().readChannel()

with(fileChannel) {
assert(!isClosedForWrite)
Expand Down

0 comments on commit 48351b2

Please sign in to comment.