Kannan AU

Kannan AU

  • NA
  • 57
  • 23.2k

API Parallel call and how to make partial fail

Jun 8 2020 4:57 PM
Hi Team,
 
I have parallel call as showed below, at present if other API is failed for one AccountID, then complete call considering as fail. Could you please let me know how I can make the call better, Even if one AccountID is failed still I need to continue with final response for the success AccountID.
  1. public async Task<List<CreditCardSummary>> Process(Dictionary<stringstring> permanentIds)  
  2. {  
  3. List<Task> tasks = new List<Task>();  
  4. List<CreditCardSummary> creditCardSummaryServiceList = new List<CreditCardSummary>();  
  5. foreach (var permanentId in permanentIds)  
  6. {  
  7. CreditCardSummaryRequest request = new CreditCardSummaryRequest()  
  8. {  
  9. AccountID = permanentId.Key, // "00001528830094858440"  
  10. AccountToken = permanentId.Value //token  
  11. };  
  12. tasks.Add(ModelServiceProcessing(request).ContinueWith(  
  13. (TResult) => creditCardSummaryServiceList.Add(TResult.Result)));  
  14. }  
  15. await Task.WhenAll(tasks.ToArray());  
  16. return creditCardSummaryServiceList;  
  17. }  
  18. public async Task<CreditCardSummary> ModelServiceProcessing(CreditCardSummaryRequest request)  
  19. {  
  20. //deleted some code for constructing header  
  21. var restServiceResponse = await restClient.Post(ccStatusInfoUrl,request ,true, header); // this is the code to make a call to other API  
  22. var restServiceResponseInformation = await restServiceResponse.Content.ReadAsStringAsync();  
  23. if (!restServiceResponse.IsSuccessStatusCode)  
  24. {  
  25. throw new CallFailureException($"Call has been failed");  
  26. }  
  27. context.LogQueue.AddDebugMessage($"ICS CardSummary success for PId ending - {Common.GetLastFour(request.AccountID)}");  
  28. var response = Common.DeSerializeObject<CreditCardSummary>(restServiceResponseInformation, false);  
  29. return response;  
  30. }