public async Task<float> Balance(string accountId)
 {
     var request = new GetBalancesRequest { Ids = { accountId } };
     var repsonse = await accountsReadClient.GetBalancesAsync(request, HttpContext.CreateHeadersWithFlowId());
     var balance = repsonse.Balances.Single();
     return balance.Balance;
 }
        public async Task <BatchData> Get([FromQuery(Name = "part")] int part, [FromQuery(Name = "total")] int total, [FromQuery(Name = "timestamp")] DateTime timestamp)
        {
            var flowId = HttpContext.Items["flowId"].ToString();

            RepeatedField <Loan>           loans    = new RepeatedField <Loan>();
            RepeatedField <Payment>        payments = new RepeatedField <Payment>();
            RepeatedField <AccountBalance> balances = new RepeatedField <AccountBalance>();

            var paymentsResponse = await paymentsReadClient.GetPartAsync(new GetPartRequest
            {
                Part       = part,
                TotalParts = total,
                Timestamp  = timestamp.ToNullableTimestamp()
            }, HttpContext.CreateHeadersWithFlowId());

            payments = paymentsResponse.Payments;

            var parallelTasks = new List <Task>();

            parallelTasks.Add(Task.Run(async() =>
            {
                var paymentsIds   = payments.Select(p => p.Id);
                var loansResponse = await loansReadClient.GetByPaymentsAsync(new GetLoansRequest {
                    Ids = { paymentsIds }
                }, HttpContext.CreateHeadersWithFlowId());
                loans = loansResponse.Loans;
            }));
            parallelTasks.Add(Task.Run(async() =>
            {
                var accountsIds      = payments.Select(p => p.AccountId).Distinct();
                var balancesResponse = await accountsReadClient.GetBalancesAsync(new GetBalancesRequest {
                    Ids = { accountsIds }
                }, HttpContext.CreateHeadersWithFlowId());
                balances = balancesResponse.Balances;
            }));

            await Task.WhenAll(parallelTasks);

            return(new BatchData
            {
                Balances = balances.Select(b => mapper.Map <BalanceDTO>(b)).ToArray(),
                Loans = loans.Select(l => mapper.Map <LoanDTO>(l)).ToArray(),
                Payments = payments.Select(p => mapper.Map <PaymentDTO>(p)).ToArray()
            });
        }