2019-04-26 03:12:01 +00:00
|
|
|
package com.github.insanusmokrassar.TelegramBotAPI.bot.Ktor.base
|
|
|
|
|
|
|
|
import com.github.insanusmokrassar.TelegramBotAPI.bot.Ktor.KtorCallFactory
|
|
|
|
import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.Request
|
|
|
|
import io.ktor.client.HttpClient
|
2020-01-15 06:28:06 +00:00
|
|
|
import io.ktor.client.request.*
|
|
|
|
import io.ktor.client.statement.HttpStatement
|
2019-12-06 10:46:38 +00:00
|
|
|
import io.ktor.http.ContentType
|
|
|
|
import io.ktor.http.HttpMethod
|
|
|
|
import kotlin.collections.set
|
2019-04-26 03:12:01 +00:00
|
|
|
|
|
|
|
abstract class AbstractRequestCallFactory : KtorCallFactory {
|
|
|
|
private val methodsCache: MutableMap<String, String> = mutableMapOf()
|
|
|
|
override suspend fun <T : Any> prepareCall(
|
|
|
|
client: HttpClient,
|
|
|
|
baseUrl: String,
|
|
|
|
request: Request<T>
|
2020-01-15 06:28:06 +00:00
|
|
|
): HttpStatement? {
|
2019-04-26 03:12:01 +00:00
|
|
|
val preparedBody = prepareCallBody(client, baseUrl, request) ?: return null
|
|
|
|
|
2020-01-15 06:28:06 +00:00
|
|
|
return HttpStatement(
|
|
|
|
HttpRequestBuilder().apply {
|
|
|
|
url(
|
|
|
|
methodsCache[request.method()] ?: "$baseUrl/${request.method()}".also {
|
|
|
|
methodsCache[request.method()] = it
|
|
|
|
}
|
|
|
|
)
|
|
|
|
method = HttpMethod.Post
|
|
|
|
accept(ContentType.Application.Json)
|
2019-04-26 03:12:01 +00:00
|
|
|
|
2020-01-15 06:28:06 +00:00
|
|
|
body = preparedBody
|
|
|
|
},
|
|
|
|
client
|
|
|
|
)
|
2019-04-26 03:12:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected abstract fun <T : Any> prepareCallBody(
|
|
|
|
client: HttpClient,
|
|
|
|
baseUrl: String,
|
|
|
|
request: Request<T>
|
|
|
|
): Any?
|
|
|
|
}
|