< Summary

Class:Azure.Security.KeyVault.KeyVaultPipeline
Assembly:Azure.Security.KeyVault.Secrets
File(s):C:\Git\azure-sdk-for-net\sdk\keyvault\Azure.Security.KeyVault.Shared\src\KeyVaultPipeline.cs
Covered lines:74
Uncovered lines:30
Coverable lines:104
Total lines:259
Line coverage:71.1% (74 of 104)
Covered branches:18
Total branches:20
Branch coverage:90% (18 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(...)-0%100%
CreateFirstPageUri(...)-0%0%
CreateRequest(...)-100%100%
CreateRequest(...)-100%100%
CreateResponse(...)-100%100%
CreateScope(...)-100%100%
GetPageAsync()-76.92%100%
GetPage(...)-76.92%100%
SendRequestAsync()-100%100%
SendRequest(...)-100%100%
SendRequestAsync()-100%100%
SendRequest(...)-100%100%
SendRequestAsync()-0%100%
SendRequest(...)-0%100%
GetResponseAsync()-0%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;
 110415        public ClientDiagnostics Diagnostics { get; }
 16
 17817        public KeyVaultPipeline(Uri vaultUri, string apiVersion, HttpPipeline pipeline, ClientDiagnostics clientDiagnost
 18        {
 17819            VaultUri = vaultUri;
 17820            _pipeline = pipeline;
 21
 17822            Diagnostics = clientDiagnostics;
 23
 17824            ApiVersion = apiVersion;
 17825        }
 26
 104627        public string ApiVersion { get; }
 28
 104629        public Uri VaultUri { get; }
 30
 31        public Uri CreateFirstPageUri(string path)
 32        {
 033            var firstPage = new RequestUriBuilder();
 034            firstPage.Reset(VaultUri);
 35
 036            firstPage.AppendPath(path, escape: false);
 037            firstPage.AppendQuery("api-version", ApiVersion);
 38
 039            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        {
 4460            Request request = _pipeline.CreateRequest();
 61
 4462            request.Headers.Add(HttpHeader.Common.JsonContentType);
 4463            request.Headers.Add(HttpHeader.Common.JsonAccept);
 4464            request.Method = method;
 4465            request.Uri.Reset(uri);
 66
 4467            return request;
 68        }
 69
 70        public Request CreateRequest(RequestMethod method, params string[] path)
 71        {
 103072            Request request = _pipeline.CreateRequest();
 73
 103074            request.Headers.Add(HttpHeader.Common.JsonContentType);
 103075            request.Headers.Add(HttpHeader.Common.JsonAccept);
 103076            request.Method = method;
 103077            request.Uri.Reset(VaultUri);
 78
 668479            foreach (var p in path)
 80            {
 231281                request.Uri.AppendPath(p, escape: false);
 82            }
 83
 103084            request.Uri.AppendQuery("api-version", ApiVersion);
 85
 103086            return request;
 87        }
 88
 89        public Response<T> CreateResponse<T>(Response response, T result)
 90            where T : IJsonDeserializable
 91        {
 99692            result.Deserialize(response.ContentStream);
 99693            return Response.FromValue(result, response);
 94        }
 95
 96        public DiagnosticScope CreateScope(string name)
 97        {
 103098            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        {
 22104            using DiagnosticScope scope = Diagnostics.CreateScope(operationName);
 22105            scope.Start();
 106
 107            try
 108            {
 109                // if we don't have a nextLink specified, use firstPageUri
 22110                if (nextLink != null)
 111                {
 14112                    firstPageUri = new Uri(nextLink);
 113                }
 114
 22115                using Request request = CreateRequest(RequestMethod.Get, firstPageUri);
 22116                Response response = await SendRequestAsync(request, cancellationToken).ConfigureAwait(false);
 117
 118                // read the respose
 22119                KeyVaultPage<T> responseAsPage = new KeyVaultPage<T>(itemFactory);
 22120                responseAsPage.Deserialize(response.ContentStream);
 121
 122                // convert from the Page<T> to PageResponse<T>
 22123                return Page<T>.FromValues(responseAsPage.Items.ToArray(), responseAsPage.NextLink?.ToString(), response)
 124            }
 0125            catch (Exception e)
 126            {
 0127                scope.Failed(e);
 0128                throw;
 129            }
 22130        }
 131
 132        public Page<T> GetPage<T>(Uri firstPageUri, string nextLink, Func<T> itemFactory, string operationName, Cancella
 133            where T : IJsonDeserializable
 134        {
 22135            using DiagnosticScope scope = Diagnostics.CreateScope(operationName);
 22136            scope.Start();
 137
 138            try
 139            {
 140                // if we don't have a nextLink specified, use firstPageUri
 22141                if (nextLink != null)
 142                {
 14143                    firstPageUri = new Uri(nextLink);
 144                }
 145
 22146                using Request request = CreateRequest(RequestMethod.Get, firstPageUri);
 22147                Response response = SendRequest(request, cancellationToken);
 148
 149                // read the respose
 22150                KeyVaultPage<T> responseAsPage = new KeyVaultPage<T>(itemFactory);
 22151                responseAsPage.Deserialize(response.ContentStream);
 152
 153                // convert from the Page<T> to PageResponse<T>
 22154                return Page<T>.FromValues(responseAsPage.Items.ToArray(), responseAsPage.NextLink?.ToString(), response)
 155            }
 0156            catch (Exception e)
 157            {
 0158                scope.Failed(e);
 0159                throw;
 160            }
 22161        }
 162
 163        public async Task<Response<TResult>> SendRequestAsync<TContent, TResult>(RequestMethod method, TContent content,
 164            where TContent : IJsonSerializable
 165            where TResult : IJsonDeserializable
 166        {
 344167            using Request request = CreateRequest(method, path);
 344168            request.Content = RequestContent.Create(content.Serialize());
 169
 344170            Response response = await SendRequestAsync(request, cancellationToken).ConfigureAwait(false);
 171
 342172            return CreateResponse(response, resultFactory());
 342173        }
 174
 175        public Response<TResult> SendRequest<TContent, TResult>(RequestMethod method, TContent content, Func<TResult> re
 176            where TContent : IJsonSerializable
 177            where TResult : IJsonDeserializable
 178        {
 344179            using Request request = CreateRequest(method, path);
 344180            request.Content = RequestContent.Create(content.Serialize());
 181
 344182            Response response = SendRequest(request, cancellationToken);
 183
 342184            return CreateResponse(response, resultFactory());
 342185        }
 186
 187        public async Task<Response<TResult>> SendRequestAsync<TResult>(RequestMethod method, Func<TResult> resultFactory
 188            where TResult : IJsonDeserializable
 189        {
 204190            using Request request = CreateRequest(method, path);
 204191            Response response = await SendRequestAsync(request, cancellationToken).ConfigureAwait(false);
 192
 188193            return CreateResponse(response, resultFactory());
 188194        }
 195
 196        public Response<TResult> SendRequest<TResult>(RequestMethod method, Func<TResult> resultFactory, CancellationTok
 197            where TResult : IJsonDeserializable
 198        {
 138199            using Request request = CreateRequest(method, path);
 138200            Response response = SendRequest(request, cancellationToken);
 201
 124202            return CreateResponse(response, resultFactory());
 124203        }
 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        {
 0218            using Request request = CreateRequest(method, path);
 0219            return await _pipeline.SendRequestAsync(request, cancellationToken).ConfigureAwait(false);
 0220        }
 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        {
 570230            Response response = await _pipeline.SendRequestAsync(request, cancellationToken).ConfigureAwait(false);
 231
 568232            switch (response.Status)
 233            {
 234                case 200:
 235                case 201:
 236                case 202:
 237                case 204:
 552238                    return response;
 239                default:
 16240                    throw await Diagnostics.CreateRequestFailedExceptionAsync(response).ConfigureAwait(false);
 241            }
 552242        }
 243        private Response SendRequest(Request request, CancellationToken cancellationToken)
 244        {
 504245            Response response = _pipeline.SendRequest(request, cancellationToken);
 246
 502247            switch (response.Status)
 248            {
 249                case 200:
 250                case 201:
 251                case 202:
 252                case 204:
 488253                    return response;
 254                default:
 14255                    throw Diagnostics.CreateRequestFailedException(response);
 256            }
 257        }
 258    }
 259}