< Summary

Class:Microsoft.Azure.ContainerRegistry.RefreshTokensOperations
Assembly:Microsoft.Azure.ContainerRegistry
File(s):C:\Git\azure-sdk-for-net\sdk\containerregistry\Microsoft.Azure.ContainerRegistry\src\Generated\RefreshTokensOperations.cs
Covered lines:60
Uncovered lines:46
Coverable lines:106
Total lines:274
Line coverage:56.6% (60 of 106)
Covered branches:32
Total branches:62
Branch coverage:51.6% (32 of 62)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.ctor(...)-80%50%
get_Client()-100%100%
GetFromExchangeWithHttpMessagesAsync()-55%51.67%

File(s)

C:\Git\azure-sdk-for-net\sdk\containerregistry\Microsoft.Azure.ContainerRegistry\src\Generated\RefreshTokensOperations.cs

#LineLine coverage
 1// <auto-generated>
 2// Copyright (c) Microsoft Corporation. All rights reserved.
 3// Licensed under the MIT License. See License.txt in the project root for
 4// license information.
 5//
 6// Code generated by Microsoft (R) AutoRest Code Generator.
 7// Changes may cause incorrect behavior and will be lost if the code is
 8// regenerated.
 9// </auto-generated>
 10
 11namespace Microsoft.Azure.ContainerRegistry
 12{
 13    using Microsoft.Rest;
 14    using Microsoft.Rest.Azure;
 15    using Models;
 16    using Newtonsoft.Json;
 17    using System.Collections;
 18    using System.Collections.Generic;
 19    using System.Linq;
 20    using System.Net;
 21    using System.Net.Http;
 22    using System.Threading;
 23    using System.Threading.Tasks;
 24
 25    /// <summary>
 26    /// RefreshTokensOperations operations.
 27    /// </summary>
 28    internal partial class RefreshTokensOperations : IServiceOperations<AzureContainerRegistryClient>, IRefreshTokensOpe
 29    {
 30        /// <summary>
 31        /// Initializes a new instance of the RefreshTokensOperations class.
 32        /// </summary>
 33        /// <param name='client'>
 34        /// Reference to the service client.
 35        /// </param>
 36        /// <exception cref="System.ArgumentNullException">
 37        /// Thrown when a required parameter is null
 38        /// </exception>
 6039        internal RefreshTokensOperations(AzureContainerRegistryClient client)
 40        {
 6041            if (client == null)
 42            {
 043                throw new System.ArgumentNullException("client");
 44            }
 6045            Client = client;
 6046        }
 47
 48        /// <summary>
 49        /// Gets a reference to the AzureContainerRegistryClient
 50        /// </summary>
 10451        public AzureContainerRegistryClient Client { get; private set; }
 52
 53        /// <summary>
 54        /// Exchange AAD tokens for an ACR refresh Token
 55        /// </summary>
 56        /// <param name='grantType'>
 57        /// Can take a value of access_token_refresh_token, or access_token, or
 58        /// refresh_token. Possible values include: 'access_token_refresh_token',
 59        /// 'access_token', 'refresh_token'
 60        /// </param>
 61        /// <param name='service'>
 62        /// Indicates the name of your Azure container registry.
 63        /// </param>
 64        /// <param name='tenant'>
 65        /// AAD tenant associated to the AAD credentials.
 66        /// </param>
 67        /// <param name='refreshToken'>
 68        /// AAD refresh token, mandatory when grant_type is access_token_refresh_token
 69        /// or refresh_token
 70        /// </param>
 71        /// <param name='accessToken'>
 72        /// AAD access token, mandatory when grant_type is access_token_refresh_token
 73        /// or access_token.
 74        /// </param>
 75        /// <param name='customHeaders'>
 76        /// Headers that will be added to request.
 77        /// </param>
 78        /// <param name='cancellationToken'>
 79        /// The cancellation token.
 80        /// </param>
 81        /// <exception cref="AcrErrorsException">
 82        /// Thrown when the operation returned an invalid status code
 83        /// </exception>
 84        /// <exception cref="SerializationException">
 85        /// Thrown when unable to deserialize the response
 86        /// </exception>
 87        /// <exception cref="ValidationException">
 88        /// Thrown when a required parameter is null
 89        /// </exception>
 90        /// <exception cref="System.ArgumentNullException">
 91        /// Thrown when a required parameter is null
 92        /// </exception>
 93        /// <return>
 94        /// A response object containing the response body and response headers.
 95        /// </return>
 96        public async Task<AzureOperationResponse<RefreshToken>> GetFromExchangeWithHttpMessagesAsync(string grantType, s
 97        {
 498            if (Client.LoginUri == null)
 99            {
 0100                throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.LoginUri");
 101            }
 4102            if (grantType == null)
 103            {
 0104                throw new ValidationException(ValidationRules.CannotBeNull, "grantType");
 105            }
 4106            if (service == null)
 107            {
 0108                throw new ValidationException(ValidationRules.CannotBeNull, "service");
 109            }
 110            // Tracing
 4111            bool _shouldTrace = ServiceClientTracing.IsEnabled;
 4112            string _invocationId = null;
 4113            if (_shouldTrace)
 114            {
 0115                _invocationId = ServiceClientTracing.NextInvocationId.ToString();
 0116                Dictionary<string, object> tracingParameters = new Dictionary<string, object>();
 0117                tracingParameters.Add("grantType", grantType);
 0118                tracingParameters.Add("service", service);
 0119                tracingParameters.Add("tenant", tenant);
 0120                tracingParameters.Add("refreshToken", refreshToken);
 0121                tracingParameters.Add("accessToken", accessToken);
 0122                tracingParameters.Add("cancellationToken", cancellationToken);
 0123                ServiceClientTracing.Enter(_invocationId, this, "GetFromExchange", tracingParameters);
 124            }
 125            // Construct URL
 4126            var _baseUrl = Client.BaseUri;
 4127            var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "oauth2/exchange";
 4128            _url = _url.Replace("{url}", Client.LoginUri);
 4129            List<string> _queryParameters = new List<string>();
 4130            if (_queryParameters.Count > 0)
 131            {
 0132                _url += (_url.Contains("?") ? "&" : "?") + string.Join("&", _queryParameters);
 133            }
 134            // Create HTTP transport objects
 4135            var _httpRequest = new HttpRequestMessage();
 4136            HttpResponseMessage _httpResponse = null;
 4137            _httpRequest.Method = new HttpMethod("POST");
 4138            _httpRequest.RequestUri = new System.Uri(_url);
 139            // Set Headers
 4140            if (Client.GenerateClientRequestId != null && Client.GenerateClientRequestId.Value)
 141            {
 4142                _httpRequest.Headers.TryAddWithoutValidation("x-ms-client-request-id", System.Guid.NewGuid().ToString())
 143            }
 4144            if (Client.AcceptLanguage != null)
 145            {
 4146                if (_httpRequest.Headers.Contains("accept-language"))
 147                {
 0148                    _httpRequest.Headers.Remove("accept-language");
 149                }
 4150                _httpRequest.Headers.TryAddWithoutValidation("accept-language", Client.AcceptLanguage);
 151            }
 152
 153
 4154            if (customHeaders != null)
 155            {
 0156                foreach(var _header in customHeaders)
 157                {
 0158                    if (_httpRequest.Headers.Contains(_header.Key))
 159                    {
 0160                        _httpRequest.Headers.Remove(_header.Key);
 161                    }
 0162                    _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value);
 163                }
 164            }
 165
 166            // Serialize Request
 4167            string _requestContent = null;
 4168            var values = new List<KeyValuePair<string, string>>();
 4169            if(grantType != null)
 170            {
 4171                values.Add(new KeyValuePair<string,string>("grant_type", grantType));
 172            }
 4173            if(service != null)
 174            {
 4175                values.Add(new KeyValuePair<string,string>("service", service));
 176            }
 4177            if(tenant != null)
 178            {
 0179                values.Add(new KeyValuePair<string,string>("tenant", tenant));
 180            }
 4181            if(refreshToken != null)
 182            {
 0183                values.Add(new KeyValuePair<string,string>("refresh_token", refreshToken));
 184            }
 4185            if(accessToken != null)
 186            {
 4187                values.Add(new KeyValuePair<string,string>("access_token", accessToken));
 188            }
 4189            var _formContent = new FormUrlEncodedContent(values);
 4190            _httpRequest.Content = _formContent;
 191            // Set Credentials
 4192            if (Client.Credentials != null)
 193            {
 4194                cancellationToken.ThrowIfCancellationRequested();
 4195                await Client.Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false);
 196            }
 197            // Send Request
 4198            if (_shouldTrace)
 199            {
 0200                ServiceClientTracing.SendRequest(_invocationId, _httpRequest);
 201            }
 4202            cancellationToken.ThrowIfCancellationRequested();
 4203            _httpResponse = await Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false);
 4204            if (_shouldTrace)
 205            {
 0206                ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse);
 207            }
 4208            HttpStatusCode _statusCode = _httpResponse.StatusCode;
 4209            cancellationToken.ThrowIfCancellationRequested();
 4210            string _responseContent = null;
 4211            if ((int)_statusCode != 200)
 212            {
 0213                var ex = new AcrErrorsException(string.Format("Operation returned an invalid status code '{0}'", _status
 214                try
 215                {
 0216                    _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
 0217                    AcrErrors _errorBody =  Rest.Serialization.SafeJsonConvert.DeserializeObject<AcrErrors>(_responseCon
 0218                    if (_errorBody != null)
 219                    {
 0220                        ex.Body = _errorBody;
 221                    }
 0222                }
 0223                catch (JsonException)
 224                {
 225                    // Ignore the exception
 0226                }
 0227                ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent);
 0228                ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent);
 0229                if (_shouldTrace)
 230                {
 0231                    ServiceClientTracing.Error(_invocationId, ex);
 232                }
 0233                _httpRequest.Dispose();
 0234                if (_httpResponse != null)
 235                {
 0236                    _httpResponse.Dispose();
 237                }
 0238                throw ex;
 239            }
 240            // Create Result
 4241            var _result = new AzureOperationResponse<RefreshToken>();
 4242            _result.Request = _httpRequest;
 4243            _result.Response = _httpResponse;
 4244            if (_httpResponse.Headers.Contains("x-ms-request-id"))
 245            {
 0246                _result.RequestId = _httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault();
 247            }
 248            // Deserialize Response
 4249            if ((int)_statusCode == 200)
 250            {
 4251                _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
 252                try
 253                {
 4254                    _result.Body = Rest.Serialization.SafeJsonConvert.DeserializeObject<RefreshToken>(_responseContent, 
 4255                }
 0256                catch (JsonException ex)
 257                {
 0258                    _httpRequest.Dispose();
 0259                    if (_httpResponse != null)
 260                    {
 0261                        _httpResponse.Dispose();
 262                    }
 0263                    throw new SerializationException("Unable to deserialize the response.", _responseContent, ex);
 264                }
 265            }
 4266            if (_shouldTrace)
 267            {
 0268                ServiceClientTracing.Exit(_invocationId, _result);
 269            }
 4270            return _result;
 4271        }
 272
 273    }
 274}