1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-06-03 00:15:27 +00:00
tgbotapi/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/Ktor/base/MultipartRequestCallFactory.kt

42 lines
1.7 KiB
Kotlin
Raw Normal View History

2020-10-04 11:06:30 +00:00
package dev.inmo.tgbotapi.bot.Ktor.base
2018-12-26 08:07:24 +00:00
import dev.inmo.tgbotapi.bot.Ktor.KtorCallFactory
2020-10-04 11:06:30 +00:00
import dev.inmo.tgbotapi.requests.abstracts.*
import dev.inmo.tgbotapi.utils.TelegramAPIUrlsKeeper
import dev.inmo.tgbotapi.utils.mapWithCommonValues
2018-12-26 08:07:24 +00:00
import io.ktor.client.HttpClient
import io.ktor.client.request.forms.MultiPartFormDataContent
import io.ktor.client.request.forms.formData
2019-05-12 11:15:31 +00:00
import io.ktor.http.Headers
import io.ktor.http.HttpHeaders
2018-12-26 08:07:24 +00:00
class MultipartRequestCallFactory : AbstractRequestCallFactory() {
2019-04-26 03:12:01 +00:00
override fun <T : Any> prepareCallBody(
2018-12-26 08:07:24 +00:00
client: HttpClient,
2020-08-23 15:43:58 +00:00
urlsKeeper: TelegramAPIUrlsKeeper,
2018-12-26 08:07:24 +00:00
request: Request<T>
2019-04-26 03:12:01 +00:00
): Any? = (request as? MultipartRequest) ?.let { castedRequest ->
MultiPartFormDataContent(
formData {
val params = castedRequest.paramsJson.mapWithCommonValues()
for ((key, value) in castedRequest.mediaMap + params) {
when (value) {
is MultipartFile -> appendInput(
2019-04-26 03:12:01 +00:00
key,
Headers.build {
append(HttpHeaders.ContentDisposition, "filename=${value.filename}")
},
block = value.file::input
)
2019-04-26 03:12:01 +00:00
is FileId -> append(key, value.fileId)
else -> append(key, value.toString())
2018-12-26 08:07:24 +00:00
}
}
2019-04-26 03:12:01 +00:00
}
)
2018-12-26 08:07:24 +00:00
}
@Deprecated("Use class MultipartRequestCallFactory() constructor call instead of just MultipartRequestCallFactory")
companion object : KtorCallFactory by MultipartRequestCallFactory()
}