mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2024-12-23 00:57:13 +00:00
add KtorPipelineStepsHolder#onRequestReturnResult
This commit is contained in:
parent
863c872f35
commit
81ad55b19f
@ -75,46 +75,51 @@ class KtorRequestsExecutor(
|
||||
}
|
||||
|
||||
override suspend fun <T : Any> execute(request: Request<T>): T {
|
||||
return safely(
|
||||
{ e ->
|
||||
pipelineStepsHolder.onRequestException(request, e) ?.let { return@safely it }
|
||||
return runCatching {
|
||||
safely(
|
||||
{ e ->
|
||||
pipelineStepsHolder.onRequestException(request, e) ?.let { return@safely it }
|
||||
|
||||
throw if (e is ClientRequestException) {
|
||||
val content = e.response.readText()
|
||||
val responseObject = jsonFormatter.decodeFromString(Response.serializer(), content)
|
||||
newRequestException(
|
||||
responseObject,
|
||||
content,
|
||||
"Can't get result object from $content"
|
||||
)
|
||||
} else {
|
||||
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
|
||||
throw if (e is ClientRequestException) {
|
||||
val content = e.response.readText()
|
||||
val responseObject = jsonFormatter.decodeFromString(Response.serializer(), content)
|
||||
newRequestException(
|
||||
responseObject,
|
||||
content,
|
||||
"Can't get result object from $content"
|
||||
)
|
||||
} else {
|
||||
e
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -34,7 +34,7 @@ interface KtorPipelineStepsHolder {
|
||||
* 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
|
||||
*/
|
||||
suspend fun <T : Any> onAfterCallFactoryMakeCall(
|
||||
suspend fun <T: Any> onAfterCallFactoryMakeCall(
|
||||
result: T?,
|
||||
request: Request<T>,
|
||||
potentialFactory: KtorCallFactory
|
||||
@ -45,7 +45,7 @@ interface KtorPipelineStepsHolder {
|
||||
* handled [request] and returned [result]. If returns value - that value will be returned from
|
||||
* [dev.inmo.tgbotapi.bot.RequestsExecutor.execute] instead
|
||||
*/
|
||||
suspend fun <T : Any> onRequestResultPresented(
|
||||
suspend fun <T: Any> onRequestResultPresented(
|
||||
result: T,
|
||||
request: Request<T>,
|
||||
resultCallFactory: KtorCallFactory,
|
||||
@ -56,8 +56,18 @@ interface KtorPipelineStepsHolder {
|
||||
* 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
|
||||
*/
|
||||
suspend fun <T : Any> onRequestResultAbsent(
|
||||
suspend fun <T: Any> onRequestResultAbsent(
|
||||
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()
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user