// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

// 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 runbooks.  (see
    /// http://aka.ms/azureautomationsdk/runbookoperations for more
    /// information)
    /// </summary>
    internal partial class RunbookOperations : IServiceOperations<AutomationManagementClient>, IRunbookOperations
    {
        /// <summary>
        /// Initializes a new instance of the RunbookOperations class.
        /// </summary>
        /// <param name='client'>
        /// Reference to the service client.
        /// </param>
        internal RunbookOperations(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>
        /// Retrieve the content of runbook identified by runbook name.  (see
        /// http://aka.ms/azureautomationsdk/runbookoperations 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='runbookName'>
        /// Required. The runbook name.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// The response model for the runbook content operation.
        /// </returns>
        public async Task<RunbookContentResponse> ContentAsync(string resourceGroupName, string automationAccount, string runbookName, CancellationToken cancellationToken)
        {
            // Validate
            if (resourceGroupName == null)
            {
                throw new ArgumentNullException("resourceGroupName");
            }
            if (automationAccount == null)
            {
                throw new ArgumentNullException("automationAccount");
            }
            if (runbookName == null)
            {
                throw new ArgumentNullException("runbookName");
            }
            
            // 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("runbookName", runbookName);
                TracingAdapter.Enter(invocationId, this, "ContentAsync", 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 + "/runbooks/";
            url = url + Uri.EscapeDataString(runbookName);
            url = url + "/content";
            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
                    RunbookContentResponse result = null;
                    // Deserialize Response
                    if (statusCode == HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
                        result = new RunbookContentResponse();
                        result.Stream = responseContent;
                        
                    }
                    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 runbook identified by runbook name.  (see
        /// http://aka.ms/azureautomationsdk/runbookoperations 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 runbook.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// The response model for the runbook create response.
        /// </returns>
        public async Task<RunbookCreateOrUpdateResponse> CreateOrUpdateAsync(string resourceGroupName, string automationAccount, RunbookCreateOrUpdateParameters 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.PublishContentLink == null)
            {
                throw new ArgumentNullException("parameters.Properties.PublishContentLink");
            }
            if (parameters.Properties.PublishContentLink.ContentHash != null)
            {
                if (parameters.Properties.PublishContentLink.ContentHash.Algorithm == null)
                {
                    throw new ArgumentNullException("parameters.Properties.PublishContentLink.ContentHash.Algorithm");
                }
                if (parameters.Properties.PublishContentLink.ContentHash.Value == null)
                {
                    throw new ArgumentNullException("parameters.Properties.PublishContentLink.ContentHash.Value");
                }
            }
            if (parameters.Properties.RunbookType == null)
            {
                throw new ArgumentNullException("parameters.Properties.RunbookType");
            }
            
            // 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 + "/runbooks/";
            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 runbookCreateOrUpdateParametersValue = new JObject();
                requestDoc = runbookCreateOrUpdateParametersValue;
                
                JObject propertiesValue = new JObject();
                runbookCreateOrUpdateParametersValue["properties"] = propertiesValue;
                
                propertiesValue["logVerbose"] = parameters.Properties.LogVerbose;
                
                propertiesValue["logProgress"] = parameters.Properties.LogProgress;
                
                propertiesValue["runbookType"] = parameters.Properties.RunbookType;
                
                JObject publishContentLinkValue = new JObject();
                propertiesValue["publishContentLink"] = publishContentLinkValue;
                
                if (parameters.Properties.PublishContentLink.Uri != null)
                {
                    publishContentLinkValue["uri"] = parameters.Properties.PublishContentLink.Uri.AbsoluteUri;
                }
                
                if (parameters.Properties.PublishContentLink.ContentHash != null)
                {
                    JObject contentHashValue = new JObject();
                    publishContentLinkValue["contentHash"] = contentHashValue;
                    
                    contentHashValue["algorithm"] = parameters.Properties.PublishContentLink.ContentHash.Algorithm;
                    
                    contentHashValue["value"] = parameters.Properties.PublishContentLink.ContentHash.Value;
                }
                
                if (parameters.Properties.PublishContentLink.Version != null)
                {
                    publishContentLinkValue["version"] = parameters.Properties.PublishContentLink.Version;
                }
                
                if (parameters.Properties.Description != null)
                {
                    propertiesValue["description"] = parameters.Properties.Description;
                }
                
                if (parameters.Name != null)
                {
                    runbookCreateOrUpdateParametersValue["name"] = parameters.Name;
                }
                
                if (parameters.Location != null)
                {
                    runbookCreateOrUpdateParametersValue["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;
                    }
                    runbookCreateOrUpdateParametersValue["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
                    RunbookCreateOrUpdateResponse result = null;
                    // Deserialize Response
                    result = new RunbookCreateOrUpdateResponse();
                    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 runbook identified by runbook name.  (see
        /// http://aka.ms/azureautomationsdk/runbookoperations 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 runbook.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// The response model for the runbook create response.
        /// </returns>
        public async Task<RunbookCreateOrUpdateResponse> CreateOrUpdateWithDraftAsync(string resourceGroupName, string automationAccount, RunbookCreateOrUpdateDraftParameters 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.Draft == null)
            {
                throw new ArgumentNullException("parameters.Properties.Draft");
            }
            if (parameters.Properties.Draft.DraftContentLink != null)
            {
                if (parameters.Properties.Draft.DraftContentLink.ContentHash != null)
                {
                    if (parameters.Properties.Draft.DraftContentLink.ContentHash.Algorithm == null)
                    {
                        throw new ArgumentNullException("parameters.Properties.Draft.DraftContentLink.ContentHash.Algorithm");
                    }
                    if (parameters.Properties.Draft.DraftContentLink.ContentHash.Value == null)
                    {
                        throw new ArgumentNullException("parameters.Properties.Draft.DraftContentLink.ContentHash.Value");
                    }
                }
            }
            if (parameters.Properties.RunbookType == null)
            {
                throw new ArgumentNullException("parameters.Properties.RunbookType");
            }
            
            // 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, "CreateOrUpdateWithDraftAsync", 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 + "/runbooks/";
            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 runbookCreateOrUpdateDraftParametersValue = new JObject();
                requestDoc = runbookCreateOrUpdateDraftParametersValue;
                
                JObject propertiesValue = new JObject();
                runbookCreateOrUpdateDraftParametersValue["properties"] = propertiesValue;
                
                propertiesValue["logVerbose"] = parameters.Properties.LogVerbose;
                
                propertiesValue["logProgress"] = parameters.Properties.LogProgress;
                
                propertiesValue["runbookType"] = parameters.Properties.RunbookType;
                
                JObject draftValue = new JObject();
                propertiesValue["draft"] = draftValue;
                
                draftValue["inEdit"] = parameters.Properties.Draft.InEdit;
                
                if (parameters.Properties.Draft.DraftContentLink != null)
                {
                    JObject draftContentLinkValue = new JObject();
                    draftValue["draftContentLink"] = draftContentLinkValue;
                    
                    if (parameters.Properties.Draft.DraftContentLink.Uri != null)
                    {
                        draftContentLinkValue["uri"] = parameters.Properties.Draft.DraftContentLink.Uri.AbsoluteUri;
                    }
                    
                    if (parameters.Properties.Draft.DraftContentLink.ContentHash != null)
                    {
                        JObject contentHashValue = new JObject();
                        draftContentLinkValue["contentHash"] = contentHashValue;
                        
                        contentHashValue["algorithm"] = parameters.Properties.Draft.DraftContentLink.ContentHash.Algorithm;
                        
                        contentHashValue["value"] = parameters.Properties.Draft.DraftContentLink.ContentHash.Value;
                    }
                    
                    if (parameters.Properties.Draft.DraftContentLink.Version != null)
                    {
                        draftContentLinkValue["version"] = parameters.Properties.Draft.DraftContentLink.Version;
                    }
                }
                
                draftValue["creationTime"] = parameters.Properties.Draft.CreationTime;
                
                draftValue["lastModifiedTime"] = parameters.Properties.Draft.LastModifiedTime;
                
                if (parameters.Properties.Draft.Parameters != null)
                {
                    if (parameters.Properties.Draft.Parameters is ILazyCollection == false || ((ILazyCollection)parameters.Properties.Draft.Parameters).IsInitialized)
                    {
                        JObject parametersDictionary = new JObject();
                        foreach (KeyValuePair<string, RunbookParameter> pair in parameters.Properties.Draft.Parameters)
                        {
                            string parametersKey = pair.Key;
                            RunbookParameter parametersValue = pair.Value;
                            JObject runbookParameterValue = new JObject();
                            parametersDictionary[parametersKey] = runbookParameterValue;
                            
                            if (parametersValue.Type != null)
                            {
                                runbookParameterValue["type"] = parametersValue.Type;
                            }
                            
                            runbookParameterValue["isMandatory"] = parametersValue.IsMandatory;
                            
                            runbookParameterValue["position"] = parametersValue.Position;
                            
                            if (parametersValue.DefaultValue != null)
                            {
                                runbookParameterValue["defaultValue"] = parametersValue.DefaultValue;
                            }
                        }
                        draftValue["parameters"] = parametersDictionary;
                    }
                }
                
                if (parameters.Properties.Description != null)
                {
                    propertiesValue["description"] = parameters.Properties.Description;
                }
                
                if (parameters.Name != null)
                {
                    runbookCreateOrUpdateDraftParametersValue["name"] = parameters.Name;
                }
                
                if (parameters.Location != null)
                {
                    runbookCreateOrUpdateDraftParametersValue["location"] = parameters.Location;
                }
                
                if (parameters.Tags != null)
                {
                    JObject tagsDictionary = new JObject();
                    foreach (KeyValuePair<string, string> pair2 in parameters.Tags)
                    {
                        string tagsKey = pair2.Key;
                        string tagsValue = pair2.Value;
                        tagsDictionary[tagsKey] = tagsValue;
                    }
                    runbookCreateOrUpdateDraftParametersValue["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
                    RunbookCreateOrUpdateResponse result = null;
                    // Deserialize Response
                    result = new RunbookCreateOrUpdateResponse();
                    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 runbook by name.  (see
        /// http://aka.ms/azureautomationsdk/runbookoperations 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='runbookName'>
        /// Required. The runbook 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 runbookName, CancellationToken cancellationToken)
        {
            // Validate
            if (resourceGroupName == null)
            {
                throw new ArgumentNullException("resourceGroupName");
            }
            if (automationAccount == null)
            {
                throw new ArgumentNullException("automationAccount");
            }
            if (runbookName == null)
            {
                throw new ArgumentNullException("runbookName");
            }
            
            // 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("runbookName", runbookName);
                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 + "/runbooks/";
            url = url + Uri.EscapeDataString(runbookName);
            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 runbook identified by runbook name.  (see
        /// http://aka.ms/azureautomationsdk/runbookoperations 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='runbookName'>
        /// Required. The runbook name.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// The response model for the get runbook operation.
        /// </returns>
        public async Task<RunbookGetResponse> GetAsync(string resourceGroupName, string automationAccount, string runbookName, CancellationToken cancellationToken)
        {
            // Validate
            if (resourceGroupName == null)
            {
                throw new ArgumentNullException("resourceGroupName");
            }
            if (automationAccount == null)
            {
                throw new ArgumentNullException("automationAccount");
            }
            if (runbookName == null)
            {
                throw new ArgumentNullException("runbookName");
            }
            
            // 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("runbookName", runbookName);
                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 + "/runbooks/";
            url = url + Uri.EscapeDataString(runbookName);
            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
                    RunbookGetResponse result = null;
                    // Deserialize Response
                    if (statusCode == HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
                        result = new RunbookGetResponse();
                        JToken responseDoc = null;
                        if (string.IsNullOrEmpty(responseContent) == false)
                        {
                            responseDoc = JToken.Parse(responseContent);
                        }
                        
                        if (responseDoc != null && responseDoc.Type != JTokenType.Null)
                        {
                            Runbook runbookInstance = new Runbook();
                            result.Runbook = runbookInstance;
                            
                            JToken propertiesValue = responseDoc["properties"];
                            if (propertiesValue != null && propertiesValue.Type != JTokenType.Null)
                            {
                                RunbookProperties propertiesInstance = new RunbookProperties();
                                runbookInstance.Properties = propertiesInstance;
                                
                                JToken runbookTypeValue = propertiesValue["runbookType"];
                                if (runbookTypeValue != null && runbookTypeValue.Type != JTokenType.Null)
                                {
                                    string runbookTypeInstance = ((string)runbookTypeValue);
                                    propertiesInstance.RunbookType = runbookTypeInstance;
                                }
                                
                                JToken publisdContentLinkValue = propertiesValue["publisdContentLink"];
                                if (publisdContentLinkValue != null && publisdContentLinkValue.Type != JTokenType.Null)
                                {
                                    ContentLink publisdContentLinkInstance = new ContentLink();
                                    propertiesInstance.PublishContentLink = publisdContentLinkInstance;
                                    
                                    JToken uriValue = publisdContentLinkValue["uri"];
                                    if (uriValue != null && uriValue.Type != JTokenType.Null)
                                    {
                                        Uri uriInstance = TypeConversion.TryParseUri(((string)uriValue));
                                        publisdContentLinkInstance.Uri = uriInstance;
                                    }
                                    
                                    JToken contentHashValue = publisdContentLinkValue["contentHash"];
                                    if (contentHashValue != null && contentHashValue.Type != JTokenType.Null)
                                    {
                                        ContentHash contentHashInstance = new ContentHash();
                                        publisdContentLinkInstance.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 versionValue = publisdContentLinkValue["version"];
                                    if (versionValue != null && versionValue.Type != JTokenType.Null)
                                    {
                                        string versionInstance = ((string)versionValue);
                                        publisdContentLinkInstance.Version = versionInstance;
                                    }
                                }
                                
                                JToken stateValue = propertiesValue["state"];
                                if (stateValue != null && stateValue.Type != JTokenType.Null)
                                {
                                    string stateInstance = ((string)stateValue);
                                    propertiesInstance.State = stateInstance;
                                }
                                
                                JToken logVerboseValue = propertiesValue["logVerbose"];
                                if (logVerboseValue != null && logVerboseValue.Type != JTokenType.Null)
                                {
                                    bool logVerboseInstance = ((bool)logVerboseValue);
                                    propertiesInstance.LogVerbose = logVerboseInstance;
                                }
                                
                                JToken logProgressValue = propertiesValue["logProgress"];
                                if (logProgressValue != null && logProgressValue.Type != JTokenType.Null)
                                {
                                    bool logProgressInstance = ((bool)logProgressValue);
                                    propertiesInstance.LogProgress = logProgressInstance;
                                }
                                
                                JToken jobCountValue = propertiesValue["jobCount"];
                                if (jobCountValue != null && jobCountValue.Type != JTokenType.Null)
                                {
                                    int jobCountInstance = ((int)jobCountValue);
                                    propertiesInstance.JobCount = jobCountInstance;
                                }
                                
                                JToken parametersSequenceElement = ((JToken)propertiesValue["parameters"]);
                                if (parametersSequenceElement != null && parametersSequenceElement.Type != JTokenType.Null)
                                {
                                    foreach (JProperty property in parametersSequenceElement)
                                    {
                                        string parametersKey = ((string)property.Name);
                                        JObject varToken = ((JObject)property.Value);
                                        RunbookParameter runbookParameterInstance = new RunbookParameter();
                                        propertiesInstance.Parameters.Add(parametersKey, runbookParameterInstance);
                                        
                                        JToken typeValue = varToken["type"];
                                        if (typeValue != null && typeValue.Type != JTokenType.Null)
                                        {
                                            string typeInstance = ((string)typeValue);
                                            runbookParameterInstance.Type = typeInstance;
                                        }
                                        
                                        JToken isMandatoryValue = varToken["isMandatory"];
                                        if (isMandatoryValue != null && isMandatoryValue.Type != JTokenType.Null)
                                        {
                                            bool isMandatoryInstance = ((bool)isMandatoryValue);
                                            runbookParameterInstance.IsMandatory = isMandatoryInstance;
                                        }
                                        
                                        JToken positionValue = varToken["position"];
                                        if (positionValue != null && positionValue.Type != JTokenType.Null)
                                        {
                                            int positionInstance = ((int)positionValue);
                                            runbookParameterInstance.Position = positionInstance;
                                        }
                                        
                                        JToken defaultValueValue = varToken["defaultValue"];
                                        if (defaultValueValue != null && defaultValueValue.Type != JTokenType.Null)
                                        {
                                            string defaultValueInstance = ((string)defaultValueValue);
                                            runbookParameterInstance.DefaultValue = defaultValueInstance;
                                        }
                                    }
                                }
                                
                                JToken draftValue = propertiesValue["draft"];
                                if (draftValue != null && draftValue.Type != JTokenType.Null)
                                {
                                    RunbookDraft draftInstance = new RunbookDraft();
                                    propertiesInstance.Draft = draftInstance;
                                    
                                    JToken inEditValue = draftValue["inEdit"];
                                    if (inEditValue != null && inEditValue.Type != JTokenType.Null)
                                    {
                                        bool inEditInstance = ((bool)inEditValue);
                                        draftInstance.InEdit = inEditInstance;
                                    }
                                    
                                    JToken draftContentLinkValue = draftValue["draftContentLink"];
                                    if (draftContentLinkValue != null && draftContentLinkValue.Type != JTokenType.Null)
                                    {
                                        ContentLink draftContentLinkInstance = new ContentLink();
                                        draftInstance.DraftContentLink = draftContentLinkInstance;
                                        
                                        JToken uriValue2 = draftContentLinkValue["uri"];
                                        if (uriValue2 != null && uriValue2.Type != JTokenType.Null)
                                        {
                                            Uri uriInstance2 = TypeConversion.TryParseUri(((string)uriValue2));
                                            draftContentLinkInstance.Uri = uriInstance2;
                                        }
                                        
                                        JToken contentHashValue2 = draftContentLinkValue["contentHash"];
                                        if (contentHashValue2 != null && contentHashValue2.Type != JTokenType.Null)
                                        {
                                            ContentHash contentHashInstance2 = new ContentHash();
                                            draftContentLinkInstance.ContentHash = contentHashInstance2;
                                            
                                            JToken algorithmValue2 = contentHashValue2["algorithm"];
                                            if (algorithmValue2 != null && algorithmValue2.Type != JTokenType.Null)
                                            {
                                                string algorithmInstance2 = ((string)algorithmValue2);
                                                contentHashInstance2.Algorithm = algorithmInstance2;
                                            }
                                            
                                            JToken valueValue2 = contentHashValue2["value"];
                                            if (valueValue2 != null && valueValue2.Type != JTokenType.Null)
                                            {
                                                string valueInstance2 = ((string)valueValue2);
                                                contentHashInstance2.Value = valueInstance2;
                                            }
                                        }
                                        
                                        JToken versionValue2 = draftContentLinkValue["version"];
                                        if (versionValue2 != null && versionValue2.Type != JTokenType.Null)
                                        {
                                            string versionInstance2 = ((string)versionValue2);
                                            draftContentLinkInstance.Version = versionInstance2;
                                        }
                                    }
                                    
                                    JToken creationTimeValue = draftValue["creationTime"];
                                    if (creationTimeValue != null && creationTimeValue.Type != JTokenType.Null)
                                    {
                                        DateTimeOffset creationTimeInstance = ((DateTimeOffset)creationTimeValue);
                                        draftInstance.CreationTime = creationTimeInstance;
                                    }
                                    
                                    JToken lastModifiedTimeValue = draftValue["lastModifiedTime"];
                                    if (lastModifiedTimeValue != null && lastModifiedTimeValue.Type != JTokenType.Null)
                                    {
                                        DateTimeOffset lastModifiedTimeInstance = ((DateTimeOffset)lastModifiedTimeValue);
                                        draftInstance.LastModifiedTime = lastModifiedTimeInstance;
                                    }
                                    
                                    JToken parametersSequenceElement2 = ((JToken)draftValue["parameters"]);
                                    if (parametersSequenceElement2 != null && parametersSequenceElement2.Type != JTokenType.Null)
                                    {
                                        foreach (JProperty property2 in parametersSequenceElement2)
                                        {
                                            string parametersKey2 = ((string)property2.Name);
                                            JObject varToken2 = ((JObject)property2.Value);
                                            RunbookParameter runbookParameterInstance2 = new RunbookParameter();
                                            draftInstance.Parameters.Add(parametersKey2, runbookParameterInstance2);
                                            
                                            JToken typeValue2 = varToken2["type"];
                                            if (typeValue2 != null && typeValue2.Type != JTokenType.Null)
                                            {
                                                string typeInstance2 = ((string)typeValue2);
                                                runbookParameterInstance2.Type = typeInstance2;
                                            }
                                            
                                            JToken isMandatoryValue2 = varToken2["isMandatory"];
                                            if (isMandatoryValue2 != null && isMandatoryValue2.Type != JTokenType.Null)
                                            {
                                                bool isMandatoryInstance2 = ((bool)isMandatoryValue2);
                                                runbookParameterInstance2.IsMandatory = isMandatoryInstance2;
                                            }
                                            
                                            JToken positionValue2 = varToken2["position"];
                                            if (positionValue2 != null && positionValue2.Type != JTokenType.Null)
                                            {
                                                int positionInstance2 = ((int)positionValue2);
                                                runbookParameterInstance2.Position = positionInstance2;
                                            }
                                            
                                            JToken defaultValueValue2 = varToken2["defaultValue"];
                                            if (defaultValueValue2 != null && defaultValueValue2.Type != JTokenType.Null)
                                            {
                                                string defaultValueInstance2 = ((string)defaultValueValue2);
                                                runbookParameterInstance2.DefaultValue = defaultValueInstance2;
                                            }
                                        }
                                    }
                                }
                                
                                JToken provisioningStateValue = propertiesValue["provisioningState"];
                                if (provisioningStateValue != null && provisioningStateValue.Type != JTokenType.Null)
                                {
                                    RunbookProvisioningState provisioningStateInstance = ((RunbookProvisioningState)Enum.Parse(typeof(RunbookProvisioningState), ((string)provisioningStateValue), true));
                                    propertiesInstance.ProvisioningState = provisioningStateInstance;
                                }
                                
                                JToken lastModifiedByValue = propertiesValue["lastModifiedBy"];
                                if (lastModifiedByValue != null && lastModifiedByValue.Type != JTokenType.Null)
                                {
                                    string lastModifiedByInstance = ((string)lastModifiedByValue);
                                    propertiesInstance.LastModifiedBy = lastModifiedByInstance;
                                }
                                
                                JToken creationTimeValue2 = propertiesValue["creationTime"];
                                if (creationTimeValue2 != null && creationTimeValue2.Type != JTokenType.Null)
                                {
                                    DateTimeOffset creationTimeInstance2 = ((DateTimeOffset)creationTimeValue2);
                                    propertiesInstance.CreationTime = creationTimeInstance2;
                                }
                                
                                JToken lastModifiedTimeValue2 = propertiesValue["lastModifiedTime"];
                                if (lastModifiedTimeValue2 != null && lastModifiedTimeValue2.Type != JTokenType.Null)
                                {
                                    DateTimeOffset lastModifiedTimeInstance2 = ((DateTimeOffset)lastModifiedTimeValue2);
                                    propertiesInstance.LastModifiedTime = lastModifiedTimeInstance2;
                                }
                                
                                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);
                                runbookInstance.Id = idInstance;
                            }
                            
                            JToken nameValue = responseDoc["name"];
                            if (nameValue != null && nameValue.Type != JTokenType.Null)
                            {
                                string nameInstance = ((string)nameValue);
                                runbookInstance.Name = nameInstance;
                            }
                            
                            JToken locationValue = responseDoc["location"];
                            if (locationValue != null && locationValue.Type != JTokenType.Null)
                            {
                                string locationInstance = ((string)locationValue);
                                runbookInstance.Location = locationInstance;
                            }
                            
                            JToken tagsSequenceElement = ((JToken)responseDoc["tags"]);
                            if (tagsSequenceElement != null && tagsSequenceElement.Type != JTokenType.Null)
                            {
                                foreach (JProperty property3 in tagsSequenceElement)
                                {
                                    string tagsKey = ((string)property3.Name);
                                    string tagsValue = ((string)property3.Value);
                                    runbookInstance.Tags.Add(tagsKey, tagsValue);
                                }
                            }
                            
                            JToken typeValue3 = responseDoc["type"];
                            if (typeValue3 != null && typeValue3.Type != JTokenType.Null)
                            {
                                string typeInstance3 = ((string)typeValue3);
                                runbookInstance.Type = typeInstance3;
                            }
                            
                            JToken etagValue = responseDoc["etag"];
                            if (etagValue != null && etagValue.Type != JTokenType.Null)
                            {
                                string etagInstance = ((string)etagValue);
                                runbookInstance.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 runbooks.  (see
        /// http://aka.ms/azureautomationsdk/runbookoperations 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 runbook operation.
        /// </returns>
        public async Task<RunbookListResponse> 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 + "/runbooks";
            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
                    RunbookListResponse result = null;
                    // Deserialize Response
                    if (statusCode == HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
                        result = new RunbookListResponse();
                        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))
                                {
                                    Runbook runbookInstance = new Runbook();
                                    result.Runbooks.Add(runbookInstance);
                                    
                                    JToken propertiesValue = valueValue["properties"];
                                    if (propertiesValue != null && propertiesValue.Type != JTokenType.Null)
                                    {
                                        RunbookProperties propertiesInstance = new RunbookProperties();
                                        runbookInstance.Properties = propertiesInstance;
                                        
                                        JToken runbookTypeValue = propertiesValue["runbookType"];
                                        if (runbookTypeValue != null && runbookTypeValue.Type != JTokenType.Null)
                                        {
                                            string runbookTypeInstance = ((string)runbookTypeValue);
                                            propertiesInstance.RunbookType = runbookTypeInstance;
                                        }
                                        
                                        JToken publisdContentLinkValue = propertiesValue["publisdContentLink"];
                                        if (publisdContentLinkValue != null && publisdContentLinkValue.Type != JTokenType.Null)
                                        {
                                            ContentLink publisdContentLinkInstance = new ContentLink();
                                            propertiesInstance.PublishContentLink = publisdContentLinkInstance;
                                            
                                            JToken uriValue = publisdContentLinkValue["uri"];
                                            if (uriValue != null && uriValue.Type != JTokenType.Null)
                                            {
                                                Uri uriInstance = TypeConversion.TryParseUri(((string)uriValue));
                                                publisdContentLinkInstance.Uri = uriInstance;
                                            }
                                            
                                            JToken contentHashValue = publisdContentLinkValue["contentHash"];
                                            if (contentHashValue != null && contentHashValue.Type != JTokenType.Null)
                                            {
                                                ContentHash contentHashInstance = new ContentHash();
                                                publisdContentLinkInstance.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 versionValue = publisdContentLinkValue["version"];
                                            if (versionValue != null && versionValue.Type != JTokenType.Null)
                                            {
                                                string versionInstance = ((string)versionValue);
                                                publisdContentLinkInstance.Version = versionInstance;
                                            }
                                        }
                                        
                                        JToken stateValue = propertiesValue["state"];
                                        if (stateValue != null && stateValue.Type != JTokenType.Null)
                                        {
                                            string stateInstance = ((string)stateValue);
                                            propertiesInstance.State = stateInstance;
                                        }
                                        
                                        JToken logVerboseValue = propertiesValue["logVerbose"];
                                        if (logVerboseValue != null && logVerboseValue.Type != JTokenType.Null)
                                        {
                                            bool logVerboseInstance = ((bool)logVerboseValue);
                                            propertiesInstance.LogVerbose = logVerboseInstance;
                                        }
                                        
                                        JToken logProgressValue = propertiesValue["logProgress"];
                                        if (logProgressValue != null && logProgressValue.Type != JTokenType.Null)
                                        {
                                            bool logProgressInstance = ((bool)logProgressValue);
                                            propertiesInstance.LogProgress = logProgressInstance;
                                        }
                                        
                                        JToken jobCountValue = propertiesValue["jobCount"];
                                        if (jobCountValue != null && jobCountValue.Type != JTokenType.Null)
                                        {
                                            int jobCountInstance = ((int)jobCountValue);
                                            propertiesInstance.JobCount = jobCountInstance;
                                        }
                                        
                                        JToken parametersSequenceElement = ((JToken)propertiesValue["parameters"]);
                                        if (parametersSequenceElement != null && parametersSequenceElement.Type != JTokenType.Null)
                                        {
                                            foreach (JProperty property in parametersSequenceElement)
                                            {
                                                string parametersKey = ((string)property.Name);
                                                JObject varToken = ((JObject)property.Value);
                                                RunbookParameter runbookParameterInstance = new RunbookParameter();
                                                propertiesInstance.Parameters.Add(parametersKey, runbookParameterInstance);
                                                
                                                JToken typeValue = varToken["type"];
                                                if (typeValue != null && typeValue.Type != JTokenType.Null)
                                                {
                                                    string typeInstance = ((string)typeValue);
                                                    runbookParameterInstance.Type = typeInstance;
                                                }
                                                
                                                JToken isMandatoryValue = varToken["isMandatory"];
                                                if (isMandatoryValue != null && isMandatoryValue.Type != JTokenType.Null)
                                                {
                                                    bool isMandatoryInstance = ((bool)isMandatoryValue);
                                                    runbookParameterInstance.IsMandatory = isMandatoryInstance;
                                                }
                                                
                                                JToken positionValue = varToken["position"];
                                                if (positionValue != null && positionValue.Type != JTokenType.Null)
                                                {
                                                    int positionInstance = ((int)positionValue);
                                                    runbookParameterInstance.Position = positionInstance;
                                                }
                                                
                                                JToken defaultValueValue = varToken["defaultValue"];
                                                if (defaultValueValue != null && defaultValueValue.Type != JTokenType.Null)
                                                {
                                                    string defaultValueInstance = ((string)defaultValueValue);
                                                    runbookParameterInstance.DefaultValue = defaultValueInstance;
                                                }
                                            }
                                        }
                                        
                                        JToken draftValue = propertiesValue["draft"];
                                        if (draftValue != null && draftValue.Type != JTokenType.Null)
                                        {
                                            RunbookDraft draftInstance = new RunbookDraft();
                                            propertiesInstance.Draft = draftInstance;
                                            
                                            JToken inEditValue = draftValue["inEdit"];
                                            if (inEditValue != null && inEditValue.Type != JTokenType.Null)
                                            {
                                                bool inEditInstance = ((bool)inEditValue);
                                                draftInstance.InEdit = inEditInstance;
                                            }
                                            
                                            JToken draftContentLinkValue = draftValue["draftContentLink"];
                                            if (draftContentLinkValue != null && draftContentLinkValue.Type != JTokenType.Null)
                                            {
                                                ContentLink draftContentLinkInstance = new ContentLink();
                                                draftInstance.DraftContentLink = draftContentLinkInstance;
                                                
                                                JToken uriValue2 = draftContentLinkValue["uri"];
                                                if (uriValue2 != null && uriValue2.Type != JTokenType.Null)
                                                {
                                                    Uri uriInstance2 = TypeConversion.TryParseUri(((string)uriValue2));
                                                    draftContentLinkInstance.Uri = uriInstance2;
                                                }
                                                
                                                JToken contentHashValue2 = draftContentLinkValue["contentHash"];
                                                if (contentHashValue2 != null && contentHashValue2.Type != JTokenType.Null)
                                                {
                                                    ContentHash contentHashInstance2 = new ContentHash();
                                                    draftContentLinkInstance.ContentHash = contentHashInstance2;
                                                    
                                                    JToken algorithmValue2 = contentHashValue2["algorithm"];
                                                    if (algorithmValue2 != null && algorithmValue2.Type != JTokenType.Null)
                                                    {
                                                        string algorithmInstance2 = ((string)algorithmValue2);
                                                        contentHashInstance2.Algorithm = algorithmInstance2;
                                                    }
                                                    
                                                    JToken valueValue3 = contentHashValue2["value"];
                                                    if (valueValue3 != null && valueValue3.Type != JTokenType.Null)
                                                    {
                                                        string valueInstance2 = ((string)valueValue3);
                                                        contentHashInstance2.Value = valueInstance2;
                                                    }
                                                }
                                                
                                                JToken versionValue2 = draftContentLinkValue["version"];
                                                if (versionValue2 != null && versionValue2.Type != JTokenType.Null)
                                                {
                                                    string versionInstance2 = ((string)versionValue2);
                                                    draftContentLinkInstance.Version = versionInstance2;
                                                }
                                            }
                                            
                                            JToken creationTimeValue = draftValue["creationTime"];
                                            if (creationTimeValue != null && creationTimeValue.Type != JTokenType.Null)
                                            {
                                                DateTimeOffset creationTimeInstance = ((DateTimeOffset)creationTimeValue);
                                                draftInstance.CreationTime = creationTimeInstance;
                                            }
                                            
                                            JToken lastModifiedTimeValue = draftValue["lastModifiedTime"];
                                            if (lastModifiedTimeValue != null && lastModifiedTimeValue.Type != JTokenType.Null)
                                            {
                                                DateTimeOffset lastModifiedTimeInstance = ((DateTimeOffset)lastModifiedTimeValue);
                                                draftInstance.LastModifiedTime = lastModifiedTimeInstance;
                                            }
                                            
                                            JToken parametersSequenceElement2 = ((JToken)draftValue["parameters"]);
                                            if (parametersSequenceElement2 != null && parametersSequenceElement2.Type != JTokenType.Null)
                                            {
                                                foreach (JProperty property2 in parametersSequenceElement2)
                                                {
                                                    string parametersKey2 = ((string)property2.Name);
                                                    JObject varToken2 = ((JObject)property2.Value);
                                                    RunbookParameter runbookParameterInstance2 = new RunbookParameter();
                                                    draftInstance.Parameters.Add(parametersKey2, runbookParameterInstance2);
                                                    
                                                    JToken typeValue2 = varToken2["type"];
                                                    if (typeValue2 != null && typeValue2.Type != JTokenType.Null)
                                                    {
                                                        string typeInstance2 = ((string)typeValue2);
                                                        runbookParameterInstance2.Type = typeInstance2;
                                                    }
                                                    
                                                    JToken isMandatoryValue2 = varToken2["isMandatory"];
                                                    if (isMandatoryValue2 != null && isMandatoryValue2.Type != JTokenType.Null)
                                                    {
                                                        bool isMandatoryInstance2 = ((bool)isMandatoryValue2);
                                                        runbookParameterInstance2.IsMandatory = isMandatoryInstance2;
                                                    }
                                                    
                                                    JToken positionValue2 = varToken2["position"];
                                                    if (positionValue2 != null && positionValue2.Type != JTokenType.Null)
                                                    {
                                                        int positionInstance2 = ((int)positionValue2);
                                                        runbookParameterInstance2.Position = positionInstance2;
                                                    }
                                                    
                                                    JToken defaultValueValue2 = varToken2["defaultValue"];
                                                    if (defaultValueValue2 != null && defaultValueValue2.Type != JTokenType.Null)
                                                    {
                                                        string defaultValueInstance2 = ((string)defaultValueValue2);
                                                        runbookParameterInstance2.DefaultValue = defaultValueInstance2;
                                                    }
                                                }
                                            }
                                        }
                                        
                                        JToken provisioningStateValue = propertiesValue["provisioningState"];
                                        if (provisioningStateValue != null && provisioningStateValue.Type != JTokenType.Null)
                                        {
                                            RunbookProvisioningState provisioningStateInstance = ((RunbookProvisioningState)Enum.Parse(typeof(RunbookProvisioningState), ((string)provisioningStateValue), true));
                                            propertiesInstance.ProvisioningState = provisioningStateInstance;
                                        }
                                        
                                        JToken lastModifiedByValue = propertiesValue["lastModifiedBy"];
                                        if (lastModifiedByValue != null && lastModifiedByValue.Type != JTokenType.Null)
                                        {
                                            string lastModifiedByInstance = ((string)lastModifiedByValue);
                                            propertiesInstance.LastModifiedBy = lastModifiedByInstance;
                                        }
                                        
                                        JToken creationTimeValue2 = propertiesValue["creationTime"];
                                        if (creationTimeValue2 != null && creationTimeValue2.Type != JTokenType.Null)
                                        {
                                            DateTimeOffset creationTimeInstance2 = ((DateTimeOffset)creationTimeValue2);
                                            propertiesInstance.CreationTime = creationTimeInstance2;
                                        }
                                        
                                        JToken lastModifiedTimeValue2 = propertiesValue["lastModifiedTime"];
                                        if (lastModifiedTimeValue2 != null && lastModifiedTimeValue2.Type != JTokenType.Null)
                                        {
                                            DateTimeOffset lastModifiedTimeInstance2 = ((DateTimeOffset)lastModifiedTimeValue2);
                                            propertiesInstance.LastModifiedTime = lastModifiedTimeInstance2;
                                        }
                                        
                                        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);
                                        runbookInstance.Id = idInstance;
                                    }
                                    
                                    JToken nameValue = valueValue["name"];
                                    if (nameValue != null && nameValue.Type != JTokenType.Null)
                                    {
                                        string nameInstance = ((string)nameValue);
                                        runbookInstance.Name = nameInstance;
                                    }
                                    
                                    JToken locationValue = valueValue["location"];
                                    if (locationValue != null && locationValue.Type != JTokenType.Null)
                                    {
                                        string locationInstance = ((string)locationValue);
                                        runbookInstance.Location = locationInstance;
                                    }
                                    
                                    JToken tagsSequenceElement = ((JToken)valueValue["tags"]);
                                    if (tagsSequenceElement != null && tagsSequenceElement.Type != JTokenType.Null)
                                    {
                                        foreach (JProperty property3 in tagsSequenceElement)
                                        {
                                            string tagsKey = ((string)property3.Name);
                                            string tagsValue = ((string)property3.Value);
                                            runbookInstance.Tags.Add(tagsKey, tagsValue);
                                        }
                                    }
                                    
                                    JToken typeValue3 = valueValue["type"];
                                    if (typeValue3 != null && typeValue3.Type != JTokenType.Null)
                                    {
                                        string typeInstance3 = ((string)typeValue3);
                                        runbookInstance.Type = typeInstance3;
                                    }
                                    
                                    JToken etagValue = valueValue["etag"];
                                    if (etagValue != null && etagValue.Type != JTokenType.Null)
                                    {
                                        string etagInstance = ((string)etagValue);
                                        runbookInstance.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 runbooks.  (see
        /// http://aka.ms/azureautomationsdk/runbookoperations 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 runbook operation.
        /// </returns>
        public async Task<RunbookListResponse> 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
                    RunbookListResponse result = null;
                    // Deserialize Response
                    if (statusCode == HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
                        result = new RunbookListResponse();
                        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))
                                {
                                    Runbook runbookInstance = new Runbook();
                                    result.Runbooks.Add(runbookInstance);
                                    
                                    JToken propertiesValue = valueValue["properties"];
                                    if (propertiesValue != null && propertiesValue.Type != JTokenType.Null)
                                    {
                                        RunbookProperties propertiesInstance = new RunbookProperties();
                                        runbookInstance.Properties = propertiesInstance;
                                        
                                        JToken runbookTypeValue = propertiesValue["runbookType"];
                                        if (runbookTypeValue != null && runbookTypeValue.Type != JTokenType.Null)
                                        {
                                            string runbookTypeInstance = ((string)runbookTypeValue);
                                            propertiesInstance.RunbookType = runbookTypeInstance;
                                        }
                                        
                                        JToken publisdContentLinkValue = propertiesValue["publisdContentLink"];
                                        if (publisdContentLinkValue != null && publisdContentLinkValue.Type != JTokenType.Null)
                                        {
                                            ContentLink publisdContentLinkInstance = new ContentLink();
                                            propertiesInstance.PublishContentLink = publisdContentLinkInstance;
                                            
                                            JToken uriValue = publisdContentLinkValue["uri"];
                                            if (uriValue != null && uriValue.Type != JTokenType.Null)
                                            {
                                                Uri uriInstance = TypeConversion.TryParseUri(((string)uriValue));
                                                publisdContentLinkInstance.Uri = uriInstance;
                                            }
                                            
                                            JToken contentHashValue = publisdContentLinkValue["contentHash"];
                                            if (contentHashValue != null && contentHashValue.Type != JTokenType.Null)
                                            {
                                                ContentHash contentHashInstance = new ContentHash();
                                                publisdContentLinkInstance.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 versionValue = publisdContentLinkValue["version"];
                                            if (versionValue != null && versionValue.Type != JTokenType.Null)
                                            {
                                                string versionInstance = ((string)versionValue);
                                                publisdContentLinkInstance.Version = versionInstance;
                                            }
                                        }
                                        
                                        JToken stateValue = propertiesValue["state"];
                                        if (stateValue != null && stateValue.Type != JTokenType.Null)
                                        {
                                            string stateInstance = ((string)stateValue);
                                            propertiesInstance.State = stateInstance;
                                        }
                                        
                                        JToken logVerboseValue = propertiesValue["logVerbose"];
                                        if (logVerboseValue != null && logVerboseValue.Type != JTokenType.Null)
                                        {
                                            bool logVerboseInstance = ((bool)logVerboseValue);
                                            propertiesInstance.LogVerbose = logVerboseInstance;
                                        }
                                        
                                        JToken logProgressValue = propertiesValue["logProgress"];
                                        if (logProgressValue != null && logProgressValue.Type != JTokenType.Null)
                                        {
                                            bool logProgressInstance = ((bool)logProgressValue);
                                            propertiesInstance.LogProgress = logProgressInstance;
                                        }
                                        
                                        JToken jobCountValue = propertiesValue["jobCount"];
                                        if (jobCountValue != null && jobCountValue.Type != JTokenType.Null)
                                        {
                                            int jobCountInstance = ((int)jobCountValue);
                                            propertiesInstance.JobCount = jobCountInstance;
                                        }
                                        
                                        JToken parametersSequenceElement = ((JToken)propertiesValue["parameters"]);
                                        if (parametersSequenceElement != null && parametersSequenceElement.Type != JTokenType.Null)
                                        {
                                            foreach (JProperty property in parametersSequenceElement)
                                            {
                                                string parametersKey = ((string)property.Name);
                                                JObject varToken = ((JObject)property.Value);
                                                RunbookParameter runbookParameterInstance = new RunbookParameter();
                                                propertiesInstance.Parameters.Add(parametersKey, runbookParameterInstance);
                                                
                                                JToken typeValue = varToken["type"];
                                                if (typeValue != null && typeValue.Type != JTokenType.Null)
                                                {
                                                    string typeInstance = ((string)typeValue);
                                                    runbookParameterInstance.Type = typeInstance;
                                                }
                                                
                                                JToken isMandatoryValue = varToken["isMandatory"];
                                                if (isMandatoryValue != null && isMandatoryValue.Type != JTokenType.Null)
                                                {
                                                    bool isMandatoryInstance = ((bool)isMandatoryValue);
                                                    runbookParameterInstance.IsMandatory = isMandatoryInstance;
                                                }
                                                
                                                JToken positionValue = varToken["position"];
                                                if (positionValue != null && positionValue.Type != JTokenType.Null)
                                                {
                                                    int positionInstance = ((int)positionValue);
                                                    runbookParameterInstance.Position = positionInstance;
                                                }
                                                
                                                JToken defaultValueValue = varToken["defaultValue"];
                                                if (defaultValueValue != null && defaultValueValue.Type != JTokenType.Null)
                                                {
                                                    string defaultValueInstance = ((string)defaultValueValue);
                                                    runbookParameterInstance.DefaultValue = defaultValueInstance;
                                                }
                                            }
                                        }
                                        
                                        JToken draftValue = propertiesValue["draft"];
                                        if (draftValue != null && draftValue.Type != JTokenType.Null)
                                        {
                                            RunbookDraft draftInstance = new RunbookDraft();
                                            propertiesInstance.Draft = draftInstance;
                                            
                                            JToken inEditValue = draftValue["inEdit"];
                                            if (inEditValue != null && inEditValue.Type != JTokenType.Null)
                                            {
                                                bool inEditInstance = ((bool)inEditValue);
                                                draftInstance.InEdit = inEditInstance;
                                            }
                                            
                                            JToken draftContentLinkValue = draftValue["draftContentLink"];
                                            if (draftContentLinkValue != null && draftContentLinkValue.Type != JTokenType.Null)
                                            {
                                                ContentLink draftContentLinkInstance = new ContentLink();
                                                draftInstance.DraftContentLink = draftContentLinkInstance;
                                                
                                                JToken uriValue2 = draftContentLinkValue["uri"];
                                                if (uriValue2 != null && uriValue2.Type != JTokenType.Null)
                                                {
                                                    Uri uriInstance2 = TypeConversion.TryParseUri(((string)uriValue2));
                                                    draftContentLinkInstance.Uri = uriInstance2;
                                                }
                                                
                                                JToken contentHashValue2 = draftContentLinkValue["contentHash"];
                                                if (contentHashValue2 != null && contentHashValue2.Type != JTokenType.Null)
                                                {
                                                    ContentHash contentHashInstance2 = new ContentHash();
                                                    draftContentLinkInstance.ContentHash = contentHashInstance2;
                                                    
                                                    JToken algorithmValue2 = contentHashValue2["algorithm"];
                                                    if (algorithmValue2 != null && algorithmValue2.Type != JTokenType.Null)
                                                    {
                                                        string algorithmInstance2 = ((string)algorithmValue2);
                                                        contentHashInstance2.Algorithm = algorithmInstance2;
                                                    }
                                                    
                                                    JToken valueValue3 = contentHashValue2["value"];
                                                    if (valueValue3 != null && valueValue3.Type != JTokenType.Null)
                                                    {
                                                        string valueInstance2 = ((string)valueValue3);
                                                        contentHashInstance2.Value = valueInstance2;
                                                    }
                                                }
                                                
                                                JToken versionValue2 = draftContentLinkValue["version"];
                                                if (versionValue2 != null && versionValue2.Type != JTokenType.Null)
                                                {
                                                    string versionInstance2 = ((string)versionValue2);
                                                    draftContentLinkInstance.Version = versionInstance2;
                                                }
                                            }
                                            
                                            JToken creationTimeValue = draftValue["creationTime"];
                                            if (creationTimeValue != null && creationTimeValue.Type != JTokenType.Null)
                                            {
                                                DateTimeOffset creationTimeInstance = ((DateTimeOffset)creationTimeValue);
                                                draftInstance.CreationTime = creationTimeInstance;
                                            }
                                            
                                            JToken lastModifiedTimeValue = draftValue["lastModifiedTime"];
                                            if (lastModifiedTimeValue != null && lastModifiedTimeValue.Type != JTokenType.Null)
                                            {
                                                DateTimeOffset lastModifiedTimeInstance = ((DateTimeOffset)lastModifiedTimeValue);
                                                draftInstance.LastModifiedTime = lastModifiedTimeInstance;
                                            }
                                            
                                            JToken parametersSequenceElement2 = ((JToken)draftValue["parameters"]);
                                            if (parametersSequenceElement2 != null && parametersSequenceElement2.Type != JTokenType.Null)
                                            {
                                                foreach (JProperty property2 in parametersSequenceElement2)
                                                {
                                                    string parametersKey2 = ((string)property2.Name);
                                                    JObject varToken2 = ((JObject)property2.Value);
                                                    RunbookParameter runbookParameterInstance2 = new RunbookParameter();
                                                    draftInstance.Parameters.Add(parametersKey2, runbookParameterInstance2);
                                                    
                                                    JToken typeValue2 = varToken2["type"];
                                                    if (typeValue2 != null && typeValue2.Type != JTokenType.Null)
                                                    {
                                                        string typeInstance2 = ((string)typeValue2);
                                                        runbookParameterInstance2.Type = typeInstance2;
                                                    }
                                                    
                                                    JToken isMandatoryValue2 = varToken2["isMandatory"];
                                                    if (isMandatoryValue2 != null && isMandatoryValue2.Type != JTokenType.Null)
                                                    {
                                                        bool isMandatoryInstance2 = ((bool)isMandatoryValue2);
                                                        runbookParameterInstance2.IsMandatory = isMandatoryInstance2;
                                                    }
                                                    
                                                    JToken positionValue2 = varToken2["position"];
                                                    if (positionValue2 != null && positionValue2.Type != JTokenType.Null)
                                                    {
                                                        int positionInstance2 = ((int)positionValue2);
                                                        runbookParameterInstance2.Position = positionInstance2;
                                                    }
                                                    
                                                    JToken defaultValueValue2 = varToken2["defaultValue"];
                                                    if (defaultValueValue2 != null && defaultValueValue2.Type != JTokenType.Null)
                                                    {
                                                        string defaultValueInstance2 = ((string)defaultValueValue2);
                                                        runbookParameterInstance2.DefaultValue = defaultValueInstance2;
                                                    }
                                                }
                                            }
                                        }
                                        
                                        JToken provisioningStateValue = propertiesValue["provisioningState"];
                                        if (provisioningStateValue != null && provisioningStateValue.Type != JTokenType.Null)
                                        {
                                            RunbookProvisioningState provisioningStateInstance = ((RunbookProvisioningState)Enum.Parse(typeof(RunbookProvisioningState), ((string)provisioningStateValue), true));
                                            propertiesInstance.ProvisioningState = provisioningStateInstance;
                                        }
                                        
                                        JToken lastModifiedByValue = propertiesValue["lastModifiedBy"];
                                        if (lastModifiedByValue != null && lastModifiedByValue.Type != JTokenType.Null)
                                        {
                                            string lastModifiedByInstance = ((string)lastModifiedByValue);
                                            propertiesInstance.LastModifiedBy = lastModifiedByInstance;
                                        }
                                        
                                        JToken creationTimeValue2 = propertiesValue["creationTime"];
                                        if (creationTimeValue2 != null && creationTimeValue2.Type != JTokenType.Null)
                                        {
                                            DateTimeOffset creationTimeInstance2 = ((DateTimeOffset)creationTimeValue2);
                                            propertiesInstance.CreationTime = creationTimeInstance2;
                                        }
                                        
                                        JToken lastModifiedTimeValue2 = propertiesValue["lastModifiedTime"];
                                        if (lastModifiedTimeValue2 != null && lastModifiedTimeValue2.Type != JTokenType.Null)
                                        {
                                            DateTimeOffset lastModifiedTimeInstance2 = ((DateTimeOffset)lastModifiedTimeValue2);
                                            propertiesInstance.LastModifiedTime = lastModifiedTimeInstance2;
                                        }
                                        
                                        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);
                                        runbookInstance.Id = idInstance;
                                    }
                                    
                                    JToken nameValue = valueValue["name"];
                                    if (nameValue != null && nameValue.Type != JTokenType.Null)
                                    {
                                        string nameInstance = ((string)nameValue);
                                        runbookInstance.Name = nameInstance;
                                    }
                                    
                                    JToken locationValue = valueValue["location"];
                                    if (locationValue != null && locationValue.Type != JTokenType.Null)
                                    {
                                        string locationInstance = ((string)locationValue);
                                        runbookInstance.Location = locationInstance;
                                    }
                                    
                                    JToken tagsSequenceElement = ((JToken)valueValue["tags"]);
                                    if (tagsSequenceElement != null && tagsSequenceElement.Type != JTokenType.Null)
                                    {
                                        foreach (JProperty property3 in tagsSequenceElement)
                                        {
                                            string tagsKey = ((string)property3.Name);
                                            string tagsValue = ((string)property3.Value);
                                            runbookInstance.Tags.Add(tagsKey, tagsValue);
                                        }
                                    }
                                    
                                    JToken typeValue3 = valueValue["type"];
                                    if (typeValue3 != null && typeValue3.Type != JTokenType.Null)
                                    {
                                        string typeInstance3 = ((string)typeValue3);
                                        runbookInstance.Type = typeInstance3;
                                    }
                                    
                                    JToken etagValue = valueValue["etag"];
                                    if (etagValue != null && etagValue.Type != JTokenType.Null)
                                    {
                                        string etagInstance = ((string)etagValue);
                                        runbookInstance.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>
        /// Update the runbook identified by runbook name.  (see
        /// http://aka.ms/azureautomationsdk/runbookoperations 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 runbook.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// The response model for the get runbook operation.
        /// </returns>
        public async Task<RunbookGetResponse> PatchAsync(string resourceGroupName, string automationAccount, RunbookPatchParameters 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");
            }
            
            // 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 + "/runbooks/";
            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 runbookPatchParametersValue = new JObject();
                requestDoc = runbookPatchParametersValue;
                
                if (parameters.Properties != null)
                {
                    JObject propertiesValue = new JObject();
                    runbookPatchParametersValue["properties"] = propertiesValue;
                    
                    if (parameters.Properties.Description != null)
                    {
                        propertiesValue["description"] = parameters.Properties.Description;
                    }
                    
                    propertiesValue["logVerbose"] = parameters.Properties.LogVerbose;
                    
                    propertiesValue["logProgress"] = parameters.Properties.LogProgress;
                }
                
                if (parameters.Name != null)
                {
                    runbookPatchParametersValue["name"] = parameters.Name;
                }
                
                if (parameters.Location != null)
                {
                    runbookPatchParametersValue["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;
                    }
                    runbookPatchParametersValue["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
                    RunbookGetResponse result = null;
                    // Deserialize Response
                    if (statusCode == HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
                        result = new RunbookGetResponse();
                        JToken responseDoc = null;
                        if (string.IsNullOrEmpty(responseContent) == false)
                        {
                            responseDoc = JToken.Parse(responseContent);
                        }
                        
                        if (responseDoc != null && responseDoc.Type != JTokenType.Null)
                        {
                            Runbook runbookInstance = new Runbook();
                            result.Runbook = runbookInstance;
                            
                            JToken propertiesValue2 = responseDoc["properties"];
                            if (propertiesValue2 != null && propertiesValue2.Type != JTokenType.Null)
                            {
                                RunbookProperties propertiesInstance = new RunbookProperties();
                                runbookInstance.Properties = propertiesInstance;
                                
                                JToken runbookTypeValue = propertiesValue2["runbookType"];
                                if (runbookTypeValue != null && runbookTypeValue.Type != JTokenType.Null)
                                {
                                    string runbookTypeInstance = ((string)runbookTypeValue);
                                    propertiesInstance.RunbookType = runbookTypeInstance;
                                }
                                
                                JToken publisdContentLinkValue = propertiesValue2["publisdContentLink"];
                                if (publisdContentLinkValue != null && publisdContentLinkValue.Type != JTokenType.Null)
                                {
                                    ContentLink publisdContentLinkInstance = new ContentLink();
                                    propertiesInstance.PublishContentLink = publisdContentLinkInstance;
                                    
                                    JToken uriValue = publisdContentLinkValue["uri"];
                                    if (uriValue != null && uriValue.Type != JTokenType.Null)
                                    {
                                        Uri uriInstance = TypeConversion.TryParseUri(((string)uriValue));
                                        publisdContentLinkInstance.Uri = uriInstance;
                                    }
                                    
                                    JToken contentHashValue = publisdContentLinkValue["contentHash"];
                                    if (contentHashValue != null && contentHashValue.Type != JTokenType.Null)
                                    {
                                        ContentHash contentHashInstance = new ContentHash();
                                        publisdContentLinkInstance.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 versionValue = publisdContentLinkValue["version"];
                                    if (versionValue != null && versionValue.Type != JTokenType.Null)
                                    {
                                        string versionInstance = ((string)versionValue);
                                        publisdContentLinkInstance.Version = versionInstance;
                                    }
                                }
                                
                                JToken stateValue = propertiesValue2["state"];
                                if (stateValue != null && stateValue.Type != JTokenType.Null)
                                {
                                    string stateInstance = ((string)stateValue);
                                    propertiesInstance.State = stateInstance;
                                }
                                
                                JToken logVerboseValue = propertiesValue2["logVerbose"];
                                if (logVerboseValue != null && logVerboseValue.Type != JTokenType.Null)
                                {
                                    bool logVerboseInstance = ((bool)logVerboseValue);
                                    propertiesInstance.LogVerbose = logVerboseInstance;
                                }
                                
                                JToken logProgressValue = propertiesValue2["logProgress"];
                                if (logProgressValue != null && logProgressValue.Type != JTokenType.Null)
                                {
                                    bool logProgressInstance = ((bool)logProgressValue);
                                    propertiesInstance.LogProgress = logProgressInstance;
                                }
                                
                                JToken jobCountValue = propertiesValue2["jobCount"];
                                if (jobCountValue != null && jobCountValue.Type != JTokenType.Null)
                                {
                                    int jobCountInstance = ((int)jobCountValue);
                                    propertiesInstance.JobCount = jobCountInstance;
                                }
                                
                                JToken parametersSequenceElement = ((JToken)propertiesValue2["parameters"]);
                                if (parametersSequenceElement != null && parametersSequenceElement.Type != JTokenType.Null)
                                {
                                    foreach (JProperty property in parametersSequenceElement)
                                    {
                                        string parametersKey = ((string)property.Name);
                                        JObject varToken = ((JObject)property.Value);
                                        RunbookParameter runbookParameterInstance = new RunbookParameter();
                                        propertiesInstance.Parameters.Add(parametersKey, runbookParameterInstance);
                                        
                                        JToken typeValue = varToken["type"];
                                        if (typeValue != null && typeValue.Type != JTokenType.Null)
                                        {
                                            string typeInstance = ((string)typeValue);
                                            runbookParameterInstance.Type = typeInstance;
                                        }
                                        
                                        JToken isMandatoryValue = varToken["isMandatory"];
                                        if (isMandatoryValue != null && isMandatoryValue.Type != JTokenType.Null)
                                        {
                                            bool isMandatoryInstance = ((bool)isMandatoryValue);
                                            runbookParameterInstance.IsMandatory = isMandatoryInstance;
                                        }
                                        
                                        JToken positionValue = varToken["position"];
                                        if (positionValue != null && positionValue.Type != JTokenType.Null)
                                        {
                                            int positionInstance = ((int)positionValue);
                                            runbookParameterInstance.Position = positionInstance;
                                        }
                                        
                                        JToken defaultValueValue = varToken["defaultValue"];
                                        if (defaultValueValue != null && defaultValueValue.Type != JTokenType.Null)
                                        {
                                            string defaultValueInstance = ((string)defaultValueValue);
                                            runbookParameterInstance.DefaultValue = defaultValueInstance;
                                        }
                                    }
                                }
                                
                                JToken draftValue = propertiesValue2["draft"];
                                if (draftValue != null && draftValue.Type != JTokenType.Null)
                                {
                                    RunbookDraft draftInstance = new RunbookDraft();
                                    propertiesInstance.Draft = draftInstance;
                                    
                                    JToken inEditValue = draftValue["inEdit"];
                                    if (inEditValue != null && inEditValue.Type != JTokenType.Null)
                                    {
                                        bool inEditInstance = ((bool)inEditValue);
                                        draftInstance.InEdit = inEditInstance;
                                    }
                                    
                                    JToken draftContentLinkValue = draftValue["draftContentLink"];
                                    if (draftContentLinkValue != null && draftContentLinkValue.Type != JTokenType.Null)
                                    {
                                        ContentLink draftContentLinkInstance = new ContentLink();
                                        draftInstance.DraftContentLink = draftContentLinkInstance;
                                        
                                        JToken uriValue2 = draftContentLinkValue["uri"];
                                        if (uriValue2 != null && uriValue2.Type != JTokenType.Null)
                                        {
                                            Uri uriInstance2 = TypeConversion.TryParseUri(((string)uriValue2));
                                            draftContentLinkInstance.Uri = uriInstance2;
                                        }
                                        
                                        JToken contentHashValue2 = draftContentLinkValue["contentHash"];
                                        if (contentHashValue2 != null && contentHashValue2.Type != JTokenType.Null)
                                        {
                                            ContentHash contentHashInstance2 = new ContentHash();
                                            draftContentLinkInstance.ContentHash = contentHashInstance2;
                                            
                                            JToken algorithmValue2 = contentHashValue2["algorithm"];
                                            if (algorithmValue2 != null && algorithmValue2.Type != JTokenType.Null)
                                            {
                                                string algorithmInstance2 = ((string)algorithmValue2);
                                                contentHashInstance2.Algorithm = algorithmInstance2;
                                            }
                                            
                                            JToken valueValue2 = contentHashValue2["value"];
                                            if (valueValue2 != null && valueValue2.Type != JTokenType.Null)
                                            {
                                                string valueInstance2 = ((string)valueValue2);
                                                contentHashInstance2.Value = valueInstance2;
                                            }
                                        }
                                        
                                        JToken versionValue2 = draftContentLinkValue["version"];
                                        if (versionValue2 != null && versionValue2.Type != JTokenType.Null)
                                        {
                                            string versionInstance2 = ((string)versionValue2);
                                            draftContentLinkInstance.Version = versionInstance2;
                                        }
                                    }
                                    
                                    JToken creationTimeValue = draftValue["creationTime"];
                                    if (creationTimeValue != null && creationTimeValue.Type != JTokenType.Null)
                                    {
                                        DateTimeOffset creationTimeInstance = ((DateTimeOffset)creationTimeValue);
                                        draftInstance.CreationTime = creationTimeInstance;
                                    }
                                    
                                    JToken lastModifiedTimeValue = draftValue["lastModifiedTime"];
                                    if (lastModifiedTimeValue != null && lastModifiedTimeValue.Type != JTokenType.Null)
                                    {
                                        DateTimeOffset lastModifiedTimeInstance = ((DateTimeOffset)lastModifiedTimeValue);
                                        draftInstance.LastModifiedTime = lastModifiedTimeInstance;
                                    }
                                    
                                    JToken parametersSequenceElement2 = ((JToken)draftValue["parameters"]);
                                    if (parametersSequenceElement2 != null && parametersSequenceElement2.Type != JTokenType.Null)
                                    {
                                        foreach (JProperty property2 in parametersSequenceElement2)
                                        {
                                            string parametersKey2 = ((string)property2.Name);
                                            JObject varToken2 = ((JObject)property2.Value);
                                            RunbookParameter runbookParameterInstance2 = new RunbookParameter();
                                            draftInstance.Parameters.Add(parametersKey2, runbookParameterInstance2);
                                            
                                            JToken typeValue2 = varToken2["type"];
                                            if (typeValue2 != null && typeValue2.Type != JTokenType.Null)
                                            {
                                                string typeInstance2 = ((string)typeValue2);
                                                runbookParameterInstance2.Type = typeInstance2;
                                            }
                                            
                                            JToken isMandatoryValue2 = varToken2["isMandatory"];
                                            if (isMandatoryValue2 != null && isMandatoryValue2.Type != JTokenType.Null)
                                            {
                                                bool isMandatoryInstance2 = ((bool)isMandatoryValue2);
                                                runbookParameterInstance2.IsMandatory = isMandatoryInstance2;
                                            }
                                            
                                            JToken positionValue2 = varToken2["position"];
                                            if (positionValue2 != null && positionValue2.Type != JTokenType.Null)
                                            {
                                                int positionInstance2 = ((int)positionValue2);
                                                runbookParameterInstance2.Position = positionInstance2;
                                            }
                                            
                                            JToken defaultValueValue2 = varToken2["defaultValue"];
                                            if (defaultValueValue2 != null && defaultValueValue2.Type != JTokenType.Null)
                                            {
                                                string defaultValueInstance2 = ((string)defaultValueValue2);
                                                runbookParameterInstance2.DefaultValue = defaultValueInstance2;
                                            }
                                        }
                                    }
                                }
                                
                                JToken provisioningStateValue = propertiesValue2["provisioningState"];
                                if (provisioningStateValue != null && provisioningStateValue.Type != JTokenType.Null)
                                {
                                    RunbookProvisioningState provisioningStateInstance = ((RunbookProvisioningState)Enum.Parse(typeof(RunbookProvisioningState), ((string)provisioningStateValue), true));
                                    propertiesInstance.ProvisioningState = provisioningStateInstance;
                                }
                                
                                JToken lastModifiedByValue = propertiesValue2["lastModifiedBy"];
                                if (lastModifiedByValue != null && lastModifiedByValue.Type != JTokenType.Null)
                                {
                                    string lastModifiedByInstance = ((string)lastModifiedByValue);
                                    propertiesInstance.LastModifiedBy = lastModifiedByInstance;
                                }
                                
                                JToken creationTimeValue2 = propertiesValue2["creationTime"];
                                if (creationTimeValue2 != null && creationTimeValue2.Type != JTokenType.Null)
                                {
                                    DateTimeOffset creationTimeInstance2 = ((DateTimeOffset)creationTimeValue2);
                                    propertiesInstance.CreationTime = creationTimeInstance2;
                                }
                                
                                JToken lastModifiedTimeValue2 = propertiesValue2["lastModifiedTime"];
                                if (lastModifiedTimeValue2 != null && lastModifiedTimeValue2.Type != JTokenType.Null)
                                {
                                    DateTimeOffset lastModifiedTimeInstance2 = ((DateTimeOffset)lastModifiedTimeValue2);
                                    propertiesInstance.LastModifiedTime = lastModifiedTimeInstance2;
                                }
                                
                                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);
                                runbookInstance.Id = idInstance;
                            }
                            
                            JToken nameValue = responseDoc["name"];
                            if (nameValue != null && nameValue.Type != JTokenType.Null)
                            {
                                string nameInstance = ((string)nameValue);
                                runbookInstance.Name = nameInstance;
                            }
                            
                            JToken locationValue = responseDoc["location"];
                            if (locationValue != null && locationValue.Type != JTokenType.Null)
                            {
                                string locationInstance = ((string)locationValue);
                                runbookInstance.Location = locationInstance;
                            }
                            
                            JToken tagsSequenceElement = ((JToken)responseDoc["tags"]);
                            if (tagsSequenceElement != null && tagsSequenceElement.Type != JTokenType.Null)
                            {
                                foreach (JProperty property3 in tagsSequenceElement)
                                {
                                    string tagsKey2 = ((string)property3.Name);
                                    string tagsValue2 = ((string)property3.Value);
                                    runbookInstance.Tags.Add(tagsKey2, tagsValue2);
                                }
                            }
                            
                            JToken typeValue3 = responseDoc["type"];
                            if (typeValue3 != null && typeValue3.Type != JTokenType.Null)
                            {
                                string typeInstance3 = ((string)typeValue3);
                                runbookInstance.Type = typeInstance3;
                            }
                            
                            JToken etagValue = responseDoc["etag"];
                            if (etagValue != null && etagValue.Type != JTokenType.Null)
                            {
                                string etagInstance = ((string)etagValue);
                                runbookInstance.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();
                }
            }
        }
    }
}
