private async Task <IEnumerable <RawQueueMessage> > RetrieveMessages( int timeout, int maxCount, string queueName, bool longPoll = false) { if (timeout < 0) { throw new ArgumentOutOfRangeException(nameof(timeout), "A negative timeout is not supported."); } if (timeout > 86400) { throw new ArgumentOutOfRangeException(nameof(timeout), "Timeout can at max be 24 hours (86400 seconds)."); } if (queueName == null) { throw new ArgumentNullException(nameof(queueName)); } if (maxCount > 100) { throw new ArgumentOutOfRangeException( nameof(maxCount), "Maximum number of 100 messages can be retrieved"); } var longPollWait = longPoll ? LongPollWaitSecAsInt : null; Response4 collection = null; await SemaphoreSlim.WaitAsync(); try { collection = await _client.ReservationsAsync( queueName, _token, new ReservationRequest() { N = maxCount, Timeout = timeout, Wait = longPollWait }); } catch (AggregateException e) { if (e.InnerException is ApiException exception && exception.StatusCode == 404) { //No queue is an empty queue collection = new Response4 { Messages = new System.Collections.ObjectModel.ObservableCollection <Message>() }; } } finally { SemaphoreSlim.Release(); } if (collection == null) { throw new InvalidOperationException($"Call to {_queueName}.Reserve(timeout: {timeout}) returned null"); } return(collection.Messages.Select(MapToRawQueueMessage)); }
public async Task MyMapper_Async_Test() { var dob = DateTime.Now; IResponseMapper mapper = new ResponseMapper(); Response1 source = new Response1 { ConsumerID = 123, Name = "XYZ", AvgNoOfPurchasesPerMonth = 10, PeriodInMonths = 12, Details = new Details1() { DOB = dob, IsDisabled = true }, MutualFund = new Fund1 { BankIdNo = 10, Name = "XYZ", FundId = 1, Address = new Address1 { StreetNo = "123N", State = new State1 { Name = "Victoria", Abbr = "VIC" }, Country = new Country1() { Name = "Australia", Abbr = "AU" } }, FundKeys = new Dictionary <string, string>() { { "ABN", "123456" }, { "TFN", "9876543" } } }, InsuranceMutualFund = new InsuranceMutualFund { MutualFundNumber = "123", TaxNo = "456" }, InsuranceSuperannuation = new InsuranceSuperannuation { SuperannuationNumber = "789", TaxFileNumber = "456" }, InsuranceEmployment = new InsuranceEmployment { EmploymentNumber = "678", TaxNumber = "456" }, InsuranceType = Entities.InsuranceType.Employment, BankingInfos = new List <BankingInfo1>() { new BankingInfo1 { AccountName = "ABC", AccountNo = "1" }, new BankingInfo1 { AccountName = "XYZ", AccountNo = "2" } }, EmploymentCodes = new Dictionary <EmploymentCode, string>() { { EmploymentCode.E, "Engineer" } } }; //Mapping source to new destination var destination = await mapper.MapAsync(source); Assert.IsTrue(destination.IDNumber == source.ConsumerID); Assert.IsTrue(destination.Name == source.Name); Assert.IsTrue(destination.TotalPurchases == source.AvgNoOfPurchasesPerMonth * source.PeriodInMonths); Assert.IsTrue(destination.Details.DateOfBirth == source.Details.DOB); Assert.IsTrue(destination.Details.IsHandicapped == source.Details.IsDisabled); Assert.IsTrue(source.PeriodInMonths == 12); Assert.IsTrue(destination.Period == "Year"); Assert.IsTrue(source.InsuranceType == InsuranceType.Employment); Assert.IsTrue(destination.InsuranceInfo.MembershipNo == source.InsuranceEmployment.EmploymentNumber); Assert.IsTrue(destination.InsuranceInfo.TaxNumber == source.InsuranceEmployment.TaxNumber); Assert.IsTrue(destination.Fund.BankIdNo == source.MutualFund.BankIdNo); Assert.IsTrue(destination.Fund.FundId == source.MutualFund.FundId); Assert.IsTrue(destination.Fund.Name == source.MutualFund.Name); Assert.IsTrue(destination.Fund.Address.StreetNo == source.MutualFund.Address.StreetNo); Assert.IsTrue(destination.Fund.Address.State.Name == source.MutualFund.Address.State.Name); Assert.IsTrue(destination.Fund.Address.State.Abbr == source.MutualFund.Address.State.Abbr); Assert.IsTrue(destination.Fund.Address.Country.Name == source.MutualFund.Address.Country.Name); Assert.IsTrue(destination.Fund.Address.Country.Abbr == source.MutualFund.Address.Country.Abbr); Assert.IsTrue(destination.Fund.FundKeys["ABN"] == source.MutualFund.FundKeys["ABN"]); Assert.IsTrue(destination.Fund.FundKeys["TFN"] == source.MutualFund.FundKeys["TFN"]); Assert.IsTrue(destination.BankingInformation.Count == source.BankingInfos.Count); Assert.IsTrue(destination.BankingInformation[0].AccountName == source.BankingInfos[0].AccountName); Assert.IsTrue(destination.BankingInformation[0].AccountNumber == source.BankingInfos[0].AccountNo); Assert.IsTrue(destination.BankingInformation[1].AccountName == source.BankingInfos[1].AccountName); Assert.IsTrue(destination.BankingInformation[1].AccountNumber == source.BankingInfos[1].AccountNo); Assert.IsTrue(destination.LabourCodes.Count == source.EmploymentCodes.Count); Assert.IsTrue(destination.LabourCodes[LabourCode.ENG] == source.EmploymentCodes[EmploymentCode.E]); //Mapping source to existing destination Response3 destination_1 = new Response3() { Existing = "Mapping to existing destination object" }; await mapper.MapAsync(source, destination_1); Assert.IsTrue(destination.IDNumber == source.ConsumerID); Assert.IsTrue(destination.Name == source.Name); Assert.IsTrue(destination_1.Existing == "Mapping to existing destination object"); Assert.IsTrue(destination.TotalPurchases == source.AvgNoOfPurchasesPerMonth * source.PeriodInMonths); Assert.IsTrue(destination.Details.DateOfBirth == source.Details.DOB); Assert.IsTrue(destination.Details.IsHandicapped == source.Details.IsDisabled); Assert.IsTrue(source.PeriodInMonths == 12); Assert.IsTrue(destination.Period == "Year"); Assert.IsTrue(source.InsuranceType == InsuranceType.Employment); Assert.IsTrue(destination.InsuranceInfo.MembershipNo == source.InsuranceEmployment.EmploymentNumber); Assert.IsTrue(destination.InsuranceInfo.TaxNumber == source.InsuranceEmployment.TaxNumber); Assert.IsTrue(destination.Fund.BankIdNo == source.MutualFund.BankIdNo); Assert.IsTrue(destination.Fund.FundId == source.MutualFund.FundId); Assert.IsTrue(destination.Fund.Name == source.MutualFund.Name); Assert.IsTrue(destination.Fund.Address.StreetNo == source.MutualFund.Address.StreetNo); Assert.IsTrue(destination.Fund.Address.State.Name == source.MutualFund.Address.State.Name); Assert.IsTrue(destination.Fund.Address.State.Abbr == source.MutualFund.Address.State.Abbr); Assert.IsTrue(destination.Fund.Address.Country.Name == source.MutualFund.Address.Country.Name); Assert.IsTrue(destination.Fund.Address.Country.Abbr == source.MutualFund.Address.Country.Abbr); Assert.IsTrue(destination.Fund.FundKeys["ABN"] == source.MutualFund.FundKeys["ABN"]); Assert.IsTrue(destination.Fund.FundKeys["TFN"] == source.MutualFund.FundKeys["TFN"]); Assert.IsTrue(destination.BankingInformation.Count == source.BankingInfos.Count); Assert.IsTrue(destination.BankingInformation[0].AccountName == source.BankingInfos[0].AccountName); Assert.IsTrue(destination.BankingInformation[0].AccountNumber == source.BankingInfos[0].AccountNo); Assert.IsTrue(destination.BankingInformation[1].AccountName == source.BankingInfos[1].AccountName); Assert.IsTrue(destination.BankingInformation[1].AccountNumber == source.BankingInfos[1].AccountNo); Assert.IsTrue(destination.LabourCodes.Count == source.EmploymentCodes.Count); Assert.IsTrue(destination.LabourCodes[LabourCode.ENG] == source.EmploymentCodes[EmploymentCode.E]); //Mapping source to existing destination (this) Response4 source_2 = new Response4() { IDNumber = "XYZ", AccountNumber = "123" }; Response5 destination_2 = new Response5(); await destination_2.MapAsync(source_2); Assert.IsTrue(destination_2.IDNumber == source_2.IDNumber); Assert.IsTrue(destination_2.AccNo == source_2.AccountNumber); }
public ActionResult Shuffle() { string DeckIdCookie = Request.Cookies["DeckIdCookie"].Value; //use an if statement to discard of web request post using HttpWebRequest WR4 = WebRequest.CreateHttp($"https://deckofcardsapi.com/api/deck/{DeckIdCookie}/shuffle/"); WR4.UserAgent = ".NET Framework Test Client"; HttpWebResponse Response4; try { Response4 = (HttpWebResponse)WR4.GetResponse(); } catch (WebException ex2) { ViewBag.Error = "Exception stage 1"; ViewBag.ErrorDescription = ex2.Message + $"<br />https://deckofcardsapi.com/api/deck/{DeckIdCookie}/shuffle/"; return(View("Index")); } if (Response4.StatusCode != HttpStatusCode.OK) { ViewBag.Error = Response4.StatusCode; ViewBag.ErrorDescription = Response4.StatusDescription; return(View("Index")); } StreamReader reader4 = new StreamReader(Response4.GetResponseStream()); string Card4Data = reader4.ReadToEnd(); try { var input = Request.Cookies["Link"] != null ? Request.Cookies["Link"].Value : String.Empty; HttpWebRequest WR2 = WebRequest.CreateHttp($"{input}"); WR2.UserAgent = ".NET Framework Test Client"; HttpWebResponse Response2; try { Response2 = (HttpWebResponse)WR2.GetResponse(); } catch (WebException ex) { ViewBag.Error = "Exception stage 2"; ViewBag.ErrorDescription = ex.Message; return(View("Index")); } if (Response2.StatusCode != HttpStatusCode.OK) { ViewBag.Error = Response2.StatusCode; ViewBag.ErrorDescription = Response2.StatusDescription; return(View("Index")); } StreamReader reader2 = new StreamReader(Response2.GetResponseStream()); string Card2Data = reader2.ReadToEnd(); try { JObject Json2Data = JObject.Parse(Card2Data); ViewBag.Cards = /*(JObject)*/ Json2Data["cards"]; } catch (Exception ex) { ViewBag.Error = "JSON Issue"; ViewBag.ErrorDescription = ex.Message; return(View("Index")); } } catch (Exception ex2) { ViewBag.Error = "JSON Issue 2"; ViewBag.ErrorDescription = ex2.Message; return(View("Index")); } return(View("Index")); }