Exemple #1
0
        private static async Task <List <GraphBatchResult> > GetBatchResults(Dictionary <string, HttpResponseMessage> responses, bool ignoreNotFound, bool ignoreRefAlreadyExists, bool canRetry)
        {
            List <GraphBatchResult> results = new List <GraphBatchResult>();

            foreach (KeyValuePair <string, HttpResponseMessage> r in responses)
            {
                using (r.Value)
                {
                    GraphBatchResult result = new GraphBatchResult();
                    result.ID        = r.Key;
                    result.IsSuccess = r.Value.IsSuccessStatusCode;
                    results.Add(result);

                    if (result.IsSuccess)
                    {
                        continue;
                    }

                    if (ignoreNotFound && r.Value.StatusCode == HttpStatusCode.NotFound)
                    {
                        result.IsSuccess = true;
                        GraphHelper.logger.Warn($"The request ({r.Key}) to remove object failed because it did not exist");
                        continue;
                    }

                    result.ErrorResponse = await GraphHelper.GetErrorResponseFromHttpResponseMessage(r);

                    if (ignoreRefAlreadyExists && r.Value.StatusCode == HttpStatusCode.BadRequest && result.ErrorResponse.Error.Message.IndexOf("object references already exist", StringComparison.OrdinalIgnoreCase) > 0)
                    {
                        result.IsSuccess = true;
                        GraphHelper.logger.Warn($"The request ({r.Key}) to add object failed because it already exists");
                        continue;
                    }

                    if (canRetry && r.Value.StatusCode == (HttpStatusCode)429)
                    {
                        if (r.Value.Headers.TryGetValues("Retry-After", out IEnumerable <string> outvalues))
                        {
                            string tryAfter = outvalues.FirstOrDefault() ?? "0";
                            result.RetryInterval = int.Parse(tryAfter);
                            GraphHelper.logger.Warn($"Rate limit encountered, backoff interval of {result.RetryInterval} found");
                        }
                        else
                        {
                            GraphHelper.logger.Warn("Rate limit encountered, but no backoff interval specified");
                        }

                        result.IsRetryable = true;
                        continue;
                    }

                    if (canRetry && r.Value.StatusCode == HttpStatusCode.NotFound && string.Equals(result.ErrorResponse.Error.Code, "Request_ResourceNotFound", StringComparison.OrdinalIgnoreCase))
                    {
                        result.IsRetryable = true;
                        continue;
                    }

                    result.IsFailed  = true;
                    result.Exception = new ServiceException(result.ErrorResponse.Error, r.Value.Headers, r.Value.StatusCode);
                }
            }

            return(results);
        }