| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Threading; |
| | 6 | | using System.Threading.Tasks; |
| | 7 | |
|
| | 8 | | namespace Azure.Core.Pipeline |
| | 9 | | { |
| | 10 | | /// <summary> |
| | 11 | | /// Represents a primitive for sending HTTP requests and receiving responses extensible by adding <see cref="HttpPip |
| | 12 | | /// </summary> |
| | 13 | | public class HttpPipeline |
| | 14 | | { |
| | 15 | | private readonly HttpPipelineTransport _transport; |
| | 16 | |
|
| | 17 | | private readonly ReadOnlyMemory<HttpPipelinePolicy> _pipeline; |
| | 18 | |
|
| | 19 | | /// <summary> |
| | 20 | | /// Creates a new instance of <see cref="HttpPipeline"/> with the provided transport, policies and response clas |
| | 21 | | /// </summary> |
| | 22 | | /// <param name="transport">The <see cref="HttpPipelineTransport"/> to use for sending the requests.</param> |
| | 23 | | /// <param name="policies">Policies to be invoked as part of the pipeline in order.</param> |
| | 24 | | /// <param name="responseClassifier">The response classifier to be used in invocations.</param> |
| 1348 | 25 | | public HttpPipeline(HttpPipelineTransport transport, HttpPipelinePolicy[]? policies = null, ResponseClassifier? |
| | 26 | | { |
| 1348 | 27 | | _transport = transport ?? throw new ArgumentNullException(nameof(transport)); |
| 1348 | 28 | | ResponseClassifier = responseClassifier ?? new ResponseClassifier(); |
| | 29 | |
|
| 1348 | 30 | | policies = policies ?? Array.Empty<HttpPipelinePolicy>(); |
| | 31 | |
|
| 1348 | 32 | | var all = new HttpPipelinePolicy[policies.Length + 1]; |
| 1348 | 33 | | all[policies.Length] = new HttpPipelineTransportPolicy(_transport); |
| 1348 | 34 | | policies.CopyTo(all, 0); |
| | 35 | |
|
| 1348 | 36 | | _pipeline = all; |
| 1348 | 37 | | } |
| | 38 | |
|
| | 39 | | /// <summary> |
| | 40 | | /// Creates a new <see cref="Request"/> instance. |
| | 41 | | /// </summary> |
| | 42 | | /// <returns>The request.</returns> |
| | 43 | | public Request CreateRequest() |
| 2938 | 44 | | => _transport.CreateRequest(); |
| | 45 | |
|
| | 46 | | /// <summary> |
| | 47 | | /// Creates a new <see cref="HttpMessage"/> instance. |
| | 48 | | /// </summary> |
| | 49 | | /// <returns>The message.</returns> |
| | 50 | | public HttpMessage CreateMessage() |
| | 51 | | { |
| 2464 | 52 | | return new HttpMessage(CreateRequest(), ResponseClassifier); |
| | 53 | | } |
| | 54 | |
|
| | 55 | | /// <summary> |
| | 56 | | /// The <see cref="ResponseClassifier"/> instance used in this pipeline invocations. |
| | 57 | | /// </summary> |
| 2560 | 58 | | public ResponseClassifier ResponseClassifier { get; } |
| | 59 | |
|
| | 60 | | /// <summary> |
| | 61 | | /// Invokes the pipeline asynchronously. After the task completes response would be set to the <see cref="HttpMe |
| | 62 | | /// </summary> |
| | 63 | | /// <param name="message">The <see cref="HttpMessage"/> to send.</param> |
| | 64 | | /// <param name="cancellationToken">The <see cref="CancellationToken"/> to use.</param> |
| | 65 | | /// <returns>The <see cref="ValueTask"/> representing the asynchronous operation.</returns> |
| | 66 | | public ValueTask SendAsync(HttpMessage message, CancellationToken cancellationToken) |
| | 67 | | { |
| 1502 | 68 | | message.CancellationToken = cancellationToken; |
| 1502 | 69 | | return _pipeline.Span[0].ProcessAsync(message, _pipeline.Slice(1)); |
| | 70 | | } |
| | 71 | |
|
| | 72 | | /// <summary> |
| | 73 | | /// Invokes the pipeline synchronously. After the task completes response would be set to the <see cref="HttpMes |
| | 74 | | /// </summary> |
| | 75 | | /// <param name="message">The <see cref="HttpMessage"/> to send.</param> |
| | 76 | | /// <param name="cancellationToken">The <see cref="CancellationToken"/> to use.</param> |
| | 77 | | public void Send(HttpMessage message, CancellationToken cancellationToken) |
| | 78 | | { |
| 1462 | 79 | | message.CancellationToken = cancellationToken; |
| 1462 | 80 | | _pipeline.Span[0].Process(message, _pipeline.Slice(1)); |
| 1222 | 81 | | } |
| | 82 | | /// <summary> |
| | 83 | | /// Invokes the pipeline asynchronously with the provided request. |
| | 84 | | /// </summary> |
| | 85 | | /// <param name="request">The <see cref="Request"/> to send.</param> |
| | 86 | | /// <param name="cancellationToken">The <see cref="CancellationToken"/> to use.</param> |
| | 87 | | /// <returns>The <see cref="ValueTask{T}"/> representing the asynchronous operation.</returns> |
| | 88 | | public async ValueTask<Response> SendRequestAsync(Request request, CancellationToken cancellationToken) |
| | 89 | | { |
| 68 | 90 | | HttpMessage message = new HttpMessage(request, ResponseClassifier); |
| 68 | 91 | | await SendAsync(message, cancellationToken).ConfigureAwait(false); |
| 68 | 92 | | return message.Response; |
| 68 | 93 | | } |
| | 94 | |
|
| | 95 | | /// <summary> |
| | 96 | | /// Invokes the pipeline synchronously with the provided request. |
| | 97 | | /// </summary> |
| | 98 | | /// <param name="request">The <see cref="Request"/> to send.</param> |
| | 99 | | /// <param name="cancellationToken">The <see cref="CancellationToken"/> to use.</param> |
| | 100 | | /// <returns>The <see cref="Response"/> from the server.</returns> |
| | 101 | | public Response SendRequest(Request request, CancellationToken cancellationToken) |
| | 102 | | { |
| 28 | 103 | | HttpMessage message = new HttpMessage(request, ResponseClassifier); |
| 28 | 104 | | Send(message, cancellationToken); |
| 28 | 105 | | return message.Response; |
| | 106 | | } |
| | 107 | |
|
| | 108 | | /// <summary> |
| | 109 | | /// Creates a scope in which all outgoing requests would use the provided |
| | 110 | | /// </summary> |
| | 111 | | /// <param name="clientRequestId">The client request id value to be sent with request.</param> |
| | 112 | | /// <returns>The <see cref="IDisposable"/> instance that needs to be disposed when client request id shouldn't b |
| | 113 | | /// <example> |
| | 114 | | /// Sample usage: |
| | 115 | | /// <code snippet="Snippet:ClientRequestId"> |
| | 116 | | /// var secretClient = new SecretClient(new Uri("http://example.com"), new DefaultAzureCredential()); |
| | 117 | | /// |
| | 118 | | /// using (HttpPipeline.CreateClientRequestIdScope("<custom-client-request-id>")) |
| | 119 | | /// { |
| | 120 | | /// // The HTTP request resulting from the client call would have x-ms-client-request-id value set to <cu |
| | 121 | | /// secretClient.GetSecret("<secret-name>"); |
| | 122 | | /// } |
| | 123 | | /// </code> |
| | 124 | | /// </example> |
| | 125 | | public static IDisposable CreateClientRequestIdScope(string? clientRequestId) |
| | 126 | | { |
| 10 | 127 | | return ReadClientRequestIdPolicy.StartScope(clientRequestId); |
| | 128 | | } |
| | 129 | | } |
| | 130 | | } |