Ejemplo n.º 1
0
        /// <summary>
        /// Parse the batch response to get the individual responses.
        /// </summary>
        /// <param name="batchResponse">The <see cref="ODataBatchResponse"/>.</param>
        /// <returns>The <see cref="Task"/>.</returns>
        static internal async Task ParseResponseAsync(ODataBatchResponse batchResponse)
        {
            if (!DetermineContentTypeBoundary(batchResponse.BatchResponseMessage, out string boundary))
            {
                throw new InvalidOperationException("Batch response must have a Content-Type of 'multipart/mixed' with a corresponding 'boundary' specified.");
            }

            using (var rs = await batchResponse.BatchResponseMessage.Content.ReadAsStreamAsync())
                using (var sr = new StreamReader(rs))
                    using (var mr = new HttpMultiPartResponseReader(sr))
                    {
                        int i = 0;
                        HttpResponseMessage response = null;

                        while ((response = await mr.ReadNextAsync()) != null)
                        {
                            if (i > batchResponse.BatchManager.Items.Count)
                            {
                                break;
                            }

                            response.RequestMessage = batchResponse.BatchManager.Items[i].RequestMessage;
                            batchResponse.BatchManager.Items[i].ResponseMessage = response;
                            i++;
                        }

                        if (i > batchResponse.BatchManager.Items.Count)
                        {
                            throw new InvalidOperationException("Batch response request and response mismatch; the number of requests and responses differ.");
                        }
                    }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Sends the batch and returns an <see cref="ODataBatchResponse"/>.
        /// </summary>
        /// <returns>The <see cref="ODataBatchResponse"/>.</returns>
        public async Task <ODataBatchResponse> SendAsync()
        {
            lock (_lock)
            {
                if (State != ODataBatchState.Ready)
                {
                    throw new InvalidOperationException("Batch is no longer in a Ready state; no further requests can be added to the batch.");
                }

                if (Items.Count == 0)
                {
                    throw new InvalidOperationException("Batch must have add at least one request item added; nothing to send.");
                }

                State = ODataBatchState.Sending;
            }

            return(await ODataInvoker.Default.InvokeAsync(this, async() =>
            {
                var request = OData.CreateRequestMessage(new ODataArgs(), "POST", null, "$batch");
                request.Content = await CreateContentAsync().ConfigureAwait(false);
                var resp = await ODataBatchResponse.CreateAsync(this, await OData.SendRequestAsync(request).ConfigureAwait(false)).ConfigureAwait(false);
                State = ODataBatchState.Sent;
                return resp;
            }, OData).ConfigureAwait(false));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Creates the <see cref="ODataBatchResponse"/>.
        /// </summary>
        /// <param name="batchManager">The <see cref="ODataBatchManager"/>.</param>
        /// <param name="batchResponseMessage">The <see cref="HttpResponseMessage"/>.</param>
        /// <returns></returns>
        static internal async Task <ODataBatchResponse> CreateAsync(ODataBatchManager batchManager, HttpResponseMessage batchResponseMessage)
        {
            var obr = new ODataBatchResponse()
            {
                BatchManager = batchManager, BatchResponseMessage = batchResponseMessage
            };

            if (obr.BatchResponseMessage.IsSuccessStatusCode)
            {
                await ParseResponseAsync(obr);
            }

            return(obr);
        }