| | | 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.Attestation |
| | | 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 | | /// PolicyOperations operations. |
| | | 27 | | /// </summary> |
| | | 28 | | internal partial class PolicyOperations : IServiceOperations<AttestationClient>, IPolicyOperations |
| | | 29 | | { |
| | | 30 | | /// <summary> |
| | | 31 | | /// Initializes a new instance of the PolicyOperations 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> |
| | 8 | 39 | | internal PolicyOperations(AttestationClient client) |
| | | 40 | | { |
| | 8 | 41 | | if (client == null) |
| | | 42 | | { |
| | 0 | 43 | | throw new System.ArgumentNullException("client"); |
| | | 44 | | } |
| | 8 | 45 | | Client = client; |
| | 8 | 46 | | } |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Gets a reference to the AttestationClient |
| | | 50 | | /// </summary> |
| | 100 | 51 | | public AttestationClient Client { get; private set; } |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Accepts a new policy document and returns a JWT which expresses used in |
| | | 55 | | /// preparation to set attestation policy. |
| | | 56 | | /// </summary> |
| | | 57 | | /// <param name='tenantBaseUrl'> |
| | | 58 | | /// The tenant name, for example https://mytenant.attest.azure.net. |
| | | 59 | | /// </param> |
| | | 60 | | /// <param name='tee'> |
| | | 61 | | /// Specifies the trusted execution environment to be used to validate the |
| | | 62 | | /// evidence. Possible values include: 'SgxEnclave', 'OpenEnclave', |
| | | 63 | | /// 'CyResComponent', 'VSMEnclave' |
| | | 64 | | /// </param> |
| | | 65 | | /// <param name='policyJws'> |
| | | 66 | | /// JSON Web Signature (See RFC7515) expressing the new policy |
| | | 67 | | /// </param> |
| | | 68 | | /// <param name='customHeaders'> |
| | | 69 | | /// Headers that will be added to request. |
| | | 70 | | /// </param> |
| | | 71 | | /// <param name='cancellationToken'> |
| | | 72 | | /// The cancellation token. |
| | | 73 | | /// </param> |
| | | 74 | | /// <exception cref="CloudException"> |
| | | 75 | | /// Thrown when the operation returned an invalid status code |
| | | 76 | | /// </exception> |
| | | 77 | | /// <exception cref="SerializationException"> |
| | | 78 | | /// Thrown when unable to deserialize the response |
| | | 79 | | /// </exception> |
| | | 80 | | /// <exception cref="ValidationException"> |
| | | 81 | | /// Thrown when a required parameter is null |
| | | 82 | | /// </exception> |
| | | 83 | | /// <exception cref="System.ArgumentNullException"> |
| | | 84 | | /// Thrown when a required parameter is null |
| | | 85 | | /// </exception> |
| | | 86 | | /// <return> |
| | | 87 | | /// A response object containing the response body and response headers. |
| | | 88 | | /// </return> |
| | | 89 | | public async Task<AzureOperationResponse<object>> PrepareToSetWithHttpMessagesAsync(string tenantBaseUrl, string |
| | | 90 | | { |
| | 2 | 91 | | if (tenantBaseUrl == null) |
| | | 92 | | { |
| | 0 | 93 | | throw new ValidationException(ValidationRules.CannotBeNull, "tenantBaseUrl"); |
| | | 94 | | } |
| | 2 | 95 | | if (tee == null) |
| | | 96 | | { |
| | 0 | 97 | | throw new ValidationException(ValidationRules.CannotBeNull, "tee"); |
| | | 98 | | } |
| | 2 | 99 | | if (policyJws == null) |
| | | 100 | | { |
| | 0 | 101 | | throw new ValidationException(ValidationRules.CannotBeNull, "policyJws"); |
| | | 102 | | } |
| | | 103 | | // Tracing |
| | 2 | 104 | | bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| | 2 | 105 | | string _invocationId = null; |
| | 2 | 106 | | if (_shouldTrace) |
| | | 107 | | { |
| | 0 | 108 | | _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| | 0 | 109 | | Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| | 0 | 110 | | tracingParameters.Add("tenantBaseUrl", tenantBaseUrl); |
| | 0 | 111 | | tracingParameters.Add("tee", tee); |
| | 0 | 112 | | tracingParameters.Add("policyJws", policyJws); |
| | 0 | 113 | | tracingParameters.Add("cancellationToken", cancellationToken); |
| | 0 | 114 | | ServiceClientTracing.Enter(_invocationId, this, "PrepareToSet", tracingParameters); |
| | | 115 | | } |
| | | 116 | | // Construct URL |
| | 2 | 117 | | var _baseUrl = Client.BaseUri; |
| | 2 | 118 | | var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "operations/policy/updatepolicy"; |
| | 2 | 119 | | _url = _url.Replace("{tenantBaseUrl}", tenantBaseUrl); |
| | 2 | 120 | | List<string> _queryParameters = new List<string>(); |
| | 2 | 121 | | if (Client.ApiVersion != null) |
| | | 122 | | { |
| | 2 | 123 | | _queryParameters.Add(string.Format("api-version={0}", System.Uri.EscapeDataString(Client.ApiVersion))); |
| | | 124 | | } |
| | 2 | 125 | | if (tee != null) |
| | | 126 | | { |
| | 2 | 127 | | _queryParameters.Add(string.Format("tee={0}", System.Uri.EscapeDataString(tee))); |
| | | 128 | | } |
| | 2 | 129 | | if (_queryParameters.Count > 0) |
| | | 130 | | { |
| | 2 | 131 | | _url += (_url.Contains("?") ? "&" : "?") + string.Join("&", _queryParameters); |
| | | 132 | | } |
| | | 133 | | // Create HTTP transport objects |
| | 2 | 134 | | var _httpRequest = new HttpRequestMessage(); |
| | 2 | 135 | | HttpResponseMessage _httpResponse = null; |
| | 2 | 136 | | _httpRequest.Method = new HttpMethod("POST"); |
| | 2 | 137 | | _httpRequest.RequestUri = new System.Uri(_url); |
| | | 138 | | // Set Headers |
| | 2 | 139 | | if (Client.GenerateClientRequestId != null && Client.GenerateClientRequestId.Value) |
| | | 140 | | { |
| | 2 | 141 | | _httpRequest.Headers.TryAddWithoutValidation("x-ms-client-request-id", System.Guid.NewGuid().ToString()) |
| | | 142 | | } |
| | 2 | 143 | | if (Client.AcceptLanguage != null) |
| | | 144 | | { |
| | 2 | 145 | | if (_httpRequest.Headers.Contains("accept-language")) |
| | | 146 | | { |
| | 0 | 147 | | _httpRequest.Headers.Remove("accept-language"); |
| | | 148 | | } |
| | 2 | 149 | | _httpRequest.Headers.TryAddWithoutValidation("accept-language", Client.AcceptLanguage); |
| | | 150 | | } |
| | | 151 | | |
| | | 152 | | |
| | 2 | 153 | | if (customHeaders != null) |
| | | 154 | | { |
| | 0 | 155 | | foreach(var _header in customHeaders) |
| | | 156 | | { |
| | 0 | 157 | | if (_httpRequest.Headers.Contains(_header.Key)) |
| | | 158 | | { |
| | 0 | 159 | | _httpRequest.Headers.Remove(_header.Key); |
| | | 160 | | } |
| | 0 | 161 | | _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| | | 162 | | } |
| | | 163 | | } |
| | | 164 | | |
| | | 165 | | // Serialize Request |
| | 2 | 166 | | string _requestContent = null; |
| | 2 | 167 | | if(policyJws != null) |
| | | 168 | | { |
| | 2 | 169 | | _requestContent = Rest.Serialization.SafeJsonConvert.SerializeObject(policyJws, Client.SerializationSett |
| | 2 | 170 | | _httpRequest.Content = new StringContent(_requestContent, System.Text.Encoding.UTF8); |
| | 2 | 171 | | _httpRequest.Content.Headers.ContentType =System.Net.Http.Headers.MediaTypeHeaderValue.Parse("text/plain |
| | | 172 | | } |
| | | 173 | | // Set Credentials |
| | 2 | 174 | | if (Client.Credentials != null) |
| | | 175 | | { |
| | 2 | 176 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 2 | 177 | | await Client.Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | | 178 | | } |
| | | 179 | | // Send Request |
| | 2 | 180 | | if (_shouldTrace) |
| | | 181 | | { |
| | 0 | 182 | | ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| | | 183 | | } |
| | 2 | 184 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 2 | 185 | | _httpResponse = await Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 2 | 186 | | if (_shouldTrace) |
| | | 187 | | { |
| | 0 | 188 | | ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| | | 189 | | } |
| | 2 | 190 | | HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| | 2 | 191 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 2 | 192 | | string _responseContent = null; |
| | 2 | 193 | | if ((int)_statusCode != 200 && (int)_statusCode != 400 && (int)_statusCode != 401) |
| | | 194 | | { |
| | 0 | 195 | | var ex = new CloudException(string.Format("Operation returned an invalid status code '{0}'", _statusCode |
| | | 196 | | try |
| | | 197 | | { |
| | 0 | 198 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | 0 | 199 | | CloudError _errorBody = Rest.Serialization.SafeJsonConvert.DeserializeObject<CloudError>(_responseC |
| | 0 | 200 | | if (_errorBody != null) |
| | | 201 | | { |
| | 0 | 202 | | ex = new CloudException(_errorBody.Message); |
| | 0 | 203 | | ex.Body = _errorBody; |
| | | 204 | | } |
| | 0 | 205 | | } |
| | 0 | 206 | | catch (JsonException) |
| | | 207 | | { |
| | | 208 | | // Ignore the exception |
| | 0 | 209 | | } |
| | 0 | 210 | | ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| | 0 | 211 | | ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| | 0 | 212 | | if (_httpResponse.Headers.Contains("x-ms-request-id")) |
| | | 213 | | { |
| | 0 | 214 | | ex.RequestId = _httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault(); |
| | | 215 | | } |
| | 0 | 216 | | if (_shouldTrace) |
| | | 217 | | { |
| | 0 | 218 | | ServiceClientTracing.Error(_invocationId, ex); |
| | | 219 | | } |
| | 0 | 220 | | _httpRequest.Dispose(); |
| | 0 | 221 | | if (_httpResponse != null) |
| | | 222 | | { |
| | 0 | 223 | | _httpResponse.Dispose(); |
| | | 224 | | } |
| | 0 | 225 | | throw ex; |
| | | 226 | | } |
| | | 227 | | // Create Result |
| | 2 | 228 | | var _result = new AzureOperationResponse<object>(); |
| | 2 | 229 | | _result.Request = _httpRequest; |
| | 2 | 230 | | _result.Response = _httpResponse; |
| | 2 | 231 | | if (_httpResponse.Headers.Contains("x-ms-request-id")) |
| | | 232 | | { |
| | 0 | 233 | | _result.RequestId = _httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault(); |
| | | 234 | | } |
| | | 235 | | // Deserialize Response |
| | 2 | 236 | | if ((int)_statusCode == 200) |
| | | 237 | | { |
| | 2 | 238 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | | 239 | | try |
| | | 240 | | { |
| | 2 | 241 | | _result.Body = Rest.Serialization.SafeJsonConvert.DeserializeObject<string>(_responseContent, Client |
| | 2 | 242 | | } |
| | 0 | 243 | | catch (JsonException ex) |
| | | 244 | | { |
| | 0 | 245 | | _httpRequest.Dispose(); |
| | 0 | 246 | | if (_httpResponse != null) |
| | | 247 | | { |
| | 0 | 248 | | _httpResponse.Dispose(); |
| | | 249 | | } |
| | 0 | 250 | | throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); |
| | | 251 | | } |
| | | 252 | | } |
| | | 253 | | // Deserialize Response |
| | 2 | 254 | | if ((int)_statusCode == 400) |
| | | 255 | | { |
| | 0 | 256 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | | 257 | | try |
| | | 258 | | { |
| | 0 | 259 | | _result.Body = Rest.Serialization.SafeJsonConvert.DeserializeObject<CloudError>(_responseContent, Cl |
| | 0 | 260 | | } |
| | 0 | 261 | | catch (JsonException ex) |
| | | 262 | | { |
| | 0 | 263 | | _httpRequest.Dispose(); |
| | 0 | 264 | | if (_httpResponse != null) |
| | | 265 | | { |
| | 0 | 266 | | _httpResponse.Dispose(); |
| | | 267 | | } |
| | 0 | 268 | | throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); |
| | | 269 | | } |
| | | 270 | | } |
| | | 271 | | // Deserialize Response |
| | 2 | 272 | | if ((int)_statusCode == 401) |
| | | 273 | | { |
| | 0 | 274 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | | 275 | | try |
| | | 276 | | { |
| | 0 | 277 | | _result.Body = Rest.Serialization.SafeJsonConvert.DeserializeObject<string>(_responseContent, Client |
| | 0 | 278 | | } |
| | 0 | 279 | | catch (JsonException ex) |
| | | 280 | | { |
| | 0 | 281 | | _httpRequest.Dispose(); |
| | 0 | 282 | | if (_httpResponse != null) |
| | | 283 | | { |
| | 0 | 284 | | _httpResponse.Dispose(); |
| | | 285 | | } |
| | 0 | 286 | | throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); |
| | | 287 | | } |
| | | 288 | | } |
| | 2 | 289 | | if (_shouldTrace) |
| | | 290 | | { |
| | 0 | 291 | | ServiceClientTracing.Exit(_invocationId, _result); |
| | | 292 | | } |
| | 2 | 293 | | return _result; |
| | 2 | 294 | | } |
| | | 295 | | |
| | | 296 | | /// <summary> |
| | | 297 | | /// Retrieves the current policy for a given kind of TEE. |
| | | 298 | | /// </summary> |
| | | 299 | | /// <param name='tenantBaseUrl'> |
| | | 300 | | /// The tenant name, for example https://mytenant.attest.azure.net. |
| | | 301 | | /// </param> |
| | | 302 | | /// <param name='tee'> |
| | | 303 | | /// Specifies the trusted execution environment to be used to validate the |
| | | 304 | | /// evidence. Possible values include: 'SgxEnclave', 'OpenEnclave', |
| | | 305 | | /// 'CyResComponent', 'VSMEnclave' |
| | | 306 | | /// </param> |
| | | 307 | | /// <param name='customHeaders'> |
| | | 308 | | /// Headers that will be added to request. |
| | | 309 | | /// </param> |
| | | 310 | | /// <param name='cancellationToken'> |
| | | 311 | | /// The cancellation token. |
| | | 312 | | /// </param> |
| | | 313 | | /// <exception cref="CloudException"> |
| | | 314 | | /// Thrown when the operation returned an invalid status code |
| | | 315 | | /// </exception> |
| | | 316 | | /// <exception cref="SerializationException"> |
| | | 317 | | /// Thrown when unable to deserialize the response |
| | | 318 | | /// </exception> |
| | | 319 | | /// <exception cref="ValidationException"> |
| | | 320 | | /// Thrown when a required parameter is null |
| | | 321 | | /// </exception> |
| | | 322 | | /// <exception cref="System.ArgumentNullException"> |
| | | 323 | | /// Thrown when a required parameter is null |
| | | 324 | | /// </exception> |
| | | 325 | | /// <return> |
| | | 326 | | /// A response object containing the response body and response headers. |
| | | 327 | | /// </return> |
| | | 328 | | public async Task<AzureOperationResponse<object>> GetWithHttpMessagesAsync(string tenantBaseUrl, string tee, Dic |
| | | 329 | | { |
| | 2 | 330 | | if (tenantBaseUrl == null) |
| | | 331 | | { |
| | 0 | 332 | | throw new ValidationException(ValidationRules.CannotBeNull, "tenantBaseUrl"); |
| | | 333 | | } |
| | 2 | 334 | | if (tee == null) |
| | | 335 | | { |
| | 0 | 336 | | throw new ValidationException(ValidationRules.CannotBeNull, "tee"); |
| | | 337 | | } |
| | | 338 | | // Tracing |
| | 2 | 339 | | bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| | 2 | 340 | | string _invocationId = null; |
| | 2 | 341 | | if (_shouldTrace) |
| | | 342 | | { |
| | 0 | 343 | | _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| | 0 | 344 | | Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| | 0 | 345 | | tracingParameters.Add("tenantBaseUrl", tenantBaseUrl); |
| | 0 | 346 | | tracingParameters.Add("tee", tee); |
| | 0 | 347 | | tracingParameters.Add("cancellationToken", cancellationToken); |
| | 0 | 348 | | ServiceClientTracing.Enter(_invocationId, this, "Get", tracingParameters); |
| | | 349 | | } |
| | | 350 | | // Construct URL |
| | 2 | 351 | | var _baseUrl = Client.BaseUri; |
| | 2 | 352 | | var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "operations/policy/current"; |
| | 2 | 353 | | _url = _url.Replace("{tenantBaseUrl}", tenantBaseUrl); |
| | 2 | 354 | | List<string> _queryParameters = new List<string>(); |
| | 2 | 355 | | if (Client.ApiVersion != null) |
| | | 356 | | { |
| | 2 | 357 | | _queryParameters.Add(string.Format("api-version={0}", System.Uri.EscapeDataString(Client.ApiVersion))); |
| | | 358 | | } |
| | 2 | 359 | | if (tee != null) |
| | | 360 | | { |
| | 2 | 361 | | _queryParameters.Add(string.Format("tee={0}", System.Uri.EscapeDataString(tee))); |
| | | 362 | | } |
| | 2 | 363 | | if (_queryParameters.Count > 0) |
| | | 364 | | { |
| | 2 | 365 | | _url += (_url.Contains("?") ? "&" : "?") + string.Join("&", _queryParameters); |
| | | 366 | | } |
| | | 367 | | // Create HTTP transport objects |
| | 2 | 368 | | var _httpRequest = new HttpRequestMessage(); |
| | 2 | 369 | | HttpResponseMessage _httpResponse = null; |
| | 2 | 370 | | _httpRequest.Method = new HttpMethod("GET"); |
| | 2 | 371 | | _httpRequest.RequestUri = new System.Uri(_url); |
| | | 372 | | // Set Headers |
| | 2 | 373 | | if (Client.GenerateClientRequestId != null && Client.GenerateClientRequestId.Value) |
| | | 374 | | { |
| | 2 | 375 | | _httpRequest.Headers.TryAddWithoutValidation("x-ms-client-request-id", System.Guid.NewGuid().ToString()) |
| | | 376 | | } |
| | 2 | 377 | | if (Client.AcceptLanguage != null) |
| | | 378 | | { |
| | 2 | 379 | | if (_httpRequest.Headers.Contains("accept-language")) |
| | | 380 | | { |
| | 0 | 381 | | _httpRequest.Headers.Remove("accept-language"); |
| | | 382 | | } |
| | 2 | 383 | | _httpRequest.Headers.TryAddWithoutValidation("accept-language", Client.AcceptLanguage); |
| | | 384 | | } |
| | | 385 | | |
| | | 386 | | |
| | 2 | 387 | | if (customHeaders != null) |
| | | 388 | | { |
| | 0 | 389 | | foreach(var _header in customHeaders) |
| | | 390 | | { |
| | 0 | 391 | | if (_httpRequest.Headers.Contains(_header.Key)) |
| | | 392 | | { |
| | 0 | 393 | | _httpRequest.Headers.Remove(_header.Key); |
| | | 394 | | } |
| | 0 | 395 | | _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| | | 396 | | } |
| | | 397 | | } |
| | | 398 | | |
| | | 399 | | // Serialize Request |
| | 2 | 400 | | string _requestContent = null; |
| | | 401 | | // Set Credentials |
| | 2 | 402 | | if (Client.Credentials != null) |
| | | 403 | | { |
| | 2 | 404 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 2 | 405 | | await Client.Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | | 406 | | } |
| | | 407 | | // Send Request |
| | 2 | 408 | | if (_shouldTrace) |
| | | 409 | | { |
| | 0 | 410 | | ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| | | 411 | | } |
| | 2 | 412 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 2 | 413 | | _httpResponse = await Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 2 | 414 | | if (_shouldTrace) |
| | | 415 | | { |
| | 0 | 416 | | ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| | | 417 | | } |
| | 2 | 418 | | HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| | 2 | 419 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 2 | 420 | | string _responseContent = null; |
| | 2 | 421 | | if ((int)_statusCode != 200 && (int)_statusCode != 400 && (int)_statusCode != 401) |
| | | 422 | | { |
| | 0 | 423 | | var ex = new CloudException(string.Format("Operation returned an invalid status code '{0}'", _statusCode |
| | | 424 | | try |
| | | 425 | | { |
| | 0 | 426 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | 0 | 427 | | CloudError _errorBody = Rest.Serialization.SafeJsonConvert.DeserializeObject<CloudError>(_responseC |
| | 0 | 428 | | if (_errorBody != null) |
| | | 429 | | { |
| | 0 | 430 | | ex = new CloudException(_errorBody.Message); |
| | 0 | 431 | | ex.Body = _errorBody; |
| | | 432 | | } |
| | 0 | 433 | | } |
| | 0 | 434 | | catch (JsonException) |
| | | 435 | | { |
| | | 436 | | // Ignore the exception |
| | 0 | 437 | | } |
| | 0 | 438 | | ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| | 0 | 439 | | ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| | 0 | 440 | | if (_httpResponse.Headers.Contains("x-ms-request-id")) |
| | | 441 | | { |
| | 0 | 442 | | ex.RequestId = _httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault(); |
| | | 443 | | } |
| | 0 | 444 | | if (_shouldTrace) |
| | | 445 | | { |
| | 0 | 446 | | ServiceClientTracing.Error(_invocationId, ex); |
| | | 447 | | } |
| | 0 | 448 | | _httpRequest.Dispose(); |
| | 0 | 449 | | if (_httpResponse != null) |
| | | 450 | | { |
| | 0 | 451 | | _httpResponse.Dispose(); |
| | | 452 | | } |
| | 0 | 453 | | throw ex; |
| | | 454 | | } |
| | | 455 | | // Create Result |
| | 2 | 456 | | var _result = new AzureOperationResponse<object>(); |
| | 2 | 457 | | _result.Request = _httpRequest; |
| | 2 | 458 | | _result.Response = _httpResponse; |
| | 2 | 459 | | if (_httpResponse.Headers.Contains("x-ms-request-id")) |
| | | 460 | | { |
| | 0 | 461 | | _result.RequestId = _httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault(); |
| | | 462 | | } |
| | | 463 | | // Deserialize Response |
| | 2 | 464 | | if ((int)_statusCode == 200) |
| | | 465 | | { |
| | 2 | 466 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | | 467 | | try |
| | | 468 | | { |
| | 2 | 469 | | _result.Body = Rest.Serialization.SafeJsonConvert.DeserializeObject<AttestationPolicy>(_responseCont |
| | 2 | 470 | | } |
| | 0 | 471 | | catch (JsonException ex) |
| | | 472 | | { |
| | 0 | 473 | | _httpRequest.Dispose(); |
| | 0 | 474 | | if (_httpResponse != null) |
| | | 475 | | { |
| | 0 | 476 | | _httpResponse.Dispose(); |
| | | 477 | | } |
| | 0 | 478 | | throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); |
| | | 479 | | } |
| | | 480 | | } |
| | | 481 | | // Deserialize Response |
| | 2 | 482 | | if ((int)_statusCode == 400) |
| | | 483 | | { |
| | 0 | 484 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | | 485 | | try |
| | | 486 | | { |
| | 0 | 487 | | _result.Body = Rest.Serialization.SafeJsonConvert.DeserializeObject<CloudError>(_responseContent, Cl |
| | 0 | 488 | | } |
| | 0 | 489 | | catch (JsonException ex) |
| | | 490 | | { |
| | 0 | 491 | | _httpRequest.Dispose(); |
| | 0 | 492 | | if (_httpResponse != null) |
| | | 493 | | { |
| | 0 | 494 | | _httpResponse.Dispose(); |
| | | 495 | | } |
| | 0 | 496 | | throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); |
| | | 497 | | } |
| | | 498 | | } |
| | | 499 | | // Deserialize Response |
| | 2 | 500 | | if ((int)_statusCode == 401) |
| | | 501 | | { |
| | 0 | 502 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | | 503 | | try |
| | | 504 | | { |
| | 0 | 505 | | _result.Body = Rest.Serialization.SafeJsonConvert.DeserializeObject<string>(_responseContent, Client |
| | 0 | 506 | | } |
| | 0 | 507 | | catch (JsonException ex) |
| | | 508 | | { |
| | 0 | 509 | | _httpRequest.Dispose(); |
| | 0 | 510 | | if (_httpResponse != null) |
| | | 511 | | { |
| | 0 | 512 | | _httpResponse.Dispose(); |
| | | 513 | | } |
| | 0 | 514 | | throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); |
| | | 515 | | } |
| | | 516 | | } |
| | 2 | 517 | | if (_shouldTrace) |
| | | 518 | | { |
| | 0 | 519 | | ServiceClientTracing.Exit(_invocationId, _result); |
| | | 520 | | } |
| | 2 | 521 | | return _result; |
| | 2 | 522 | | } |
| | | 523 | | |
| | | 524 | | /// <summary> |
| | | 525 | | /// Sets the policy for a given kind of TEE. |
| | | 526 | | /// </summary> |
| | | 527 | | /// <param name='tenantBaseUrl'> |
| | | 528 | | /// The tenant name, for example https://mytenant.attest.azure.net. |
| | | 529 | | /// </param> |
| | | 530 | | /// <param name='tee'> |
| | | 531 | | /// Specifies the trusted execution environment to be used to validate the |
| | | 532 | | /// evidence. Possible values include: 'SgxEnclave', 'OpenEnclave', |
| | | 533 | | /// 'CyResComponent', 'VSMEnclave' |
| | | 534 | | /// </param> |
| | | 535 | | /// <param name='newAttestationPolicy'> |
| | | 536 | | /// JWT Expressing the new policy |
| | | 537 | | /// </param> |
| | | 538 | | /// <param name='customHeaders'> |
| | | 539 | | /// Headers that will be added to request. |
| | | 540 | | /// </param> |
| | | 541 | | /// <param name='cancellationToken'> |
| | | 542 | | /// The cancellation token. |
| | | 543 | | /// </param> |
| | | 544 | | /// <exception cref="CloudException"> |
| | | 545 | | /// Thrown when the operation returned an invalid status code |
| | | 546 | | /// </exception> |
| | | 547 | | /// <exception cref="SerializationException"> |
| | | 548 | | /// Thrown when unable to deserialize the response |
| | | 549 | | /// </exception> |
| | | 550 | | /// <exception cref="ValidationException"> |
| | | 551 | | /// Thrown when a required parameter is null |
| | | 552 | | /// </exception> |
| | | 553 | | /// <exception cref="System.ArgumentNullException"> |
| | | 554 | | /// Thrown when a required parameter is null |
| | | 555 | | /// </exception> |
| | | 556 | | /// <return> |
| | | 557 | | /// A response object containing the response body and response headers. |
| | | 558 | | /// </return> |
| | | 559 | | public async Task<AzureOperationResponse<object>> SetWithHttpMessagesAsync(string tenantBaseUrl, string tee, str |
| | | 560 | | { |
| | 2 | 561 | | if (tenantBaseUrl == null) |
| | | 562 | | { |
| | 0 | 563 | | throw new ValidationException(ValidationRules.CannotBeNull, "tenantBaseUrl"); |
| | | 564 | | } |
| | 2 | 565 | | if (tee == null) |
| | | 566 | | { |
| | 0 | 567 | | throw new ValidationException(ValidationRules.CannotBeNull, "tee"); |
| | | 568 | | } |
| | 2 | 569 | | if (newAttestationPolicy == null) |
| | | 570 | | { |
| | 0 | 571 | | throw new ValidationException(ValidationRules.CannotBeNull, "newAttestationPolicy"); |
| | | 572 | | } |
| | | 573 | | // Tracing |
| | 2 | 574 | | bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| | 2 | 575 | | string _invocationId = null; |
| | 2 | 576 | | if (_shouldTrace) |
| | | 577 | | { |
| | 0 | 578 | | _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| | 0 | 579 | | Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| | 0 | 580 | | tracingParameters.Add("tenantBaseUrl", tenantBaseUrl); |
| | 0 | 581 | | tracingParameters.Add("tee", tee); |
| | 0 | 582 | | tracingParameters.Add("newAttestationPolicy", newAttestationPolicy); |
| | 0 | 583 | | tracingParameters.Add("cancellationToken", cancellationToken); |
| | 0 | 584 | | ServiceClientTracing.Enter(_invocationId, this, "Set", tracingParameters); |
| | | 585 | | } |
| | | 586 | | // Construct URL |
| | 2 | 587 | | var _baseUrl = Client.BaseUri; |
| | 2 | 588 | | var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "operations/policy/current"; |
| | 2 | 589 | | _url = _url.Replace("{tenantBaseUrl}", tenantBaseUrl); |
| | 2 | 590 | | List<string> _queryParameters = new List<string>(); |
| | 2 | 591 | | if (Client.ApiVersion != null) |
| | | 592 | | { |
| | 2 | 593 | | _queryParameters.Add(string.Format("api-version={0}", System.Uri.EscapeDataString(Client.ApiVersion))); |
| | | 594 | | } |
| | 2 | 595 | | if (tee != null) |
| | | 596 | | { |
| | 2 | 597 | | _queryParameters.Add(string.Format("tee={0}", System.Uri.EscapeDataString(tee))); |
| | | 598 | | } |
| | 2 | 599 | | if (_queryParameters.Count > 0) |
| | | 600 | | { |
| | 2 | 601 | | _url += (_url.Contains("?") ? "&" : "?") + string.Join("&", _queryParameters); |
| | | 602 | | } |
| | | 603 | | // Create HTTP transport objects |
| | 2 | 604 | | var _httpRequest = new HttpRequestMessage(); |
| | 2 | 605 | | HttpResponseMessage _httpResponse = null; |
| | 2 | 606 | | _httpRequest.Method = new HttpMethod("PUT"); |
| | 2 | 607 | | _httpRequest.RequestUri = new System.Uri(_url); |
| | | 608 | | // Set Headers |
| | 2 | 609 | | if (Client.GenerateClientRequestId != null && Client.GenerateClientRequestId.Value) |
| | | 610 | | { |
| | 2 | 611 | | _httpRequest.Headers.TryAddWithoutValidation("x-ms-client-request-id", System.Guid.NewGuid().ToString()) |
| | | 612 | | } |
| | 2 | 613 | | if (Client.AcceptLanguage != null) |
| | | 614 | | { |
| | 2 | 615 | | if (_httpRequest.Headers.Contains("accept-language")) |
| | | 616 | | { |
| | 0 | 617 | | _httpRequest.Headers.Remove("accept-language"); |
| | | 618 | | } |
| | 2 | 619 | | _httpRequest.Headers.TryAddWithoutValidation("accept-language", Client.AcceptLanguage); |
| | | 620 | | } |
| | | 621 | | |
| | | 622 | | |
| | 2 | 623 | | if (customHeaders != null) |
| | | 624 | | { |
| | 0 | 625 | | foreach(var _header in customHeaders) |
| | | 626 | | { |
| | 0 | 627 | | if (_httpRequest.Headers.Contains(_header.Key)) |
| | | 628 | | { |
| | 0 | 629 | | _httpRequest.Headers.Remove(_header.Key); |
| | | 630 | | } |
| | 0 | 631 | | _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| | | 632 | | } |
| | | 633 | | } |
| | | 634 | | |
| | | 635 | | // Serialize Request |
| | 2 | 636 | | string _requestContent = null; |
| | 2 | 637 | | if(newAttestationPolicy != null) |
| | | 638 | | { |
| | 2 | 639 | | _requestContent = Rest.Serialization.SafeJsonConvert.SerializeObject(newAttestationPolicy, Client.Serial |
| | 2 | 640 | | _httpRequest.Content = new StringContent(_requestContent, System.Text.Encoding.UTF8); |
| | 2 | 641 | | _httpRequest.Content.Headers.ContentType =System.Net.Http.Headers.MediaTypeHeaderValue.Parse("text/plain |
| | | 642 | | } |
| | | 643 | | // Set Credentials |
| | 2 | 644 | | if (Client.Credentials != null) |
| | | 645 | | { |
| | 2 | 646 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 2 | 647 | | await Client.Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | | 648 | | } |
| | | 649 | | // Send Request |
| | 2 | 650 | | if (_shouldTrace) |
| | | 651 | | { |
| | 0 | 652 | | ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| | | 653 | | } |
| | 2 | 654 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 2 | 655 | | _httpResponse = await Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 2 | 656 | | if (_shouldTrace) |
| | | 657 | | { |
| | 0 | 658 | | ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| | | 659 | | } |
| | 2 | 660 | | HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| | 2 | 661 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 2 | 662 | | string _responseContent = null; |
| | 2 | 663 | | if ((int)_statusCode != 200 && (int)_statusCode != 400 && (int)_statusCode != 401) |
| | | 664 | | { |
| | 0 | 665 | | var ex = new CloudException(string.Format("Operation returned an invalid status code '{0}'", _statusCode |
| | | 666 | | try |
| | | 667 | | { |
| | 0 | 668 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | 0 | 669 | | CloudError _errorBody = Rest.Serialization.SafeJsonConvert.DeserializeObject<CloudError>(_responseC |
| | 0 | 670 | | if (_errorBody != null) |
| | | 671 | | { |
| | 0 | 672 | | ex = new CloudException(_errorBody.Message); |
| | 0 | 673 | | ex.Body = _errorBody; |
| | | 674 | | } |
| | 0 | 675 | | } |
| | 0 | 676 | | catch (JsonException) |
| | | 677 | | { |
| | | 678 | | // Ignore the exception |
| | 0 | 679 | | } |
| | 0 | 680 | | ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| | 0 | 681 | | ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| | 0 | 682 | | if (_httpResponse.Headers.Contains("x-ms-request-id")) |
| | | 683 | | { |
| | 0 | 684 | | ex.RequestId = _httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault(); |
| | | 685 | | } |
| | 0 | 686 | | if (_shouldTrace) |
| | | 687 | | { |
| | 0 | 688 | | ServiceClientTracing.Error(_invocationId, ex); |
| | | 689 | | } |
| | 0 | 690 | | _httpRequest.Dispose(); |
| | 0 | 691 | | if (_httpResponse != null) |
| | | 692 | | { |
| | 0 | 693 | | _httpResponse.Dispose(); |
| | | 694 | | } |
| | 0 | 695 | | throw ex; |
| | | 696 | | } |
| | | 697 | | // Create Result |
| | 2 | 698 | | var _result = new AzureOperationResponse<object>(); |
| | 2 | 699 | | _result.Request = _httpRequest; |
| | 2 | 700 | | _result.Response = _httpResponse; |
| | 2 | 701 | | if (_httpResponse.Headers.Contains("x-ms-request-id")) |
| | | 702 | | { |
| | 0 | 703 | | _result.RequestId = _httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault(); |
| | | 704 | | } |
| | | 705 | | // Deserialize Response |
| | 2 | 706 | | if ((int)_statusCode == 400) |
| | | 707 | | { |
| | 0 | 708 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | | 709 | | try |
| | | 710 | | { |
| | 0 | 711 | | _result.Body = Rest.Serialization.SafeJsonConvert.DeserializeObject<CloudError>(_responseContent, Cl |
| | 0 | 712 | | } |
| | 0 | 713 | | catch (JsonException ex) |
| | | 714 | | { |
| | 0 | 715 | | _httpRequest.Dispose(); |
| | 0 | 716 | | if (_httpResponse != null) |
| | | 717 | | { |
| | 0 | 718 | | _httpResponse.Dispose(); |
| | | 719 | | } |
| | 0 | 720 | | throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); |
| | | 721 | | } |
| | | 722 | | } |
| | | 723 | | // Deserialize Response |
| | 2 | 724 | | if ((int)_statusCode == 401) |
| | | 725 | | { |
| | 0 | 726 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | | 727 | | try |
| | | 728 | | { |
| | 0 | 729 | | _result.Body = Rest.Serialization.SafeJsonConvert.DeserializeObject<string>(_responseContent, Client |
| | 0 | 730 | | } |
| | 0 | 731 | | catch (JsonException ex) |
| | | 732 | | { |
| | 0 | 733 | | _httpRequest.Dispose(); |
| | 0 | 734 | | if (_httpResponse != null) |
| | | 735 | | { |
| | 0 | 736 | | _httpResponse.Dispose(); |
| | | 737 | | } |
| | 0 | 738 | | throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); |
| | | 739 | | } |
| | | 740 | | } |
| | 2 | 741 | | if (_shouldTrace) |
| | | 742 | | { |
| | 0 | 743 | | ServiceClientTracing.Exit(_invocationId, _result); |
| | | 744 | | } |
| | 2 | 745 | | return _result; |
| | 2 | 746 | | } |
| | | 747 | | |
| | | 748 | | /// <summary> |
| | | 749 | | /// Resets the attestation policy for the specified tenant and reverts to the |
| | | 750 | | /// default policy. |
| | | 751 | | /// </summary> |
| | | 752 | | /// <param name='tenantBaseUrl'> |
| | | 753 | | /// The tenant name, for example https://mytenant.attest.azure.net. |
| | | 754 | | /// </param> |
| | | 755 | | /// <param name='tee'> |
| | | 756 | | /// Specifies the trusted execution environment to be used to validate the |
| | | 757 | | /// evidence. Possible values include: 'SgxEnclave', 'OpenEnclave', |
| | | 758 | | /// 'CyResComponent', 'VSMEnclave' |
| | | 759 | | /// </param> |
| | | 760 | | /// <param name='policyJws'> |
| | | 761 | | /// JSON Web Signature with an empty policy document |
| | | 762 | | /// </param> |
| | | 763 | | /// <param name='customHeaders'> |
| | | 764 | | /// Headers that will be added to request. |
| | | 765 | | /// </param> |
| | | 766 | | /// <param name='cancellationToken'> |
| | | 767 | | /// The cancellation token. |
| | | 768 | | /// </param> |
| | | 769 | | /// <exception cref="CloudException"> |
| | | 770 | | /// Thrown when the operation returned an invalid status code |
| | | 771 | | /// </exception> |
| | | 772 | | /// <exception cref="SerializationException"> |
| | | 773 | | /// Thrown when unable to deserialize the response |
| | | 774 | | /// </exception> |
| | | 775 | | /// <exception cref="ValidationException"> |
| | | 776 | | /// Thrown when a required parameter is null |
| | | 777 | | /// </exception> |
| | | 778 | | /// <exception cref="System.ArgumentNullException"> |
| | | 779 | | /// Thrown when a required parameter is null |
| | | 780 | | /// </exception> |
| | | 781 | | /// <return> |
| | | 782 | | /// A response object containing the response body and response headers. |
| | | 783 | | /// </return> |
| | | 784 | | public async Task<AzureOperationResponse<object>> ResetWithHttpMessagesAsync(string tenantBaseUrl, string tee, s |
| | | 785 | | { |
| | 2 | 786 | | if (tenantBaseUrl == null) |
| | | 787 | | { |
| | 0 | 788 | | throw new ValidationException(ValidationRules.CannotBeNull, "tenantBaseUrl"); |
| | | 789 | | } |
| | 2 | 790 | | if (tee == null) |
| | | 791 | | { |
| | 0 | 792 | | throw new ValidationException(ValidationRules.CannotBeNull, "tee"); |
| | | 793 | | } |
| | 2 | 794 | | if (policyJws == null) |
| | | 795 | | { |
| | 0 | 796 | | throw new ValidationException(ValidationRules.CannotBeNull, "policyJws"); |
| | | 797 | | } |
| | | 798 | | // Tracing |
| | 2 | 799 | | bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| | 2 | 800 | | string _invocationId = null; |
| | 2 | 801 | | if (_shouldTrace) |
| | | 802 | | { |
| | 0 | 803 | | _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| | 0 | 804 | | Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| | 0 | 805 | | tracingParameters.Add("tenantBaseUrl", tenantBaseUrl); |
| | 0 | 806 | | tracingParameters.Add("tee", tee); |
| | 0 | 807 | | tracingParameters.Add("policyJws", policyJws); |
| | 0 | 808 | | tracingParameters.Add("cancellationToken", cancellationToken); |
| | 0 | 809 | | ServiceClientTracing.Enter(_invocationId, this, "Reset", tracingParameters); |
| | | 810 | | } |
| | | 811 | | // Construct URL |
| | 2 | 812 | | var _baseUrl = Client.BaseUri; |
| | 2 | 813 | | var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "operations/policy/current"; |
| | 2 | 814 | | _url = _url.Replace("{tenantBaseUrl}", tenantBaseUrl); |
| | 2 | 815 | | List<string> _queryParameters = new List<string>(); |
| | 2 | 816 | | if (Client.ApiVersion != null) |
| | | 817 | | { |
| | 2 | 818 | | _queryParameters.Add(string.Format("api-version={0}", System.Uri.EscapeDataString(Client.ApiVersion))); |
| | | 819 | | } |
| | 2 | 820 | | if (tee != null) |
| | | 821 | | { |
| | 2 | 822 | | _queryParameters.Add(string.Format("tee={0}", System.Uri.EscapeDataString(tee))); |
| | | 823 | | } |
| | 2 | 824 | | if (_queryParameters.Count > 0) |
| | | 825 | | { |
| | 2 | 826 | | _url += (_url.Contains("?") ? "&" : "?") + string.Join("&", _queryParameters); |
| | | 827 | | } |
| | | 828 | | // Create HTTP transport objects |
| | 2 | 829 | | var _httpRequest = new HttpRequestMessage(); |
| | 2 | 830 | | HttpResponseMessage _httpResponse = null; |
| | 2 | 831 | | _httpRequest.Method = new HttpMethod("POST"); |
| | 2 | 832 | | _httpRequest.RequestUri = new System.Uri(_url); |
| | | 833 | | // Set Headers |
| | 2 | 834 | | if (Client.GenerateClientRequestId != null && Client.GenerateClientRequestId.Value) |
| | | 835 | | { |
| | 2 | 836 | | _httpRequest.Headers.TryAddWithoutValidation("x-ms-client-request-id", System.Guid.NewGuid().ToString()) |
| | | 837 | | } |
| | 2 | 838 | | if (Client.AcceptLanguage != null) |
| | | 839 | | { |
| | 2 | 840 | | if (_httpRequest.Headers.Contains("accept-language")) |
| | | 841 | | { |
| | 0 | 842 | | _httpRequest.Headers.Remove("accept-language"); |
| | | 843 | | } |
| | 2 | 844 | | _httpRequest.Headers.TryAddWithoutValidation("accept-language", Client.AcceptLanguage); |
| | | 845 | | } |
| | | 846 | | |
| | | 847 | | |
| | 2 | 848 | | if (customHeaders != null) |
| | | 849 | | { |
| | 0 | 850 | | foreach(var _header in customHeaders) |
| | | 851 | | { |
| | 0 | 852 | | if (_httpRequest.Headers.Contains(_header.Key)) |
| | | 853 | | { |
| | 0 | 854 | | _httpRequest.Headers.Remove(_header.Key); |
| | | 855 | | } |
| | 0 | 856 | | _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| | | 857 | | } |
| | | 858 | | } |
| | | 859 | | |
| | | 860 | | // Serialize Request |
| | 2 | 861 | | string _requestContent = null; |
| | 2 | 862 | | if(policyJws != null) |
| | | 863 | | { |
| | 2 | 864 | | _requestContent = Rest.Serialization.SafeJsonConvert.SerializeObject(policyJws, Client.SerializationSett |
| | 2 | 865 | | _httpRequest.Content = new StringContent(_requestContent, System.Text.Encoding.UTF8); |
| | 2 | 866 | | _httpRequest.Content.Headers.ContentType =System.Net.Http.Headers.MediaTypeHeaderValue.Parse("text/plain |
| | | 867 | | } |
| | | 868 | | // Set Credentials |
| | 2 | 869 | | if (Client.Credentials != null) |
| | | 870 | | { |
| | 2 | 871 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 2 | 872 | | await Client.Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | | 873 | | } |
| | | 874 | | // Send Request |
| | 2 | 875 | | if (_shouldTrace) |
| | | 876 | | { |
| | 0 | 877 | | ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| | | 878 | | } |
| | 2 | 879 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 2 | 880 | | _httpResponse = await Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 2 | 881 | | if (_shouldTrace) |
| | | 882 | | { |
| | 0 | 883 | | ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| | | 884 | | } |
| | 2 | 885 | | HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| | 2 | 886 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 2 | 887 | | string _responseContent = null; |
| | 2 | 888 | | if ((int)_statusCode != 200 && (int)_statusCode != 400 && (int)_statusCode != 401) |
| | | 889 | | { |
| | 0 | 890 | | var ex = new CloudException(string.Format("Operation returned an invalid status code '{0}'", _statusCode |
| | | 891 | | try |
| | | 892 | | { |
| | 0 | 893 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | 0 | 894 | | CloudError _errorBody = Rest.Serialization.SafeJsonConvert.DeserializeObject<CloudError>(_responseC |
| | 0 | 895 | | if (_errorBody != null) |
| | | 896 | | { |
| | 0 | 897 | | ex = new CloudException(_errorBody.Message); |
| | 0 | 898 | | ex.Body = _errorBody; |
| | | 899 | | } |
| | 0 | 900 | | } |
| | 0 | 901 | | catch (JsonException) |
| | | 902 | | { |
| | | 903 | | // Ignore the exception |
| | 0 | 904 | | } |
| | 0 | 905 | | ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| | 0 | 906 | | ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| | 0 | 907 | | if (_httpResponse.Headers.Contains("x-ms-request-id")) |
| | | 908 | | { |
| | 0 | 909 | | ex.RequestId = _httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault(); |
| | | 910 | | } |
| | 0 | 911 | | if (_shouldTrace) |
| | | 912 | | { |
| | 0 | 913 | | ServiceClientTracing.Error(_invocationId, ex); |
| | | 914 | | } |
| | 0 | 915 | | _httpRequest.Dispose(); |
| | 0 | 916 | | if (_httpResponse != null) |
| | | 917 | | { |
| | 0 | 918 | | _httpResponse.Dispose(); |
| | | 919 | | } |
| | 0 | 920 | | throw ex; |
| | | 921 | | } |
| | | 922 | | // Create Result |
| | 2 | 923 | | var _result = new AzureOperationResponse<object>(); |
| | 2 | 924 | | _result.Request = _httpRequest; |
| | 2 | 925 | | _result.Response = _httpResponse; |
| | 2 | 926 | | if (_httpResponse.Headers.Contains("x-ms-request-id")) |
| | | 927 | | { |
| | 0 | 928 | | _result.RequestId = _httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault(); |
| | | 929 | | } |
| | | 930 | | // Deserialize Response |
| | 2 | 931 | | if ((int)_statusCode == 200) |
| | | 932 | | { |
| | 2 | 933 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | | 934 | | try |
| | | 935 | | { |
| | 2 | 936 | | _result.Body = Rest.Serialization.SafeJsonConvert.DeserializeObject<string>(_responseContent, Client |
| | 2 | 937 | | } |
| | 0 | 938 | | catch (JsonException ex) |
| | | 939 | | { |
| | 0 | 940 | | _httpRequest.Dispose(); |
| | 0 | 941 | | if (_httpResponse != null) |
| | | 942 | | { |
| | 0 | 943 | | _httpResponse.Dispose(); |
| | | 944 | | } |
| | 0 | 945 | | throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); |
| | | 946 | | } |
| | | 947 | | } |
| | | 948 | | // Deserialize Response |
| | 2 | 949 | | if ((int)_statusCode == 400) |
| | | 950 | | { |
| | 0 | 951 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | | 952 | | try |
| | | 953 | | { |
| | 0 | 954 | | _result.Body = Rest.Serialization.SafeJsonConvert.DeserializeObject<CloudError>(_responseContent, Cl |
| | 0 | 955 | | } |
| | 0 | 956 | | catch (JsonException ex) |
| | | 957 | | { |
| | 0 | 958 | | _httpRequest.Dispose(); |
| | 0 | 959 | | if (_httpResponse != null) |
| | | 960 | | { |
| | 0 | 961 | | _httpResponse.Dispose(); |
| | | 962 | | } |
| | 0 | 963 | | throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); |
| | | 964 | | } |
| | | 965 | | } |
| | | 966 | | // Deserialize Response |
| | 2 | 967 | | if ((int)_statusCode == 401) |
| | | 968 | | { |
| | 0 | 969 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | | 970 | | try |
| | | 971 | | { |
| | 0 | 972 | | _result.Body = Rest.Serialization.SafeJsonConvert.DeserializeObject<string>(_responseContent, Client |
| | 0 | 973 | | } |
| | 0 | 974 | | catch (JsonException ex) |
| | | 975 | | { |
| | 0 | 976 | | _httpRequest.Dispose(); |
| | 0 | 977 | | if (_httpResponse != null) |
| | | 978 | | { |
| | 0 | 979 | | _httpResponse.Dispose(); |
| | | 980 | | } |
| | 0 | 981 | | throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); |
| | | 982 | | } |
| | | 983 | | } |
| | 2 | 984 | | if (_shouldTrace) |
| | | 985 | | { |
| | 0 | 986 | | ServiceClientTracing.Exit(_invocationId, _result); |
| | | 987 | | } |
| | 2 | 988 | | return _result; |
| | 2 | 989 | | } |
| | | 990 | | |
| | | 991 | | } |
| | | 992 | | } |