mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2024-11-22 16:23:48 +00:00
Merge pull request #45 from InsanusMokrassar/task/28-migration_to_mpp
Migration onto Kotlin Multiplatform
This commit is contained in:
commit
7660102435
@ -1,5 +1,13 @@
|
||||
# TelegramBotAPI changelog
|
||||
|
||||
## 0.20.0 MPP Migration
|
||||
|
||||
* Time library change: `joda-time` -> `com.soywiz.korlibs.klock:klock`
|
||||
* `Currencied` now using as `currency` value with type `String`
|
||||
* For `Java` there is `Currencied#javaCurrency` extension function, which will give an old currency work way
|
||||
* `User` now have no field `userLocale`
|
||||
* For `Java` there is `User#javaLocale` extension function, which will give an old locale work way
|
||||
|
||||
## 0.19.0 ImplicitReflection removing
|
||||
|
||||
* Total rework of serialization for requests. Now all `SimpleRequest` children have:
|
||||
|
57
build.gradle
57
build.gradle
@ -1,6 +1,3 @@
|
||||
project.version = "0.19.0"
|
||||
project.group = "com.github.insanusmokrassar"
|
||||
|
||||
buildscript {
|
||||
repositories {
|
||||
mavenLocal()
|
||||
@ -15,9 +12,13 @@ buildscript {
|
||||
}
|
||||
}
|
||||
|
||||
apply plugin: 'java-library'
|
||||
apply plugin: 'kotlin'
|
||||
apply plugin: 'kotlinx-serialization'
|
||||
plugins {
|
||||
id "org.jetbrains.kotlin.multiplatform" version "$kotlin_version"
|
||||
id "org.jetbrains.kotlin.plugin.serialization" version "$kotlin_version"
|
||||
}
|
||||
|
||||
project.version = "0.19.0-kmpp-migration"
|
||||
project.group = "com.github.insanusmokrassar"
|
||||
|
||||
apply from: "publish.gradle"
|
||||
|
||||
@ -28,17 +29,49 @@ repositories {
|
||||
maven { url "https://kotlin.bintray.com/kotlinx" }
|
||||
}
|
||||
|
||||
kotlin {
|
||||
jvm()
|
||||
// js()
|
||||
|
||||
sourceSets {
|
||||
commonMain {
|
||||
dependencies {
|
||||
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
||||
implementation kotlin('stdlib')
|
||||
api "org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlin_coroutines_version"
|
||||
api "org.jetbrains.kotlinx:kotlinx-serialization-runtime-common:$kotlin_serialisation_runtime_version"
|
||||
api "com.soywiz.korlibs.klock:klock:$klock_version"
|
||||
|
||||
api "io.ktor:ktor-client-core:$ktor_version"
|
||||
}
|
||||
}
|
||||
commonTest {
|
||||
dependencies {
|
||||
implementation kotlin('test-common')
|
||||
implementation kotlin('test-annotations-common')
|
||||
}
|
||||
}
|
||||
|
||||
jvmMain {
|
||||
dependencies {
|
||||
api "org.jetbrains.kotlinx:kotlinx-serialization-runtime:$kotlin_serialisation_runtime_version"
|
||||
api "joda-time:joda-time:$joda_time_version"
|
||||
|
||||
api "io.ktor:ktor-client:$ktor_version"
|
||||
api "io.ktor:ktor-client-cio:$ktor_version"
|
||||
|
||||
api "io.ktor:ktor-server:$ktor_version"
|
||||
api "io.ktor:ktor-server-host-common:$ktor_version"
|
||||
|
||||
testImplementation "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
|
||||
api "io.ktor:ktor-server-host-common:$ktor_version"
|
||||
api "io.ktor:ktor-client-cio:$ktor_version"
|
||||
}
|
||||
}
|
||||
jvmTest {
|
||||
dependencies {
|
||||
implementation kotlin('test-junit')
|
||||
}
|
||||
}
|
||||
|
||||
// jsMain {
|
||||
// dependencies {
|
||||
// api "org.jetbrains.kotlinx:kotlinx-serialization-runtime-js:$kotlin_serialisation_runtime_version"
|
||||
// }
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
@ -2,7 +2,7 @@ kotlin.code.style=official
|
||||
kotlin_version=1.3.61
|
||||
kotlin_coroutines_version=1.3.2
|
||||
kotlin_serialisation_runtime_version=0.14.0
|
||||
joda_time_version=2.10.5
|
||||
klock_version=1.8.0
|
||||
ktor_version=1.2.6
|
||||
|
||||
gradle_bintray_plugin_version=1.8.4
|
||||
|
@ -1,55 +1,51 @@
|
||||
apply plugin: 'maven-publish'
|
||||
apply plugin: 'signing'
|
||||
|
||||
task sourcesJar(type: Jar) {
|
||||
from sourceSets.main.allSource
|
||||
classifier = 'sources'
|
||||
}
|
||||
|
||||
task javadocJar(type: Jar) {
|
||||
from javadoc
|
||||
task javadocsJar(type: Jar) {
|
||||
classifier = 'javadoc'
|
||||
}
|
||||
|
||||
publishing {
|
||||
publications {
|
||||
maven(MavenPublication) {
|
||||
from components.java
|
||||
afterEvaluate {
|
||||
project.publishing.publications.all {
|
||||
// rename artifacts
|
||||
groupId "${project.group}"
|
||||
artifactId "${project.name}"
|
||||
version "${project.version}"
|
||||
if (it.name.contains('kotlinMultiplatform')) {
|
||||
artifactId = "${project.name}"
|
||||
} else {
|
||||
artifactId = "${project.name}-$name"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
artifact sourcesJar
|
||||
artifact javadocJar
|
||||
publishing {
|
||||
publications.all {
|
||||
artifact javadocsJar
|
||||
|
||||
pom.withXml {
|
||||
asNode().children().last() + {
|
||||
resolveStrategy = Closure.DELEGATE_FIRST
|
||||
name "${project_public_name}"
|
||||
description "${project_public_description}"
|
||||
url "https://insanusmokrassar.github.io/${project.name}"
|
||||
|
||||
description "Library for Object-Oriented and type-safe work with Telegram Bot API"
|
||||
name "Telegram Bot API"
|
||||
url "https://insanusmokrassar.github.io/TelegramBotAPI"
|
||||
|
||||
scm {
|
||||
connection "scm:git:git://github.com/insanusmokrassar/${project.name}.git"
|
||||
developerConnection "scm:git:[fetch=]https://github.com/insanusmokrassar/${project.name}.git[push=]ssh:git@github.com:insanusmokrassar/${project.name}.git"
|
||||
url "https://github.com/insanusmokrassar/${project.name}"
|
||||
|
||||
developerConnection "scm:git:[fetch=]https://github.com/insanusmokrassar/TelegramBotAPI.git[push=]https://github.com/insanusmokrassar/TelegramBotAPI.git"
|
||||
url "https://github.com/insanusmokrassar/TelegramBotAPI.git"
|
||||
}
|
||||
|
||||
developers {
|
||||
developer {
|
||||
id "InsanusMokrassar"
|
||||
name "Ovsyannikov Alexey"
|
||||
name "Ovsiannikov Aleksei"
|
||||
email "ovsyannikov.alexey95@gmail.com"
|
||||
}
|
||||
}
|
||||
|
||||
licenses {
|
||||
license {
|
||||
name 'The Apache Software License, Version 2.0'
|
||||
url 'https://github.com/InsanusMokrassar/TelegramBotAPI/blob/master/LICENSE'
|
||||
distribution 'repo'
|
||||
}
|
||||
name "Apache Software License 2.0"
|
||||
url "https://github.com/InsanusMokrassar/TelegramBotAPI/blob/master/LICENSE"
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -59,5 +55,5 @@ publishing {
|
||||
|
||||
signing {
|
||||
useGpgCmd()
|
||||
sign publishing.publications.maven
|
||||
sign(publishing.publications)
|
||||
}
|
||||
|
1
mpp_publish_template.json
Normal file
1
mpp_publish_template.json
Normal file
@ -0,0 +1 @@
|
||||
{"bintrayConfig":{"repo":"StandardRepository","packageName":"${project.name}","packageVcs":"https://github.com/InsanusMokrassar/TelegramBotAPI"},"licenses":[{"id":"Apache-2.0","title":"Apache Software License 2.0","url":"https://github.com/InsanusMokrassar/TelegramBotAPI/blob/master/LICENSE"}],"mavenConfig":{"name":"Telegram Bot API","description":"Library for Object-Oriented and type-safe work with Telegram Bot API","url":"https://insanusmokrassar.github.io/TelegramBotAPI","vcsUrl":"https://github.com/insanusmokrassar/TelegramBotAPI.git","developers":[{"id":"InsanusMokrassar","name":"Ovsiannikov Aleksei","eMail":"ovsyannikov.alexey95@gmail.com"}]}}
|
@ -1,33 +1,27 @@
|
||||
apply plugin: 'com.jfrog.bintray'
|
||||
|
||||
ext {
|
||||
projectBintrayDir = "${project.group}/".replace(".", "/") + "${project.name}/${project.version}"
|
||||
}
|
||||
apply from: "maven.publish.gradle"
|
||||
|
||||
bintray {
|
||||
user = project.hasProperty('BINTRAY_USER') ? project.property('BINTRAY_USER') : System.getenv('BINTRAY_USER')
|
||||
key = project.hasProperty('BINTRAY_KEY') ? project.property('BINTRAY_KEY') : System.getenv('BINTRAY_KEY')
|
||||
publications = ["maven"]
|
||||
filesSpec {
|
||||
into "$projectBintrayDir"
|
||||
from("build/libs") {
|
||||
include "**/*.asc"
|
||||
}
|
||||
from("build/publications/maven") {
|
||||
rename 'pom-default.xml(.*)', "${project.name}-${project.version}.pom\$1"
|
||||
}
|
||||
}
|
||||
pkg {
|
||||
repo = 'StandardRepository'
|
||||
repo = "StandardRepository"
|
||||
name = "${project.name}"
|
||||
vcsUrl = "https://github.com/InsanusMokrassar/${project.name}"
|
||||
licenses = ['Apache-2.0']
|
||||
vcsUrl = "https://github.com/InsanusMokrassar/TelegramBotAPI"
|
||||
licenses = ["Apache-2.0"]
|
||||
version {
|
||||
name = "${project.version}"
|
||||
released = new Date()
|
||||
vcsTag = name
|
||||
vcsTag = "${project.version}"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
apply from: "maven.publish.gradle"
|
||||
bintrayUpload.doFirst {
|
||||
publications = publishing.publications.collect {
|
||||
it.name
|
||||
}
|
||||
}
|
||||
|
||||
bintrayUpload.dependsOn publishToMavenLocal
|
@ -12,8 +12,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.RetryAfterError
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.TelegramAPIUrlsKeeper
|
||||
import io.ktor.client.HttpClient
|
||||
import io.ktor.client.call.HttpClientCall
|
||||
import io.ktor.client.engine.HttpClientEngine
|
||||
import io.ktor.util.cio.toByteArray
|
||||
import io.ktor.client.call.receive
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.serialization.json.Json
|
||||
|
||||
@ -49,9 +48,7 @@ class KtorRequestsExecutor(
|
||||
if (call == null) {
|
||||
throw IllegalArgumentException("Can't execute request: $request")
|
||||
}
|
||||
val content = call.response.use {
|
||||
it.content.toByteArray().toString(Charsets.UTF_8)
|
||||
}
|
||||
val content = call.response.receive<String>()
|
||||
val responseObject = jsonFormatter.parse(Response.serializer(), content)
|
||||
|
||||
(responseObject.result ?.let {
|
@ -5,10 +5,8 @@ import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.Request
|
||||
import io.ktor.client.HttpClient
|
||||
import io.ktor.client.call.HttpClientCall
|
||||
import io.ktor.client.call.call
|
||||
import io.ktor.client.request.accept
|
||||
import io.ktor.client.request.url
|
||||
import io.ktor.http.ContentType
|
||||
import io.ktor.http.HttpMethod
|
||||
import io.ktor.client.request.*
|
||||
import io.ktor.http.*
|
||||
|
||||
abstract class AbstractRequestCallFactory : KtorCallFactory {
|
||||
private val methodsCache: MutableMap<String, String> = mutableMapOf()
|
@ -21,14 +21,15 @@ class MultipartRequestCallFactory : AbstractRequestCallFactory() {
|
||||
val params = castedRequest.paramsJson.mapWithCommonValues()
|
||||
for ((key, value) in castedRequest.mediaMap + params) {
|
||||
when (value) {
|
||||
is MultipartFile -> append(
|
||||
is MultipartFile -> appendInput(
|
||||
key,
|
||||
value.file.asInput().readBytes(),
|
||||
Headers.build {
|
||||
append(HttpHeaders.ContentType, value.mimeType)
|
||||
append(HttpHeaders.ContentDisposition, "filename=${value.fileId}")
|
||||
}
|
||||
)
|
||||
) {
|
||||
value.file.asInput()
|
||||
}
|
||||
is FileId -> append(key, value.fileId)
|
||||
else -> append(key, value.toString())
|
||||
}
|
@ -1,10 +1,11 @@
|
||||
package com.github.insanusmokrassar.TelegramBotAPI.bot
|
||||
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.bot.exceptions.RequestException
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.Request
|
||||
import kotlinx.io.core.Closeable
|
||||
|
||||
interface RequestsExecutor : Closeable {
|
||||
@Throws(RequestException::class)
|
||||
/**
|
||||
* @throws com.github.insanusmokrassar.TelegramBotAPI.bot.exceptions.RequestException
|
||||
*/
|
||||
suspend fun <T : Any> execute(request: Request<T>): T
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
package com.github.insanusmokrassar.TelegramBotAPI.bot.exceptions
|
||||
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.Response
|
||||
import java.io.IOException
|
||||
import kotlinx.io.errors.IOException
|
||||
|
||||
fun newRequestException(
|
||||
response: Response,
|
||||
@ -20,7 +20,7 @@ sealed class RequestException constructor(
|
||||
message: String? = null,
|
||||
cause: Throwable? = null
|
||||
) : IOException(
|
||||
message,
|
||||
message ?: "Something went wrong",
|
||||
cause
|
||||
)
|
||||
|
@ -1,9 +1,10 @@
|
||||
package com.github.insanusmokrassar.TelegramBotAPI.bot.settings.limiters
|
||||
|
||||
import com.soywiz.klock.DateTime
|
||||
import kotlinx.coroutines.*
|
||||
import kotlinx.coroutines.channels.Channel
|
||||
|
||||
private fun now(): Long = System.currentTimeMillis()
|
||||
private fun now(): Long = DateTime.nowUnixLong()
|
||||
|
||||
class CommonLimiter(
|
||||
private val lockCount: Int = 10,
|
@ -4,8 +4,8 @@ import kotlinx.coroutines.*
|
||||
import kotlinx.coroutines.channels.Channel
|
||||
import kotlinx.serialization.Serializable
|
||||
import kotlinx.serialization.Transient
|
||||
import java.util.concurrent.Executors
|
||||
import kotlin.coroutines.*
|
||||
import kotlin.math.pow
|
||||
|
||||
private sealed class RequestEvent
|
||||
private class AddRequest(
|
||||
@ -21,9 +21,7 @@ data class PowLimiter(
|
||||
private val powK: Double = 0.0016
|
||||
) : RequestLimiter {
|
||||
@Transient
|
||||
private val scope = CoroutineScope(
|
||||
Executors.newFixedThreadPool(3).asCoroutineDispatcher()
|
||||
)
|
||||
private val scope = CoroutineScope(Dispatchers.Default)
|
||||
@Transient
|
||||
private val eventsChannel = Channel<RequestEvent>(Channel.UNLIMITED)
|
||||
@Transient
|
||||
@ -35,7 +33,7 @@ data class PowLimiter(
|
||||
for (event in eventsChannel) {
|
||||
when (event) {
|
||||
is AddRequest -> {
|
||||
val awaitTime = ((Math.pow(requestsInWork, powValue) * powK) * 1000L).toLong()
|
||||
val awaitTime = (((requestsInWork.pow(powValue) * powK) * 1000L).toLong())
|
||||
requestsInWork++
|
||||
|
||||
event.continuation.resume(
|
@ -3,7 +3,6 @@ package com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.StorageFile
|
||||
import kotlinx.serialization.*
|
||||
import kotlinx.serialization.internal.StringDescriptor
|
||||
import java.io.File
|
||||
|
||||
@Serializable(InputFileSerializer::class)
|
||||
sealed class InputFile {
|
||||
@ -40,7 +39,3 @@ data class MultipartFile (
|
||||
) : InputFile() {
|
||||
override val fileId: String = file.generateCustomName()
|
||||
}
|
||||
|
||||
fun File.toInputFile(): InputFile = MultipartFile(
|
||||
StorageFile(this)
|
||||
)
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue
Block a user