| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using Azure.Core; |
| | 5 | | using Azure.Core.Pipeline; |
| | 6 | | using System; |
| | 7 | | using System.Threading; |
| | 8 | | using System.Threading.Tasks; |
| | 9 | |
|
| | 10 | | namespace Azure.Security.KeyVault |
| | 11 | | { |
| | 12 | | internal class KeyVaultPipeline |
| | 13 | | { |
| | 14 | | private readonly HttpPipeline _pipeline; |
| 2170 | 15 | | public ClientDiagnostics Diagnostics { get; } |
| | 16 | |
|
| 1138 | 17 | | public KeyVaultPipeline(Uri vaultUri, string apiVersion, HttpPipeline pipeline, ClientDiagnostics clientDiagnost |
| | 18 | | { |
| 1138 | 19 | | VaultUri = vaultUri; |
| 1138 | 20 | | _pipeline = pipeline; |
| | 21 | |
|
| 1138 | 22 | | Diagnostics = clientDiagnostics; |
| | 23 | |
|
| 1138 | 24 | | ApiVersion = apiVersion; |
| 1138 | 25 | | } |
| | 26 | |
|
| 1458 | 27 | | public string ApiVersion { get; } |
| | 28 | |
|
| 1458 | 29 | | public Uri VaultUri { get; } |
| | 30 | |
|
| | 31 | | public Uri CreateFirstPageUri(string path) |
| | 32 | | { |
| 20 | 33 | | var firstPage = new RequestUriBuilder(); |
| 20 | 34 | | firstPage.Reset(VaultUri); |
| | 35 | |
|
| 20 | 36 | | firstPage.AppendPath(path, escape: false); |
| 20 | 37 | | firstPage.AppendQuery("api-version", ApiVersion); |
| | 38 | |
|
| 20 | 39 | | return firstPage.ToUri(); |
| | 40 | | } |
| | 41 | |
|
| | 42 | | public Uri CreateFirstPageUri(string path, params ValueTuple<string, string>[] queryParams) |
| | 43 | | { |
| 0 | 44 | | var firstPage = new RequestUriBuilder(); |
| 0 | 45 | | firstPage.Reset(VaultUri); |
| | 46 | |
|
| 0 | 47 | | firstPage.AppendPath(path, escape: false); |
| 0 | 48 | | firstPage.AppendQuery("api-version", ApiVersion); |
| | 49 | |
|
| 0 | 50 | | foreach ((string, string) tuple in queryParams) |
| | 51 | | { |
| 0 | 52 | | firstPage.AppendQuery(tuple.Item1, tuple.Item2); |
| | 53 | | } |
| | 54 | |
|
| 0 | 55 | | return firstPage.ToUri(); |
| | 56 | | } |
| | 57 | |
|
| | 58 | | public Request CreateRequest(RequestMethod method, Uri uri) |
| | 59 | | { |
| 20 | 60 | | Request request = _pipeline.CreateRequest(); |
| | 61 | |
|
| 20 | 62 | | request.Headers.Add(HttpHeader.Common.JsonContentType); |
| 20 | 63 | | request.Headers.Add(HttpHeader.Common.JsonAccept); |
| 20 | 64 | | request.Method = method; |
| 20 | 65 | | request.Uri.Reset(uri); |
| | 66 | |
|
| 20 | 67 | | return request; |
| | 68 | | } |
| | 69 | |
|
| | 70 | | public Request CreateRequest(RequestMethod method, params string[] path) |
| | 71 | | { |
| 1438 | 72 | | Request request = _pipeline.CreateRequest(); |
| | 73 | |
|
| 1438 | 74 | | request.Headers.Add(HttpHeader.Common.JsonContentType); |
| 1438 | 75 | | request.Headers.Add(HttpHeader.Common.JsonAccept); |
| 1438 | 76 | | request.Method = method; |
| 1438 | 77 | | request.Uri.Reset(VaultUri); |
| | 78 | |
|
| 8980 | 79 | | foreach (var p in path) |
| | 80 | | { |
| 3052 | 81 | | request.Uri.AppendPath(p, escape: false); |
| | 82 | | } |
| | 83 | |
|
| 1438 | 84 | | request.Uri.AppendQuery("api-version", ApiVersion); |
| | 85 | |
|
| 1438 | 86 | | return request; |
| | 87 | | } |
| | 88 | |
|
| | 89 | | public Response<T> CreateResponse<T>(Response response, T result) |
| | 90 | | where T : IJsonDeserializable |
| | 91 | | { |
| 1196 | 92 | | result.Deserialize(response.ContentStream); |
| 1196 | 93 | | return Response.FromValue(result, response); |
| | 94 | | } |
| | 95 | |
|
| | 96 | | public DiagnosticScope CreateScope(string name) |
| | 97 | | { |
| 2098 | 98 | | return Diagnostics.CreateScope(name); |
| | 99 | | } |
| | 100 | |
|
| | 101 | | public async Task<Page<T>> GetPageAsync<T>(Uri firstPageUri, string nextLink, Func<T> itemFactory, string operat |
| | 102 | | where T : IJsonDeserializable |
| | 103 | | { |
| 10 | 104 | | using DiagnosticScope scope = Diagnostics.CreateScope(operationName); |
| 10 | 105 | | scope.Start(); |
| | 106 | |
|
| | 107 | | try |
| | 108 | | { |
| | 109 | | // if we don't have a nextLink specified, use firstPageUri |
| 10 | 110 | | if (nextLink != null) |
| | 111 | | { |
| 0 | 112 | | firstPageUri = new Uri(nextLink); |
| | 113 | | } |
| | 114 | |
|
| 10 | 115 | | using Request request = CreateRequest(RequestMethod.Get, firstPageUri); |
| 10 | 116 | | Response response = await SendRequestAsync(request, cancellationToken).ConfigureAwait(false); |
| | 117 | |
|
| | 118 | | // read the respose |
| 10 | 119 | | KeyVaultPage<T> responseAsPage = new KeyVaultPage<T>(itemFactory); |
| 10 | 120 | | responseAsPage.Deserialize(response.ContentStream); |
| | 121 | |
|
| | 122 | | // convert from the Page<T> to PageResponse<T> |
| 10 | 123 | | return Page<T>.FromValues(responseAsPage.Items.ToArray(), responseAsPage.NextLink?.ToString(), response) |
| | 124 | | } |
| 0 | 125 | | catch (Exception e) |
| | 126 | | { |
| 0 | 127 | | scope.Failed(e); |
| 0 | 128 | | throw; |
| | 129 | | } |
| 10 | 130 | | } |
| | 131 | |
|
| | 132 | | public Page<T> GetPage<T>(Uri firstPageUri, string nextLink, Func<T> itemFactory, string operationName, Cancella |
| | 133 | | where T : IJsonDeserializable |
| | 134 | | { |
| 10 | 135 | | using DiagnosticScope scope = Diagnostics.CreateScope(operationName); |
| 10 | 136 | | scope.Start(); |
| | 137 | |
|
| | 138 | | try |
| | 139 | | { |
| | 140 | | // if we don't have a nextLink specified, use firstPageUri |
| 10 | 141 | | if (nextLink != null) |
| | 142 | | { |
| 0 | 143 | | firstPageUri = new Uri(nextLink); |
| | 144 | | } |
| | 145 | |
|
| 10 | 146 | | using Request request = CreateRequest(RequestMethod.Get, firstPageUri); |
| 10 | 147 | | Response response = SendRequest(request, cancellationToken); |
| | 148 | |
|
| | 149 | | // read the respose |
| 10 | 150 | | KeyVaultPage<T> responseAsPage = new KeyVaultPage<T>(itemFactory); |
| 10 | 151 | | responseAsPage.Deserialize(response.ContentStream); |
| | 152 | |
|
| | 153 | | // convert from the Page<T> to PageResponse<T> |
| 10 | 154 | | return Page<T>.FromValues(responseAsPage.Items.ToArray(), responseAsPage.NextLink?.ToString(), response) |
| | 155 | | } |
| 0 | 156 | | catch (Exception e) |
| | 157 | | { |
| 0 | 158 | | scope.Failed(e); |
| 0 | 159 | | throw; |
| | 160 | | } |
| 10 | 161 | | } |
| | 162 | |
|
| | 163 | | public async Task<Response<TResult>> SendRequestAsync<TContent, TResult>(RequestMethod method, TContent content, |
| | 164 | | where TContent : IJsonSerializable |
| | 165 | | where TResult : IJsonDeserializable |
| | 166 | | { |
| 494 | 167 | | using Request request = CreateRequest(method, path); |
| 494 | 168 | | request.Content = RequestContent.Create(content.Serialize()); |
| | 169 | |
|
| 494 | 170 | | Response response = await SendRequestAsync(request, cancellationToken).ConfigureAwait(false); |
| | 171 | |
|
| 488 | 172 | | return CreateResponse(response, resultFactory()); |
| 488 | 173 | | } |
| | 174 | |
|
| | 175 | | public Response<TResult> SendRequest<TContent, TResult>(RequestMethod method, TContent content, Func<TResult> re |
| | 176 | | where TContent : IJsonSerializable |
| | 177 | | where TResult : IJsonDeserializable |
| | 178 | | { |
| 490 | 179 | | using Request request = CreateRequest(method, path); |
| 490 | 180 | | request.Content = RequestContent.Create(content.Serialize()); |
| | 181 | |
|
| 490 | 182 | | Response response = SendRequest(request, cancellationToken); |
| | 183 | |
|
| 488 | 184 | | return CreateResponse(response, resultFactory()); |
| 488 | 185 | | } |
| | 186 | |
|
| | 187 | | public async Task<Response<TResult>> SendRequestAsync<TResult>(RequestMethod method, Func<TResult> resultFactory |
| | 188 | | where TResult : IJsonDeserializable |
| | 189 | | { |
| 134 | 190 | | using Request request = CreateRequest(method, path); |
| 134 | 191 | | Response response = await SendRequestAsync(request, cancellationToken).ConfigureAwait(false); |
| | 192 | |
|
| 110 | 193 | | return CreateResponse(response, resultFactory()); |
| 110 | 194 | | } |
| | 195 | |
|
| | 196 | | public Response<TResult> SendRequest<TResult>(RequestMethod method, Func<TResult> resultFactory, CancellationTok |
| | 197 | | where TResult : IJsonDeserializable |
| | 198 | | { |
| 134 | 199 | | using Request request = CreateRequest(method, path); |
| 134 | 200 | | Response response = SendRequest(request, cancellationToken); |
| | 201 | |
|
| 110 | 202 | | return CreateResponse(response, resultFactory()); |
| 110 | 203 | | } |
| | 204 | | public async Task<Response> SendRequestAsync(RequestMethod method, CancellationToken cancellationToken, params s |
| | 205 | | { |
| 0 | 206 | | using Request request = CreateRequest(method, path); |
| 0 | 207 | | return await SendRequestAsync(request, cancellationToken).ConfigureAwait(false); |
| 0 | 208 | | } |
| | 209 | |
|
| | 210 | | public Response SendRequest(RequestMethod method, CancellationToken cancellationToken, params string[] path) |
| | 211 | | { |
| 0 | 212 | | using Request request = CreateRequest(method, path); |
| 0 | 213 | | return SendRequest(request, cancellationToken); |
| 0 | 214 | | } |
| | 215 | |
|
| | 216 | | public async Task<Response> GetResponseAsync(RequestMethod method, CancellationToken cancellationToken, params s |
| | 217 | | { |
| 186 | 218 | | using Request request = CreateRequest(method, path); |
| 186 | 219 | | return await _pipeline.SendRequestAsync(request, cancellationToken).ConfigureAwait(false); |
| 186 | 220 | | } |
| | 221 | |
|
| | 222 | | public Response GetResponse(RequestMethod method, CancellationToken cancellationToken, params string[] path) |
| | 223 | | { |
| 0 | 224 | | using Request request = CreateRequest(method, path); |
| 0 | 225 | | return _pipeline.SendRequest(request, cancellationToken); |
| 0 | 226 | | } |
| | 227 | |
|
| | 228 | | private async Task<Response> SendRequestAsync(Request request, CancellationToken cancellationToken) |
| | 229 | | { |
| 638 | 230 | | Response response = await _pipeline.SendRequestAsync(request, cancellationToken).ConfigureAwait(false); |
| | 231 | |
|
| 636 | 232 | | switch (response.Status) |
| | 233 | | { |
| | 234 | | case 200: |
| | 235 | | case 201: |
| | 236 | | case 202: |
| | 237 | | case 204: |
| 608 | 238 | | return response; |
| | 239 | | default: |
| 28 | 240 | | throw await Diagnostics.CreateRequestFailedExceptionAsync(response).ConfigureAwait(false); |
| | 241 | | } |
| 608 | 242 | | } |
| | 243 | | private Response SendRequest(Request request, CancellationToken cancellationToken) |
| | 244 | | { |
| 634 | 245 | | Response response = _pipeline.SendRequest(request, cancellationToken); |
| | 246 | |
|
| 632 | 247 | | switch (response.Status) |
| | 248 | | { |
| | 249 | | case 200: |
| | 250 | | case 201: |
| | 251 | | case 202: |
| | 252 | | case 204: |
| 608 | 253 | | return response; |
| | 254 | | default: |
| 24 | 255 | | throw Diagnostics.CreateRequestFailedException(response); |
| | 256 | | } |
| | 257 | | } |
| | 258 | | } |
| | 259 | | } |