| | 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 | |
|
| | 11 | | namespace 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> |
| 60 | 39 | | internal RefreshTokensOperations(AzureContainerRegistryClient client) |
| | 40 | | { |
| 60 | 41 | | if (client == null) |
| | 42 | | { |
| 0 | 43 | | throw new System.ArgumentNullException("client"); |
| | 44 | | } |
| 60 | 45 | | Client = client; |
| 60 | 46 | | } |
| | 47 | |
|
| | 48 | | /// <summary> |
| | 49 | | /// Gets a reference to the AzureContainerRegistryClient |
| | 50 | | /// </summary> |
| 104 | 51 | | 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 | | { |
| 4 | 98 | | if (Client.LoginUri == null) |
| | 99 | | { |
| 0 | 100 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.LoginUri"); |
| | 101 | | } |
| 4 | 102 | | if (grantType == null) |
| | 103 | | { |
| 0 | 104 | | throw new ValidationException(ValidationRules.CannotBeNull, "grantType"); |
| | 105 | | } |
| 4 | 106 | | if (service == null) |
| | 107 | | { |
| 0 | 108 | | throw new ValidationException(ValidationRules.CannotBeNull, "service"); |
| | 109 | | } |
| | 110 | | // Tracing |
| 4 | 111 | | bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| 4 | 112 | | string _invocationId = null; |
| 4 | 113 | | if (_shouldTrace) |
| | 114 | | { |
| 0 | 115 | | _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| 0 | 116 | | Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| 0 | 117 | | tracingParameters.Add("grantType", grantType); |
| 0 | 118 | | tracingParameters.Add("service", service); |
| 0 | 119 | | tracingParameters.Add("tenant", tenant); |
| 0 | 120 | | tracingParameters.Add("refreshToken", refreshToken); |
| 0 | 121 | | tracingParameters.Add("accessToken", accessToken); |
| 0 | 122 | | tracingParameters.Add("cancellationToken", cancellationToken); |
| 0 | 123 | | ServiceClientTracing.Enter(_invocationId, this, "GetFromExchange", tracingParameters); |
| | 124 | | } |
| | 125 | | // Construct URL |
| 4 | 126 | | var _baseUrl = Client.BaseUri; |
| 4 | 127 | | var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "oauth2/exchange"; |
| 4 | 128 | | _url = _url.Replace("{url}", Client.LoginUri); |
| 4 | 129 | | List<string> _queryParameters = new List<string>(); |
| 4 | 130 | | if (_queryParameters.Count > 0) |
| | 131 | | { |
| 0 | 132 | | _url += (_url.Contains("?") ? "&" : "?") + string.Join("&", _queryParameters); |
| | 133 | | } |
| | 134 | | // Create HTTP transport objects |
| 4 | 135 | | var _httpRequest = new HttpRequestMessage(); |
| 4 | 136 | | HttpResponseMessage _httpResponse = null; |
| 4 | 137 | | _httpRequest.Method = new HttpMethod("POST"); |
| 4 | 138 | | _httpRequest.RequestUri = new System.Uri(_url); |
| | 139 | | // Set Headers |
| 4 | 140 | | if (Client.GenerateClientRequestId != null && Client.GenerateClientRequestId.Value) |
| | 141 | | { |
| 4 | 142 | | _httpRequest.Headers.TryAddWithoutValidation("x-ms-client-request-id", System.Guid.NewGuid().ToString()) |
| | 143 | | } |
| 4 | 144 | | if (Client.AcceptLanguage != null) |
| | 145 | | { |
| 4 | 146 | | if (_httpRequest.Headers.Contains("accept-language")) |
| | 147 | | { |
| 0 | 148 | | _httpRequest.Headers.Remove("accept-language"); |
| | 149 | | } |
| 4 | 150 | | _httpRequest.Headers.TryAddWithoutValidation("accept-language", Client.AcceptLanguage); |
| | 151 | | } |
| | 152 | |
|
| | 153 | |
|
| 4 | 154 | | if (customHeaders != null) |
| | 155 | | { |
| 0 | 156 | | foreach(var _header in customHeaders) |
| | 157 | | { |
| 0 | 158 | | if (_httpRequest.Headers.Contains(_header.Key)) |
| | 159 | | { |
| 0 | 160 | | _httpRequest.Headers.Remove(_header.Key); |
| | 161 | | } |
| 0 | 162 | | _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| | 163 | | } |
| | 164 | | } |
| | 165 | |
|
| | 166 | | // Serialize Request |
| 4 | 167 | | string _requestContent = null; |
| 4 | 168 | | var values = new List<KeyValuePair<string, string>>(); |
| 4 | 169 | | if(grantType != null) |
| | 170 | | { |
| 4 | 171 | | values.Add(new KeyValuePair<string,string>("grant_type", grantType)); |
| | 172 | | } |
| 4 | 173 | | if(service != null) |
| | 174 | | { |
| 4 | 175 | | values.Add(new KeyValuePair<string,string>("service", service)); |
| | 176 | | } |
| 4 | 177 | | if(tenant != null) |
| | 178 | | { |
| 0 | 179 | | values.Add(new KeyValuePair<string,string>("tenant", tenant)); |
| | 180 | | } |
| 4 | 181 | | if(refreshToken != null) |
| | 182 | | { |
| 0 | 183 | | values.Add(new KeyValuePair<string,string>("refresh_token", refreshToken)); |
| | 184 | | } |
| 4 | 185 | | if(accessToken != null) |
| | 186 | | { |
| 4 | 187 | | values.Add(new KeyValuePair<string,string>("access_token", accessToken)); |
| | 188 | | } |
| 4 | 189 | | var _formContent = new FormUrlEncodedContent(values); |
| 4 | 190 | | _httpRequest.Content = _formContent; |
| | 191 | | // Set Credentials |
| 4 | 192 | | if (Client.Credentials != null) |
| | 193 | | { |
| 4 | 194 | | cancellationToken.ThrowIfCancellationRequested(); |
| 4 | 195 | | await Client.Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 196 | | } |
| | 197 | | // Send Request |
| 4 | 198 | | if (_shouldTrace) |
| | 199 | | { |
| 0 | 200 | | ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| | 201 | | } |
| 4 | 202 | | cancellationToken.ThrowIfCancellationRequested(); |
| 4 | 203 | | _httpResponse = await Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| 4 | 204 | | if (_shouldTrace) |
| | 205 | | { |
| 0 | 206 | | ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| | 207 | | } |
| 4 | 208 | | HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| 4 | 209 | | cancellationToken.ThrowIfCancellationRequested(); |
| 4 | 210 | | string _responseContent = null; |
| 4 | 211 | | if ((int)_statusCode != 200) |
| | 212 | | { |
| 0 | 213 | | var ex = new AcrErrorsException(string.Format("Operation returned an invalid status code '{0}'", _status |
| | 214 | | try |
| | 215 | | { |
| 0 | 216 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| 0 | 217 | | AcrErrors _errorBody = Rest.Serialization.SafeJsonConvert.DeserializeObject<AcrErrors>(_responseCon |
| 0 | 218 | | if (_errorBody != null) |
| | 219 | | { |
| 0 | 220 | | ex.Body = _errorBody; |
| | 221 | | } |
| 0 | 222 | | } |
| 0 | 223 | | catch (JsonException) |
| | 224 | | { |
| | 225 | | // Ignore the exception |
| 0 | 226 | | } |
| 0 | 227 | | ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| 0 | 228 | | ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| 0 | 229 | | if (_shouldTrace) |
| | 230 | | { |
| 0 | 231 | | ServiceClientTracing.Error(_invocationId, ex); |
| | 232 | | } |
| 0 | 233 | | _httpRequest.Dispose(); |
| 0 | 234 | | if (_httpResponse != null) |
| | 235 | | { |
| 0 | 236 | | _httpResponse.Dispose(); |
| | 237 | | } |
| 0 | 238 | | throw ex; |
| | 239 | | } |
| | 240 | | // Create Result |
| 4 | 241 | | var _result = new AzureOperationResponse<RefreshToken>(); |
| 4 | 242 | | _result.Request = _httpRequest; |
| 4 | 243 | | _result.Response = _httpResponse; |
| 4 | 244 | | if (_httpResponse.Headers.Contains("x-ms-request-id")) |
| | 245 | | { |
| 0 | 246 | | _result.RequestId = _httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault(); |
| | 247 | | } |
| | 248 | | // Deserialize Response |
| 4 | 249 | | if ((int)_statusCode == 200) |
| | 250 | | { |
| 4 | 251 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | 252 | | try |
| | 253 | | { |
| 4 | 254 | | _result.Body = Rest.Serialization.SafeJsonConvert.DeserializeObject<RefreshToken>(_responseContent, |
| 4 | 255 | | } |
| 0 | 256 | | catch (JsonException ex) |
| | 257 | | { |
| 0 | 258 | | _httpRequest.Dispose(); |
| 0 | 259 | | if (_httpResponse != null) |
| | 260 | | { |
| 0 | 261 | | _httpResponse.Dispose(); |
| | 262 | | } |
| 0 | 263 | | throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); |
| | 264 | | } |
| | 265 | | } |
| 4 | 266 | | if (_shouldTrace) |
| | 267 | | { |
| 0 | 268 | | ServiceClientTracing.Exit(_invocationId, _result); |
| | 269 | | } |
| 4 | 270 | | return _result; |
| 4 | 271 | | } |
| | 272 | |
|
| | 273 | | } |
| | 274 | | } |