| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.IO; |
| | 7 | | using Azure.Core; |
| | 8 | |
|
| | 9 | | namespace Azure.Storage.Blobs.Specialized |
| | 10 | | { |
| | 11 | | /// <summary> |
| | 12 | | /// A Response that can be returned before a request is actually sent and |
| | 13 | | /// will throw until a live response is provided to wrap. |
| | 14 | | /// </summary> |
| | 15 | | internal class DelayedResponse : Response |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// The live Response to wrap. |
| | 19 | | /// </summary> |
| | 20 | | private Response _live; |
| | 21 | |
|
| | 22 | | /// <summary> |
| | 23 | | /// An optional function that can be used to process the response. It |
| | 24 | | /// is responsible for parsing response bodies and throwing exceptions. |
| | 25 | | /// (This would be more meaningful if we add a DelayedResponse{T} down |
| | 26 | | /// the road.) |
| | 27 | | /// |
| | 28 | | /// This is intended to be one of the |
| | 29 | | /// BlobRestClient.Group.OperationName_CreateResponse methods which |
| | 30 | | /// correctly throw when necessary. |
| | 31 | | /// </summary> |
| | 32 | | private readonly Func<Response, Response> _processResponse = null; |
| | 33 | |
|
| | 34 | | /// <summary> |
| | 35 | | /// Gets the live Response or throws an InvalidOperationException if |
| | 36 | | /// you attempt to use the Response before the batch operation has been |
| | 37 | | /// submitted. |
| | 38 | | /// </summary> |
| | 39 | | private Response LiveResponse |
| | 40 | | { |
| 156 | 41 | | get => _live ?? throw BatchErrors.UseDelayedResponseEarly(); |
| | 42 | | } |
| | 43 | |
|
| | 44 | | /// <summary> |
| | 45 | | /// Creates a new instance of the <see cref="DelayedResponse"/> class. |
| | 46 | | /// </summary> |
| | 47 | | /// <param name="message">The message this is a response for.</param> |
| | 48 | | /// <param name="processResponse"> |
| | 49 | | /// An optional function that can be used to process the response. It |
| | 50 | | /// is responsible for parsing response bodies and throwing exceptions. |
| | 51 | | /// </param> |
| 2328 | 52 | | public DelayedResponse(HttpMessage message, Func<Response, Response> processResponse = null) |
| | 53 | | { |
| | 54 | | // Have the BatchPipelineTransport associate this response with the |
| | 55 | | // message when it's sent. |
| 2328 | 56 | | message.SetProperty(BatchConstants.DelayedResponsePropertyName, this); |
| 2328 | 57 | | _processResponse = processResponse; |
| 2328 | 58 | | } |
| | 59 | |
|
| | 60 | | /// <summary> |
| | 61 | | /// Set the live <see cref="Response"/>. |
| | 62 | | /// </summary> |
| | 63 | | /// <param name="live">The live <see cref="Response"/>.</param> |
| | 64 | | /// <param name="throwOnFailure"> |
| | 65 | | /// A value indicating whether or not we should throw for Response |
| | 66 | | /// failures. |
| | 67 | | /// </param> |
| | 68 | | public void SetLiveResponse(Response live, bool throwOnFailure) |
| | 69 | | { |
| 1270 | 70 | | _live = live; |
| | 71 | |
|
| | 72 | | // Set the live response before possibly throwing in case someone |
| | 73 | | // tries to catch the exception but then interrogate raw responses |
| 1270 | 74 | | if (throwOnFailure && _processResponse != null) |
| | 75 | | { |
| 1164 | 76 | | _live = _processResponse(_live); |
| | 77 | | } |
| 1222 | 78 | | } |
| | 79 | |
|
| | 80 | | public override string ToString() |
| | 81 | | { |
| 4 | 82 | | return _live == null ? "Status: NotSent, the batch has not been submitted yet" : base.ToString(); |
| | 83 | | } |
| | 84 | |
|
| | 85 | | // We directly forward the entire Response interface to LiveResponse |
| | 86 | | #region forward Response members to Live |
| | 87 | | /// <inheritdoc /> |
| | 88 | | public override int Status => |
| 154 | 89 | | _live == null ? |
| 154 | 90 | | BatchConstants.NoStatusCode : // Give users a hint that this is an exploding Response |
| 154 | 91 | | LiveResponse.Status; |
| | 92 | |
|
| | 93 | | /// <inheritdoc /> |
| 2 | 94 | | public override string ReasonPhrase => LiveResponse.ReasonPhrase; |
| | 95 | |
|
| | 96 | | /// <inheritdoc /> |
| | 97 | | public override Stream ContentStream |
| | 98 | | { |
| 0 | 99 | | get => LiveResponse.ContentStream; |
| 0 | 100 | | set => LiveResponse.ContentStream = value; |
| | 101 | | } |
| | 102 | |
|
| | 103 | | /// <inheritdoc /> |
| | 104 | | public override string ClientRequestId |
| | 105 | | { |
| 4 | 106 | | get => LiveResponse.ClientRequestId; |
| 0 | 107 | | set => LiveResponse.ClientRequestId = value; |
| | 108 | | } |
| | 109 | |
|
| | 110 | | /// <inheritdoc /> |
| | 111 | | public override void Dispose() => |
| 0 | 112 | | _live?.Dispose(); // Don't want to throw so we just use _live |
| | 113 | |
|
| | 114 | | /// <inheritdoc /> |
| | 115 | | protected override bool ContainsHeader(string name) => |
| 0 | 116 | | LiveResponse.Headers.Contains(name); |
| | 117 | |
|
| | 118 | | /// <inheritdoc /> |
| | 119 | | protected override IEnumerable<HttpHeader> EnumerateHeaders() => |
| 0 | 120 | | LiveResponse.Headers; |
| | 121 | |
|
| | 122 | | /// <inheritdoc /> |
| | 123 | | protected override bool TryGetHeader(string name, out string value) => |
| 0 | 124 | | LiveResponse.Headers.TryGetValue(name, out value); |
| | 125 | |
|
| | 126 | | /// <inheritdoc /> |
| | 127 | | protected override bool TryGetHeaderValues(string name, out IEnumerable<string> values) => |
| 0 | 128 | | LiveResponse.Headers.TryGetValues(name, out values); |
| | 129 | | #endregion forward Response members to Live |
| | 130 | | } |
| | 131 | | } |