diff --git a/CHANGELOG.md b/CHANGELOG.md index 160aa1893d..8c76366160 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,9 @@ must be regular text * `RequestsExecutor#executeAsync(Request, CoroutineScope)` now will return `Deferred` for cases when you need result * `RequestsExecutor#executeUnsafe` will automatically retry request if it was unsuccessful and retries > 0 +* Add `RequestError` sealed class and described in documentation known errors +* Add `ResponseParametersRaw` which can create error based on input parameters +* Add `parameters` field in `Response` and remove useless fields from `Response` ### 0.9.3 diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/RequestError.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/RequestError.kt new file mode 100644 index 0000000000..1d0bbe31f2 --- /dev/null +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/RequestError.kt @@ -0,0 +1,18 @@ +package com.github.insanusmokrassar.TelegramBotAPI.types + +import java.util.concurrent.TimeUnit + +sealed class RequestError + +data class RetryAfterError( + val seconds: Long, + val startCountingMillis: Long +) : RequestError() { + val canContinue = TimeUnit.SECONDS.toMillis(seconds) + startCountingMillis +} + +data class MigrateChatId( + val newChatId: ChatId +) : RequestError() + + diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/Response.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/Response.kt index adea55b8b4..a0089723fe 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/Response.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/Response.kt @@ -15,22 +15,11 @@ data class Response( val ok: Boolean = false, @Optional val description: String? = null, - @SerialName("migrate_to_chat_id") - @Optional - val migrateToChatId: Identifier? = null, - @SerialName("retry_after") - @Optional - val retryAfter: Int? = null, @SerialName("error_code") @Optional val errorCode: Int? = null, @Optional - val result: T? = null -) { - @Transient - val waitUntil: DateTime? by lazy { - retryAfter ?.let { - DateTime.now().plus(TimeUnit.SECONDS.toMillis(it.toLong())) - } - } -} + val result: T? = null, + @Optional + val parameters: ResponseParametersRaw? = null +) diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ResponseParametersRaw.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ResponseParametersRaw.kt new file mode 100644 index 0000000000..dc9ca9d7bf --- /dev/null +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ResponseParametersRaw.kt @@ -0,0 +1,24 @@ +package com.github.insanusmokrassar.TelegramBotAPI.types + +import kotlinx.serialization.* + +@Serializable +data class ResponseParametersRaw( + @SerialName("migrate_to_chat_id") + @Optional + private val migrateToChatId: ChatId? = null, + @SerialName("retry_after") + @Optional + private val retryAfter: Long? = null +) { + @Transient + private val createTime: Long = System.currentTimeMillis() + + val error: RequestError? by lazy { + when { + migrateToChatId != null -> MigrateChatId(migrateToChatId); + retryAfter != null -> RetryAfterError(retryAfter, createTime); + else -> null + } + } +}