fix ending of host url in TelegramAPIUrlsKeeper

This commit is contained in:
InsanusMokrassar 2020-11-17 13:18:11 +06:00
parent ec74111a9d
commit bc1b7c3f25
2 changed files with 22 additions and 2 deletions

View File

@ -5,6 +5,8 @@
* `Common`:
* `Version`:
* `MicroUtils`: `0.4.0` -> `0.4.1`
* `Core`:
* `TelegramAPIUrlsKeeper` will fix ending of host url since this version
## 0.30.6

View File

@ -2,10 +2,28 @@ package dev.inmo.tgbotapi.utils
const val telegramBotAPIDefaultUrl = "https://api.telegram.org"
private inline val String.withoutLastSlash: String
get() {
var correctedUrl = this
while (true) {
val withoutSuffix = correctedUrl.removeSuffix("/")
if (withoutSuffix == correctedUrl) {
return correctedUrl
}
correctedUrl = withoutSuffix
}
}
class TelegramAPIUrlsKeeper(
token: String,
hostUrl: String = telegramBotAPIDefaultUrl
) {
val commonAPIUrl = "$hostUrl/bot$token"
val fileBaseUrl = "$hostUrl/file/bot$token"
val commonAPIUrl: String
val fileBaseUrl: String
init {
val correctedHost = hostUrl.withoutLastSlash
commonAPIUrl = "$correctedHost/bot$token"
fileBaseUrl = "$correctedHost/file/bot$token"
}
}