fbodr
06/30/2022, 12:28 PMRaphael TEYSSANDIER
06/30/2022, 5:42 PMMartin Gaens
07/03/2022, 12:02 PMkotlinx.serialization
for serializing objects and here's my issue: The Telegram API supports a lot of methods.
Here's an example of one such method:
The getFile
method. It has one parameter: file_id
which is of type String
.
These methods can receive their parameters through JSON objects. So, if I want to send a request to this method, I first create a class
called GetFileJSONRequest
with @SerialName()
annotations (because in Kotlin, the parameter is called fileId
and in JSON it has to be file_id
). And then, I send the request like this:
val response: HttpResponse = <http://client.post|client.post>("<https://api.telegram.org/bot123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11/getFile|https://api.telegram.org/bot123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11/getFile>") {
contentType(ContentType.Application.Json)
setBody(GetFileJSONRequest(fileId = fileId))
}
This whole process with creating classes seems very tedious and I wonder if there's a much better solution to my problem.fbodr
07/04/2022, 7:25 AMMartin Gaens
07/04/2022, 9:16 AMkotlinx.serialization
one? I saw on Stack Overflow that it's an unsolved mystery, but I feel like it is super important to know a good fix because it might be a deal breaker for some people.Jonas Trollvik
07/04/2022, 11:49 AMhhariri
Piotr Krzemiński
07/05/2022, 11:49 AMphldavies
07/05/2022, 4:33 PMNick Halase
07/05/2022, 5:41 PMimport <http://io.ktor.server.resources.post|io.ktor.server.resources.post> // GOOD
import <http://io.ktor.server.routing.post|io.ktor.server.routing.post> // BAD
Leonardo Borges
07/06/2022, 6:44 AMjeggy
07/06/2022, 12:05 PMprepareGet
but doesn't seem like this function existsDirk
07/06/2022, 3:31 PMspierce7
07/06/2022, 8:42 PMJonathan Mew
07/08/2022, 9:42 AMclayly
07/09/2022, 8:49 PMktor.application.modules = []
), but it fails with error like this:
Exception in thread "main" com.typesafe.config.ConfigException$WrongType: Command-line options: tasks has type STRING rather than LIST
Martin Gaens
07/11/2022, 12:34 AMprivate val client = HttpClient(CIO) {
install(DefaultRequest)
install(ContentNegotiation) {
json(Config.json)
}
install(HttpTimeout) {
connectTimeoutMillis = 30_000
requestTimeoutMillis = 30_000
}
install(Logging) {
level = LogLevel.ALL
}
defaultRequest {
url("<https://api.telegram.org/bot${bot.token}/>")
}
}
And this is how I handle the updates:
internal val updateFlow: Flow<Update> = flow {
while (true) {
try {
val updates = getUpdates(
offset = currentOffset,
timeout = KramBot.LONG_POLLING_TIMEOUT_SECONDS
)
// Telegram servers return an empty JSON array after the timeout if there are no updates
if (updates.isEmpty()) continue
updates.forEach { emit(it) }
currentOffset = updates.last().updateId + 1
} catch (e: HttpRequestTimeoutException) {
println("Timed out.")
}
}
}
private suspend fun getUpdates(
offset: Int? = null,
limit: Int? = null,
timeout: Int? = null,
allowedUpdates: List<String>? = null
): List<Update> {
val response = <http://client.post|client.post>("getUpdates") {
with(url.parameters) {
offset?.let { append("offset", it.toString()) }
limit?.let { append("limit", it.toString()) }
timeout?.let { append("timeout", it.toString()) }
allowedUpdates?.let { append("allowed_updates", it.toString()) }
}
}
when (val apiResponse = response.body<ApiResponse<List<UpdateDto>>>()) {
is ApiResponse.Ok -> return apiResponse.result.map { it.toEntity() }
is ApiResponse.Error -> error(
"""
Got an error response from the getUpdates method which should be impossible.
Error code: ${apiResponse.errorCode}
Description: ${apiResponse.description}
""".trimIndent()
)
}
}
albrechtroehm
07/11/2022, 11:33 AMFailed to parse HTTP response: unexpected EOF
. After searching for a while I stumbled upon a java example where they were using this command: System.setProperty("jdk.tls.maxHandshakeMessageSize", "65536")
. It did not change a thing when using HttpClient(CIO) but with HttpClient(Apache) the call is going through just fine now. Is there some other way to change the message size for CIO or should i just stick with the Apache engine?Hamza GATTAL
07/11/2022, 3:09 PMMikhail
07/11/2022, 4:22 PMVinay Pothnis
07/11/2022, 4:23 PMktor-client
? It looks like the ktor-server
has some metrics integrations - but I couldn’t quite get the details for the client.Jonathan De Leon
07/11/2022, 4:48 PMktor-server
? I'm using 1.6.8
.Louis Gautier
07/11/2022, 6:59 PMhhariri
Javier
07/13/2022, 12:46 PMHttpClient
without having to create other HttpClient
?steamstreet
07/13/2022, 6:34 PMget
for compressed data at a URL, and then call response.body<ByteArray>()
the response bytes are truncated to the content length of the response, but that value is the compressed size. So it’s taking the uncompressed bytes, and truncated them to the size of the compressed data. Is there another way to get at the uncompressed data without doing the gzip stuff myself?Hien Nguyen
07/14/2022, 10:21 AMSatyam Agarwal
07/14/2022, 1:07 PMCLOVIS
07/14/2022, 3:21 PMLawrence
07/14/2022, 8:31 PMcurl -X POST \
$url \
-H "Accept: application/json" \
-H "Content-Type: application/octet-stream" \
--data-binary "@$FILE"
i tried sending the file as body but I quickly ran out of memory with setBody(file.readBytes()
. Is there a streaming implementation without using MultiPartForm?