| | 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; |
| 894 | 15 | | public ClientDiagnostics Diagnostics { get; } |
| | 16 | |
|
| 172 | 17 | | public KeyVaultPipeline(Uri vaultUri, string apiVersion, HttpPipeline pipeline, ClientDiagnostics clientDiagnost |
| | 18 | | { |
| 172 | 19 | | VaultUri = vaultUri; |
| 172 | 20 | | _pipeline = pipeline; |
| | 21 | |
|
| 172 | 22 | | Diagnostics = clientDiagnostics; |
| | 23 | |
|
| 172 | 24 | | ApiVersion = apiVersion; |
| 172 | 25 | | } |
| | 26 | |
|
| 890 | 27 | | public string ApiVersion { get; } |
| | 28 | |
|
| 894 | 29 | | public Uri VaultUri { get; } |
| | 30 | |
|
| | 31 | | public Uri CreateFirstPageUri(string path) |
| | 32 | | { |
| 4 | 33 | | var firstPage = new RequestUriBuilder(); |
| 4 | 34 | | firstPage.Reset(VaultUri); |
| | 35 | |
|
| 4 | 36 | | firstPage.AppendPath(path, escape: false); |
| 4 | 37 | | firstPage.AppendQuery("api-version", ApiVersion); |
| | 38 | |
|
| 4 | 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 | | { |
| 4 | 60 | | Request request = _pipeline.CreateRequest(); |
| | 61 | |
|
| 4 | 62 | | request.Headers.Add(HttpHeader.Common.JsonContentType); |
| 4 | 63 | | request.Headers.Add(HttpHeader.Common.JsonAccept); |
| 4 | 64 | | request.Method = method; |
| 4 | 65 | | request.Uri.Reset(uri); |
| | 66 | |
|
| 4 | 67 | | return request; |
| | 68 | | } |
| | 69 | |
|
| | 70 | | public Request CreateRequest(RequestMethod method, params string[] path) |
| | 71 | | { |
| 886 | 72 | | Request request = _pipeline.CreateRequest(); |
| | 73 | |
|
| 886 | 74 | | request.Headers.Add(HttpHeader.Common.JsonContentType); |
| 886 | 75 | | request.Headers.Add(HttpHeader.Common.JsonAccept); |
| 886 | 76 | | request.Method = method; |
| 886 | 77 | | request.Uri.Reset(VaultUri); |
| | 78 | |
|
| 6936 | 79 | | foreach (var p in path) |
| | 80 | | { |
| 2582 | 81 | | request.Uri.AppendPath(p, escape: false); |
| | 82 | | } |
| | 83 | |
|
| 886 | 84 | | request.Uri.AppendQuery("api-version", ApiVersion); |
| | 85 | |
|
| 886 | 86 | | return request; |
| | 87 | | } |
| | 88 | |
|
| | 89 | | public Response<T> CreateResponse<T>(Response response, T result) |
| | 90 | | where T : IJsonDeserializable |
| | 91 | | { |
| 866 | 92 | | result.Deserialize(response.ContentStream); |
| 866 | 93 | | return Response.FromValue(result, response); |
| | 94 | | } |
| | 95 | |
|
| | 96 | | public DiagnosticScope CreateScope(string name) |
| | 97 | | { |
| 886 | 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 | | { |
| 2 | 104 | | using DiagnosticScope scope = Diagnostics.CreateScope(operationName); |
| 2 | 105 | | scope.Start(); |
| | 106 | |
|
| | 107 | | try |
| | 108 | | { |
| | 109 | | // if we don't have a nextLink specified, use firstPageUri |
| 2 | 110 | | if (nextLink != null) |
| | 111 | | { |
| 0 | 112 | | firstPageUri = new Uri(nextLink); |
| | 113 | | } |
| | 114 | |
|
| 2 | 115 | | using Request request = CreateRequest(RequestMethod.Get, firstPageUri); |
| 2 | 116 | | Response response = await SendRequestAsync(request, cancellationToken).ConfigureAwait(false); |
| | 117 | |
|
| | 118 | | // read the respose |
| 2 | 119 | | KeyVaultPage<T> responseAsPage = new KeyVaultPage<T>(itemFactory); |
| 2 | 120 | | responseAsPage.Deserialize(response.ContentStream); |
| | 121 | |
|
| | 122 | | // convert from the Page<T> to PageResponse<T> |
| 2 | 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 | | } |
| 2 | 130 | | } |
| | 131 | |
|
| | 132 | | public Page<T> GetPage<T>(Uri firstPageUri, string nextLink, Func<T> itemFactory, string operationName, Cancella |
| | 133 | | where T : IJsonDeserializable |
| | 134 | | { |
| 2 | 135 | | using DiagnosticScope scope = Diagnostics.CreateScope(operationName); |
| 2 | 136 | | scope.Start(); |
| | 137 | |
|
| | 138 | | try |
| | 139 | | { |
| | 140 | | // if we don't have a nextLink specified, use firstPageUri |
| 2 | 141 | | if (nextLink != null) |
| | 142 | | { |
| 0 | 143 | | firstPageUri = new Uri(nextLink); |
| | 144 | | } |
| | 145 | |
|
| 2 | 146 | | using Request request = CreateRequest(RequestMethod.Get, firstPageUri); |
| 2 | 147 | | Response response = SendRequest(request, cancellationToken); |
| | 148 | |
|
| | 149 | | // read the respose |
| 2 | 150 | | KeyVaultPage<T> responseAsPage = new KeyVaultPage<T>(itemFactory); |
| 2 | 151 | | responseAsPage.Deserialize(response.ContentStream); |
| | 152 | |
|
| | 153 | | // convert from the Page<T> to PageResponse<T> |
| 2 | 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 | | } |
| 2 | 161 | | } |
| | 162 | |
|
| | 163 | | public async Task<Response<TResult>> SendRequestAsync<TContent, TResult>(RequestMethod method, TContent content, |
| | 164 | | where TContent : IJsonSerializable |
| | 165 | | where TResult : IJsonDeserializable |
| | 166 | | { |
| 62 | 167 | | using Request request = CreateRequest(method, path); |
| 62 | 168 | | request.Content = RequestContent.Create(content.Serialize()); |
| | 169 | |
|
| 62 | 170 | | Response response = await SendRequestAsync(request, cancellationToken).ConfigureAwait(false); |
| | 171 | |
|
| 60 | 172 | | return CreateResponse(response, resultFactory()); |
| 60 | 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 | | { |
| 58 | 179 | | using Request request = CreateRequest(method, path); |
| 58 | 180 | | request.Content = RequestContent.Create(content.Serialize()); |
| | 181 | |
|
| 58 | 182 | | Response response = SendRequest(request, cancellationToken); |
| | 183 | |
|
| 56 | 184 | | return CreateResponse(response, resultFactory()); |
| 56 | 185 | | } |
| | 186 | |
|
| | 187 | | public async Task<Response<TResult>> SendRequestAsync<TResult>(RequestMethod method, Func<TResult> resultFactory |
| | 188 | | where TResult : IJsonDeserializable |
| | 189 | | { |
| 46 | 190 | | using Request request = CreateRequest(method, path); |
| 46 | 191 | | Response response = await SendRequestAsync(request, cancellationToken).ConfigureAwait(false); |
| | 192 | |
|
| 44 | 193 | | return CreateResponse(response, resultFactory()); |
| 44 | 194 | | } |
| | 195 | |
|
| | 196 | | public Response<TResult> SendRequest<TResult>(RequestMethod method, Func<TResult> resultFactory, CancellationTok |
| | 197 | | where TResult : IJsonDeserializable |
| | 198 | | { |
| 42 | 199 | | using Request request = CreateRequest(method, path); |
| 42 | 200 | | Response response = SendRequest(request, cancellationToken); |
| | 201 | |
|
| 40 | 202 | | return CreateResponse(response, resultFactory()); |
| 40 | 203 | | } |
| | 204 | | public async Task<Response> SendRequestAsync(RequestMethod method, CancellationToken cancellationToken, params s |
| | 205 | | { |
| 2 | 206 | | using Request request = CreateRequest(method, path); |
| 2 | 207 | | return await SendRequestAsync(request, cancellationToken).ConfigureAwait(false); |
| 2 | 208 | | } |
| | 209 | |
|
| | 210 | | public Response SendRequest(RequestMethod method, CancellationToken cancellationToken, params string[] path) |
| | 211 | | { |
| 2 | 212 | | using Request request = CreateRequest(method, path); |
| 2 | 213 | | return SendRequest(request, cancellationToken); |
| 2 | 214 | | } |
| | 215 | |
|
| | 216 | | public async Task<Response> GetResponseAsync(RequestMethod method, CancellationToken cancellationToken, params s |
| | 217 | | { |
| 364 | 218 | | using Request request = CreateRequest(method, path); |
| 364 | 219 | | return await _pipeline.SendRequestAsync(request, cancellationToken).ConfigureAwait(false); |
| 364 | 220 | | } |
| | 221 | |
|
| | 222 | | public Response GetResponse(RequestMethod method, CancellationToken cancellationToken, params string[] path) |
| | 223 | | { |
| 310 | 224 | | using Request request = CreateRequest(method, path); |
| 310 | 225 | | return _pipeline.SendRequest(request, cancellationToken); |
| 310 | 226 | | } |
| | 227 | |
|
| | 228 | | private async Task<Response> SendRequestAsync(Request request, CancellationToken cancellationToken) |
| | 229 | | { |
| 112 | 230 | | Response response = await _pipeline.SendRequestAsync(request, cancellationToken).ConfigureAwait(false); |
| | 231 | |
|
| 110 | 232 | | switch (response.Status) |
| | 233 | | { |
| | 234 | | case 200: |
| | 235 | | case 201: |
| | 236 | | case 202: |
| | 237 | | case 204: |
| 108 | 238 | | return response; |
| | 239 | | default: |
| 2 | 240 | | throw await Diagnostics.CreateRequestFailedExceptionAsync(response).ConfigureAwait(false); |
| | 241 | | } |
| 108 | 242 | | } |
| | 243 | | private Response SendRequest(Request request, CancellationToken cancellationToken) |
| | 244 | | { |
| 104 | 245 | | Response response = _pipeline.SendRequest(request, cancellationToken); |
| | 246 | |
|
| 102 | 247 | | switch (response.Status) |
| | 248 | | { |
| | 249 | | case 200: |
| | 250 | | case 201: |
| | 251 | | case 202: |
| | 252 | | case 204: |
| 100 | 253 | | return response; |
| | 254 | | default: |
| 2 | 255 | | throw Diagnostics.CreateRequestFailedException(response); |
| | 256 | | } |
| | 257 | | } |
| | 258 | | } |
| | 259 | | } |