1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-22 16:23:48 +00:00

update ktor

This commit is contained in:
InsanusMokrassar 2020-01-15 12:28:06 +06:00
parent e98e61747c
commit 562459f0b7
11 changed files with 39 additions and 36 deletions

View File

@ -32,6 +32,7 @@ bot.
### 0.21.1 ### 0.21.1
* **`KtorCallFactory` must return `HttpStatement` instead of `HttpClientCall`**
* `SendMessage` was renamed to `SendTextMessage` and previous `SendMessage` is deprecated * `SendMessage` was renamed to `SendTextMessage` and previous `SendMessage` is deprecated
* Most part of requests have changed return type. They are listed below: * Most part of requests have changed return type. They are listed below:
<details> <details>
@ -80,6 +81,8 @@ bot.
* `StickerContent` * `StickerContent`
</details> </details>
* Version updates:
* Ktor `1.2.6` -> `1.3.0`
## 0.20.0 MPP Migration ## 0.20.0 MPP Migration

View File

@ -4,7 +4,7 @@ kotlin_coroutines_version=1.3.3
kotlin_serialisation_runtime_version=0.14.0 kotlin_serialisation_runtime_version=0.14.0
klock_version=1.8.6 klock_version=1.8.6
uuid_version=0.0.7 uuid_version=0.0.7
ktor_version=1.2.6 ktor_version=1.3.0
gradle_bintray_plugin_version=1.8.4 gradle_bintray_plugin_version=1.8.4

View File

@ -3,11 +3,12 @@ package com.github.insanusmokrassar.TelegramBotAPI.bot.Ktor
import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.Request import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.Request
import io.ktor.client.HttpClient import io.ktor.client.HttpClient
import io.ktor.client.call.HttpClientCall import io.ktor.client.call.HttpClientCall
import io.ktor.client.statement.HttpStatement
interface KtorCallFactory { interface KtorCallFactory {
suspend fun <T: Any> prepareCall( suspend fun <T: Any> prepareCall(
client: HttpClient, client: HttpClient,
baseUrl: String, baseUrl: String,
request: Request<T> request: Request<T>
) : HttpClientCall? ) : HttpStatement?
} }

View File

@ -11,10 +11,10 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.Response
import com.github.insanusmokrassar.TelegramBotAPI.types.RetryAfterError import com.github.insanusmokrassar.TelegramBotAPI.types.RetryAfterError
import com.github.insanusmokrassar.TelegramBotAPI.utils.TelegramAPIUrlsKeeper import com.github.insanusmokrassar.TelegramBotAPI.utils.TelegramAPIUrlsKeeper
import io.ktor.client.HttpClient import io.ktor.client.HttpClient
import io.ktor.client.call.HttpClientCall
import io.ktor.client.call.receive import io.ktor.client.call.receive
import io.ktor.client.features.ClientRequestException import io.ktor.client.features.ClientRequestException
import io.ktor.client.response.readText import io.ktor.client.statement.HttpStatement
import io.ktor.client.statement.readText
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
@ -36,22 +36,20 @@ class KtorRequestsExecutor(
override suspend fun <T : Any> execute(request: Request<T>): T { override suspend fun <T : Any> execute(request: Request<T>): T {
return requestsLimiter.limit { return requestsLimiter.limit {
var call: HttpClientCall? = null var statement: HttpStatement? = null
for (factory in callsFactories) { for (factory in callsFactories) {
call = factory.prepareCall( statement = factory.prepareCall(
client, client,
telegramAPIUrlsKeeper.commonAPIUrl, telegramAPIUrlsKeeper.commonAPIUrl,
request request
) )
if (call != null) { if (statement != null) {
break break
} }
} }
if (call == null) {
throw IllegalArgumentException("Can't execute request: $request")
}
try { try {
val content = call.response.receive<String>() val response = statement ?.execute() ?: throw IllegalArgumentException("Can't execute request: $request")
val content = response.receive<String>()
val responseObject = jsonFormatter.parse(Response.serializer(), content) val responseObject = jsonFormatter.parse(Response.serializer(), content)
(responseObject.result?.let { (responseObject.result?.let {
@ -64,7 +62,7 @@ class KtorRequestsExecutor(
} else { } else {
null null
} }
} ?: call.let { } ?: response.let {
throw newRequestException( throw newRequestException(
responseObject, responseObject,
content, content,

View File

@ -4,9 +4,8 @@ import com.github.insanusmokrassar.TelegramBotAPI.bot.Ktor.KtorCallFactory
import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.Request import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.Request
import io.ktor.client.HttpClient import io.ktor.client.HttpClient
import io.ktor.client.call.HttpClientCall import io.ktor.client.call.HttpClientCall
import io.ktor.client.call.call import io.ktor.client.request.*
import io.ktor.client.request.accept import io.ktor.client.statement.HttpStatement
import io.ktor.client.request.url
import io.ktor.http.ContentType import io.ktor.http.ContentType
import io.ktor.http.HttpMethod import io.ktor.http.HttpMethod
import kotlin.collections.set import kotlin.collections.set
@ -17,20 +16,23 @@ abstract class AbstractRequestCallFactory : KtorCallFactory {
client: HttpClient, client: HttpClient,
baseUrl: String, baseUrl: String,
request: Request<T> request: Request<T>
): HttpClientCall? { ): HttpStatement? {
val preparedBody = prepareCallBody(client, baseUrl, request) ?: return null val preparedBody = prepareCallBody(client, baseUrl, request) ?: return null
return client.call { return HttpStatement(
url( HttpRequestBuilder().apply {
methodsCache[request.method()] ?: "$baseUrl/${request.method()}".also { url(
methodsCache[request.method()] = it methodsCache[request.method()] ?: "$baseUrl/${request.method()}".also {
} methodsCache[request.method()] = it
) }
method = HttpMethod.Post )
accept(ContentType.Application.Json) method = HttpMethod.Post
accept(ContentType.Application.Json)
body = preparedBody body = preparedBody
} },
client
)
} }
protected abstract fun <T : Any> prepareCallBody( protected abstract fun <T : Any> prepareCallBody(

View File

@ -1,7 +1,7 @@
package com.github.insanusmokrassar.TelegramBotAPI.bot package com.github.insanusmokrassar.TelegramBotAPI.bot
import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.Request import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.Request
import kotlinx.io.core.Closeable import io.ktor.utils.io.core.Closeable
interface RequestsExecutor : Closeable { interface RequestsExecutor : Closeable {
/** /**

View File

@ -1,8 +1,8 @@
package com.github.insanusmokrassar.TelegramBotAPI.bot package com.github.insanusmokrassar.TelegramBotAPI.bot
import io.ktor.utils.io.core.Closeable
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.io.core.Closeable
interface UpdatesPoller : Closeable { interface UpdatesPoller : Closeable {
fun start(scope: CoroutineScope = CoroutineScope(Dispatchers.Default)) fun start(scope: CoroutineScope = CoroutineScope(Dispatchers.Default))

View File

@ -1,7 +1,7 @@
package com.github.insanusmokrassar.TelegramBotAPI.bot.exceptions package com.github.insanusmokrassar.TelegramBotAPI.bot.exceptions
import com.github.insanusmokrassar.TelegramBotAPI.types.Response import com.github.insanusmokrassar.TelegramBotAPI.types.Response
import kotlinx.io.errors.IOException import kotlinx.io.IOException
fun newRequestException( fun newRequestException(
response: Response, response: Response,
@ -22,10 +22,9 @@ sealed class RequestException constructor(
val response: Response, val response: Response,
val plainAnswer: String, val plainAnswer: String,
message: String? = null, message: String? = null,
cause: Throwable? = null override val cause: Throwable? = null
) : IOException( ) : IOException(
message ?: "Something went wrong", message ?: "Something went wrong"
cause
) )
class CommonRequestException(response: Response, plainAnswer: String, message: String?, cause: Throwable?) : class CommonRequestException(response: Response, plainAnswer: String, message: String?, cause: Throwable?) :

View File

@ -1,6 +1,6 @@
package com.github.insanusmokrassar.TelegramBotAPI.utils package com.github.insanusmokrassar.TelegramBotAPI.utils
import kotlinx.io.core.Input import io.ktor.utils.io.core.Input
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
@Serializable @Serializable

View File

@ -1,7 +1,7 @@
package com.github.insanusmokrassar.TelegramBotAPI.utils package com.github.insanusmokrassar.TelegramBotAPI.utils
import com.benasher44.uuid.uuid4 import com.benasher44.uuid.uuid4
import kotlinx.io.core.Input import io.ktor.utils.io.core.Input
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
import kotlinx.serialization.Transient import kotlinx.serialization.Transient

View File

@ -1,8 +1,8 @@
package com.github.insanusmokrassar.TelegramBotAPI.utils package com.github.insanusmokrassar.TelegramBotAPI.utils
import com.benasher44.uuid.uuid4 import com.benasher44.uuid.uuid4
import kotlinx.io.core.Input import io.ktor.utils.io.core.Input
import kotlinx.io.streams.asInput import io.ktor.utils.io.streams.asInput
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
import kotlinx.serialization.Transient import kotlinx.serialization.Transient
import java.io.File import java.io.File