< Summary

Class:Azure.Security.KeyVault.KeyVaultPipeline
Assembly:Azure.Security.KeyVault.Certificates
File(s):C:\Git\azure-sdk-for-net\sdk\keyvault\Azure.Security.KeyVault.Shared\src\KeyVaultPipeline.cs
Covered lines:89
Uncovered lines:15
Coverable lines:104
Total lines:259
Line coverage:85.5% (89 of 104)
Covered branches:14
Total branches:20
Branch coverage:70% (14 of 20)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
get_Diagnostics()-100%100%
.ctor(...)-100%100%
get_ApiVersion()-100%100%
get_VaultUri()-100%100%
CreateFirstPageUri(...)-100%100%
CreateFirstPageUri(...)-0%0%
CreateRequest(...)-100%100%
CreateRequest(...)-100%100%
CreateResponse(...)-100%100%
CreateScope(...)-100%100%
GetPageAsync()-69.23%50%
GetPage(...)-69.23%50%
SendRequestAsync()-100%100%
SendRequest(...)-100%100%
SendRequestAsync()-100%100%
SendRequest(...)-100%100%
SendRequestAsync()-100%100%
SendRequest(...)-100%100%
GetResponseAsync()-100%100%
GetResponse(...)-100%100%
SendRequestAsync()-100%100%
SendRequest(...)-100%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\keyvault\Azure.Security.KeyVault.Shared\src\KeyVaultPipeline.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using Azure.Core;
 5using Azure.Core.Pipeline;
 6using System;
 7using System.Threading;
 8using System.Threading.Tasks;
 9
 10namespace Azure.Security.KeyVault
 11{
 12    internal class KeyVaultPipeline
 13    {
 14        private readonly HttpPipeline _pipeline;
 89415        public ClientDiagnostics Diagnostics { get; }
 16
 17217        public KeyVaultPipeline(Uri vaultUri, string apiVersion, HttpPipeline pipeline, ClientDiagnostics clientDiagnost
 18        {
 17219            VaultUri = vaultUri;
 17220            _pipeline = pipeline;
 21
 17222            Diagnostics = clientDiagnostics;
 23
 17224            ApiVersion = apiVersion;
 17225        }
 26
 89027        public string ApiVersion { get; }
 28
 89429        public Uri VaultUri { get; }
 30
 31        public Uri CreateFirstPageUri(string path)
 32        {
 433            var firstPage = new RequestUriBuilder();
 434            firstPage.Reset(VaultUri);
 35
 436            firstPage.AppendPath(path, escape: false);
 437            firstPage.AppendQuery("api-version", ApiVersion);
 38
 439            return firstPage.ToUri();
 40        }
 41
 42        public Uri CreateFirstPageUri(string path, params ValueTuple<string, string>[] queryParams)
 43        {
 044            var firstPage = new RequestUriBuilder();
 045            firstPage.Reset(VaultUri);
 46
 047            firstPage.AppendPath(path, escape: false);
 048            firstPage.AppendQuery("api-version", ApiVersion);
 49
 050            foreach ((string, string) tuple in queryParams)
 51            {
 052                firstPage.AppendQuery(tuple.Item1, tuple.Item2);
 53            }
 54
 055            return firstPage.ToUri();
 56        }
 57
 58        public Request CreateRequest(RequestMethod method, Uri uri)
 59        {
 460            Request request = _pipeline.CreateRequest();
 61
 462            request.Headers.Add(HttpHeader.Common.JsonContentType);
 463            request.Headers.Add(HttpHeader.Common.JsonAccept);
 464            request.Method = method;
 465            request.Uri.Reset(uri);
 66
 467            return request;
 68        }
 69
 70        public Request CreateRequest(RequestMethod method, params string[] path)
 71        {
 88672            Request request = _pipeline.CreateRequest();
 73
 88674            request.Headers.Add(HttpHeader.Common.JsonContentType);
 88675            request.Headers.Add(HttpHeader.Common.JsonAccept);
 88676            request.Method = method;
 88677            request.Uri.Reset(VaultUri);
 78
 693679            foreach (var p in path)
 80            {
 258281                request.Uri.AppendPath(p, escape: false);
 82            }
 83
 88684            request.Uri.AppendQuery("api-version", ApiVersion);
 85
 88686            return request;
 87        }
 88
 89        public Response<T> CreateResponse<T>(Response response, T result)
 90            where T : IJsonDeserializable
 91        {
 86692            result.Deserialize(response.ContentStream);
 86693            return Response.FromValue(result, response);
 94        }
 95
 96        public DiagnosticScope CreateScope(string name)
 97        {
 88698            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        {
 2104            using DiagnosticScope scope = Diagnostics.CreateScope(operationName);
 2105            scope.Start();
 106
 107            try
 108            {
 109                // if we don't have a nextLink specified, use firstPageUri
 2110                if (nextLink != null)
 111                {
 0112                    firstPageUri = new Uri(nextLink);
 113                }
 114
 2115                using Request request = CreateRequest(RequestMethod.Get, firstPageUri);
 2116                Response response = await SendRequestAsync(request, cancellationToken).ConfigureAwait(false);
 117
 118                // read the respose
 2119                KeyVaultPage<T> responseAsPage = new KeyVaultPage<T>(itemFactory);
 2120                responseAsPage.Deserialize(response.ContentStream);
 121
 122                // convert from the Page<T> to PageResponse<T>
 2123                return Page<T>.FromValues(responseAsPage.Items.ToArray(), responseAsPage.NextLink?.ToString(), response)
 124            }
 0125            catch (Exception e)
 126            {
 0127                scope.Failed(e);
 0128                throw;
 129            }
 2130        }
 131
 132        public Page<T> GetPage<T>(Uri firstPageUri, string nextLink, Func<T> itemFactory, string operationName, Cancella
 133            where T : IJsonDeserializable
 134        {
 2135            using DiagnosticScope scope = Diagnostics.CreateScope(operationName);
 2136            scope.Start();
 137
 138            try
 139            {
 140                // if we don't have a nextLink specified, use firstPageUri
 2141                if (nextLink != null)
 142                {
 0143                    firstPageUri = new Uri(nextLink);
 144                }
 145
 2146                using Request request = CreateRequest(RequestMethod.Get, firstPageUri);
 2147                Response response = SendRequest(request, cancellationToken);
 148
 149                // read the respose
 2150                KeyVaultPage<T> responseAsPage = new KeyVaultPage<T>(itemFactory);
 2151                responseAsPage.Deserialize(response.ContentStream);
 152
 153                // convert from the Page<T> to PageResponse<T>
 2154                return Page<T>.FromValues(responseAsPage.Items.ToArray(), responseAsPage.NextLink?.ToString(), response)
 155            }
 0156            catch (Exception e)
 157            {
 0158                scope.Failed(e);
 0159                throw;
 160            }
 2161        }
 162
 163        public async Task<Response<TResult>> SendRequestAsync<TContent, TResult>(RequestMethod method, TContent content,
 164            where TContent : IJsonSerializable
 165            where TResult : IJsonDeserializable
 166        {
 62167            using Request request = CreateRequest(method, path);
 62168            request.Content = RequestContent.Create(content.Serialize());
 169
 62170            Response response = await SendRequestAsync(request, cancellationToken).ConfigureAwait(false);
 171
 60172            return CreateResponse(response, resultFactory());
 60173        }
 174
 175        public Response<TResult> SendRequest<TContent, TResult>(RequestMethod method, TContent content, Func<TResult> re
 176            where TContent : IJsonSerializable
 177            where TResult : IJsonDeserializable
 178        {
 58179            using Request request = CreateRequest(method, path);
 58180            request.Content = RequestContent.Create(content.Serialize());
 181
 58182            Response response = SendRequest(request, cancellationToken);
 183
 56184            return CreateResponse(response, resultFactory());
 56185        }
 186
 187        public async Task<Response<TResult>> SendRequestAsync<TResult>(RequestMethod method, Func<TResult> resultFactory
 188            where TResult : IJsonDeserializable
 189        {
 46190            using Request request = CreateRequest(method, path);
 46191            Response response = await SendRequestAsync(request, cancellationToken).ConfigureAwait(false);
 192
 44193            return CreateResponse(response, resultFactory());
 44194        }
 195
 196        public Response<TResult> SendRequest<TResult>(RequestMethod method, Func<TResult> resultFactory, CancellationTok
 197            where TResult : IJsonDeserializable
 198        {
 42199            using Request request = CreateRequest(method, path);
 42200            Response response = SendRequest(request, cancellationToken);
 201
 40202            return CreateResponse(response, resultFactory());
 40203        }
 204        public async Task<Response> SendRequestAsync(RequestMethod method, CancellationToken cancellationToken, params s
 205        {
 2206            using Request request = CreateRequest(method, path);
 2207            return await SendRequestAsync(request, cancellationToken).ConfigureAwait(false);
 2208        }
 209
 210        public Response SendRequest(RequestMethod method, CancellationToken cancellationToken, params string[] path)
 211        {
 2212            using Request request = CreateRequest(method, path);
 2213            return SendRequest(request, cancellationToken);
 2214        }
 215
 216        public async Task<Response> GetResponseAsync(RequestMethod method, CancellationToken cancellationToken, params s
 217        {
 364218            using Request request = CreateRequest(method, path);
 364219            return await _pipeline.SendRequestAsync(request, cancellationToken).ConfigureAwait(false);
 364220        }
 221
 222        public Response GetResponse(RequestMethod method, CancellationToken cancellationToken, params string[] path)
 223        {
 310224            using Request request = CreateRequest(method, path);
 310225            return _pipeline.SendRequest(request, cancellationToken);
 310226        }
 227
 228        private async Task<Response> SendRequestAsync(Request request, CancellationToken cancellationToken)
 229        {
 112230            Response response = await _pipeline.SendRequestAsync(request, cancellationToken).ConfigureAwait(false);
 231
 110232            switch (response.Status)
 233            {
 234                case 200:
 235                case 201:
 236                case 202:
 237                case 204:
 108238                    return response;
 239                default:
 2240                    throw await Diagnostics.CreateRequestFailedExceptionAsync(response).ConfigureAwait(false);
 241            }
 108242        }
 243        private Response SendRequest(Request request, CancellationToken cancellationToken)
 244        {
 104245            Response response = _pipeline.SendRequest(request, cancellationToken);
 246
 102247            switch (response.Status)
 248            {
 249                case 200:
 250                case 201:
 251                case 202:
 252                case 204:
 100253                    return response;
 254                default:
 2255                    throw Diagnostics.CreateRequestFailedException(response);
 256            }
 257        }
 258    }
 259}