Angular rxjs连续的http调用集

时间:2017-11-07 11:50:51

标签: angular typescript rxjs observable

我正在使用concatMap编写一个函数来顺序进行HTTP调用,例如:添加一个人,使用返回的一些信息添加联系人然后添加一些帐户。

该函数接收一个列表(在我的情况下是投资组合),对于每个投资组合,我需要创建对addAccount和addInvestorAccount的调用。

例如,两个投资组合需要对这些端点进行两组调用(addInvestorAccount取决于addAccount的结果)

我不确定如何创建这些动态HTTP调用集并将它们附加到当前流。我看过forkJoin,但这似乎是针对独立的并行调用。

希望这有一定意义,这里有一些演示代码:

AddPerson(person: Person, portfolios: Portfolio[]) {
const addPerson = new AddPersonRequest();
// additional request props
return this.hostSvc.put(addPersonRequest)
    .concatMap((response: Person) => {
        const emailContactRequest = new CreateContactRequest();
        emailContactRequest.PersonId = response.Id;
        // additional request props
        return this.hostSvc.put(emailContactRequest)
    })
    .concatMap((response: AccountName) => {
        const addAccountRequest = new AddAccountRequest();
        addAccountRequest.Data = new Account();
        addAccountRequest.Data.AccountId = response.Id 
        addAccountRequest.Data.PortfolioId =  portfolios[0].Id
        // additional request props

        return this.hostSvc.put(addAccountRequest);
    })
    .concatMap((response: Account) => {

        const addInvestorAccountRequest = new AddInvestorAccountRequest();
        addInvestorAccountRequest.Data = new InvestorAccount();
        addInvestorAccountRequest.Data.AccountType = response.AccountType
        // additional request props
        return this.hostSvc.put(addInvestorAccountRequest);
    }, (error) => console.log(error))
}

如上所示,我正在使用Portfolio [0],显然我需要迭代addAccountRequest和addInvestorAccountRequest的投资组合

谢谢!

1 个答案:

答案 0 :(得分:2)

我认为你使用forkJoin走在正确的轨道上。你可以forkJoin(并行)调用串行。我会创建一个链接你的2个相关调用的单独方法。

makePortfolio(accountName: AccountName, portfolio: Portfolio){
    const addAccountRequest = new AddAccountRequest();
    addAccountRequest.Data = new Account();
    addAccountRequest.Data.AccountId = accountName.Id 
    addAccountRequest.Data.PortfolioId =  portfolio.Id;

    return this.hostSvc.put(addAccountRequest)
        .concatMap((response: Account) => {
            const addInvestorAccountRequest = new AddInvestorAccountRequest();
            addInvestorAccountRequest.Data = new InvestorAccount();
            addInvestorAccountRequest.Data.AccountType = response.AccountType
            // additional request props
            return this.hostSvc.put(addInvestorAccountRequest);
        });
}

这将用于制作x个链式投资组合请求。现在,在您的原始方法中,创建所需的所有投资组合数组

AddPerson(person: Person, portfolios: Portfolio[]) {
    const addPerson = new AddPersonRequest();
    // additional request props
    return this.hostSvc.put(addPersonRequest)
        .concatMap((response: Person) => {
            const emailContactRequest = new CreateContactRequest();
            emailContactRequest.PersonId = response.Id;
            // additional request props
            return this.hostSvc.put(emailContactRequest)
        })
        .concatMap((response: AccountName) => {
            // build out all portfolio requests
            let portfolioObservables = portfolios.map((portfolio: Portfolio) => {
                return this.makePortfolio(response, portfolio);
            }

            // make portfolio requests in parallel
            return Observable.forkJoin(...portfolioObservables);
        });
}

所以实际发生的是addPersonRequests - > emailContactRequest - > in parallel (addAccountRequest -> addInvestorAccount Request)

显然有很多要求。如果可能,我建议您更新HTTP请求,以便立即接受多个投资组合/账户。但如果这不是一个选项,这将是一种完成链式序列的方法。