1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-22 08:13:47 +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,7 +75,8 @@ class KtorRequestsExecutor(
}
override suspend fun <T : Any> execute(request: Request<T>): T {
return safely(
return runCatching {
safely(
{ e ->
pipelineStepsHolder.onRequestException(request, e) ?.let { return@safely it }
@ -90,6 +91,7 @@ class KtorRequestsExecutor(
} else {
e
}
}
) {
pipelineStepsHolder.onBeforeSearchCallFactory(request, callsFactories)
@ -116,6 +118,9 @@ class KtorRequestsExecutor(
} ?: pipelineStepsHolder.onRequestResultAbsent(request, callsFactories) ?: error("Can't execute request: $request")
}
}
}.let {
pipelineStepsHolder.onRequestReturnResult(it, request, callsFactories)
}
}
override fun close() {

View File

@ -60,4 +60,14 @@ interface KtorPipelineStepsHolder {
request: Request<T>,
callsFactories: List<KtorCallFactory>
): 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()
}