Пример #1
0
        public async Task AssignBatchActionResultsAsync(IODataClient client,
                                                        ODataResponse batchResponse, IList <Func <IODataClient, Task> > actions, IList <int> responseIndexes)
        {
            var exceptions = new List <Exception>();

            for (var actionIndex = 0; actionIndex < actions.Count && !exceptions.Any(); actionIndex++)
            {
                var responseIndex = responseIndexes[actionIndex];
                if (responseIndex >= 0 && responseIndex < batchResponse.Batch.Count)
                {
                    var actionResponse = batchResponse.Batch[responseIndex];
                    if (actionResponse.Exception != null)
                    {
                        exceptions.Add(actionResponse.Exception);
                    }
                    else if (actionResponse.StatusCode >= (int)HttpStatusCode.BadRequest)
                    {
                        exceptions.Add(WebRequestException.CreateFromStatusCode((HttpStatusCode)actionResponse.StatusCode));
                    }
                    else
                    {
                        await actions[actionIndex](new ODataClient(client as ODataClient, actionResponse)).ConfigureAwait(false);
                    }
                }
            }

            if (exceptions.Any())
            {
                throw exceptions.First();
            }
        }
Пример #2
0
        private async Task ExecuteBatchActionsAsync(IList <Func <IODataClient, Task> > actions, CancellationToken cancellationToken)
        {
            if (!actions.Any())
            {
                return;
            }

            var responseIndexes = new List <int>();

            // Write batch operations into a batch content
            var lastOperationId = 0;

            foreach (var action in actions)
            {
                await action(this);

                var responseIndex = -1;
                if (_lazyBatchWriter.Value.LastOperationId > lastOperationId)
                {
                    lastOperationId = _lazyBatchWriter.Value.LastOperationId;
                    responseIndex   = lastOperationId - 1;
                }
                responseIndexes.Add(responseIndex);
            }

            if (_lazyBatchWriter.Value.HasOperations)
            {
                // Create batch request message
                var requestMessage = await _lazyBatchWriter.Value.EndBatchAsync();

                var request = new ODataRequest(RestVerbs.Post, _session, ODataLiteral.Batch, requestMessage);

                // Execute batch and get response
                ODataResponse batchResponse;
                using (var response = await _requestRunner.ExecuteRequestAsync(request, cancellationToken))
                {
                    var responseReader = _session.Adapter.GetResponseReader();
                    batchResponse = await responseReader.GetResponseAsync(response);
                }

                // Replay batch operations to assign results
                for (int actionIndex = 0; actionIndex < actions.Count; actionIndex++)
                {
                    var responseIndex = responseIndexes[actionIndex];
                    if (responseIndex >= 0)
                    {
                        var actionResponse = batchResponse.Batch[responseIndex];
                        if (actionResponse.StatusCode >= 400)
                        {
                            throw WebRequestException.CreateFromStatusCode((HttpStatusCode)actionResponse.StatusCode);
                        }

                        var   client = new ODataClient(this, actionResponse);
                        await actions[actionIndex](client);
                    }
                }
            }
        }
 public static ODataResponse FromStatusCode(int statusCode, Stream responseStream)
 {
     if (statusCode >= (int)HttpStatusCode.BadRequest)
     {
         var responseContent = Utils.StreamToString(responseStream, true);
         return(new ODataResponse
         {
             StatusCode = statusCode,
             Exception = WebRequestException.CreateFromStatusCode((HttpStatusCode)statusCode, responseContent),
         });
     }
     else
     {
         return(new ODataResponse
         {
             StatusCode = statusCode,
         });
     }
 }
Пример #4
0
 internal static ODataResponse FromStatusCode(ITypeCache typeCache, int statusCode, IEnumerable <KeyValuePair <string, string> > headers, Stream responseStream, WebRequestExceptionMessageSource exceptionMessageSource)
 {
     if (statusCode >= (int)HttpStatusCode.BadRequest)
     {
         var responseContent = Utils.StreamToString(responseStream, true);
         return(new ODataResponse(typeCache)
         {
             StatusCode = statusCode,
             Exception = WebRequestException.CreateFromStatusCode((HttpStatusCode)statusCode, exceptionMessageSource, responseContent),
             Headers = headers
         });
     }
     else
     {
         return(new ODataResponse(typeCache)
         {
             StatusCode = statusCode,
             Headers = headers
         });
     }
 }