private HttpResponseMessage GetEmailResponseMessage(GraphBatchRequest request, string errorCode, string message, HttpStatusCode statusCode)
        {
            var graphResponses = request.Requests.Select(e => new GraphResponse
            {
                Status = statusCode,
                Body   = new GraphResponseBody {
                    Error = new GraphResponseError {
                        Code = errorCode, Messsage = message
                    }
                },
                Id = e.Id,
            });

            var graphBatchResponse = new GraphBatchResponse
            {
                Responses = graphResponses.ToList(),
            };

            return(new HttpResponseMessage(statusCode)
            {
                Content = new StringContent(JsonConvert.SerializeObject(graphBatchResponse)),
            });
        }
Пример #2
0
        /// <inheritdoc/>
        public async Task <IList <NotificationBatchItemResponse> > ProcessEmailRequestBatch(AuthenticationHeaderValue authenticationHeaderValue, GraphBatchRequest graphBatchRequest)
        {
            this.logger.TraceInformation($"Started {nameof(this.ProcessEmailRequestBatch)} method of {nameof(MSGraphProvider)}.");
            List <NotificationBatchItemResponse> responses = new List <NotificationBatchItemResponse>();

            this.httpClient.DefaultRequestHeaders.Authorization = authenticationHeaderValue;
            var requestPayLoad           = JsonConvert.SerializeObject(graphBatchRequest, this.jsonSerializerSettings);
            HttpResponseMessage response = null;

            response = await this.httpClient.PostAsync(
                $"{this.mSGraphSetting.BaseUrl}/{this.mSGraphSetting.GraphAPIVersion}/{this.mSGraphSetting.BatchRequestUrl}",
                new StringContent(requestPayLoad, Encoding.UTF8, ApplicationConstants.JsonMIMEType)).ConfigureAwait(false);

            this.logger.TraceInformation($"Method {nameof(this.ProcessEmailRequestBatch)}:Completed Graph Batch Call");
            var traceProps = new Dictionary <string, string>();

            traceProps["GraphRequest"]  = requestPayLoad;
            traceProps["GraphResponse"] = JsonConvert.SerializeObject(response, this.jsonSerializerSettings);
            this.logger.TraceInformation($"Graph Request and Graph Response", traceProps);

            _ = traceProps.Remove("GraphRequest");
            _ = traceProps.Remove("GraphResponse");

            if (response?.IsSuccessStatusCode ?? false)
            {
                // Read and deserialize response.
                var graphResponses = JsonConvert.DeserializeObject <GraphBatchResponse>(await response.Content.ReadAsStringAsync().ConfigureAwait(false));
                foreach (var graphResponse in graphResponses?.Responses)
                {
                    if (graphResponse?.Body?.Error?.Code != null)
                    {
                        traceProps["NotificationId"] = graphResponse?.Id;
                        traceProps["ErrorCode"]      = graphResponse?.Body?.Error?.Code;
                        this.logger.WriteCustomEvent("Error Sending Notification", traceProps);
                    }

                    responses.Add(graphResponse.ToNotificationBatchItemResponse());
                }
            }
            else
            {
                string content = string.Empty;
                if (response != null)
                {
                    content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
                }

                throw new System.Exception($"An error occurred while processing notification batch. Details: {content}");
            }

            this.logger.TraceInformation($"Finished {nameof(this.ProcessEmailRequestBatch)} method of {nameof(MSGraphProvider)}.");
            return(responses);
        }