public static DeliveryStatus DeliveryStatusAsEnum(this NotificationCallbackRequest request)
 {
     return(request.Status switch
     {
         "delivered" => DeliveryStatus.Delivered,
         "permanent-failure" => DeliveryStatus.Failed,
         "temporary-failure" => DeliveryStatus.Failed,
         "technical-failure" => DeliveryStatus.Failed,
         _ => throw new ArgumentOutOfRangeException($"{request.Status} is not a recognised status")
     });
示例#2
0
        public void GivenIHaveADeliveryReceipt()
        {
            var notification = _context.RecentNotification;

            CallbackRequest = new NotificationCallbackRequest
            {
                Id        = notification.id,
                Reference = notification.reference,
                Status    = "delivered"
            };
        }
示例#3
0
        public async Task <IActionResult> HandleCallbackAsync(NotificationCallbackRequest notificationCallbackRequest)
        {
            var notificationId = notificationCallbackRequest.ReferenceAsGuid();
            var deliveryStatus = notificationCallbackRequest.DeliveryStatusAsEnum();
            var externalId     = notificationCallbackRequest.Id;
            var command        = new UpdateNotificationDeliveryStatusCommand(notificationId, externalId, deliveryStatus);

            await _commandHandler.Handle(command);

            return(Ok());
        }
        public void GivenIHaveUpdateRequestWithMismatchedIds()
        {
            var notification = _context.TestRun.NotificationsCreated.Last();
            var request      = new NotificationCallbackRequest
            {
                Id        = Guid.NewGuid().ToString(),
                Reference = notification.Id.ToString(),
                Status    = "permanent-failure"
            };

            _context.Uri        = NotificationEndpoints.UpdateNotification;
            _context.HttpMethod = HttpMethod.Post;
            var jsonBody = RequestHelper.Serialise(request);

            _context.HttpContent = new StringContent(jsonBody, Encoding.UTF8, "application/json");
        }
 public static Guid ReferenceAsGuid(this NotificationCallbackRequest request)
 {
     return(Guid.Parse(request.Reference));
 }
示例#6
0
        /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
        /// <summary>Process callbacks from Gov Notify API</summary>
        /// <exception cref="NotificationApiException">A server side error occurred.</exception>
        public async System.Threading.Tasks.Task HandleCallbackAsync(NotificationCallbackRequest notificationCallbackRequest, System.Threading.CancellationToken cancellationToken)
        {
            if (notificationCallbackRequest == null)
            {
                throw new System.ArgumentNullException("notificationCallbackRequest");
            }

            var urlBuilder_ = new System.Text.StringBuilder();

            urlBuilder_.Append(BaseUrl != null ? BaseUrl.TrimEnd('/') : "").Append("/notification/callback");

            var client_        = _httpClient;
            var disposeClient_ = false;

            try
            {
                using (var request_ = new System.Net.Http.HttpRequestMessage())
                {
                    var content_ = new System.Net.Http.StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(notificationCallbackRequest, _settings.Value));
                    content_.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse("application/json");
                    request_.Content             = content_;
                    request_.Method = new System.Net.Http.HttpMethod("POST");

                    PrepareRequest(client_, request_, urlBuilder_);
                    var url_ = urlBuilder_.ToString();
                    request_.RequestUri = new System.Uri(url_, System.UriKind.RelativeOrAbsolute);
                    PrepareRequest(client_, request_, url_);

                    var response_ = await client_.SendAsync(request_, System.Net.Http.HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false);

                    var disposeResponse_ = true;
                    try
                    {
                        var headers_ = System.Linq.Enumerable.ToDictionary(response_.Headers, h_ => h_.Key, h_ => h_.Value);
                        if (response_.Content != null && response_.Content.Headers != null)
                        {
                            foreach (var item_ in response_.Content.Headers)
                            {
                                headers_[item_.Key] = item_.Value;
                            }
                        }

                        ProcessResponse(client_, response_);

                        var status_ = (int)response_.StatusCode;
                        if (status_ == 200)
                        {
                            return;
                        }
                        else
                        if (status_ == 500)
                        {
                            string responseText_ = (response_.Content == null) ? string.Empty : await response_.Content.ReadAsStringAsync().ConfigureAwait(false);

                            throw new NotificationApiException("A server side error occurred.", status_, responseText_, headers_, null);
                        }
                        else
                        if (status_ == 400)
                        {
                            var objectResponse_ = await ReadObjectResponseAsync <ProblemDetails>(response_, headers_).ConfigureAwait(false);

                            if (objectResponse_.Object == null)
                            {
                                throw new NotificationApiException("Response was null which was not expected.", status_, objectResponse_.Text, headers_, null);
                            }
                            throw new NotificationApiException <ProblemDetails>("A server side error occurred.", status_, objectResponse_.Text, headers_, objectResponse_.Object, null);
                        }
                        else
                        if (status_ == 401)
                        {
                            string responseText_ = (response_.Content == null) ? string.Empty : await response_.Content.ReadAsStringAsync().ConfigureAwait(false);

                            throw new NotificationApiException("Unauthorized", status_, responseText_, headers_, null);
                        }
                        else
                        {
                            var responseData_ = response_.Content == null ? null : await response_.Content.ReadAsStringAsync().ConfigureAwait(false);

                            throw new NotificationApiException("The HTTP status code of the response was not expected (" + status_ + ").", status_, responseData_, headers_, null);
                        }
                    }
                    finally
                    {
                        if (disposeResponse_)
                        {
                            response_.Dispose();
                        }
                    }
                }
            }
            finally
            {
                if (disposeClient_)
                {
                    client_.Dispose();
                }
            }
        }
示例#7
0
 /// <summary>Process callbacks from Gov Notify API</summary>
 /// <exception cref="NotificationApiException">A server side error occurred.</exception>
 public System.Threading.Tasks.Task HandleCallbackAsync(NotificationCallbackRequest notificationCallbackRequest)
 {
     return(HandleCallbackAsync(notificationCallbackRequest, System.Threading.CancellationToken.None));
 }