From 6d8190e5aaf0cda6534c6cb791f61cb30e925092 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 20 Apr 2019 12:55:11 +0800 Subject: [PATCH] commonly of webhooks and other requests-related code --- CHANGELOG.md | 5 +++ build.gradle | 11 ++----- gradle.properties | 2 +- .../insanusmokrassar/TelegramBotAPI/Index.kt | 16 ---------- .../bot/Ktor/KtorRequestsExecutor.kt | 7 ++-- .../TelegramBotAPI/bot/Ktor/ProxySettings.kt | 32 ------------------- .../TelegramBotAPI/bot/ProxySettings.kt | 18 ----------- .../utils/extensions/Webhooks.kt | 30 ++++++++--------- 8 files changed, 25 insertions(+), 96 deletions(-) delete mode 100644 src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/Index.kt delete mode 100644 src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/Ktor/ProxySettings.kt delete mode 100644 src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/ProxySettings.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index c7f3264e0f..da0ff57509 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,11 @@ ## 0.14.0 +* Now library have no default engine for both webhooks and requests executor. It is required for clients to set +some default library +* All proxy help methods was removed . They are will be replaced in separated project +* `Ktor` version `1.1.3` -> `1.1.4` + ## 0.13.0 Telegram Polls * Type `PollOption` and `AnonymousPollOption` added diff --git a/build.gradle b/build.gradle index d34c7e57b7..a7ed40969e 100644 --- a/build.gradle +++ b/build.gradle @@ -26,7 +26,6 @@ repositories { jcenter() mavenCentral() maven { url "https://kotlin.bintray.com/kotlinx" } - maven { url "https://dl.bintray.com/kotlin/ktor" } } dependencies { @@ -35,14 +34,10 @@ dependencies { implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime:$kotlin_serialisation_runtime_version" implementation "joda-time:joda-time:$joda_time_version" - implementation "io.ktor:ktor-client-core:$ktor_version" - implementation "io.ktor:ktor-client-okhttp:$ktor_version" + implementation "io.ktor:ktor-client:$ktor_version" - implementation "io.ktor:ktor-server-core:$ktor_version" - implementation "io.ktor:ktor-server-netty:$ktor_version" - - // Use JUnit test framework - testImplementation 'junit:junit:4.12' + implementation "io.ktor:ktor-server:$ktor_version" + implementation "io.ktor:ktor-server-host-common:$ktor_version" } compileKotlin { diff --git a/gradle.properties b/gradle.properties index 43c7278bdd..7f46ad5b74 100644 --- a/gradle.properties +++ b/gradle.properties @@ -3,7 +3,7 @@ kotlin_version=1.3.30 kotlin_coroutines_version=1.2.0 kotlin_serialisation_runtime_version=0.11.0 joda_time_version=2.10.1 -ktor_version=1.1.3 +ktor_version=1.1.4 gradle_bintray_plugin_version=1.8.4 diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/Index.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/Index.kt deleted file mode 100644 index bf1c66aab4..0000000000 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/Index.kt +++ /dev/null @@ -1,16 +0,0 @@ -package com.github.insanusmokrassar.TelegramBotAPI - -import com.github.insanusmokrassar.TelegramBotAPI.bot.Ktor.KtorRequestsExecutor -import io.ktor.client.engine.okhttp.OkHttp -import kotlinx.coroutines.runBlocking - -fun main(args: Array) { - runBlocking { - KtorRequestsExecutor( - args[0], - OkHttp.create() - ).apply { - // It is just template of creating requests executor - } - } -} diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/Ktor/KtorRequestsExecutor.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/Ktor/KtorRequestsExecutor.kt index 285bf033e9..448fada318 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/Ktor/KtorRequestsExecutor.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/Ktor/KtorRequestsExecutor.kt @@ -12,7 +12,6 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.RetryAfterError import io.ktor.client.HttpClient import io.ktor.client.call.HttpClientCall import io.ktor.client.engine.HttpClientEngine -import io.ktor.client.engine.okhttp.OkHttp import io.ktor.util.cio.toByteArray import kotlinx.coroutines.delay import kotlinx.io.charsets.Charset @@ -20,7 +19,7 @@ import kotlinx.serialization.json.Json class KtorRequestsExecutor( token: String, - private val client: HttpClient = HttpClient(OkHttp), + private val client: HttpClient = HttpClient(), hostUrl: String = "https://api.telegram.org", callsFactories: List = emptyList(), excludeDefaultFactories: Boolean = false, @@ -29,11 +28,11 @@ class KtorRequestsExecutor( ) : BaseRequestsExecutor(token, hostUrl) { constructor( token: String, - engine: HttpClientEngine = OkHttp.create(), + engine: HttpClientEngine? = null, hostUrl: String = "https://api.telegram.org" ) : this( token, - HttpClient(engine), + engine ?.let { HttpClient(engine) } ?: HttpClient(), hostUrl ) diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/Ktor/ProxySettings.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/Ktor/ProxySettings.kt deleted file mode 100644 index 745c4ecdf8..0000000000 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/Ktor/ProxySettings.kt +++ /dev/null @@ -1,32 +0,0 @@ -package com.github.insanusmokrassar.TelegramBotAPI.bot.Ktor - -import com.github.insanusmokrassar.TelegramBotAPI.bot.settings.ProxySettings -import io.ktor.http.HttpHeaders -import okhttp3.Credentials -import okhttp3.OkHttpClient -import java.net.InetSocketAddress -import java.net.Proxy - -fun OkHttpClient.Builder.useWith(proxySettings: ProxySettings) { - proxy( - Proxy( - Proxy.Type.SOCKS, - InetSocketAddress( - proxySettings.host, - proxySettings.port - ) - ) - ) - proxySettings.password ?.let { - password -> - proxyAuthenticator { - _, response -> - response.request().newBuilder().apply { - addHeader( - HttpHeaders.ProxyAuthorization, - Credentials.basic(proxySettings.username ?: "", password) - ) - }.build() - } - } -} diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/ProxySettings.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/ProxySettings.kt deleted file mode 100644 index 12e3ab9e71..0000000000 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/ProxySettings.kt +++ /dev/null @@ -1,18 +0,0 @@ -package com.github.insanusmokrassar.TelegramBotAPI.bot - -import com.github.insanusmokrassar.TelegramBotAPI.bot.Ktor.useWith -import com.github.insanusmokrassar.TelegramBotAPI.bot.settings.ProxySettings -import okhttp3.OkHttpClient - -@Deprecated( - "Replaced in settings package", - ReplaceWith("ProxySettings", "com.github.insanusmokrassar.TelegramBotAPI.bot.settings.ProxySettings") -) -typealias ProxySettings = com.github.insanusmokrassar.TelegramBotAPI.bot.settings.ProxySettings - - -@Deprecated( - "Replaced in Ktor package", - ReplaceWith("useWith", "com.github.insanusmokrassar.TelegramBotAPI.bot.Ktor.useWith") -) -fun OkHttpClient.Builder.useWith(proxySettings: ProxySettings) = useWith(proxySettings) diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/Webhooks.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/Webhooks.kt index 7d0af080a7..c198a22872 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/Webhooks.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/Webhooks.kt @@ -16,7 +16,6 @@ import io.ktor.response.respond import io.ktor.routing.post import io.ktor.routing.routing import io.ktor.server.engine.* -import io.ktor.server.netty.Netty import kotlinx.coroutines.* import kotlinx.coroutines.channels.Channel import kotlinx.serialization.json.Json @@ -36,12 +35,12 @@ import java.util.concurrent.TimeUnit suspend fun RequestsExecutor.setWebhook( url: String, port: Int, + engineFactory: ApplicationEngineFactory<*, *>, certificate: InputFile? = null, privateKeyConfig: WebhookPrivateKeyConfig? = null, scope: CoroutineScope = CoroutineScope(Executors.newFixedThreadPool(4).asCoroutineDispatcher()), allowedUpdates: List? = null, maxAllowedConnections: Int? = null, - engineFactory: ApplicationEngineFactory<*, *> = Netty, block: UpdateReceiver ): Job { val executeDeferred = certificate ?.let { @@ -69,20 +68,17 @@ suspend fun RequestsExecutor.setWebhook( val env = applicationEngineEnvironment { module { - fun Application.main() { - routing { - post { - val deserialized = call.receiveText() - val update = Json.nonstrict.parse( - RawUpdate.serializer(), - deserialized - ) - updatesChannel.send(update.asUpdate) - call.respond("Ok") - } + routing { + post { + val deserialized = call.receiveText() + val update = Json.nonstrict.parse( + RawUpdate.serializer(), + deserialized + ) + updatesChannel.send(update.asUpdate) + call.respond("Ok") } } - main() } privateKeyConfig ?.let { sslConnector( @@ -140,19 +136,19 @@ suspend fun RequestsExecutor.setWebhook( url: String, port: Int, filter: UpdatesFilter, + engineFactory: ApplicationEngineFactory<*, *>, certificate: InputFile? = null, privateKeyConfig: WebhookPrivateKeyConfig? = null, scope: CoroutineScope = CoroutineScope(Executors.newFixedThreadPool(4).asCoroutineDispatcher()), - maxAllowedConnections: Int? = null, - engineFactory: ApplicationEngineFactory<*, *> = Netty + maxAllowedConnections: Int? = null ): Job = setWebhook( url, port, + engineFactory, certificate, privateKeyConfig, scope, filter.allowedUpdates, maxAllowedConnections, - engineFactory, filter.asUpdateReceiver )