PageRenderTime 60ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/src/MediaServicesManagement/Generated/AccountOperations.cs

https://github.com/edumunoz/azure-sdk-for-net
C# | 970 lines | 730 code | 80 blank | 160 comment | 192 complexity | fbf1656eedd062137b87bf64d08b3545 MD5 | raw file
  1. //
  2. // Copyright (c) Microsoft and contributors. All rights reserved.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. //
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. // Warning: This code was generated by a tool.
  17. //
  18. // Changes to this file may cause incorrect behavior and will be lost if the
  19. // code is regenerated.
  20. using System;
  21. using System.Collections.Generic;
  22. using System.Linq;
  23. using System.Net;
  24. using System.Net.Http;
  25. using System.Net.Http.Headers;
  26. using System.Text;
  27. using System.Threading;
  28. using System.Threading.Tasks;
  29. using System.Xml.Linq;
  30. using Hyak.Common;
  31. using Hyak.Common.Internals;
  32. using Microsoft.Azure;
  33. using Microsoft.WindowsAzure.Management.MediaServices;
  34. using Microsoft.WindowsAzure.Management.MediaServices.Models;
  35. using Newtonsoft.Json.Linq;
  36. namespace Microsoft.WindowsAzure.Management.MediaServices
  37. {
  38. internal partial class AccountOperations : IServiceOperations<MediaServicesManagementClient>, IAccountOperations
  39. {
  40. /// <summary>
  41. /// Initializes a new instance of the AccountOperations class.
  42. /// </summary>
  43. /// <param name='client'>
  44. /// Reference to the service client.
  45. /// </param>
  46. internal AccountOperations(MediaServicesManagementClient client)
  47. {
  48. this._client = client;
  49. }
  50. private MediaServicesManagementClient _client;
  51. /// <summary>
  52. /// Gets a reference to the
  53. /// Microsoft.WindowsAzure.Management.MediaServices.MediaServicesManagementClient.
  54. /// </summary>
  55. public MediaServicesManagementClient Client
  56. {
  57. get { return this._client; }
  58. }
  59. /// <summary>
  60. /// The Create Media Services Account operation creates a new media
  61. /// services account in Windows Azure. (see
  62. /// http://msdn.microsoft.com/en-us/library/windowsazure/dn194267.aspx
  63. /// for more information)
  64. /// </summary>
  65. /// <param name='parameters'>
  66. /// Required. Parameters supplied to the Create Media Services Account
  67. /// operation.
  68. /// </param>
  69. /// <param name='cancellationToken'>
  70. /// Cancellation token.
  71. /// </param>
  72. /// <returns>
  73. /// The Create Media Services Account operation response.
  74. /// </returns>
  75. public async Task<MediaServicesAccountCreateResponse> CreateAsync(MediaServicesAccountCreateParameters parameters, CancellationToken cancellationToken)
  76. {
  77. // Validate
  78. if (parameters == null)
  79. {
  80. throw new ArgumentNullException("parameters");
  81. }
  82. if (parameters.AccountName == null)
  83. {
  84. throw new ArgumentNullException("parameters.AccountName");
  85. }
  86. if (parameters.AccountName.Length < 3)
  87. {
  88. throw new ArgumentOutOfRangeException("parameters.AccountName");
  89. }
  90. if (parameters.AccountName.Length > 24)
  91. {
  92. throw new ArgumentOutOfRangeException("parameters.AccountName");
  93. }
  94. if (parameters.BlobStorageEndpointUri == null)
  95. {
  96. throw new ArgumentNullException("parameters.BlobStorageEndpointUri");
  97. }
  98. if (parameters.Region == null)
  99. {
  100. throw new ArgumentNullException("parameters.Region");
  101. }
  102. if (parameters.Region.Length < 3)
  103. {
  104. throw new ArgumentOutOfRangeException("parameters.Region");
  105. }
  106. if (parameters.Region.Length > 256)
  107. {
  108. throw new ArgumentOutOfRangeException("parameters.Region");
  109. }
  110. if (parameters.StorageAccountKey == null)
  111. {
  112. throw new ArgumentNullException("parameters.StorageAccountKey");
  113. }
  114. if (parameters.StorageAccountKey.Length < 14)
  115. {
  116. throw new ArgumentOutOfRangeException("parameters.StorageAccountKey");
  117. }
  118. if (parameters.StorageAccountKey.Length > 256)
  119. {
  120. throw new ArgumentOutOfRangeException("parameters.StorageAccountKey");
  121. }
  122. if (parameters.StorageAccountName == null)
  123. {
  124. throw new ArgumentNullException("parameters.StorageAccountName");
  125. }
  126. if (parameters.StorageAccountName.Length < 3)
  127. {
  128. throw new ArgumentOutOfRangeException("parameters.StorageAccountName");
  129. }
  130. if (parameters.StorageAccountName.Length > 24)
  131. {
  132. throw new ArgumentOutOfRangeException("parameters.StorageAccountName");
  133. }
  134. foreach (char storageAccountNameChar in parameters.StorageAccountName)
  135. {
  136. if (char.IsLower(storageAccountNameChar) == false && char.IsDigit(storageAccountNameChar) == false)
  137. {
  138. throw new ArgumentOutOfRangeException("parameters.StorageAccountName");
  139. }
  140. }
  141. // Tracing
  142. bool shouldTrace = TracingAdapter.IsEnabled;
  143. string invocationId = null;
  144. if (shouldTrace)
  145. {
  146. invocationId = TracingAdapter.NextInvocationId.ToString();
  147. Dictionary<string, object> tracingParameters = new Dictionary<string, object>();
  148. tracingParameters.Add("parameters", parameters);
  149. TracingAdapter.Enter(invocationId, this, "CreateAsync", tracingParameters);
  150. }
  151. // Construct URL
  152. string url = "";
  153. url = url + "/";
  154. if (this.Client.Credentials.SubscriptionId != null)
  155. {
  156. url = url + Uri.EscapeDataString(this.Client.Credentials.SubscriptionId);
  157. }
  158. url = url + "/services/mediaservices/Accounts";
  159. string baseUrl = this.Client.BaseUri.AbsoluteUri;
  160. // Trim '/' character from the end of baseUrl and beginning of url.
  161. if (baseUrl[baseUrl.Length - 1] == '/')
  162. {
  163. baseUrl = baseUrl.Substring(0, baseUrl.Length - 1);
  164. }
  165. if (url[0] == '/')
  166. {
  167. url = url.Substring(1);
  168. }
  169. url = baseUrl + "/" + url;
  170. url = url.Replace(" ", "%20");
  171. // Create HTTP transport objects
  172. HttpRequestMessage httpRequest = null;
  173. try
  174. {
  175. httpRequest = new HttpRequestMessage();
  176. httpRequest.Method = HttpMethod.Post;
  177. httpRequest.RequestUri = new Uri(url);
  178. // Set Headers
  179. httpRequest.Headers.Add("x-ms-version", "2011-10-01");
  180. // Set Credentials
  181. cancellationToken.ThrowIfCancellationRequested();
  182. await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false);
  183. // Serialize Request
  184. string requestContent = null;
  185. XDocument requestDoc = new XDocument();
  186. XElement accountCreationRequestElement = new XElement(XName.Get("AccountCreationRequest", "http://schemas.datacontract.org/2004/07/Microsoft.Cloud.Media.Management.ResourceProvider.Models"));
  187. requestDoc.Add(accountCreationRequestElement);
  188. XElement accountNameElement = new XElement(XName.Get("AccountName", "http://schemas.datacontract.org/2004/07/Microsoft.Cloud.Media.Management.ResourceProvider.Models"));
  189. accountNameElement.Value = parameters.AccountName;
  190. accountCreationRequestElement.Add(accountNameElement);
  191. XElement blobStorageEndpointUriElement = new XElement(XName.Get("BlobStorageEndpointUri", "http://schemas.datacontract.org/2004/07/Microsoft.Cloud.Media.Management.ResourceProvider.Models"));
  192. blobStorageEndpointUriElement.Value = parameters.BlobStorageEndpointUri.AbsoluteUri;
  193. accountCreationRequestElement.Add(blobStorageEndpointUriElement);
  194. XElement regionElement = new XElement(XName.Get("Region", "http://schemas.datacontract.org/2004/07/Microsoft.Cloud.Media.Management.ResourceProvider.Models"));
  195. regionElement.Value = parameters.Region;
  196. accountCreationRequestElement.Add(regionElement);
  197. XElement storageAccountKeyElement = new XElement(XName.Get("StorageAccountKey", "http://schemas.datacontract.org/2004/07/Microsoft.Cloud.Media.Management.ResourceProvider.Models"));
  198. storageAccountKeyElement.Value = parameters.StorageAccountKey;
  199. accountCreationRequestElement.Add(storageAccountKeyElement);
  200. XElement storageAccountNameElement = new XElement(XName.Get("StorageAccountName", "http://schemas.datacontract.org/2004/07/Microsoft.Cloud.Media.Management.ResourceProvider.Models"));
  201. storageAccountNameElement.Value = parameters.StorageAccountName;
  202. accountCreationRequestElement.Add(storageAccountNameElement);
  203. requestContent = requestDoc.ToString();
  204. httpRequest.Content = new StringContent(requestContent, Encoding.UTF8);
  205. httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/xml");
  206. // Send Request
  207. HttpResponseMessage httpResponse = null;
  208. try
  209. {
  210. if (shouldTrace)
  211. {
  212. TracingAdapter.SendRequest(invocationId, httpRequest);
  213. }
  214. cancellationToken.ThrowIfCancellationRequested();
  215. httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);
  216. if (shouldTrace)
  217. {
  218. TracingAdapter.ReceiveResponse(invocationId, httpResponse);
  219. }
  220. HttpStatusCode statusCode = httpResponse.StatusCode;
  221. if (statusCode != HttpStatusCode.Created)
  222. {
  223. cancellationToken.ThrowIfCancellationRequested();
  224. CloudException ex = CloudException.Create(httpRequest, requestContent, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false));
  225. if (shouldTrace)
  226. {
  227. TracingAdapter.Error(invocationId, ex);
  228. }
  229. throw ex;
  230. }
  231. // Create Result
  232. MediaServicesAccountCreateResponse result = null;
  233. // Deserialize Response
  234. if (statusCode == HttpStatusCode.Created)
  235. {
  236. cancellationToken.ThrowIfCancellationRequested();
  237. string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
  238. result = new MediaServicesAccountCreateResponse();
  239. JToken responseDoc = null;
  240. if (string.IsNullOrEmpty(responseContent) == false)
  241. {
  242. responseDoc = JToken.Parse(responseContent);
  243. }
  244. if (responseDoc != null && responseDoc.Type != JTokenType.Null)
  245. {
  246. MediaServicesCreatedAccount accountInstance = new MediaServicesCreatedAccount();
  247. result.Account = accountInstance;
  248. JToken accountIdValue = responseDoc["AccountId"];
  249. if (accountIdValue != null && accountIdValue.Type != JTokenType.Null)
  250. {
  251. string accountIdInstance = ((string)accountIdValue);
  252. accountInstance.AccountId = accountIdInstance;
  253. }
  254. JToken accountNameValue = responseDoc["AccountName"];
  255. if (accountNameValue != null && accountNameValue.Type != JTokenType.Null)
  256. {
  257. string accountNameInstance = ((string)accountNameValue);
  258. accountInstance.AccountName = accountNameInstance;
  259. }
  260. JToken subscriptionValue = responseDoc["Subscription"];
  261. if (subscriptionValue != null && subscriptionValue.Type != JTokenType.Null)
  262. {
  263. string subscriptionInstance = ((string)subscriptionValue);
  264. accountInstance.SubscriptionId = subscriptionInstance;
  265. }
  266. }
  267. }
  268. result.StatusCode = statusCode;
  269. if (httpResponse.Headers.Contains("x-ms-request-id"))
  270. {
  271. result.RequestId = httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault();
  272. }
  273. if (shouldTrace)
  274. {
  275. TracingAdapter.Exit(invocationId, result);
  276. }
  277. return result;
  278. }
  279. finally
  280. {
  281. if (httpResponse != null)
  282. {
  283. httpResponse.Dispose();
  284. }
  285. }
  286. }
  287. finally
  288. {
  289. if (httpRequest != null)
  290. {
  291. httpRequest.Dispose();
  292. }
  293. }
  294. }
  295. /// <summary>
  296. /// The Delete Media Services Account operation deletes an existing
  297. /// media services account in Windows Azure. (see
  298. /// http://msdn.microsoft.com/en-us/library/windowsazure/dn194273.aspx
  299. /// for more information)
  300. /// </summary>
  301. /// <param name='accountName'>
  302. /// Required. The name of the media services account.
  303. /// </param>
  304. /// <param name='cancellationToken'>
  305. /// Cancellation token.
  306. /// </param>
  307. /// <returns>
  308. /// A standard service response including an HTTP status code and
  309. /// request ID.
  310. /// </returns>
  311. public async Task<AzureOperationResponse> DeleteAsync(string accountName, CancellationToken cancellationToken)
  312. {
  313. // Validate
  314. if (accountName == null)
  315. {
  316. throw new ArgumentNullException("accountName");
  317. }
  318. // Tracing
  319. bool shouldTrace = TracingAdapter.IsEnabled;
  320. string invocationId = null;
  321. if (shouldTrace)
  322. {
  323. invocationId = TracingAdapter.NextInvocationId.ToString();
  324. Dictionary<string, object> tracingParameters = new Dictionary<string, object>();
  325. tracingParameters.Add("accountName", accountName);
  326. TracingAdapter.Enter(invocationId, this, "DeleteAsync", tracingParameters);
  327. }
  328. // Construct URL
  329. string url = "";
  330. url = url + "/";
  331. if (this.Client.Credentials.SubscriptionId != null)
  332. {
  333. url = url + Uri.EscapeDataString(this.Client.Credentials.SubscriptionId);
  334. }
  335. url = url + "/services/mediaservices/Accounts/";
  336. url = url + Uri.EscapeDataString(accountName);
  337. string baseUrl = this.Client.BaseUri.AbsoluteUri;
  338. // Trim '/' character from the end of baseUrl and beginning of url.
  339. if (baseUrl[baseUrl.Length - 1] == '/')
  340. {
  341. baseUrl = baseUrl.Substring(0, baseUrl.Length - 1);
  342. }
  343. if (url[0] == '/')
  344. {
  345. url = url.Substring(1);
  346. }
  347. url = baseUrl + "/" + url;
  348. url = url.Replace(" ", "%20");
  349. // Create HTTP transport objects
  350. HttpRequestMessage httpRequest = null;
  351. try
  352. {
  353. httpRequest = new HttpRequestMessage();
  354. httpRequest.Method = HttpMethod.Delete;
  355. httpRequest.RequestUri = new Uri(url);
  356. // Set Headers
  357. httpRequest.Headers.Add("x-ms-version", "2011-10-01");
  358. // Set Credentials
  359. cancellationToken.ThrowIfCancellationRequested();
  360. await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false);
  361. // Send Request
  362. HttpResponseMessage httpResponse = null;
  363. try
  364. {
  365. if (shouldTrace)
  366. {
  367. TracingAdapter.SendRequest(invocationId, httpRequest);
  368. }
  369. cancellationToken.ThrowIfCancellationRequested();
  370. httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);
  371. if (shouldTrace)
  372. {
  373. TracingAdapter.ReceiveResponse(invocationId, httpResponse);
  374. }
  375. HttpStatusCode statusCode = httpResponse.StatusCode;
  376. if (statusCode != HttpStatusCode.NoContent)
  377. {
  378. cancellationToken.ThrowIfCancellationRequested();
  379. CloudException ex = CloudException.Create(httpRequest, null, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false));
  380. if (shouldTrace)
  381. {
  382. TracingAdapter.Error(invocationId, ex);
  383. }
  384. throw ex;
  385. }
  386. // Create Result
  387. AzureOperationResponse result = null;
  388. // Deserialize Response
  389. result = new AzureOperationResponse();
  390. result.StatusCode = statusCode;
  391. if (httpResponse.Headers.Contains("x-ms-request-id"))
  392. {
  393. result.RequestId = httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault();
  394. }
  395. if (shouldTrace)
  396. {
  397. TracingAdapter.Exit(invocationId, result);
  398. }
  399. return result;
  400. }
  401. finally
  402. {
  403. if (httpResponse != null)
  404. {
  405. httpResponse.Dispose();
  406. }
  407. }
  408. }
  409. finally
  410. {
  411. if (httpRequest != null)
  412. {
  413. httpRequest.Dispose();
  414. }
  415. }
  416. }
  417. /// <summary>
  418. /// The Get Media Services Account operation gets detailed information
  419. /// about a media services account in Windows Azure. (see
  420. /// http://msdn.microsoft.com/en-us/library/windowsazure/dn166974.aspx
  421. /// for more information)
  422. /// </summary>
  423. /// <param name='accountName'>
  424. /// Required. The name of the Media Services account.
  425. /// </param>
  426. /// <param name='cancellationToken'>
  427. /// Cancellation token.
  428. /// </param>
  429. /// <returns>
  430. /// The Get Media Services Account operation response.
  431. /// </returns>
  432. public async Task<MediaServicesAccountGetResponse> GetAsync(string accountName, CancellationToken cancellationToken)
  433. {
  434. // Validate
  435. if (accountName == null)
  436. {
  437. throw new ArgumentNullException("accountName");
  438. }
  439. // Tracing
  440. bool shouldTrace = TracingAdapter.IsEnabled;
  441. string invocationId = null;
  442. if (shouldTrace)
  443. {
  444. invocationId = TracingAdapter.NextInvocationId.ToString();
  445. Dictionary<string, object> tracingParameters = new Dictionary<string, object>();
  446. tracingParameters.Add("accountName", accountName);
  447. TracingAdapter.Enter(invocationId, this, "GetAsync", tracingParameters);
  448. }
  449. // Construct URL
  450. string url = "";
  451. url = url + "/";
  452. if (this.Client.Credentials.SubscriptionId != null)
  453. {
  454. url = url + Uri.EscapeDataString(this.Client.Credentials.SubscriptionId);
  455. }
  456. url = url + "/services/mediaservices/Accounts/";
  457. url = url + Uri.EscapeDataString(accountName);
  458. string baseUrl = this.Client.BaseUri.AbsoluteUri;
  459. // Trim '/' character from the end of baseUrl and beginning of url.
  460. if (baseUrl[baseUrl.Length - 1] == '/')
  461. {
  462. baseUrl = baseUrl.Substring(0, baseUrl.Length - 1);
  463. }
  464. if (url[0] == '/')
  465. {
  466. url = url.Substring(1);
  467. }
  468. url = baseUrl + "/" + url;
  469. url = url.Replace(" ", "%20");
  470. // Create HTTP transport objects
  471. HttpRequestMessage httpRequest = null;
  472. try
  473. {
  474. httpRequest = new HttpRequestMessage();
  475. httpRequest.Method = HttpMethod.Get;
  476. httpRequest.RequestUri = new Uri(url);
  477. // Set Headers
  478. httpRequest.Headers.Add("x-ms-version", "2011-10-01");
  479. // Set Credentials
  480. cancellationToken.ThrowIfCancellationRequested();
  481. await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false);
  482. // Send Request
  483. HttpResponseMessage httpResponse = null;
  484. try
  485. {
  486. if (shouldTrace)
  487. {
  488. TracingAdapter.SendRequest(invocationId, httpRequest);
  489. }
  490. cancellationToken.ThrowIfCancellationRequested();
  491. httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);
  492. if (shouldTrace)
  493. {
  494. TracingAdapter.ReceiveResponse(invocationId, httpResponse);
  495. }
  496. HttpStatusCode statusCode = httpResponse.StatusCode;
  497. if (statusCode != HttpStatusCode.OK)
  498. {
  499. cancellationToken.ThrowIfCancellationRequested();
  500. CloudException ex = CloudException.Create(httpRequest, null, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false));
  501. if (shouldTrace)
  502. {
  503. TracingAdapter.Error(invocationId, ex);
  504. }
  505. throw ex;
  506. }
  507. // Create Result
  508. MediaServicesAccountGetResponse result = null;
  509. // Deserialize Response
  510. if (statusCode == HttpStatusCode.OK)
  511. {
  512. cancellationToken.ThrowIfCancellationRequested();
  513. string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
  514. result = new MediaServicesAccountGetResponse();
  515. JToken responseDoc = null;
  516. if (string.IsNullOrEmpty(responseContent) == false)
  517. {
  518. responseDoc = JToken.Parse(responseContent);
  519. }
  520. if (responseDoc != null && responseDoc.Type != JTokenType.Null)
  521. {
  522. MediaServicesAccount accountInstance = new MediaServicesAccount();
  523. result.Account = accountInstance;
  524. JToken accountNameValue = responseDoc["AccountName"];
  525. if (accountNameValue != null && accountNameValue.Type != JTokenType.Null)
  526. {
  527. string accountNameInstance = ((string)accountNameValue);
  528. accountInstance.AccountName = accountNameInstance;
  529. }
  530. JToken accountKeyValue = responseDoc["AccountKey"];
  531. if (accountKeyValue != null && accountKeyValue.Type != JTokenType.Null)
  532. {
  533. string accountKeyInstance = ((string)accountKeyValue);
  534. accountInstance.AccountKey = accountKeyInstance;
  535. }
  536. JToken accountKeysValue = responseDoc["AccountKeys"];
  537. if (accountKeysValue != null && accountKeysValue.Type != JTokenType.Null)
  538. {
  539. MediaServicesAccount.AccountKeys accountKeysInstance = new MediaServicesAccount.AccountKeys();
  540. accountInstance.StorageAccountKeys = accountKeysInstance;
  541. JToken primaryValue = accountKeysValue["Primary"];
  542. if (primaryValue != null && primaryValue.Type != JTokenType.Null)
  543. {
  544. string primaryInstance = ((string)primaryValue);
  545. accountKeysInstance.Primary = primaryInstance;
  546. }
  547. JToken secondaryValue = accountKeysValue["Secondary"];
  548. if (secondaryValue != null && secondaryValue.Type != JTokenType.Null)
  549. {
  550. string secondaryInstance = ((string)secondaryValue);
  551. accountKeysInstance.Secondary = secondaryInstance;
  552. }
  553. }
  554. JToken accountRegionValue = responseDoc["AccountRegion"];
  555. if (accountRegionValue != null && accountRegionValue.Type != JTokenType.Null)
  556. {
  557. string accountRegionInstance = ((string)accountRegionValue);
  558. accountInstance.AccountRegion = accountRegionInstance;
  559. }
  560. JToken storageAccountNameValue = responseDoc["StorageAccountName"];
  561. if (storageAccountNameValue != null && storageAccountNameValue.Type != JTokenType.Null)
  562. {
  563. string storageAccountNameInstance = ((string)storageAccountNameValue);
  564. accountInstance.StorageAccountName = storageAccountNameInstance;
  565. }
  566. }
  567. }
  568. result.StatusCode = statusCode;
  569. if (httpResponse.Headers.Contains("x-ms-request-id"))
  570. {
  571. result.RequestId = httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault();
  572. }
  573. if (shouldTrace)
  574. {
  575. TracingAdapter.Exit(invocationId, result);
  576. }
  577. return result;
  578. }
  579. finally
  580. {
  581. if (httpResponse != null)
  582. {
  583. httpResponse.Dispose();
  584. }
  585. }
  586. }
  587. finally
  588. {
  589. if (httpRequest != null)
  590. {
  591. httpRequest.Dispose();
  592. }
  593. }
  594. }
  595. /// <summary>
  596. /// The List Media Services Account operation gets information about
  597. /// all existing media services accounts associated with the current
  598. /// subscription in Windows Azure. (see
  599. /// http://msdn.microsoft.com/en-us/library/windowsazure/dn166989.aspx
  600. /// for more information)
  601. /// </summary>
  602. /// <param name='cancellationToken'>
  603. /// Cancellation token.
  604. /// </param>
  605. /// <returns>
  606. /// The List Media Accounts operation response.
  607. /// </returns>
  608. public async Task<MediaServicesAccountListResponse> ListAsync(CancellationToken cancellationToken)
  609. {
  610. // Validate
  611. // Tracing
  612. bool shouldTrace = TracingAdapter.IsEnabled;
  613. string invocationId = null;
  614. if (shouldTrace)
  615. {
  616. invocationId = TracingAdapter.NextInvocationId.ToString();
  617. Dictionary<string, object> tracingParameters = new Dictionary<string, object>();
  618. TracingAdapter.Enter(invocationId, this, "ListAsync", tracingParameters);
  619. }
  620. // Construct URL
  621. string url = "";
  622. url = url + "/";
  623. if (this.Client.Credentials.SubscriptionId != null)
  624. {
  625. url = url + Uri.EscapeDataString(this.Client.Credentials.SubscriptionId);
  626. }
  627. url = url + "/services/mediaservices/Accounts";
  628. string baseUrl = this.Client.BaseUri.AbsoluteUri;
  629. // Trim '/' character from the end of baseUrl and beginning of url.
  630. if (baseUrl[baseUrl.Length - 1] == '/')
  631. {
  632. baseUrl = baseUrl.Substring(0, baseUrl.Length - 1);
  633. }
  634. if (url[0] == '/')
  635. {
  636. url = url.Substring(1);
  637. }
  638. url = baseUrl + "/" + url;
  639. url = url.Replace(" ", "%20");
  640. // Create HTTP transport objects
  641. HttpRequestMessage httpRequest = null;
  642. try
  643. {
  644. httpRequest = new HttpRequestMessage();
  645. httpRequest.Method = HttpMethod.Get;
  646. httpRequest.RequestUri = new Uri(url);
  647. // Set Headers
  648. httpRequest.Headers.Add("x-ms-version", "2011-10-01");
  649. // Set Credentials
  650. cancellationToken.ThrowIfCancellationRequested();
  651. await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false);
  652. // Send Request
  653. HttpResponseMessage httpResponse = null;
  654. try
  655. {
  656. if (shouldTrace)
  657. {
  658. TracingAdapter.SendRequest(invocationId, httpRequest);
  659. }
  660. cancellationToken.ThrowIfCancellationRequested();
  661. httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);
  662. if (shouldTrace)
  663. {
  664. TracingAdapter.ReceiveResponse(invocationId, httpResponse);
  665. }
  666. HttpStatusCode statusCode = httpResponse.StatusCode;
  667. if (statusCode != HttpStatusCode.OK)
  668. {
  669. cancellationToken.ThrowIfCancellationRequested();
  670. CloudException ex = CloudException.Create(httpRequest, null, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false));
  671. if (shouldTrace)
  672. {
  673. TracingAdapter.Error(invocationId, ex);
  674. }
  675. throw ex;
  676. }
  677. // Create Result
  678. MediaServicesAccountListResponse result = null;
  679. // Deserialize Response
  680. if (statusCode == HttpStatusCode.OK)
  681. {
  682. cancellationToken.ThrowIfCancellationRequested();
  683. string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
  684. result = new MediaServicesAccountListResponse();
  685. XDocument responseDoc = XDocument.Parse(responseContent);
  686. XElement serviceResourcesSequenceElement = responseDoc.Element(XName.Get("ServiceResources", "http://schemas.microsoft.com/windowsazure"));
  687. if (serviceResourcesSequenceElement != null)
  688. {
  689. foreach (XElement serviceResourcesElement in serviceResourcesSequenceElement.Elements(XName.Get("ServiceResource", "http://schemas.microsoft.com/windowsazure")))
  690. {
  691. MediaServicesAccountListResponse.MediaServiceAccount serviceResourceInstance = new MediaServicesAccountListResponse.MediaServiceAccount();
  692. result.Accounts.Add(serviceResourceInstance);
  693. XElement nameElement = serviceResourcesElement.Element(XName.Get("Name", "http://schemas.microsoft.com/windowsazure"));
  694. if (nameElement != null)
  695. {
  696. string nameInstance = nameElement.Value;
  697. serviceResourceInstance.Name = nameInstance;
  698. }
  699. XElement typeElement = serviceResourcesElement.Element(XName.Get("Type", "http://schemas.microsoft.com/windowsazure"));
  700. if (typeElement != null)
  701. {
  702. string typeInstance = typeElement.Value;
  703. serviceResourceInstance.Type = typeInstance;
  704. }
  705. XElement stateElement = serviceResourcesElement.Element(XName.Get("State", "http://schemas.microsoft.com/windowsazure"));
  706. if (stateElement != null)
  707. {
  708. string stateInstance = stateElement.Value;
  709. serviceResourceInstance.State = stateInstance;
  710. }
  711. XElement selfLinkElement = serviceResourcesElement.Element(XName.Get("SelfLink", "http://schemas.microsoft.com/windowsazure"));
  712. if (selfLinkElement != null)
  713. {
  714. Uri selfLinkInstance = TypeConversion.TryParseUri(selfLinkElement.Value);
  715. serviceResourceInstance.Uri = selfLinkInstance;
  716. }
  717. XElement parentLinkElement = serviceResourcesElement.Element(XName.Get("ParentLink", "http://schemas.microsoft.com/windowsazure"));
  718. if (parentLinkElement != null)
  719. {
  720. Uri parentLinkInstance = TypeConversion.TryParseUri(parentLinkElement.Value);
  721. serviceResourceInstance.ParentUri = parentLinkInstance;
  722. }
  723. XElement accountIdElement = serviceResourcesElement.Element(XName.Get("AccountId", "http://schemas.microsoft.com/windowsazure"));
  724. if (accountIdElement != null)
  725. {
  726. string accountIdInstance = accountIdElement.Value;
  727. serviceResourceInstance.AccountId = accountIdInstance;
  728. }
  729. }
  730. }
  731. }
  732. result.StatusCode = statusCode;
  733. if (httpResponse.Headers.Contains("x-ms-request-id"))
  734. {
  735. result.RequestId = httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault();
  736. }
  737. if (shouldTrace)
  738. {
  739. TracingAdapter.Exit(invocationId, result);
  740. }
  741. return result;
  742. }
  743. finally
  744. {
  745. if (httpResponse != null)
  746. {
  747. httpResponse.Dispose();
  748. }
  749. }
  750. }
  751. finally
  752. {
  753. if (httpRequest != null)
  754. {
  755. httpRequest.Dispose();
  756. }
  757. }
  758. }
  759. /// <summary>
  760. /// The Regenerate Media Services Account Key operation regenerates an
  761. /// account key for the given Media Services account in Windows Azure.
  762. /// (see
  763. /// http://msdn.microsoft.com/en-us/library/windowsazure/dn167010.aspx
  764. /// for more information)
  765. /// </summary>
  766. /// <param name='accountName'>
  767. /// Required. The name of the Media Services Account.
  768. /// </param>
  769. /// <param name='keyType'>
  770. /// Required. The type of key to regenerate (primary or secondary)
  771. /// </param>
  772. /// <param name='cancellationToken'>
  773. /// Cancellation token.
  774. /// </param>
  775. /// <returns>
  776. /// A standard service response including an HTTP status code and
  777. /// request ID.
  778. /// </returns>
  779. public async Task<AzureOperationResponse> RegenerateKeyAsync(string accountName, MediaServicesKeyType keyType, CancellationToken cancellationToken)
  780. {
  781. // Validate
  782. if (accountName == null)
  783. {
  784. throw new ArgumentNullException("accountName");
  785. }
  786. // Tracing
  787. bool shouldTrace = TracingAdapter.IsEnabled;
  788. string invocationId = null;
  789. if (shouldTrace)
  790. {
  791. invocationId = TracingAdapter.NextInvocationId.ToString();
  792. Dictionary<string, object> tracingParameters = new Dictionary<string, object>();
  793. tracingParameters.Add("accountName", accountName);
  794. tracingParameters.Add("keyType", keyType);
  795. TracingAdapter.Enter(invocationId, this, "RegenerateKeyAsync", tracingParameters);
  796. }
  797. // Construct URL
  798. string url = "";
  799. url = url + "/";
  800. if (this.Client.Credentials.SubscriptionId != null)
  801. {
  802. url = url + Uri.EscapeDataString(this.Client.Credentials.SubscriptionId);
  803. }
  804. url = url + "/services/mediaservices/Accounts/";
  805. url = url + Uri.EscapeDataString(accountName);
  806. url = url + "/AccountKeys/";
  807. url = url + Uri.EscapeDataString(keyType.ToString());
  808. url = url + "/Regenerate";
  809. string baseUrl = this.Client.BaseUri.AbsoluteUri;
  810. // Trim '/' character from the end of baseUrl and beginning of url.
  811. if (baseUrl[baseUrl.Length - 1] == '/')
  812. {
  813. baseUrl = baseUrl.Substring(0, baseUrl.Length - 1);
  814. }
  815. if (url[0] == '/')
  816. {
  817. url = url.Substring(1);
  818. }
  819. url = baseUrl + "/" + url;
  820. url = url.Replace(" ", "%20");
  821. // Create HTTP transport objects
  822. HttpRequestMessage httpRequest = null;
  823. try
  824. {
  825. httpRequest = new HttpRequestMessage();
  826. httpRequest.Method = HttpMethod.Post;
  827. httpRequest.RequestUri = new Uri(url);
  828. // Set Headers
  829. httpRequest.Headers.Add("x-ms-version", "2011-10-01");
  830. // Set Credentials
  831. cancellationToken.ThrowIfCancellationRequested();
  832. await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false);
  833. // Send Request
  834. HttpResponseMessage httpResponse = null;
  835. try
  836. {
  837. if (shouldTrace)
  838. {
  839. TracingAdapter.SendRequest(invocationId, httpRequest);
  840. }
  841. cancellationToken.ThrowIfCancellationRequested();
  842. httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);
  843. if (shouldTrace)
  844. {
  845. TracingAdapter.ReceiveResponse(invocationId, httpResponse);
  846. }
  847. HttpStatusCode statusCode = httpResponse.StatusCode;
  848. if (statusCode != HttpStatusCode.NoContent)
  849. {
  850. cancellationToken.ThrowIfCancellationRequested();
  851. CloudException ex = CloudException.Create(httpRequest, null, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false));
  852. if (shouldTrace)
  853. {
  854. TracingAdapter.Error(invocationId, ex);
  855. }
  856. throw ex;
  857. }
  858. // Create Result
  859. AzureOperationResponse result = null;
  860. // Deserialize Response
  861. result = new AzureOperationResponse();
  862. result.StatusCode = statusCode;
  863. if (httpResponse.Headers.Contains("x-ms-request-id"))
  864. {
  865. result.RequestId = httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault();
  866. }
  867. if (shouldTrace)
  868. {
  869. TracingAdapter.Exit(invocationId, result);
  870. }
  871. return result;
  872. }
  873. finally
  874. {
  875. if (httpResponse != null)
  876. {
  877. httpResponse.Dispose();
  878. }
  879. }
  880. }
  881. finally
  882. {
  883. if (httpRequest != null)
  884. {
  885. httpRequest.Dispose();
  886. }
  887. }
  888. }
  889. }
  890. }