1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-25 19:48:43 +00:00

add KtorPipelineStepsHolder#onRequestReturnResult

This commit is contained in:
InsanusMokrassar 2022-04-11 14:41:02 +06:00
parent 863c872f35
commit 81ad55b19f
2 changed files with 54 additions and 39 deletions

View File

@ -75,46 +75,51 @@ class KtorRequestsExecutor(
} }
override suspend fun <T : Any> execute(request: Request<T>): T { override suspend fun <T : Any> execute(request: Request<T>): T {
return safely( return runCatching {
{ e -> safely(
pipelineStepsHolder.onRequestException(request, e) ?.let { return@safely it } { e ->
pipelineStepsHolder.onRequestException(request, e) ?.let { return@safely it }
throw if (e is ClientRequestException) { throw if (e is ClientRequestException) {
val content = e.response.readText() val content = e.response.readText()
val responseObject = jsonFormatter.decodeFromString(Response.serializer(), content) val responseObject = jsonFormatter.decodeFromString(Response.serializer(), content)
newRequestException( newRequestException(
responseObject, responseObject,
content, content,
"Can't get result object from $content" "Can't get result object from $content"
) )
} else { } else {
e e
}
}
) {
pipelineStepsHolder.onBeforeSearchCallFactory(request, callsFactories)
requestsLimiter.limit {
var result: T? = null
lateinit var factoryHandledRequest: KtorCallFactory
for (potentialFactory in callsFactories) {
pipelineStepsHolder.onBeforeCallFactoryMakeCall(request, potentialFactory)
result = potentialFactory.makeCall(
client,
telegramAPIUrlsKeeper,
request,
jsonFormatter
)
result = pipelineStepsHolder.onAfterCallFactoryMakeCall(result, request, potentialFactory)
if (result != null) {
factoryHandledRequest = potentialFactory
break
} }
}
result ?.let { }
pipelineStepsHolder.onRequestResultPresented(it, request, factoryHandledRequest, callsFactories) ) {
} ?: pipelineStepsHolder.onRequestResultAbsent(request, callsFactories) ?: error("Can't execute request: $request") pipelineStepsHolder.onBeforeSearchCallFactory(request, callsFactories)
requestsLimiter.limit {
var result: T? = null
lateinit var factoryHandledRequest: KtorCallFactory
for (potentialFactory in callsFactories) {
pipelineStepsHolder.onBeforeCallFactoryMakeCall(request, potentialFactory)
result = potentialFactory.makeCall(
client,
telegramAPIUrlsKeeper,
request,
jsonFormatter
)
result = pipelineStepsHolder.onAfterCallFactoryMakeCall(result, request, potentialFactory)
if (result != null) {
factoryHandledRequest = potentialFactory
break
}
}
result ?.let {
pipelineStepsHolder.onRequestResultPresented(it, request, factoryHandledRequest, callsFactories)
} ?: pipelineStepsHolder.onRequestResultAbsent(request, callsFactories) ?: error("Can't execute request: $request")
}
} }
}.let {
pipelineStepsHolder.onRequestReturnResult(it, request, callsFactories)
} }
} }

View File

@ -34,7 +34,7 @@ interface KtorPipelineStepsHolder {
* Will always be called after [potentialFactory] has tried to make [request] and got some [result]. If returns * Will always be called after [potentialFactory] has tried to make [request] and got some [result]. If returns
* value - that value will be returned from [dev.inmo.tgbotapi.bot.RequestsExecutor.execute] instead * value - that value will be returned from [dev.inmo.tgbotapi.bot.RequestsExecutor.execute] instead
*/ */
suspend fun <T : Any> onAfterCallFactoryMakeCall( suspend fun <T: Any> onAfterCallFactoryMakeCall(
result: T?, result: T?,
request: Request<T>, request: Request<T>,
potentialFactory: KtorCallFactory potentialFactory: KtorCallFactory
@ -45,7 +45,7 @@ interface KtorPipelineStepsHolder {
* handled [request] and returned [result]. If returns value - that value will be returned from * handled [request] and returned [result]. If returns value - that value will be returned from
* [dev.inmo.tgbotapi.bot.RequestsExecutor.execute] instead * [dev.inmo.tgbotapi.bot.RequestsExecutor.execute] instead
*/ */
suspend fun <T : Any> onRequestResultPresented( suspend fun <T: Any> onRequestResultPresented(
result: T, result: T,
request: Request<T>, request: Request<T>,
resultCallFactory: KtorCallFactory, resultCallFactory: KtorCallFactory,
@ -56,8 +56,18 @@ interface KtorPipelineStepsHolder {
* Will be called when there is no [KtorCallFactory] from [callsFactories] which may handle [request]. If returns * Will be called when there is no [KtorCallFactory] from [callsFactories] which may handle [request]. If returns
* value - that value will be returned from [dev.inmo.tgbotapi.bot.RequestsExecutor.execute] instead * value - that value will be returned from [dev.inmo.tgbotapi.bot.RequestsExecutor.execute] instead
*/ */
suspend fun <T : Any> onRequestResultAbsent( suspend fun <T: Any> onRequestResultAbsent(
request: Request<T>, request: Request<T>,
callsFactories: List<KtorCallFactory> callsFactories: List<KtorCallFactory>
): T? = null ): T? = null
/**
* This step will be called when the [result] has been retrieved (or exception has happened). If returns value -
* that value will be returned from [dev.inmo.tgbotapi.bot.RequestsExecutor.execute] instead
*/
suspend fun <T: Any> onRequestReturnResult(
result: Result<T>,
request: Request<T>,
callsFactories: List<KtorCallFactory>
): T = result.getOrThrow()
} }