// 
// Copyright (c) Microsoft and contributors.  All rights reserved.
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//   http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// 
// See the License for the specific language governing permissions and
// limitations under the License.
// 

// Warning: This code was generated by a tool.
// 
// Changes to this file may cause incorrect behavior and will be lost if the
// code is regenerated.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using Hyak.Common;
using Hyak.Common.Internals;
using Microsoft.Azure;
using Microsoft.Azure.Management.Automation;
using Microsoft.Azure.Management.Automation.Models;
using Newtonsoft.Json.Linq;

namespace Microsoft.Azure.Management.Automation
{
    /// <summary>
    /// Service operation for automation modules.  (see
    /// http://aka.ms/azureautomationsdk/moduleoperations for more information)
    /// </summary>
    internal partial class ModuleOperations : IServiceOperations<AutomationManagementClient>, IModuleOperations
    {
        /// <summary>
        /// Initializes a new instance of the ModuleOperations class.
        /// </summary>
        /// <param name='client'>
        /// Reference to the service client.
        /// </param>
        internal ModuleOperations(AutomationManagementClient client)
        {
            this._client = client;
        }
        
        private AutomationManagementClient _client;
        
        /// <summary>
        /// Gets a reference to the
        /// Microsoft.Azure.Management.Automation.AutomationManagementClient.
        /// </summary>
        public AutomationManagementClient Client
        {
            get { return this._client; }
        }
        
        /// <summary>
        /// Create the module identified by module name.  (see
        /// http://aka.ms/azureautomationsdk/moduleoperations for more
        /// information)
        /// </summary>
        /// <param name='resourceGroupName'>
        /// Required. The name of the resource group
        /// </param>
        /// <param name='automationAccount'>
        /// Required. The automation account name.
        /// </param>
        /// <param name='parameters'>
        /// Required. The create or update parameters for module.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// The response model for the create or update module operation.
        /// </returns>
        public async Task<ModuleCreateOrUpdateResponse> CreateOrUpdateAsync(string resourceGroupName, string automationAccount, ModuleCreateOrUpdateParameters parameters, CancellationToken cancellationToken)
        {
            // Validate
            if (resourceGroupName == null)
            {
                throw new ArgumentNullException("resourceGroupName");
            }
            if (automationAccount == null)
            {
                throw new ArgumentNullException("automationAccount");
            }
            if (parameters == null)
            {
                throw new ArgumentNullException("parameters");
            }
            if (parameters.Properties == null)
            {
                throw new ArgumentNullException("parameters.Properties");
            }
            if (parameters.Properties.ContentLink == null)
            {
                throw new ArgumentNullException("parameters.Properties.ContentLink");
            }
            if (parameters.Properties.ContentLink.ContentHash != null)
            {
                if (parameters.Properties.ContentLink.ContentHash.Algorithm == null)
                {
                    throw new ArgumentNullException("parameters.Properties.ContentLink.ContentHash.Algorithm");
                }
                if (parameters.Properties.ContentLink.ContentHash.Value == null)
                {
                    throw new ArgumentNullException("parameters.Properties.ContentLink.ContentHash.Value");
                }
            }
            
            // Tracing
            bool shouldTrace = TracingAdapter.IsEnabled;
            string invocationId = null;
            if (shouldTrace)
            {
                invocationId = TracingAdapter.NextInvocationId.ToString();
                Dictionary<string, object> tracingParameters = new Dictionary<string, object>();
                tracingParameters.Add("resourceGroupName", resourceGroupName);
                tracingParameters.Add("automationAccount", automationAccount);
                tracingParameters.Add("parameters", parameters);
                TracingAdapter.Enter(invocationId, this, "CreateOrUpdateAsync", tracingParameters);
            }
            
            // Construct URL
            string url = "";
            url = url + "/subscriptions/";
            if (this.Client.Credentials.SubscriptionId != null)
            {
                url = url + Uri.EscapeDataString(this.Client.Credentials.SubscriptionId);
            }
            url = url + "/resourceGroups/";
            url = url + Uri.EscapeDataString(resourceGroupName);
            url = url + "/providers/";
            if (this.Client.ResourceNamespace != null)
            {
                url = url + Uri.EscapeDataString(this.Client.ResourceNamespace);
            }
            url = url + "/automationAccounts/";
            url = url + Uri.EscapeDataString(automationAccount);
            url = url + "/modules/";
            if (parameters.Name != null)
            {
                url = url + Uri.EscapeDataString(parameters.Name);
            }
            List<string> queryParameters = new List<string>();
            queryParameters.Add("api-version=2015-01-01-preview");
            if (queryParameters.Count > 0)
            {
                url = url + "?" + string.Join("&", queryParameters);
            }
            string baseUrl = this.Client.BaseUri.AbsoluteUri;
            // Trim '/' character from the end of baseUrl and beginning of url.
            if (baseUrl[baseUrl.Length - 1] == '/')
            {
                baseUrl = baseUrl.Substring(0, baseUrl.Length - 1);
            }
            if (url[0] == '/')
            {
                url = url.Substring(1);
            }
            url = baseUrl + "/" + url;
            url = url.Replace(" ", "%20");
            
            // Create HTTP transport objects
            HttpRequestMessage httpRequest = null;
            try
            {
                httpRequest = new HttpRequestMessage();
                httpRequest.Method = HttpMethod.Put;
                httpRequest.RequestUri = new Uri(url);
                
                // Set Headers
                httpRequest.Headers.Add("Accept", "application/json");
                httpRequest.Headers.Add("x-ms-version", "2014-06-01");
                
                // Set Credentials
                cancellationToken.ThrowIfCancellationRequested();
                await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false);
                
                // Serialize Request
                string requestContent = null;
                JToken requestDoc = null;
                
                JObject moduleCreateOrUpdateParametersValue = new JObject();
                requestDoc = moduleCreateOrUpdateParametersValue;
                
                JObject propertiesValue = new JObject();
                moduleCreateOrUpdateParametersValue["properties"] = propertiesValue;
                
                JObject contentLinkValue = new JObject();
                propertiesValue["contentLink"] = contentLinkValue;
                
                if (parameters.Properties.ContentLink.Uri != null)
                {
                    contentLinkValue["uri"] = parameters.Properties.ContentLink.Uri.AbsoluteUri;
                }
                
                if (parameters.Properties.ContentLink.ContentHash != null)
                {
                    JObject contentHashValue = new JObject();
                    contentLinkValue["contentHash"] = contentHashValue;
                    
                    contentHashValue["algorithm"] = parameters.Properties.ContentLink.ContentHash.Algorithm;
                    
                    contentHashValue["value"] = parameters.Properties.ContentLink.ContentHash.Value;
                }
                
                if (parameters.Properties.ContentLink.Version != null)
                {
                    contentLinkValue["version"] = parameters.Properties.ContentLink.Version;
                }
                
                if (parameters.Name != null)
                {
                    moduleCreateOrUpdateParametersValue["name"] = parameters.Name;
                }
                
                if (parameters.Location != null)
                {
                    moduleCreateOrUpdateParametersValue["location"] = parameters.Location;
                }
                
                if (parameters.Tags != null)
                {
                    JObject tagsDictionary = new JObject();
                    foreach (KeyValuePair<string, string> pair in parameters.Tags)
                    {
                        string tagsKey = pair.Key;
                        string tagsValue = pair.Value;
                        tagsDictionary[tagsKey] = tagsValue;
                    }
                    moduleCreateOrUpdateParametersValue["tags"] = tagsDictionary;
                }
                
                requestContent = requestDoc.ToString(Newtonsoft.Json.Formatting.Indented);
                httpRequest.Content = new StringContent(requestContent, Encoding.UTF8);
                httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8");
                
                // Send Request
                HttpResponseMessage httpResponse = null;
                try
                {
                    if (shouldTrace)
                    {
                        TracingAdapter.SendRequest(invocationId, httpRequest);
                    }
                    cancellationToken.ThrowIfCancellationRequested();
                    httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);
                    if (shouldTrace)
                    {
                        TracingAdapter.ReceiveResponse(invocationId, httpResponse);
                    }
                    HttpStatusCode statusCode = httpResponse.StatusCode;
                    if (statusCode != HttpStatusCode.OK && statusCode != HttpStatusCode.Created)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        CloudException ex = CloudException.Create(httpRequest, requestContent, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false));
                        if (shouldTrace)
                        {
                            TracingAdapter.Error(invocationId, ex);
                        }
                        throw ex;
                    }
                    
                    // Create Result
                    ModuleCreateOrUpdateResponse result = null;
                    // Deserialize Response
                    if (statusCode == HttpStatusCode.OK || statusCode == HttpStatusCode.Created)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
                        result = new ModuleCreateOrUpdateResponse();
                        JToken responseDoc = null;
                        if (string.IsNullOrEmpty(responseContent) == false)
                        {
                            responseDoc = JToken.Parse(responseContent);
                        }
                        
                        if (responseDoc != null && responseDoc.Type != JTokenType.Null)
                        {
                            Module moduleInstance = new Module();
                            result.Module = moduleInstance;
                            
                            JToken propertiesValue2 = responseDoc["properties"];
                            if (propertiesValue2 != null && propertiesValue2.Type != JTokenType.Null)
                            {
                                ModuleProperties propertiesInstance = new ModuleProperties();
                                moduleInstance.Properties = propertiesInstance;
                                
                                JToken isGlobalValue = propertiesValue2["isGlobal"];
                                if (isGlobalValue != null && isGlobalValue.Type != JTokenType.Null)
                                {
                                    bool isGlobalInstance = ((bool)isGlobalValue);
                                    propertiesInstance.IsGlobal = isGlobalInstance;
                                }
                                
                                JToken versionValue = propertiesValue2["version"];
                                if (versionValue != null && versionValue.Type != JTokenType.Null)
                                {
                                    string versionInstance = ((string)versionValue);
                                    propertiesInstance.Version = versionInstance;
                                }
                                
                                JToken sizeInBytesValue = propertiesValue2["sizeInBytes"];
                                if (sizeInBytesValue != null && sizeInBytesValue.Type != JTokenType.Null)
                                {
                                    long sizeInBytesInstance = ((long)sizeInBytesValue);
                                    propertiesInstance.SizeInBytes = sizeInBytesInstance;
                                }
                                
                                JToken activityCountValue = propertiesValue2["activityCount"];
                                if (activityCountValue != null && activityCountValue.Type != JTokenType.Null)
                                {
                                    int activityCountInstance = ((int)activityCountValue);
                                    propertiesInstance.ActivityCount = activityCountInstance;
                                }
                                
                                JToken provisioningStateValue = propertiesValue2["provisioningState"];
                                if (provisioningStateValue != null && provisioningStateValue.Type != JTokenType.Null)
                                {
                                    ModuleProvisioningState provisioningStateInstance = ((ModuleProvisioningState)Enum.Parse(typeof(ModuleProvisioningState), ((string)provisioningStateValue), true));
                                    propertiesInstance.ProvisioningState = provisioningStateInstance;
                                }
                                
                                JToken contentLinkValue2 = propertiesValue2["contentLink"];
                                if (contentLinkValue2 != null && contentLinkValue2.Type != JTokenType.Null)
                                {
                                    ContentLink contentLinkInstance = new ContentLink();
                                    propertiesInstance.ContentLink = contentLinkInstance;
                                    
                                    JToken uriValue = contentLinkValue2["uri"];
                                    if (uriValue != null && uriValue.Type != JTokenType.Null)
                                    {
                                        Uri uriInstance = TypeConversion.TryParseUri(((string)uriValue));
                                        contentLinkInstance.Uri = uriInstance;
                                    }
                                    
                                    JToken contentHashValue2 = contentLinkValue2["contentHash"];
                                    if (contentHashValue2 != null && contentHashValue2.Type != JTokenType.Null)
                                    {
                                        ContentHash contentHashInstance = new ContentHash();
                                        contentLinkInstance.ContentHash = contentHashInstance;
                                        
                                        JToken algorithmValue = contentHashValue2["algorithm"];
                                        if (algorithmValue != null && algorithmValue.Type != JTokenType.Null)
                                        {
                                            string algorithmInstance = ((string)algorithmValue);
                                            contentHashInstance.Algorithm = algorithmInstance;
                                        }
                                        
                                        JToken valueValue = contentHashValue2["value"];
                                        if (valueValue != null && valueValue.Type != JTokenType.Null)
                                        {
                                            string valueInstance = ((string)valueValue);
                                            contentHashInstance.Value = valueInstance;
                                        }
                                    }
                                    
                                    JToken versionValue2 = contentLinkValue2["version"];
                                    if (versionValue2 != null && versionValue2.Type != JTokenType.Null)
                                    {
                                        string versionInstance2 = ((string)versionValue2);
                                        contentLinkInstance.Version = versionInstance2;
                                    }
                                }
                                
                                JToken creationTimeValue = propertiesValue2["creationTime"];
                                if (creationTimeValue != null && creationTimeValue.Type != JTokenType.Null)
                                {
                                    DateTimeOffset creationTimeInstance = ((DateTimeOffset)creationTimeValue);
                                    propertiesInstance.CreationTime = creationTimeInstance;
                                }
                                
                                JToken lastModifiedTimeValue = propertiesValue2["lastModifiedTime"];
                                if (lastModifiedTimeValue != null && lastModifiedTimeValue.Type != JTokenType.Null)
                                {
                                    DateTimeOffset lastModifiedTimeInstance = ((DateTimeOffset)lastModifiedTimeValue);
                                    propertiesInstance.LastModifiedTime = lastModifiedTimeInstance;
                                }
                                
                                JToken descriptionValue = propertiesValue2["description"];
                                if (descriptionValue != null && descriptionValue.Type != JTokenType.Null)
                                {
                                    string descriptionInstance = ((string)descriptionValue);
                                    propertiesInstance.Description = descriptionInstance;
                                }
                            }
                            
                            JToken idValue = responseDoc["id"];
                            if (idValue != null && idValue.Type != JTokenType.Null)
                            {
                                string idInstance = ((string)idValue);
                                moduleInstance.Id = idInstance;
                            }
                            
                            JToken nameValue = responseDoc["name"];
                            if (nameValue != null && nameValue.Type != JTokenType.Null)
                            {
                                string nameInstance = ((string)nameValue);
                                moduleInstance.Name = nameInstance;
                            }
                            
                            JToken locationValue = responseDoc["location"];
                            if (locationValue != null && locationValue.Type != JTokenType.Null)
                            {
                                string locationInstance = ((string)locationValue);
                                moduleInstance.Location = locationInstance;
                            }
                            
                            JToken tagsSequenceElement = ((JToken)responseDoc["tags"]);
                            if (tagsSequenceElement != null && tagsSequenceElement.Type != JTokenType.Null)
                            {
                                foreach (JProperty property in tagsSequenceElement)
                                {
                                    string tagsKey2 = ((string)property.Name);
                                    string tagsValue2 = ((string)property.Value);
                                    moduleInstance.Tags.Add(tagsKey2, tagsValue2);
                                }
                            }
                            
                            JToken typeValue = responseDoc["type"];
                            if (typeValue != null && typeValue.Type != JTokenType.Null)
                            {
                                string typeInstance = ((string)typeValue);
                                moduleInstance.Type = typeInstance;
                            }
                            
                            JToken etagValue = responseDoc["etag"];
                            if (etagValue != null && etagValue.Type != JTokenType.Null)
                            {
                                string etagInstance = ((string)etagValue);
                                moduleInstance.Etag = etagInstance;
                            }
                        }
                        
                    }
                    result.StatusCode = statusCode;
                    if (httpResponse.Headers.Contains("x-ms-request-id"))
                    {
                        result.RequestId = httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault();
                    }
                    
                    if (shouldTrace)
                    {
                        TracingAdapter.Exit(invocationId, result);
                    }
                    return result;
                }
                finally
                {
                    if (httpResponse != null)
                    {
                        httpResponse.Dispose();
                    }
                }
            }
            finally
            {
                if (httpRequest != null)
                {
                    httpRequest.Dispose();
                }
            }
        }
        
        /// <summary>
        /// Delete the module by name.  (see
        /// http://aka.ms/azureautomationsdk/moduleoperations for more
        /// information)
        /// </summary>
        /// <param name='resourceGroupName'>
        /// Required. The name of the resource group
        /// </param>
        /// <param name='automationAccount'>
        /// Required. The automation account name.
        /// </param>
        /// <param name='moduleName'>
        /// Required. The module name.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// A standard service response including an HTTP status code and
        /// request ID.
        /// </returns>
        public async Task<AzureOperationResponse> DeleteAsync(string resourceGroupName, string automationAccount, string moduleName, CancellationToken cancellationToken)
        {
            // Validate
            if (resourceGroupName == null)
            {
                throw new ArgumentNullException("resourceGroupName");
            }
            if (automationAccount == null)
            {
                throw new ArgumentNullException("automationAccount");
            }
            if (moduleName == null)
            {
                throw new ArgumentNullException("moduleName");
            }
            
            // Tracing
            bool shouldTrace = TracingAdapter.IsEnabled;
            string invocationId = null;
            if (shouldTrace)
            {
                invocationId = TracingAdapter.NextInvocationId.ToString();
                Dictionary<string, object> tracingParameters = new Dictionary<string, object>();
                tracingParameters.Add("resourceGroupName", resourceGroupName);
                tracingParameters.Add("automationAccount", automationAccount);
                tracingParameters.Add("moduleName", moduleName);
                TracingAdapter.Enter(invocationId, this, "DeleteAsync", tracingParameters);
            }
            
            // Construct URL
            string url = "";
            url = url + "/subscriptions/";
            if (this.Client.Credentials.SubscriptionId != null)
            {
                url = url + Uri.EscapeDataString(this.Client.Credentials.SubscriptionId);
            }
            url = url + "/resourceGroups/";
            url = url + Uri.EscapeDataString(resourceGroupName);
            url = url + "/providers/";
            if (this.Client.ResourceNamespace != null)
            {
                url = url + Uri.EscapeDataString(this.Client.ResourceNamespace);
            }
            url = url + "/automationAccounts/";
            url = url + Uri.EscapeDataString(automationAccount);
            url = url + "/modules/";
            url = url + Uri.EscapeDataString(moduleName);
            List<string> queryParameters = new List<string>();
            queryParameters.Add("api-version=2015-01-01-preview");
            if (queryParameters.Count > 0)
            {
                url = url + "?" + string.Join("&", queryParameters);
            }
            string baseUrl = this.Client.BaseUri.AbsoluteUri;
            // Trim '/' character from the end of baseUrl and beginning of url.
            if (baseUrl[baseUrl.Length - 1] == '/')
            {
                baseUrl = baseUrl.Substring(0, baseUrl.Length - 1);
            }
            if (url[0] == '/')
            {
                url = url.Substring(1);
            }
            url = baseUrl + "/" + url;
            url = url.Replace(" ", "%20");
            
            // Create HTTP transport objects
            HttpRequestMessage httpRequest = null;
            try
            {
                httpRequest = new HttpRequestMessage();
                httpRequest.Method = HttpMethod.Delete;
                httpRequest.RequestUri = new Uri(url);
                
                // Set Headers
                httpRequest.Headers.Add("Accept", "application/json");
                httpRequest.Headers.Add("x-ms-version", "2014-06-01");
                
                // Set Credentials
                cancellationToken.ThrowIfCancellationRequested();
                await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false);
                
                // Send Request
                HttpResponseMessage httpResponse = null;
                try
                {
                    if (shouldTrace)
                    {
                        TracingAdapter.SendRequest(invocationId, httpRequest);
                    }
                    cancellationToken.ThrowIfCancellationRequested();
                    httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);
                    if (shouldTrace)
                    {
                        TracingAdapter.ReceiveResponse(invocationId, httpResponse);
                    }
                    HttpStatusCode statusCode = httpResponse.StatusCode;
                    if (statusCode != HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        CloudException ex = CloudException.Create(httpRequest, null, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false));
                        if (shouldTrace)
                        {
                            TracingAdapter.Error(invocationId, ex);
                        }
                        throw ex;
                    }
                    
                    // Create Result
                    AzureOperationResponse result = null;
                    // Deserialize Response
                    result = new AzureOperationResponse();
                    result.StatusCode = statusCode;
                    if (httpResponse.Headers.Contains("x-ms-request-id"))
                    {
                        result.RequestId = httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault();
                    }
                    
                    if (shouldTrace)
                    {
                        TracingAdapter.Exit(invocationId, result);
                    }
                    return result;
                }
                finally
                {
                    if (httpResponse != null)
                    {
                        httpResponse.Dispose();
                    }
                }
            }
            finally
            {
                if (httpRequest != null)
                {
                    httpRequest.Dispose();
                }
            }
        }
        
        /// <summary>
        /// Retrieve the module identified by module name.  (see
        /// http://aka.ms/azureautomationsdk/moduleoperations for more
        /// information)
        /// </summary>
        /// <param name='resourceGroupName'>
        /// Required. The name of the resource group
        /// </param>
        /// <param name='automationAccount'>
        /// Required. The automation account name.
        /// </param>
        /// <param name='moduleName'>
        /// Required. The module name.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// The response model for the get module operation.
        /// </returns>
        public async Task<ModuleGetResponse> GetAsync(string resourceGroupName, string automationAccount, string moduleName, CancellationToken cancellationToken)
        {
            // Validate
            if (resourceGroupName == null)
            {
                throw new ArgumentNullException("resourceGroupName");
            }
            if (automationAccount == null)
            {
                throw new ArgumentNullException("automationAccount");
            }
            if (moduleName == null)
            {
                throw new ArgumentNullException("moduleName");
            }
            
            // Tracing
            bool shouldTrace = TracingAdapter.IsEnabled;
            string invocationId = null;
            if (shouldTrace)
            {
                invocationId = TracingAdapter.NextInvocationId.ToString();
                Dictionary<string, object> tracingParameters = new Dictionary<string, object>();
                tracingParameters.Add("resourceGroupName", resourceGroupName);
                tracingParameters.Add("automationAccount", automationAccount);
                tracingParameters.Add("moduleName", moduleName);
                TracingAdapter.Enter(invocationId, this, "GetAsync", tracingParameters);
            }
            
            // Construct URL
            string url = "";
            url = url + "/subscriptions/";
            if (this.Client.Credentials.SubscriptionId != null)
            {
                url = url + Uri.EscapeDataString(this.Client.Credentials.SubscriptionId);
            }
            url = url + "/resourceGroups/";
            url = url + Uri.EscapeDataString(resourceGroupName);
            url = url + "/providers/";
            if (this.Client.ResourceNamespace != null)
            {
                url = url + Uri.EscapeDataString(this.Client.ResourceNamespace);
            }
            url = url + "/automationAccounts/";
            url = url + Uri.EscapeDataString(automationAccount);
            url = url + "/modules/";
            url = url + Uri.EscapeDataString(moduleName);
            List<string> queryParameters = new List<string>();
            queryParameters.Add("api-version=2015-01-01-preview");
            if (queryParameters.Count > 0)
            {
                url = url + "?" + string.Join("&", queryParameters);
            }
            string baseUrl = this.Client.BaseUri.AbsoluteUri;
            // Trim '/' character from the end of baseUrl and beginning of url.
            if (baseUrl[baseUrl.Length - 1] == '/')
            {
                baseUrl = baseUrl.Substring(0, baseUrl.Length - 1);
            }
            if (url[0] == '/')
            {
                url = url.Substring(1);
            }
            url = baseUrl + "/" + url;
            url = url.Replace(" ", "%20");
            
            // Create HTTP transport objects
            HttpRequestMessage httpRequest = null;
            try
            {
                httpRequest = new HttpRequestMessage();
                httpRequest.Method = HttpMethod.Get;
                httpRequest.RequestUri = new Uri(url);
                
                // Set Headers
                httpRequest.Headers.Add("Accept", "application/json");
                httpRequest.Headers.Add("x-ms-version", "2014-06-01");
                
                // Set Credentials
                cancellationToken.ThrowIfCancellationRequested();
                await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false);
                
                // Send Request
                HttpResponseMessage httpResponse = null;
                try
                {
                    if (shouldTrace)
                    {
                        TracingAdapter.SendRequest(invocationId, httpRequest);
                    }
                    cancellationToken.ThrowIfCancellationRequested();
                    httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);
                    if (shouldTrace)
                    {
                        TracingAdapter.ReceiveResponse(invocationId, httpResponse);
                    }
                    HttpStatusCode statusCode = httpResponse.StatusCode;
                    if (statusCode != HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        CloudException ex = CloudException.Create(httpRequest, null, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false));
                        if (shouldTrace)
                        {
                            TracingAdapter.Error(invocationId, ex);
                        }
                        throw ex;
                    }
                    
                    // Create Result
                    ModuleGetResponse result = null;
                    // Deserialize Response
                    if (statusCode == HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
                        result = new ModuleGetResponse();
                        JToken responseDoc = null;
                        if (string.IsNullOrEmpty(responseContent) == false)
                        {
                            responseDoc = JToken.Parse(responseContent);
                        }
                        
                        if (responseDoc != null && responseDoc.Type != JTokenType.Null)
                        {
                            Module moduleInstance = new Module();
                            result.Module = moduleInstance;
                            
                            JToken propertiesValue = responseDoc["properties"];
                            if (propertiesValue != null && propertiesValue.Type != JTokenType.Null)
                            {
                                ModuleProperties propertiesInstance = new ModuleProperties();
                                moduleInstance.Properties = propertiesInstance;
                                
                                JToken isGlobalValue = propertiesValue["isGlobal"];
                                if (isGlobalValue != null && isGlobalValue.Type != JTokenType.Null)
                                {
                                    bool isGlobalInstance = ((bool)isGlobalValue);
                                    propertiesInstance.IsGlobal = isGlobalInstance;
                                }
                                
                                JToken versionValue = propertiesValue["version"];
                                if (versionValue != null && versionValue.Type != JTokenType.Null)
                                {
                                    string versionInstance = ((string)versionValue);
                                    propertiesInstance.Version = versionInstance;
                                }
                                
                                JToken sizeInBytesValue = propertiesValue["sizeInBytes"];
                                if (sizeInBytesValue != null && sizeInBytesValue.Type != JTokenType.Null)
                                {
                                    long sizeInBytesInstance = ((long)sizeInBytesValue);
                                    propertiesInstance.SizeInBytes = sizeInBytesInstance;
                                }
                                
                                JToken activityCountValue = propertiesValue["activityCount"];
                                if (activityCountValue != null && activityCountValue.Type != JTokenType.Null)
                                {
                                    int activityCountInstance = ((int)activityCountValue);
                                    propertiesInstance.ActivityCount = activityCountInstance;
                                }
                                
                                JToken provisioningStateValue = propertiesValue["provisioningState"];
                                if (provisioningStateValue != null && provisioningStateValue.Type != JTokenType.Null)
                                {
                                    ModuleProvisioningState provisioningStateInstance = ((ModuleProvisioningState)Enum.Parse(typeof(ModuleProvisioningState), ((string)provisioningStateValue), true));
                                    propertiesInstance.ProvisioningState = provisioningStateInstance;
                                }
                                
                                JToken contentLinkValue = propertiesValue["contentLink"];
                                if (contentLinkValue != null && contentLinkValue.Type != JTokenType.Null)
                                {
                                    ContentLink contentLinkInstance = new ContentLink();
                                    propertiesInstance.ContentLink = contentLinkInstance;
                                    
                                    JToken uriValue = contentLinkValue["uri"];
                                    if (uriValue != null && uriValue.Type != JTokenType.Null)
                                    {
                                        Uri uriInstance = TypeConversion.TryParseUri(((string)uriValue));
                                        contentLinkInstance.Uri = uriInstance;
                                    }
                                    
                                    JToken contentHashValue = contentLinkValue["contentHash"];
                                    if (contentHashValue != null && contentHashValue.Type != JTokenType.Null)
                                    {
                                        ContentHash contentHashInstance = new ContentHash();
                                        contentLinkInstance.ContentHash = contentHashInstance;
                                        
                                        JToken algorithmValue = contentHashValue["algorithm"];
                                        if (algorithmValue != null && algorithmValue.Type != JTokenType.Null)
                                        {
                                            string algorithmInstance = ((string)algorithmValue);
                                            contentHashInstance.Algorithm = algorithmInstance;
                                        }
                                        
                                        JToken valueValue = contentHashValue["value"];
                                        if (valueValue != null && valueValue.Type != JTokenType.Null)
                                        {
                                            string valueInstance = ((string)valueValue);
                                            contentHashInstance.Value = valueInstance;
                                        }
                                    }
                                    
                                    JToken versionValue2 = contentLinkValue["version"];
                                    if (versionValue2 != null && versionValue2.Type != JTokenType.Null)
                                    {
                                        string versionInstance2 = ((string)versionValue2);
                                        contentLinkInstance.Version = versionInstance2;
                                    }
                                }
                                
                                JToken creationTimeValue = propertiesValue["creationTime"];
                                if (creationTimeValue != null && creationTimeValue.Type != JTokenType.Null)
                                {
                                    DateTimeOffset creationTimeInstance = ((DateTimeOffset)creationTimeValue);
                                    propertiesInstance.CreationTime = creationTimeInstance;
                                }
                                
                                JToken lastModifiedTimeValue = propertiesValue["lastModifiedTime"];
                                if (lastModifiedTimeValue != null && lastModifiedTimeValue.Type != JTokenType.Null)
                                {
                                    DateTimeOffset lastModifiedTimeInstance = ((DateTimeOffset)lastModifiedTimeValue);
                                    propertiesInstance.LastModifiedTime = lastModifiedTimeInstance;
                                }
                                
                                JToken descriptionValue = propertiesValue["description"];
                                if (descriptionValue != null && descriptionValue.Type != JTokenType.Null)
                                {
                                    string descriptionInstance = ((string)descriptionValue);
                                    propertiesInstance.Description = descriptionInstance;
                                }
                            }
                            
                            JToken idValue = responseDoc["id"];
                            if (idValue != null && idValue.Type != JTokenType.Null)
                            {
                                string idInstance = ((string)idValue);
                                moduleInstance.Id = idInstance;
                            }
                            
                            JToken nameValue = responseDoc["name"];
                            if (nameValue != null && nameValue.Type != JTokenType.Null)
                            {
                                string nameInstance = ((string)nameValue);
                                moduleInstance.Name = nameInstance;
                            }
                            
                            JToken locationValue = responseDoc["location"];
                            if (locationValue != null && locationValue.Type != JTokenType.Null)
                            {
                                string locationInstance = ((string)locationValue);
                                moduleInstance.Location = locationInstance;
                            }
                            
                            JToken tagsSequenceElement = ((JToken)responseDoc["tags"]);
                            if (tagsSequenceElement != null && tagsSequenceElement.Type != JTokenType.Null)
                            {
                                foreach (JProperty property in tagsSequenceElement)
                                {
                                    string tagsKey = ((string)property.Name);
                                    string tagsValue = ((string)property.Value);
                                    moduleInstance.Tags.Add(tagsKey, tagsValue);
                                }
                            }
                            
                            JToken typeValue = responseDoc["type"];
                            if (typeValue != null && typeValue.Type != JTokenType.Null)
                            {
                                string typeInstance = ((string)typeValue);
                                moduleInstance.Type = typeInstance;
                            }
                            
                            JToken etagValue = responseDoc["etag"];
                            if (etagValue != null && etagValue.Type != JTokenType.Null)
                            {
                                string etagInstance = ((string)etagValue);
                                moduleInstance.Etag = etagInstance;
                            }
                        }
                        
                    }
                    result.StatusCode = statusCode;
                    if (httpResponse.Headers.Contains("x-ms-request-id"))
                    {
                        result.RequestId = httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault();
                    }
                    
                    if (shouldTrace)
                    {
                        TracingAdapter.Exit(invocationId, result);
                    }
                    return result;
                }
                finally
                {
                    if (httpResponse != null)
                    {
                        httpResponse.Dispose();
                    }
                }
            }
            finally
            {
                if (httpRequest != null)
                {
                    httpRequest.Dispose();
                }
            }
        }
        
        /// <summary>
        /// Retrieve a list of modules.  (see
        /// http://aka.ms/azureautomationsdk/moduleoperations for more
        /// information)
        /// </summary>
        /// <param name='resourceGroupName'>
        /// Required. The name of the resource group
        /// </param>
        /// <param name='automationAccount'>
        /// Required. The automation account name.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// The response model for the list module operation.
        /// </returns>
        public async Task<ModuleListResponse> ListAsync(string resourceGroupName, string automationAccount, CancellationToken cancellationToken)
        {
            // Validate
            if (resourceGroupName == null)
            {
                throw new ArgumentNullException("resourceGroupName");
            }
            if (automationAccount == null)
            {
                throw new ArgumentNullException("automationAccount");
            }
            
            // Tracing
            bool shouldTrace = TracingAdapter.IsEnabled;
            string invocationId = null;
            if (shouldTrace)
            {
                invocationId = TracingAdapter.NextInvocationId.ToString();
                Dictionary<string, object> tracingParameters = new Dictionary<string, object>();
                tracingParameters.Add("resourceGroupName", resourceGroupName);
                tracingParameters.Add("automationAccount", automationAccount);
                TracingAdapter.Enter(invocationId, this, "ListAsync", tracingParameters);
            }
            
            // Construct URL
            string url = "";
            url = url + "/subscriptions/";
            if (this.Client.Credentials.SubscriptionId != null)
            {
                url = url + Uri.EscapeDataString(this.Client.Credentials.SubscriptionId);
            }
            url = url + "/resourceGroups/";
            url = url + Uri.EscapeDataString(resourceGroupName);
            url = url + "/providers/";
            if (this.Client.ResourceNamespace != null)
            {
                url = url + Uri.EscapeDataString(this.Client.ResourceNamespace);
            }
            url = url + "/automationAccounts/";
            url = url + Uri.EscapeDataString(automationAccount);
            url = url + "/modules";
            List<string> queryParameters = new List<string>();
            queryParameters.Add("api-version=2015-01-01-preview");
            if (queryParameters.Count > 0)
            {
                url = url + "?" + string.Join("&", queryParameters);
            }
            string baseUrl = this.Client.BaseUri.AbsoluteUri;
            // Trim '/' character from the end of baseUrl and beginning of url.
            if (baseUrl[baseUrl.Length - 1] == '/')
            {
                baseUrl = baseUrl.Substring(0, baseUrl.Length - 1);
            }
            if (url[0] == '/')
            {
                url = url.Substring(1);
            }
            url = baseUrl + "/" + url;
            url = url.Replace(" ", "%20");
            
            // Create HTTP transport objects
            HttpRequestMessage httpRequest = null;
            try
            {
                httpRequest = new HttpRequestMessage();
                httpRequest.Method = HttpMethod.Get;
                httpRequest.RequestUri = new Uri(url);
                
                // Set Headers
                httpRequest.Headers.Add("Accept", "application/json");
                httpRequest.Headers.Add("ocp-referer", url);
                httpRequest.Headers.Add("x-ms-version", "2014-06-01");
                
                // Set Credentials
                cancellationToken.ThrowIfCancellationRequested();
                await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false);
                
                // Send Request
                HttpResponseMessage httpResponse = null;
                try
                {
                    if (shouldTrace)
                    {
                        TracingAdapter.SendRequest(invocationId, httpRequest);
                    }
                    cancellationToken.ThrowIfCancellationRequested();
                    httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);
                    if (shouldTrace)
                    {
                        TracingAdapter.ReceiveResponse(invocationId, httpResponse);
                    }
                    HttpStatusCode statusCode = httpResponse.StatusCode;
                    if (statusCode != HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        CloudException ex = CloudException.Create(httpRequest, null, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false));
                        if (shouldTrace)
                        {
                            TracingAdapter.Error(invocationId, ex);
                        }
                        throw ex;
                    }
                    
                    // Create Result
                    ModuleListResponse result = null;
                    // Deserialize Response
                    if (statusCode == HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
                        result = new ModuleListResponse();
                        JToken responseDoc = null;
                        if (string.IsNullOrEmpty(responseContent) == false)
                        {
                            responseDoc = JToken.Parse(responseContent);
                        }
                        
                        if (responseDoc != null && responseDoc.Type != JTokenType.Null)
                        {
                            JToken valueArray = responseDoc["value"];
                            if (valueArray != null && valueArray.Type != JTokenType.Null)
                            {
                                foreach (JToken valueValue in ((JArray)valueArray))
                                {
                                    Module moduleInstance = new Module();
                                    result.Modules.Add(moduleInstance);
                                    
                                    JToken propertiesValue = valueValue["properties"];
                                    if (propertiesValue != null && propertiesValue.Type != JTokenType.Null)
                                    {
                                        ModuleProperties propertiesInstance = new ModuleProperties();
                                        moduleInstance.Properties = propertiesInstance;
                                        
                                        JToken isGlobalValue = propertiesValue["isGlobal"];
                                        if (isGlobalValue != null && isGlobalValue.Type != JTokenType.Null)
                                        {
                                            bool isGlobalInstance = ((bool)isGlobalValue);
                                            propertiesInstance.IsGlobal = isGlobalInstance;
                                        }
                                        
                                        JToken versionValue = propertiesValue["version"];
                                        if (versionValue != null && versionValue.Type != JTokenType.Null)
                                        {
                                            string versionInstance = ((string)versionValue);
                                            propertiesInstance.Version = versionInstance;
                                        }
                                        
                                        JToken sizeInBytesValue = propertiesValue["sizeInBytes"];
                                        if (sizeInBytesValue != null && sizeInBytesValue.Type != JTokenType.Null)
                                        {
                                            long sizeInBytesInstance = ((long)sizeInBytesValue);
                                            propertiesInstance.SizeInBytes = sizeInBytesInstance;
                                        }
                                        
                                        JToken activityCountValue = propertiesValue["activityCount"];
                                        if (activityCountValue != null && activityCountValue.Type != JTokenType.Null)
                                        {
                                            int activityCountInstance = ((int)activityCountValue);
                                            propertiesInstance.ActivityCount = activityCountInstance;
                                        }
                                        
                                        JToken provisioningStateValue = propertiesValue["provisioningState"];
                                        if (provisioningStateValue != null && provisioningStateValue.Type != JTokenType.Null)
                                        {
                                            ModuleProvisioningState provisioningStateInstance = ((ModuleProvisioningState)Enum.Parse(typeof(ModuleProvisioningState), ((string)provisioningStateValue), true));
                                            propertiesInstance.ProvisioningState = provisioningStateInstance;
                                        }
                                        
                                        JToken contentLinkValue = propertiesValue["contentLink"];
                                        if (contentLinkValue != null && contentLinkValue.Type != JTokenType.Null)
                                        {
                                            ContentLink contentLinkInstance = new ContentLink();
                                            propertiesInstance.ContentLink = contentLinkInstance;
                                            
                                            JToken uriValue = contentLinkValue["uri"];
                                            if (uriValue != null && uriValue.Type != JTokenType.Null)
                                            {
                                                Uri uriInstance = TypeConversion.TryParseUri(((string)uriValue));
                                                contentLinkInstance.Uri = uriInstance;
                                            }
                                            
                                            JToken contentHashValue = contentLinkValue["contentHash"];
                                            if (contentHashValue != null && contentHashValue.Type != JTokenType.Null)
                                            {
                                                ContentHash contentHashInstance = new ContentHash();
                                                contentLinkInstance.ContentHash = contentHashInstance;
                                                
                                                JToken algorithmValue = contentHashValue["algorithm"];
                                                if (algorithmValue != null && algorithmValue.Type != JTokenType.Null)
                                                {
                                                    string algorithmInstance = ((string)algorithmValue);
                                                    contentHashInstance.Algorithm = algorithmInstance;
                                                }
                                                
                                                JToken valueValue2 = contentHashValue["value"];
                                                if (valueValue2 != null && valueValue2.Type != JTokenType.Null)
                                                {
                                                    string valueInstance = ((string)valueValue2);
                                                    contentHashInstance.Value = valueInstance;
                                                }
                                            }
                                            
                                            JToken versionValue2 = contentLinkValue["version"];
                                            if (versionValue2 != null && versionValue2.Type != JTokenType.Null)
                                            {
                                                string versionInstance2 = ((string)versionValue2);
                                                contentLinkInstance.Version = versionInstance2;
                                            }
                                        }
                                        
                                        JToken creationTimeValue = propertiesValue["creationTime"];
                                        if (creationTimeValue != null && creationTimeValue.Type != JTokenType.Null)
                                        {
                                            DateTimeOffset creationTimeInstance = ((DateTimeOffset)creationTimeValue);
                                            propertiesInstance.CreationTime = creationTimeInstance;
                                        }
                                        
                                        JToken lastModifiedTimeValue = propertiesValue["lastModifiedTime"];
                                        if (lastModifiedTimeValue != null && lastModifiedTimeValue.Type != JTokenType.Null)
                                        {
                                            DateTimeOffset lastModifiedTimeInstance = ((DateTimeOffset)lastModifiedTimeValue);
                                            propertiesInstance.LastModifiedTime = lastModifiedTimeInstance;
                                        }
                                        
                                        JToken descriptionValue = propertiesValue["description"];
                                        if (descriptionValue != null && descriptionValue.Type != JTokenType.Null)
                                        {
                                            string descriptionInstance = ((string)descriptionValue);
                                            propertiesInstance.Description = descriptionInstance;
                                        }
                                    }
                                    
                                    JToken idValue = valueValue["id"];
                                    if (idValue != null && idValue.Type != JTokenType.Null)
                                    {
                                        string idInstance = ((string)idValue);
                                        moduleInstance.Id = idInstance;
                                    }
                                    
                                    JToken nameValue = valueValue["name"];
                                    if (nameValue != null && nameValue.Type != JTokenType.Null)
                                    {
                                        string nameInstance = ((string)nameValue);
                                        moduleInstance.Name = nameInstance;
                                    }
                                    
                                    JToken locationValue = valueValue["location"];
                                    if (locationValue != null && locationValue.Type != JTokenType.Null)
                                    {
                                        string locationInstance = ((string)locationValue);
                                        moduleInstance.Location = locationInstance;
                                    }
                                    
                                    JToken tagsSequenceElement = ((JToken)valueValue["tags"]);
                                    if (tagsSequenceElement != null && tagsSequenceElement.Type != JTokenType.Null)
                                    {
                                        foreach (JProperty property in tagsSequenceElement)
                                        {
                                            string tagsKey = ((string)property.Name);
                                            string tagsValue = ((string)property.Value);
                                            moduleInstance.Tags.Add(tagsKey, tagsValue);
                                        }
                                    }
                                    
                                    JToken typeValue = valueValue["type"];
                                    if (typeValue != null && typeValue.Type != JTokenType.Null)
                                    {
                                        string typeInstance = ((string)typeValue);
                                        moduleInstance.Type = typeInstance;
                                    }
                                    
                                    JToken etagValue = valueValue["etag"];
                                    if (etagValue != null && etagValue.Type != JTokenType.Null)
                                    {
                                        string etagInstance = ((string)etagValue);
                                        moduleInstance.Etag = etagInstance;
                                    }
                                }
                            }
                            
                            JToken odatanextLinkValue = responseDoc["odata.nextLink"];
                            if (odatanextLinkValue != null && odatanextLinkValue.Type != JTokenType.Null)
                            {
                                string odatanextLinkInstance = Regex.Match(((string)odatanextLinkValue), "^.*[&\\?]\\$skiptoken=([^&]*)(&.*)?").Groups[1].Value;
                                result.SkipToken = odatanextLinkInstance;
                            }
                            
                            JToken nextLinkValue = responseDoc["nextLink"];
                            if (nextLinkValue != null && nextLinkValue.Type != JTokenType.Null)
                            {
                                string nextLinkInstance = ((string)nextLinkValue);
                                result.NextLink = nextLinkInstance;
                            }
                        }
                        
                    }
                    result.StatusCode = statusCode;
                    if (httpResponse.Headers.Contains("x-ms-request-id"))
                    {
                        result.RequestId = httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault();
                    }
                    
                    if (shouldTrace)
                    {
                        TracingAdapter.Exit(invocationId, result);
                    }
                    return result;
                }
                finally
                {
                    if (httpResponse != null)
                    {
                        httpResponse.Dispose();
                    }
                }
            }
            finally
            {
                if (httpRequest != null)
                {
                    httpRequest.Dispose();
                }
            }
        }
        
        /// <summary>
        /// Retrieve next list of modules.  (see
        /// http://aka.ms/azureautomationsdk/moduleoperations for more
        /// information)
        /// </summary>
        /// <param name='nextLink'>
        /// Required. The link to retrieve next set of items.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// The response model for the list module operation.
        /// </returns>
        public async Task<ModuleListResponse> ListNextAsync(string nextLink, CancellationToken cancellationToken)
        {
            // Validate
            if (nextLink == null)
            {
                throw new ArgumentNullException("nextLink");
            }
            
            // Tracing
            bool shouldTrace = TracingAdapter.IsEnabled;
            string invocationId = null;
            if (shouldTrace)
            {
                invocationId = TracingAdapter.NextInvocationId.ToString();
                Dictionary<string, object> tracingParameters = new Dictionary<string, object>();
                tracingParameters.Add("nextLink", nextLink);
                TracingAdapter.Enter(invocationId, this, "ListNextAsync", tracingParameters);
            }
            
            // Construct URL
            string url = "";
            url = url + nextLink;
            url = url.Replace(" ", "%20");
            
            // Create HTTP transport objects
            HttpRequestMessage httpRequest = null;
            try
            {
                httpRequest = new HttpRequestMessage();
                httpRequest.Method = HttpMethod.Get;
                httpRequest.RequestUri = new Uri(url);
                
                // Set Headers
                httpRequest.Headers.Add("Accept", "application/json");
                httpRequest.Headers.Add("ocp-referer", url);
                httpRequest.Headers.Add("x-ms-version", "2014-06-01");
                
                // Set Credentials
                cancellationToken.ThrowIfCancellationRequested();
                await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false);
                
                // Send Request
                HttpResponseMessage httpResponse = null;
                try
                {
                    if (shouldTrace)
                    {
                        TracingAdapter.SendRequest(invocationId, httpRequest);
                    }
                    cancellationToken.ThrowIfCancellationRequested();
                    httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);
                    if (shouldTrace)
                    {
                        TracingAdapter.ReceiveResponse(invocationId, httpResponse);
                    }
                    HttpStatusCode statusCode = httpResponse.StatusCode;
                    if (statusCode != HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        CloudException ex = CloudException.Create(httpRequest, null, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false));
                        if (shouldTrace)
                        {
                            TracingAdapter.Error(invocationId, ex);
                        }
                        throw ex;
                    }
                    
                    // Create Result
                    ModuleListResponse result = null;
                    // Deserialize Response
                    if (statusCode == HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
                        result = new ModuleListResponse();
                        JToken responseDoc = null;
                        if (string.IsNullOrEmpty(responseContent) == false)
                        {
                            responseDoc = JToken.Parse(responseContent);
                        }
                        
                        if (responseDoc != null && responseDoc.Type != JTokenType.Null)
                        {
                            JToken valueArray = responseDoc["value"];
                            if (valueArray != null && valueArray.Type != JTokenType.Null)
                            {
                                foreach (JToken valueValue in ((JArray)valueArray))
                                {
                                    Module moduleInstance = new Module();
                                    result.Modules.Add(moduleInstance);
                                    
                                    JToken propertiesValue = valueValue["properties"];
                                    if (propertiesValue != null && propertiesValue.Type != JTokenType.Null)
                                    {
                                        ModuleProperties propertiesInstance = new ModuleProperties();
                                        moduleInstance.Properties = propertiesInstance;
                                        
                                        JToken isGlobalValue = propertiesValue["isGlobal"];
                                        if (isGlobalValue != null && isGlobalValue.Type != JTokenType.Null)
                                        {
                                            bool isGlobalInstance = ((bool)isGlobalValue);
                                            propertiesInstance.IsGlobal = isGlobalInstance;
                                        }
                                        
                                        JToken versionValue = propertiesValue["version"];
                                        if (versionValue != null && versionValue.Type != JTokenType.Null)
                                        {
                                            string versionInstance = ((string)versionValue);
                                            propertiesInstance.Version = versionInstance;
                                        }
                                        
                                        JToken sizeInBytesValue = propertiesValue["sizeInBytes"];
                                        if (sizeInBytesValue != null && sizeInBytesValue.Type != JTokenType.Null)
                                        {
                                            long sizeInBytesInstance = ((long)sizeInBytesValue);
                                            propertiesInstance.SizeInBytes = sizeInBytesInstance;
                                        }
                                        
                                        JToken activityCountValue = propertiesValue["activityCount"];
                                        if (activityCountValue != null && activityCountValue.Type != JTokenType.Null)
                                        {
                                            int activityCountInstance = ((int)activityCountValue);
                                            propertiesInstance.ActivityCount = activityCountInstance;
                                        }
                                        
                                        JToken provisioningStateValue = propertiesValue["provisioningState"];
                                        if (provisioningStateValue != null && provisioningStateValue.Type != JTokenType.Null)
                                        {
                                            ModuleProvisioningState provisioningStateInstance = ((ModuleProvisioningState)Enum.Parse(typeof(ModuleProvisioningState), ((string)provisioningStateValue), true));
                                            propertiesInstance.ProvisioningState = provisioningStateInstance;
                                        }
                                        
                                        JToken contentLinkValue = propertiesValue["contentLink"];
                                        if (contentLinkValue != null && contentLinkValue.Type != JTokenType.Null)
                                        {
                                            ContentLink contentLinkInstance = new ContentLink();
                                            propertiesInstance.ContentLink = contentLinkInstance;
                                            
                                            JToken uriValue = contentLinkValue["uri"];
                                            if (uriValue != null && uriValue.Type != JTokenType.Null)
                                            {
                                                Uri uriInstance = TypeConversion.TryParseUri(((string)uriValue));
                                                contentLinkInstance.Uri = uriInstance;
                                            }
                                            
                                            JToken contentHashValue = contentLinkValue["contentHash"];
                                            if (contentHashValue != null && contentHashValue.Type != JTokenType.Null)
                                            {
                                                ContentHash contentHashInstance = new ContentHash();
                                                contentLinkInstance.ContentHash = contentHashInstance;
                                                
                                                JToken algorithmValue = contentHashValue["algorithm"];
                                                if (algorithmValue != null && algorithmValue.Type != JTokenType.Null)
                                                {
                                                    string algorithmInstance = ((string)algorithmValue);
                                                    contentHashInstance.Algorithm = algorithmInstance;
                                                }
                                                
                                                JToken valueValue2 = contentHashValue["value"];
                                                if (valueValue2 != null && valueValue2.Type != JTokenType.Null)
                                                {
                                                    string valueInstance = ((string)valueValue2);
                                                    contentHashInstance.Value = valueInstance;
                                                }
                                            }
                                            
                                            JToken versionValue2 = contentLinkValue["version"];
                                            if (versionValue2 != null && versionValue2.Type != JTokenType.Null)
                                            {
                                                string versionInstance2 = ((string)versionValue2);
                                                contentLinkInstance.Version = versionInstance2;
                                            }
                                        }
                                        
                                        JToken creationTimeValue = propertiesValue["creationTime"];
                                        if (creationTimeValue != null && creationTimeValue.Type != JTokenType.Null)
                                        {
                                            DateTimeOffset creationTimeInstance = ((DateTimeOffset)creationTimeValue);
                                            propertiesInstance.CreationTime = creationTimeInstance;
                                        }
                                        
                                        JToken lastModifiedTimeValue = propertiesValue["lastModifiedTime"];
                                        if (lastModifiedTimeValue != null && lastModifiedTimeValue.Type != JTokenType.Null)
                                        {
                                            DateTimeOffset lastModifiedTimeInstance = ((DateTimeOffset)lastModifiedTimeValue);
                                            propertiesInstance.LastModifiedTime = lastModifiedTimeInstance;
                                        }
                                        
                                        JToken descriptionValue = propertiesValue["description"];
                                        if (descriptionValue != null && descriptionValue.Type != JTokenType.Null)
                                        {
                                            string descriptionInstance = ((string)descriptionValue);
                                            propertiesInstance.Description = descriptionInstance;
                                        }
                                    }
                                    
                                    JToken idValue = valueValue["id"];
                                    if (idValue != null && idValue.Type != JTokenType.Null)
                                    {
                                        string idInstance = ((string)idValue);
                                        moduleInstance.Id = idInstance;
                                    }
                                    
                                    JToken nameValue = valueValue["name"];
                                    if (nameValue != null && nameValue.Type != JTokenType.Null)
                                    {
                                        string nameInstance = ((string)nameValue);
                                        moduleInstance.Name = nameInstance;
                                    }
                                    
                                    JToken locationValue = valueValue["location"];
                                    if (locationValue != null && locationValue.Type != JTokenType.Null)
                                    {
                                        string locationInstance = ((string)locationValue);
                                        moduleInstance.Location = locationInstance;
                                    }
                                    
                                    JToken tagsSequenceElement = ((JToken)valueValue["tags"]);
                                    if (tagsSequenceElement != null && tagsSequenceElement.Type != JTokenType.Null)
                                    {
                                        foreach (JProperty property in tagsSequenceElement)
                                        {
                                            string tagsKey = ((string)property.Name);
                                            string tagsValue = ((string)property.Value);
                                            moduleInstance.Tags.Add(tagsKey, tagsValue);
                                        }
                                    }
                                    
                                    JToken typeValue = valueValue["type"];
                                    if (typeValue != null && typeValue.Type != JTokenType.Null)
                                    {
                                        string typeInstance = ((string)typeValue);
                                        moduleInstance.Type = typeInstance;
                                    }
                                    
                                    JToken etagValue = valueValue["etag"];
                                    if (etagValue != null && etagValue.Type != JTokenType.Null)
                                    {
                                        string etagInstance = ((string)etagValue);
                                        moduleInstance.Etag = etagInstance;
                                    }
                                }
                            }
                            
                            JToken odatanextLinkValue = responseDoc["odata.nextLink"];
                            if (odatanextLinkValue != null && odatanextLinkValue.Type != JTokenType.Null)
                            {
                                string odatanextLinkInstance = Regex.Match(((string)odatanextLinkValue), "^.*[&\\?]\\$skiptoken=([^&]*)(&.*)?").Groups[1].Value;
                                result.SkipToken = odatanextLinkInstance;
                            }
                            
                            JToken nextLinkValue = responseDoc["nextLink"];
                            if (nextLinkValue != null && nextLinkValue.Type != JTokenType.Null)
                            {
                                string nextLinkInstance = ((string)nextLinkValue);
                                result.NextLink = nextLinkInstance;
                            }
                        }
                        
                    }
                    result.StatusCode = statusCode;
                    if (httpResponse.Headers.Contains("x-ms-request-id"))
                    {
                        result.RequestId = httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault();
                    }
                    
                    if (shouldTrace)
                    {
                        TracingAdapter.Exit(invocationId, result);
                    }
                    return result;
                }
                finally
                {
                    if (httpResponse != null)
                    {
                        httpResponse.Dispose();
                    }
                }
            }
            finally
            {
                if (httpRequest != null)
                {
                    httpRequest.Dispose();
                }
            }
        }
        
        /// <summary>
        /// Create the module identified by module name.  (see
        /// http://aka.ms/azureautomationsdk/moduleoperations for more
        /// information)
        /// </summary>
        /// <param name='resourceGroupName'>
        /// Required. The name of the resource group
        /// </param>
        /// <param name='automationAccount'>
        /// Required. The automation account name.
        /// </param>
        /// <param name='parameters'>
        /// Required. The patch parameters for module.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// The response model for the get module operation.
        /// </returns>
        public async Task<ModuleGetResponse> PatchAsync(string resourceGroupName, string automationAccount, ModulePatchParameters parameters, CancellationToken cancellationToken)
        {
            // Validate
            if (resourceGroupName == null)
            {
                throw new ArgumentNullException("resourceGroupName");
            }
            if (automationAccount == null)
            {
                throw new ArgumentNullException("automationAccount");
            }
            if (parameters == null)
            {
                throw new ArgumentNullException("parameters");
            }
            if (parameters.Properties == null)
            {
                throw new ArgumentNullException("parameters.Properties");
            }
            if (parameters.Properties.ContentLink != null)
            {
                if (parameters.Properties.ContentLink.ContentHash != null)
                {
                    if (parameters.Properties.ContentLink.ContentHash.Algorithm == null)
                    {
                        throw new ArgumentNullException("parameters.Properties.ContentLink.ContentHash.Algorithm");
                    }
                    if (parameters.Properties.ContentLink.ContentHash.Value == null)
                    {
                        throw new ArgumentNullException("parameters.Properties.ContentLink.ContentHash.Value");
                    }
                }
            }
            
            // Tracing
            bool shouldTrace = TracingAdapter.IsEnabled;
            string invocationId = null;
            if (shouldTrace)
            {
                invocationId = TracingAdapter.NextInvocationId.ToString();
                Dictionary<string, object> tracingParameters = new Dictionary<string, object>();
                tracingParameters.Add("resourceGroupName", resourceGroupName);
                tracingParameters.Add("automationAccount", automationAccount);
                tracingParameters.Add("parameters", parameters);
                TracingAdapter.Enter(invocationId, this, "PatchAsync", tracingParameters);
            }
            
            // Construct URL
            string url = "";
            url = url + "/subscriptions/";
            if (this.Client.Credentials.SubscriptionId != null)
            {
                url = url + Uri.EscapeDataString(this.Client.Credentials.SubscriptionId);
            }
            url = url + "/resourceGroups/";
            url = url + Uri.EscapeDataString(resourceGroupName);
            url = url + "/providers/";
            if (this.Client.ResourceNamespace != null)
            {
                url = url + Uri.EscapeDataString(this.Client.ResourceNamespace);
            }
            url = url + "/automationAccounts/";
            url = url + Uri.EscapeDataString(automationAccount);
            url = url + "/modules/";
            if (parameters.Name != null)
            {
                url = url + Uri.EscapeDataString(parameters.Name);
            }
            List<string> queryParameters = new List<string>();
            queryParameters.Add("api-version=2015-01-01-preview");
            if (queryParameters.Count > 0)
            {
                url = url + "?" + string.Join("&", queryParameters);
            }
            string baseUrl = this.Client.BaseUri.AbsoluteUri;
            // Trim '/' character from the end of baseUrl and beginning of url.
            if (baseUrl[baseUrl.Length - 1] == '/')
            {
                baseUrl = baseUrl.Substring(0, baseUrl.Length - 1);
            }
            if (url[0] == '/')
            {
                url = url.Substring(1);
            }
            url = baseUrl + "/" + url;
            url = url.Replace(" ", "%20");
            
            // Create HTTP transport objects
            HttpRequestMessage httpRequest = null;
            try
            {
                httpRequest = new HttpRequestMessage();
                httpRequest.Method = new HttpMethod("PATCH");
                httpRequest.RequestUri = new Uri(url);
                
                // Set Headers
                httpRequest.Headers.Add("Accept", "application/json");
                httpRequest.Headers.Add("x-ms-version", "2014-06-01");
                
                // Set Credentials
                cancellationToken.ThrowIfCancellationRequested();
                await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false);
                
                // Serialize Request
                string requestContent = null;
                JToken requestDoc = null;
                
                JObject modulePatchParametersValue = new JObject();
                requestDoc = modulePatchParametersValue;
                
                JObject propertiesValue = new JObject();
                modulePatchParametersValue["properties"] = propertiesValue;
                
                if (parameters.Properties.ContentLink != null)
                {
                    JObject contentLinkValue = new JObject();
                    propertiesValue["contentLink"] = contentLinkValue;
                    
                    if (parameters.Properties.ContentLink.Uri != null)
                    {
                        contentLinkValue["uri"] = parameters.Properties.ContentLink.Uri.AbsoluteUri;
                    }
                    
                    if (parameters.Properties.ContentLink.ContentHash != null)
                    {
                        JObject contentHashValue = new JObject();
                        contentLinkValue["contentHash"] = contentHashValue;
                        
                        contentHashValue["algorithm"] = parameters.Properties.ContentLink.ContentHash.Algorithm;
                        
                        contentHashValue["value"] = parameters.Properties.ContentLink.ContentHash.Value;
                    }
                    
                    if (parameters.Properties.ContentLink.Version != null)
                    {
                        contentLinkValue["version"] = parameters.Properties.ContentLink.Version;
                    }
                }
                
                if (parameters.Name != null)
                {
                    modulePatchParametersValue["name"] = parameters.Name;
                }
                
                if (parameters.Location != null)
                {
                    modulePatchParametersValue["location"] = parameters.Location;
                }
                
                if (parameters.Tags != null)
                {
                    JObject tagsDictionary = new JObject();
                    foreach (KeyValuePair<string, string> pair in parameters.Tags)
                    {
                        string tagsKey = pair.Key;
                        string tagsValue = pair.Value;
                        tagsDictionary[tagsKey] = tagsValue;
                    }
                    modulePatchParametersValue["tags"] = tagsDictionary;
                }
                
                requestContent = requestDoc.ToString(Newtonsoft.Json.Formatting.Indented);
                httpRequest.Content = new StringContent(requestContent, Encoding.UTF8);
                httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8");
                
                // Send Request
                HttpResponseMessage httpResponse = null;
                try
                {
                    if (shouldTrace)
                    {
                        TracingAdapter.SendRequest(invocationId, httpRequest);
                    }
                    cancellationToken.ThrowIfCancellationRequested();
                    httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);
                    if (shouldTrace)
                    {
                        TracingAdapter.ReceiveResponse(invocationId, httpResponse);
                    }
                    HttpStatusCode statusCode = httpResponse.StatusCode;
                    if (statusCode != HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        CloudException ex = CloudException.Create(httpRequest, requestContent, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false));
                        if (shouldTrace)
                        {
                            TracingAdapter.Error(invocationId, ex);
                        }
                        throw ex;
                    }
                    
                    // Create Result
                    ModuleGetResponse result = null;
                    // Deserialize Response
                    if (statusCode == HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
                        result = new ModuleGetResponse();
                        JToken responseDoc = null;
                        if (string.IsNullOrEmpty(responseContent) == false)
                        {
                            responseDoc = JToken.Parse(responseContent);
                        }
                        
                        if (responseDoc != null && responseDoc.Type != JTokenType.Null)
                        {
                            Module moduleInstance = new Module();
                            result.Module = moduleInstance;
                            
                            JToken propertiesValue2 = responseDoc["properties"];
                            if (propertiesValue2 != null && propertiesValue2.Type != JTokenType.Null)
                            {
                                ModuleProperties propertiesInstance = new ModuleProperties();
                                moduleInstance.Properties = propertiesInstance;
                                
                                JToken isGlobalValue = propertiesValue2["isGlobal"];
                                if (isGlobalValue != null && isGlobalValue.Type != JTokenType.Null)
                                {
                                    bool isGlobalInstance = ((bool)isGlobalValue);
                                    propertiesInstance.IsGlobal = isGlobalInstance;
                                }
                                
                                JToken versionValue = propertiesValue2["version"];
                                if (versionValue != null && versionValue.Type != JTokenType.Null)
                                {
                                    string versionInstance = ((string)versionValue);
                                    propertiesInstance.Version = versionInstance;
                                }
                                
                                JToken sizeInBytesValue = propertiesValue2["sizeInBytes"];
                                if (sizeInBytesValue != null && sizeInBytesValue.Type != JTokenType.Null)
                                {
                                    long sizeInBytesInstance = ((long)sizeInBytesValue);
                                    propertiesInstance.SizeInBytes = sizeInBytesInstance;
                                }
                                
                                JToken activityCountValue = propertiesValue2["activityCount"];
                                if (activityCountValue != null && activityCountValue.Type != JTokenType.Null)
                                {
                                    int activityCountInstance = ((int)activityCountValue);
                                    propertiesInstance.ActivityCount = activityCountInstance;
                                }
                                
                                JToken provisioningStateValue = propertiesValue2["provisioningState"];
                                if (provisioningStateValue != null && provisioningStateValue.Type != JTokenType.Null)
                                {
                                    ModuleProvisioningState provisioningStateInstance = ((ModuleProvisioningState)Enum.Parse(typeof(ModuleProvisioningState), ((string)provisioningStateValue), true));
                                    propertiesInstance.ProvisioningState = provisioningStateInstance;
                                }
                                
                                JToken contentLinkValue2 = propertiesValue2["contentLink"];
                                if (contentLinkValue2 != null && contentLinkValue2.Type != JTokenType.Null)
                                {
                                    ContentLink contentLinkInstance = new ContentLink();
                                    propertiesInstance.ContentLink = contentLinkInstance;
                                    
                                    JToken uriValue = contentLinkValue2["uri"];
                                    if (uriValue != null && uriValue.Type != JTokenType.Null)
                                    {
                                        Uri uriInstance = TypeConversion.TryParseUri(((string)uriValue));
                                        contentLinkInstance.Uri = uriInstance;
                                    }
                                    
                                    JToken contentHashValue2 = contentLinkValue2["contentHash"];
                                    if (contentHashValue2 != null && contentHashValue2.Type != JTokenType.Null)
                                    {
                                        ContentHash contentHashInstance = new ContentHash();
                                        contentLinkInstance.ContentHash = contentHashInstance;
                                        
                                        JToken algorithmValue = contentHashValue2["algorithm"];
                                        if (algorithmValue != null && algorithmValue.Type != JTokenType.Null)
                                        {
                                            string algorithmInstance = ((string)algorithmValue);
                                            contentHashInstance.Algorithm = algorithmInstance;
                                        }
                                        
                                        JToken valueValue = contentHashValue2["value"];
                                        if (valueValue != null && valueValue.Type != JTokenType.Null)
                                        {
                                            string valueInstance = ((string)valueValue);
                                            contentHashInstance.Value = valueInstance;
                                        }
                                    }
                                    
                                    JToken versionValue2 = contentLinkValue2["version"];
                                    if (versionValue2 != null && versionValue2.Type != JTokenType.Null)
                                    {
                                        string versionInstance2 = ((string)versionValue2);
                                        contentLinkInstance.Version = versionInstance2;
                                    }
                                }
                                
                                JToken creationTimeValue = propertiesValue2["creationTime"];
                                if (creationTimeValue != null && creationTimeValue.Type != JTokenType.Null)
                                {
                                    DateTimeOffset creationTimeInstance = ((DateTimeOffset)creationTimeValue);
                                    propertiesInstance.CreationTime = creationTimeInstance;
                                }
                                
                                JToken lastModifiedTimeValue = propertiesValue2["lastModifiedTime"];
                                if (lastModifiedTimeValue != null && lastModifiedTimeValue.Type != JTokenType.Null)
                                {
                                    DateTimeOffset lastModifiedTimeInstance = ((DateTimeOffset)lastModifiedTimeValue);
                                    propertiesInstance.LastModifiedTime = lastModifiedTimeInstance;
                                }
                                
                                JToken descriptionValue = propertiesValue2["description"];
                                if (descriptionValue != null && descriptionValue.Type != JTokenType.Null)
                                {
                                    string descriptionInstance = ((string)descriptionValue);
                                    propertiesInstance.Description = descriptionInstance;
                                }
                            }
                            
                            JToken idValue = responseDoc["id"];
                            if (idValue != null && idValue.Type != JTokenType.Null)
                            {
                                string idInstance = ((string)idValue);
                                moduleInstance.Id = idInstance;
                            }
                            
                            JToken nameValue = responseDoc["name"];
                            if (nameValue != null && nameValue.Type != JTokenType.Null)
                            {
                                string nameInstance = ((string)nameValue);
                                moduleInstance.Name = nameInstance;
                            }
                            
                            JToken locationValue = responseDoc["location"];
                            if (locationValue != null && locationValue.Type != JTokenType.Null)
                            {
                                string locationInstance = ((string)locationValue);
                                moduleInstance.Location = locationInstance;
                            }
                            
                            JToken tagsSequenceElement = ((JToken)responseDoc["tags"]);
                            if (tagsSequenceElement != null && tagsSequenceElement.Type != JTokenType.Null)
                            {
                                foreach (JProperty property in tagsSequenceElement)
                                {
                                    string tagsKey2 = ((string)property.Name);
                                    string tagsValue2 = ((string)property.Value);
                                    moduleInstance.Tags.Add(tagsKey2, tagsValue2);
                                }
                            }
                            
                            JToken typeValue = responseDoc["type"];
                            if (typeValue != null && typeValue.Type != JTokenType.Null)
                            {
                                string typeInstance = ((string)typeValue);
                                moduleInstance.Type = typeInstance;
                            }
                            
                            JToken etagValue = responseDoc["etag"];
                            if (etagValue != null && etagValue.Type != JTokenType.Null)
                            {
                                string etagInstance = ((string)etagValue);
                                moduleInstance.Etag = etagInstance;
                            }
                        }
                        
                    }
                    result.StatusCode = statusCode;
                    if (httpResponse.Headers.Contains("x-ms-request-id"))
                    {
                        result.RequestId = httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault();
                    }
                    
                    if (shouldTrace)
                    {
                        TracingAdapter.Exit(invocationId, result);
                    }
                    return result;
                }
                finally
                {
                    if (httpResponse != null)
                    {
                        httpResponse.Dispose();
                    }
                }
            }
            finally
            {
                if (httpRequest != null)
                {
                    httpRequest.Dispose();
                }
            }
        }
    }
}
