< Summary

Class:Azure.Security.KeyVault.KeyVaultPipeline
Assembly:Azure.Security.KeyVault.Keys
File(s):C:\Git\azure-sdk-for-net\sdk\keyvault\Azure.Security.KeyVault.Shared\src\KeyVaultPipeline.cs
Covered lines:80
Uncovered lines:24
Coverable lines:104
Total lines:259
Line coverage:76.9% (80 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()-0%100%
SendRequest(...)-0%100%
GetResponseAsync()-100%100%
GetResponse(...)-0%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;
 217015        public ClientDiagnostics Diagnostics { get; }
 16
 113817        public KeyVaultPipeline(Uri vaultUri, string apiVersion, HttpPipeline pipeline, ClientDiagnostics clientDiagnost
 18        {
 113819            VaultUri = vaultUri;
 113820            _pipeline = pipeline;
 21
 113822            Diagnostics = clientDiagnostics;
 23
 113824            ApiVersion = apiVersion;
 113825        }
 26
 145827        public string ApiVersion { get; }
 28
 145829        public Uri VaultUri { get; }
 30
 31        public Uri CreateFirstPageUri(string path)
 32        {
 2033            var firstPage = new RequestUriBuilder();
 2034            firstPage.Reset(VaultUri);
 35
 2036            firstPage.AppendPath(path, escape: false);
 2037            firstPage.AppendQuery("api-version", ApiVersion);
 38
 2039            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        {
 2060            Request request = _pipeline.CreateRequest();
 61
 2062            request.Headers.Add(HttpHeader.Common.JsonContentType);
 2063            request.Headers.Add(HttpHeader.Common.JsonAccept);
 2064            request.Method = method;
 2065            request.Uri.Reset(uri);
 66
 2067            return request;
 68        }
 69
 70        public Request CreateRequest(RequestMethod method, params string[] path)
 71        {
 143872            Request request = _pipeline.CreateRequest();
 73
 143874            request.Headers.Add(HttpHeader.Common.JsonContentType);
 143875            request.Headers.Add(HttpHeader.Common.JsonAccept);
 143876            request.Method = method;
 143877            request.Uri.Reset(VaultUri);
 78
 898079            foreach (var p in path)
 80            {
 305281                request.Uri.AppendPath(p, escape: false);
 82            }
 83
 143884            request.Uri.AppendQuery("api-version", ApiVersion);
 85
 143886            return request;
 87        }
 88
 89        public Response<T> CreateResponse<T>(Response response, T result)
 90            where T : IJsonDeserializable
 91        {
 119692            result.Deserialize(response.ContentStream);
 119693            return Response.FromValue(result, response);
 94        }
 95
 96        public DiagnosticScope CreateScope(string name)
 97        {
 209898            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        {
 10104            using DiagnosticScope scope = Diagnostics.CreateScope(operationName);
 10105            scope.Start();
 106
 107            try
 108            {
 109                // if we don't have a nextLink specified, use firstPageUri
 10110                if (nextLink != null)
 111                {
 0112                    firstPageUri = new Uri(nextLink);
 113                }
 114
 10115                using Request request = CreateRequest(RequestMethod.Get, firstPageUri);
 10116                Response response = await SendRequestAsync(request, cancellationToken).ConfigureAwait(false);
 117
 118                // read the respose
 10119                KeyVaultPage<T> responseAsPage = new KeyVaultPage<T>(itemFactory);
 10120                responseAsPage.Deserialize(response.ContentStream);
 121
 122                // convert from the Page<T> to PageResponse<T>
 10123                return Page<T>.FromValues(responseAsPage.Items.ToArray(), responseAsPage.NextLink?.ToString(), response)
 124            }
 0125            catch (Exception e)
 126            {
 0127                scope.Failed(e);
 0128                throw;
 129            }
 10130        }
 131
 132        public Page<T> GetPage<T>(Uri firstPageUri, string nextLink, Func<T> itemFactory, string operationName, Cancella
 133            where T : IJsonDeserializable
 134        {
 10135            using DiagnosticScope scope = Diagnostics.CreateScope(operationName);
 10136            scope.Start();
 137
 138            try
 139            {
 140                // if we don't have a nextLink specified, use firstPageUri
 10141                if (nextLink != null)
 142                {
 0143                    firstPageUri = new Uri(nextLink);
 144                }
 145
 10146                using Request request = CreateRequest(RequestMethod.Get, firstPageUri);
 10147                Response response = SendRequest(request, cancellationToken);
 148
 149                // read the respose
 10150                KeyVaultPage<T> responseAsPage = new KeyVaultPage<T>(itemFactory);
 10151                responseAsPage.Deserialize(response.ContentStream);
 152
 153                // convert from the Page<T> to PageResponse<T>
 10154                return Page<T>.FromValues(responseAsPage.Items.ToArray(), responseAsPage.NextLink?.ToString(), response)
 155            }
 0156            catch (Exception e)
 157            {
 0158                scope.Failed(e);
 0159                throw;
 160            }
 10161        }
 162
 163        public async Task<Response<TResult>> SendRequestAsync<TContent, TResult>(RequestMethod method, TContent content,
 164            where TContent : IJsonSerializable
 165            where TResult : IJsonDeserializable
 166        {
 494167            using Request request = CreateRequest(method, path);
 494168            request.Content = RequestContent.Create(content.Serialize());
 169
 494170            Response response = await SendRequestAsync(request, cancellationToken).ConfigureAwait(false);
 171
 488172            return CreateResponse(response, resultFactory());
 488173        }
 174
 175        public Response<TResult> SendRequest<TContent, TResult>(RequestMethod method, TContent content, Func<TResult> re
 176            where TContent : IJsonSerializable
 177            where TResult : IJsonDeserializable
 178        {
 490179            using Request request = CreateRequest(method, path);
 490180            request.Content = RequestContent.Create(content.Serialize());
 181
 490182            Response response = SendRequest(request, cancellationToken);
 183
 488184            return CreateResponse(response, resultFactory());
 488185        }
 186
 187        public async Task<Response<TResult>> SendRequestAsync<TResult>(RequestMethod method, Func<TResult> resultFactory
 188            where TResult : IJsonDeserializable
 189        {
 134190            using Request request = CreateRequest(method, path);
 134191            Response response = await SendRequestAsync(request, cancellationToken).ConfigureAwait(false);
 192
 110193            return CreateResponse(response, resultFactory());
 110194        }
 195
 196        public Response<TResult> SendRequest<TResult>(RequestMethod method, Func<TResult> resultFactory, CancellationTok
 197            where TResult : IJsonDeserializable
 198        {
 134199            using Request request = CreateRequest(method, path);
 134200            Response response = SendRequest(request, cancellationToken);
 201
 110202            return CreateResponse(response, resultFactory());
 110203        }
 204        public async Task<Response> SendRequestAsync(RequestMethod method, CancellationToken cancellationToken, params s
 205        {
 0206            using Request request = CreateRequest(method, path);
 0207            return await SendRequestAsync(request, cancellationToken).ConfigureAwait(false);
 0208        }
 209
 210        public Response SendRequest(RequestMethod method, CancellationToken cancellationToken, params string[] path)
 211        {
 0212            using Request request = CreateRequest(method, path);
 0213            return SendRequest(request, cancellationToken);
 0214        }
 215
 216        public async Task<Response> GetResponseAsync(RequestMethod method, CancellationToken cancellationToken, params s
 217        {
 186218            using Request request = CreateRequest(method, path);
 186219            return await _pipeline.SendRequestAsync(request, cancellationToken).ConfigureAwait(false);
 186220        }
 221
 222        public Response GetResponse(RequestMethod method, CancellationToken cancellationToken, params string[] path)
 223        {
 0224            using Request request = CreateRequest(method, path);
 0225            return _pipeline.SendRequest(request, cancellationToken);
 0226        }
 227
 228        private async Task<Response> SendRequestAsync(Request request, CancellationToken cancellationToken)
 229        {
 638230            Response response = await _pipeline.SendRequestAsync(request, cancellationToken).ConfigureAwait(false);
 231
 636232            switch (response.Status)
 233            {
 234                case 200:
 235                case 201:
 236                case 202:
 237                case 204:
 608238                    return response;
 239                default:
 28240                    throw await Diagnostics.CreateRequestFailedExceptionAsync(response).ConfigureAwait(false);
 241            }
 608242        }
 243        private Response SendRequest(Request request, CancellationToken cancellationToken)
 244        {
 634245            Response response = _pipeline.SendRequest(request, cancellationToken);
 246
 632247            switch (response.Status)
 248            {
 249                case 200:
 250                case 201:
 251                case 202:
 252                case 204:
 608253                    return response;
 254                default:
 24255                    throw Diagnostics.CreateRequestFailedException(response);
 256            }
 257        }
 258    }
 259}