public void TestOk() { var result = RestHttpActionResult <JsonModel> .Ok(_jsonModel); Assert.AreEqual(HttpStatusCode.OK, result.StatusCode); Assert.AreSame(_jsonModel, result.Content); }
public IHttpActionResult ProcessStripeEvent([FromBody] StripeEvent stripeEvent) { if (stripeEvent == null || !ModelState.IsValid) { if (_logger.IsDebugEnabled) { _logger.Debug("Received invalid Stripe event " + stripeEvent); } return(BadRequest(ModelState)); } _logger.Debug("Received Stripe Event " + stripeEvent.Type); if (_liveMode != stripeEvent.LiveMode) { _logger.Debug("Dropping Stripe Event " + stripeEvent.Type + " because LiveMode was " + stripeEvent.LiveMode); return(Ok()); } StripeEventResponseDTO response = null; try { if (_asynchronous) { _logger.Debug("Enqueueing Stripe event " + stripeEvent.Type + " because AsynchronousProcessingMode was true"); var message = _messageFactory.CreateMessage(stripeEvent); _eventQueue.Send(message, MessageQueueTransactionType.None); response = new StripeEventResponseDTO { Message = "Queued event for asynchronous processing" }; } else { _logger.Debug("Processing Stripe event " + stripeEvent.Type + " because AsynchronousProcessingMode was false"); response = _stripeEventService.ProcessStripeEvent(stripeEvent); } } catch (Exception e) { var msg = "Unexpected error processing Stripe Event " + stripeEvent.Type; _logger.Error(msg, e); var responseDto = new StripeEventResponseDTO() { Exception = new ApplicationException(msg, e), Message = msg }; return(RestHttpActionResult <StripeEventResponseDTO> .ServerError(responseDto)); } return(response == null ? Ok() : (IHttpActionResult)RestHttpActionResult <StripeEventResponseDTO> .Ok(response)); }
public IHttpActionResult Save([FromBody] TripApplicationDto dto) { if (!ModelState.IsValid) { var errors = ModelState.Values.SelectMany(val => val.Errors).Aggregate("", (current, err) => current + err.Exception.Message); var dataError = new ApiErrorDto("Save Trip Application Data Invalid", new InvalidOperationException("Invalid Save Data" + errors)); throw new HttpResponseException(dataError.HttpResponseMessage); } TripApplicationResponseDto response; try { var participantPledgeInfo = _tripService.CreateTripParticipant(dto.ContactId, dto.PledgeCampaignId); var message = _messageFactory.CreateMessage(dto); _eventQueue.Send(message, MessageQueueTransactionType.None); response = new TripApplicationResponseDto { Message = "Queued event for asynchronous processing", DepositAmount = participantPledgeInfo.Deposit, DonorId = participantPledgeInfo.DonorId, ProgramId = participantPledgeInfo.ProgramId, ProgramName = participantPledgeInfo.ProgramName }; new Task(() => { _tripService.SendTripIsFullMessage(dto.PledgeCampaignId); }).Start(); } catch (TripFullException e) { var json = JsonConvert.SerializeObject(e.Message, Formatting.None); var message = new HttpResponseMessage(HttpStatusCode.Conflict); message.Content = new StringContent(json); throw new HttpResponseException(message); } catch (Exception e) { const string msg = "Unexpected error processing Trip Application Save"; var responseDto = new TripApplicationResponseDto() { Exception = new ApplicationException(msg, e), Message = msg }; return(RestHttpActionResult <TripApplicationResponseDto> .ServerError(responseDto)); } return((IHttpActionResult)RestHttpActionResult <TripApplicationResponseDto> .Ok(response)); }
public void TestExecuteAsync() { var result = RestHttpActionResult <JsonModel> .Ok(_jsonModel); var task = result.ExecuteAsync(new CancellationToken()); task.Wait(); var response = task.Result; Assert.NotNull(response); Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); var content = response.Content; Assert.AreEqual(MediaTypeHeaderValue.Parse("application/json"), response.Content.Headers.ContentType); Assert.IsInstanceOf <StringContent>(content); var stringContentTask = content.ReadAsStringAsync(); stringContentTask.Wait(); Assert.AreEqual(JsonConvert.SerializeObject(_jsonModel), stringContentTask.Result); }
public IHttpActionResult ProcessStripeEvent([FromBody] StripeEvent stripeEvent) { if (stripeEvent == null || !ModelState.IsValid) { if (_logger.IsDebugEnabled) { _logger.Debug("Received invalid Stripe event " + stripeEvent); } return(BadRequest(ModelState)); } _logger.Debug("Received Stripe Event " + stripeEvent.Type); if (_liveMode != stripeEvent.LiveMode) { _logger.Debug("Dropping Stripe Event " + stripeEvent.Type + " because LiveMode was " + stripeEvent.LiveMode); return(Ok()); } StripeEventResponseDTO response; try { if (_asynchronous) { _logger.Debug("Enqueueing Stripe event " + stripeEvent.Type + " because AsynchronousProcessingMode was true"); var message = _messageFactory.CreateMessage(stripeEvent); _eventQueue.Send(message, MessageQueueTransactionType.None); response = new StripeEventResponseDTO { Message = "Queued event for asynchronous processing" }; } else { _logger.Debug("Processing Stripe event " + stripeEvent.Type + " because AsynchronousProcessingMode was false"); response = _stripeEventService.ProcessStripeEvent(stripeEvent); } } catch (Exception e) { var msg = "Unexpected error processing Stripe Event " + stripeEvent.Type; if (e is DonationNotFoundException) { // Sometimes we receive a webhook callback before the donation has been // added to the database. This is a known issue, so just do minimal // logging without a full stack trace. _logger.Error($"ProcessStripeEvent: Donation not found processing {stripeEvent.Type}: {e.Message}"); } else { _logger.Error(msg, e); } var responseDto = new StripeEventResponseDTO() { Exception = new ApplicationException(msg, e), Message = msg }; return(RestHttpActionResult <StripeEventResponseDTO> .ServerError(responseDto)); } return(response == null ? Ok() : (IHttpActionResult)RestHttpActionResult <StripeEventResponseDTO> .Ok(response)); }