/// <summary> /// Fetch rates for a List of Package /// Upon USPS API communication issues, this request will continue to retry. You will need to set a timeout handler or cancel the request from the calling app if this is an issue. /// </summary> /// <param name="pkgs"></param> /// <returns></returns> public static async Task <List <UspsApi.Models.RateAPI.Response.Package> > GetRatesAsync(List <UspsApi.Models.RateAPI.Request.Package> pkgs) { List <UspsApi.Models.RateAPI.Response.Package> result = await FetchRatesAsync(pkgs); foreach (var pkg in result) { if (pkg.Error != null) { continue; } pkg.Postage.First().TotalPostage = Convert.ToDecimal(pkg.Postage.First().Rate); UspsApi.Models.RateAPI.Request.Package inputPkg = pkgs.First(o => o.ID == pkg.ID); if (inputPkg.SpecialServices.SpecialService != null && inputPkg.SpecialServices.SpecialService.Count > 0) { foreach (var service in inputPkg.SpecialServices.SpecialService) { if (pkg.Postage.First().SpecialServices.SpecialService.Any(o => o.ServiceID == service.ToString())) { pkg.Postage.First().TotalPostage += Convert.ToDecimal(pkg.Postage.First().SpecialServices.SpecialService.First(o => o.ServiceID == service.ToString()).Price); } } } } return(result); }
/// <summary> /// Fetch rates for a single Package. /// Upon USPS API communication issues, this request will continue to retry. You will need to set a timeout handler or cancel the request from the calling app if this is an issue. /// </summary> /// <param name="pkg"></param> /// <returns></returns> public static UspsApi.Models.RateAPI.Response.Package GetRates(UspsApi.Models.RateAPI.Request.Package pkg) { List <UspsApi.Models.RateAPI.Request.Package> list = new() { pkg }; List <Models.RateAPI.Response.Package> resp = FetchRatesAsync(list).Result; Package result = resp.First(); if (result.Error != null) { return(result); } result.Postage.First().TotalPostage = Convert.ToDecimal(result.Postage.First().Rate); if (pkg.SpecialServices.SpecialService != null && pkg.SpecialServices.SpecialService.Count > 0) { foreach (var service in pkg.SpecialServices.SpecialService) { if (result.Postage.First().SpecialServices.SpecialService.Any(o => o.ServiceID == service.ToString())) { result.Postage.First().TotalPostage += Convert.ToDecimal(result.Postage.First().SpecialServices.SpecialService.First(o => o.ServiceID == service.ToString()).Price); } } } return(result); }