从流进行异步HTTP调用 [英] Making asynchronous HTTP calls from flows
问题描述
在Corda中,如何从流中发出异步HTTP请求?
In Corda, how can I make an asynchronous HTTP request from within a flow? Is there an API to suspend a flow while awaiting the response to the HTTP call, or a way to provide a callback?
推荐答案
是否有API在等待对HTTP调用的响应时挂起流? Corda目前没有提供一种机制来发出异步HTTP请求,然后在等待响应时挂起该流,或者提供一种在收到响应时将被调用的回调。
Corda doesn't currently provide a mechanism for making an asynchronous HTTP request and then either suspending the flow while awaiting a response, or providing a callback that will be called when the response is received.
相反,建议您在启动流之前发出HTTP请求,然后在实例化流时将响应作为参数传递。
Instead, it is recommended that you make the HTTP request before initiating the flow, then pass in the response as an argument when instantiating the flow.
有时,这是不可能的。例如,HTTP请求可能是自动启动的响应流所必需的,或者可能取决于从交易对手接收到的消息的内容。
Sometimes, this isn't possible. For example, the HTTP request may be required by a response flow that is initiated automatically, or it may depend on the contents of a message received from a counterparty.
在这种情况下,您仍然可以按如下方式支持这种工作流程。假设您正在为贷款申请编写一个CorDapp,表示为 LoanApplicationState
s。没有HTTP调用,响应者将不知道是否接受贷款申请。
In this case, you can still support this kind of workflow as follows. Suppose you are writing a CorDapp for loan applications, which are represented as LoanApplicationState
s. Without an HTTP call, the responder doesn't know whether to accept the loan application or not.
而不是创建 LoanApplicationState $ c直接在$ c>中,该流将创建一个
UnacceptedLoanApplicationState
,响应者将其存储在其分类帐中。流结束后,响应者可以在流框架之外进行HTTP调用。根据HTTP调用的结果,响应者将启动批准流程,该流程将创建事务以将 UnacceptedLoanApplicationState
转换为 LoanApplicationState
,或启动拒绝流程以消耗 UnacceptedLoanApplicationState
状态,而不发出接受的 LoanApplicationState
。
Instead of creating a LoanApplicationState
directly, the flow would create an UnacceptedLoanApplicationState
, which the responder would store on their ledger. Once the flow ends, the responder can make an HTTP call outside of the flow framework. Based on the result of the HTTP call, the responder will either kick off an approve flow that creates a transaction to transform the UnacceptedLoanApplicationState
into an LoanApplicationState
, or kick off a reject flow to consume the UnacceptedLoanApplicationState
state without issuing an accepted LoanApplicationState
.
或者,您可以在 LoanApplicationState
中添加状态字段,指定是否批准贷款。最初,贷款状态会将字段设置为未批准。然后,基于HTTP请求的结果,响应者将启动两个流之一来更新 LoanApplicationState
,将其更新为批准状态或拒绝状态。
Alternatively, you could add a status field to the LoanApplicationState
, specifying whether the loan is approved or not. Initially, the loan state would have the field set to unapproved. Then, based on the result of the HTTP request, the responder would initiate one of two flows to update the LoanApplicationState
, updating it with either an approved or a rejected status.
这篇关于从流进行异步HTTP调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!