|
| 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.Management.RedisCache |
| 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 | + /// AsyncOperationStatusOperations operations. |
| 27 | + /// </summary> |
| 28 | + internal partial class AsyncOperationStatusOperations : IServiceOperations<RedisManagementClient>, IAsyncOperationStatusOperations |
| 29 | + { |
| 30 | + /// <summary> |
| 31 | + /// Initializes a new instance of the AsyncOperationStatusOperations 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> |
| 39 | + internal AsyncOperationStatusOperations(RedisManagementClient client) |
| 40 | + { |
| 41 | + if (client == null) |
| 42 | + { |
| 43 | + throw new System.ArgumentNullException("client"); |
| 44 | + } |
| 45 | + Client = client; |
| 46 | + } |
| 47 | + |
| 48 | + /// <summary> |
| 49 | + /// Gets a reference to the RedisManagementClient |
| 50 | + /// </summary> |
| 51 | + public RedisManagementClient Client { get; private set; } |
| 52 | + |
| 53 | + /// <summary> |
| 54 | + /// For checking the ongoing status of an operation |
| 55 | + /// </summary> |
| 56 | + /// <param name='location'> |
| 57 | + /// The location at which operation was triggered |
| 58 | + /// </param> |
| 59 | + /// <param name='operationId'> |
| 60 | + /// The ID of asynchronous operation |
| 61 | + /// </param> |
| 62 | + /// <param name='customHeaders'> |
| 63 | + /// Headers that will be added to request. |
| 64 | + /// </param> |
| 65 | + /// <param name='cancellationToken'> |
| 66 | + /// The cancellation token. |
| 67 | + /// </param> |
| 68 | + /// <exception cref="ErrorResponseException"> |
| 69 | + /// Thrown when the operation returned an invalid status code |
| 70 | + /// </exception> |
| 71 | + /// <exception cref="SerializationException"> |
| 72 | + /// Thrown when unable to deserialize the response |
| 73 | + /// </exception> |
| 74 | + /// <exception cref="ValidationException"> |
| 75 | + /// Thrown when a required parameter is null |
| 76 | + /// </exception> |
| 77 | + /// <exception cref="System.ArgumentNullException"> |
| 78 | + /// Thrown when a required parameter is null |
| 79 | + /// </exception> |
| 80 | + /// <return> |
| 81 | + /// A response object containing the response body and response headers. |
| 82 | + /// </return> |
| 83 | + public async Task<AzureOperationResponse<OperationStatus>> GetWithHttpMessagesAsync(string location, string operationId, Dictionary<string, List<string>> customHeaders = null, CancellationToken cancellationToken = default(CancellationToken)) |
| 84 | + { |
| 85 | + if (location == null) |
| 86 | + { |
| 87 | + throw new ValidationException(ValidationRules.CannotBeNull, "location"); |
| 88 | + } |
| 89 | + if (operationId == null) |
| 90 | + { |
| 91 | + throw new ValidationException(ValidationRules.CannotBeNull, "operationId"); |
| 92 | + } |
| 93 | + if (Client.ApiVersion == null) |
| 94 | + { |
| 95 | + throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.ApiVersion"); |
| 96 | + } |
| 97 | + if (Client.SubscriptionId == null) |
| 98 | + { |
| 99 | + throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.SubscriptionId"); |
| 100 | + } |
| 101 | + // Tracing |
| 102 | + bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| 103 | + string _invocationId = null; |
| 104 | + if (_shouldTrace) |
| 105 | + { |
| 106 | + _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| 107 | + Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| 108 | + tracingParameters.Add("location", location); |
| 109 | + tracingParameters.Add("operationId", operationId); |
| 110 | + tracingParameters.Add("cancellationToken", cancellationToken); |
| 111 | + ServiceClientTracing.Enter(_invocationId, this, "Get", tracingParameters); |
| 112 | + } |
| 113 | + // Construct URL |
| 114 | + var _baseUrl = Client.BaseUri.AbsoluteUri; |
| 115 | + var _url = new System.Uri(new System.Uri(_baseUrl + (_baseUrl.EndsWith("/") ? "" : "/")), "subscriptions/{subscriptionId}/providers/Microsoft.Cache/locations/{location}/asyncOperations/{operationId}").ToString(); |
| 116 | + _url = _url.Replace("{location}", System.Uri.EscapeDataString(location)); |
| 117 | + _url = _url.Replace("{operationId}", System.Uri.EscapeDataString(operationId)); |
| 118 | + _url = _url.Replace("{subscriptionId}", System.Uri.EscapeDataString(Client.SubscriptionId)); |
| 119 | + List<string> _queryParameters = new List<string>(); |
| 120 | + if (Client.ApiVersion != null) |
| 121 | + { |
| 122 | + _queryParameters.Add(string.Format("api-version={0}", System.Uri.EscapeDataString(Client.ApiVersion))); |
| 123 | + } |
| 124 | + if (_queryParameters.Count > 0) |
| 125 | + { |
| 126 | + _url += (_url.Contains("?") ? "&" : "?") + string.Join("&", _queryParameters); |
| 127 | + } |
| 128 | + // Create HTTP transport objects |
| 129 | + var _httpRequest = new HttpRequestMessage(); |
| 130 | + HttpResponseMessage _httpResponse = null; |
| 131 | + _httpRequest.Method = new HttpMethod("GET"); |
| 132 | + _httpRequest.RequestUri = new System.Uri(_url); |
| 133 | + // Set Headers |
| 134 | + if (Client.GenerateClientRequestId != null && Client.GenerateClientRequestId.Value) |
| 135 | + { |
| 136 | + _httpRequest.Headers.TryAddWithoutValidation("x-ms-client-request-id", System.Guid.NewGuid().ToString()); |
| 137 | + } |
| 138 | + if (Client.AcceptLanguage != null) |
| 139 | + { |
| 140 | + if (_httpRequest.Headers.Contains("accept-language")) |
| 141 | + { |
| 142 | + _httpRequest.Headers.Remove("accept-language"); |
| 143 | + } |
| 144 | + _httpRequest.Headers.TryAddWithoutValidation("accept-language", Client.AcceptLanguage); |
| 145 | + } |
| 146 | + |
| 147 | + |
| 148 | + if (customHeaders != null) |
| 149 | + { |
| 150 | + foreach(var _header in customHeaders) |
| 151 | + { |
| 152 | + if (_httpRequest.Headers.Contains(_header.Key)) |
| 153 | + { |
| 154 | + _httpRequest.Headers.Remove(_header.Key); |
| 155 | + } |
| 156 | + _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + // Serialize Request |
| 161 | + string _requestContent = null; |
| 162 | + // Set Credentials |
| 163 | + if (Client.Credentials != null) |
| 164 | + { |
| 165 | + cancellationToken.ThrowIfCancellationRequested(); |
| 166 | + await Client.Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| 167 | + } |
| 168 | + // Send Request |
| 169 | + if (_shouldTrace) |
| 170 | + { |
| 171 | + ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| 172 | + } |
| 173 | + cancellationToken.ThrowIfCancellationRequested(); |
| 174 | + _httpResponse = await Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| 175 | + if (_shouldTrace) |
| 176 | + { |
| 177 | + ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| 178 | + } |
| 179 | + HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| 180 | + cancellationToken.ThrowIfCancellationRequested(); |
| 181 | + string _responseContent = null; |
| 182 | + if ((int)_statusCode != 200) |
| 183 | + { |
| 184 | + var ex = new ErrorResponseException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); |
| 185 | + try |
| 186 | + { |
| 187 | + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| 188 | + ErrorResponse _errorBody = Rest.Serialization.SafeJsonConvert.DeserializeObject<ErrorResponse>(_responseContent, Client.DeserializationSettings); |
| 189 | + if (_errorBody != null) |
| 190 | + { |
| 191 | + ex.Body = _errorBody; |
| 192 | + } |
| 193 | + } |
| 194 | + catch (JsonException) |
| 195 | + { |
| 196 | + // Ignore the exception |
| 197 | + } |
| 198 | + ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| 199 | + ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| 200 | + if (_shouldTrace) |
| 201 | + { |
| 202 | + ServiceClientTracing.Error(_invocationId, ex); |
| 203 | + } |
| 204 | + _httpRequest.Dispose(); |
| 205 | + if (_httpResponse != null) |
| 206 | + { |
| 207 | + _httpResponse.Dispose(); |
| 208 | + } |
| 209 | + throw ex; |
| 210 | + } |
| 211 | + // Create Result |
| 212 | + var _result = new AzureOperationResponse<OperationStatus>(); |
| 213 | + _result.Request = _httpRequest; |
| 214 | + _result.Response = _httpResponse; |
| 215 | + if (_httpResponse.Headers.Contains("x-ms-request-id")) |
| 216 | + { |
| 217 | + _result.RequestId = _httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault(); |
| 218 | + } |
| 219 | + // Deserialize Response |
| 220 | + if ((int)_statusCode == 200) |
| 221 | + { |
| 222 | + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| 223 | + try |
| 224 | + { |
| 225 | + _result.Body = Rest.Serialization.SafeJsonConvert.DeserializeObject<OperationStatus>(_responseContent, Client.DeserializationSettings); |
| 226 | + } |
| 227 | + catch (JsonException ex) |
| 228 | + { |
| 229 | + _httpRequest.Dispose(); |
| 230 | + if (_httpResponse != null) |
| 231 | + { |
| 232 | + _httpResponse.Dispose(); |
| 233 | + } |
| 234 | + throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); |
| 235 | + } |
| 236 | + } |
| 237 | + if (_shouldTrace) |
| 238 | + { |
| 239 | + ServiceClientTracing.Exit(_invocationId, _result); |
| 240 | + } |
| 241 | + return _result; |
| 242 | + } |
| 243 | + |
| 244 | + } |
| 245 | +} |
0 commit comments