/// <summary>
 /// Create or update a transformation for a stream analytics job. The
 /// raw json content will be used.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.Azure.Management.StreamAnalytics.ITransformationOperations.
 /// </param>
 /// <param name='resourceGroupName'>
 /// Required. The resource group name of the stream analytics job.
 /// </param>
 /// <param name='jobName'>
 /// Required. The name of the stream analytics job.
 /// </param>
 /// <param name='transformationName'>
 /// Required. The name of the transformation for the stream analytics
 /// job.
 /// </param>
 /// <param name='parameters'>
 /// Required. The parameters required to create or update a
 /// transformation for the stream analytics job. It is in json format.
 /// </param>
 /// <returns>
 /// The response of the transformation create operation.
 /// </returns>
 public static TransformationCreateOrUpdateResponse CreateOrUpdateWithRawJsonContent(this ITransformationOperations operations, string resourceGroupName, string jobName, string transformationName, TransformationCreateOrUpdateWithRawJsonContentParameters parameters)
 {
     return(Task.Factory.StartNew((object s) =>
     {
         return ((ITransformationOperations)s).CreateOrUpdateWithRawJsonContentAsync(resourceGroupName, jobName, transformationName, parameters);
     }
                                  , operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult());
 }
 /// <summary>
 /// Create or update a transformation for a stream analytics job. The
 /// raw json content will be used.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.Azure.Management.StreamAnalytics.ITransformationOperations.
 /// </param>
 /// <param name='resourceGroupName'>
 /// Required. The resource group name of the stream analytics job.
 /// </param>
 /// <param name='jobName'>
 /// Required. The name of the stream analytics job.
 /// </param>
 /// <param name='transformationName'>
 /// Required. The name of the transformation for the stream analytics
 /// job.
 /// </param>
 /// <param name='parameters'>
 /// Required. The parameters required to create or update a
 /// transformation for the stream analytics job. It is in json format.
 /// </param>
 /// <returns>
 /// The response of the transformation create operation.
 /// </returns>
 public static Task <TransformationCreateOrUpdateResponse> CreateOrUpdateWithRawJsonContentAsync(this ITransformationOperations operations, string resourceGroupName, string jobName, string transformationName, TransformationCreateOrUpdateWithRawJsonContentParameters parameters)
 {
     return(operations.CreateOrUpdateWithRawJsonContentAsync(resourceGroupName, jobName, transformationName, parameters, CancellationToken.None));
 }
        /// <summary>
        /// Create or update a transformation for a stream analytics job. The
        /// raw json content will be used.
        /// </summary>
        /// <param name='resourceGroupName'>
        /// Required. The resource group name of the stream analytics job.
        /// </param>
        /// <param name='jobName'>
        /// Required. The name of the stream analytics job.
        /// </param>
        /// <param name='transformationName'>
        /// Required. The name of the transformation for the stream analytics
        /// job.
        /// </param>
        /// <param name='parameters'>
        /// Required. The parameters required to create or update a
        /// transformation for the stream analytics job. It is in json format.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// The response of the transformation create operation.
        /// </returns>
        public async Task <TransformationCreateOrUpdateResponse> CreateOrUpdateWithRawJsonContentAsync(string resourceGroupName, string jobName, string transformationName, TransformationCreateOrUpdateWithRawJsonContentParameters parameters, CancellationToken cancellationToken)
        {
            // Validate
            if (resourceGroupName == null)
            {
                throw new ArgumentNullException("resourceGroupName");
            }
            if (jobName == null)
            {
                throw new ArgumentNullException("jobName");
            }
            if (transformationName == null)
            {
                throw new ArgumentNullException("transformationName");
            }
            if (parameters == null)
            {
                throw new ArgumentNullException("parameters");
            }
            if (parameters.Content == null)
            {
                throw new ArgumentNullException("parameters.Content");
            }

            // 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("jobName", jobName);
                tracingParameters.Add("transformationName", transformationName);
                tracingParameters.Add("parameters", parameters);
                TracingAdapter.Enter(invocationId, this, "CreateOrUpdateWithRawJsonContentAsync", 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/Microsoft.StreamAnalytics/streamingjobs/";
            url = url + Uri.EscapeDataString(jobName);
            url = url + "/transformations/";
            url = url + Uri.EscapeDataString(transformationName);
            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("x-ms-client-request-id", Guid.NewGuid().ToString());

                // Set Credentials
                cancellationToken.ThrowIfCancellationRequested();
                await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false);

                // Serialize Request
                string requestContent = parameters.Content;
                httpRequest.Content = new StringContent(requestContent, Encoding.UTF8);
                httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");

                // 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
                    TransformationCreateOrUpdateResponse result = null;
                    // Deserialize Response
                    if (statusCode == HttpStatusCode.OK || statusCode == HttpStatusCode.Created)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

                        result = new TransformationCreateOrUpdateResponse();
                        JToken responseDoc = null;
                        if (string.IsNullOrEmpty(responseContent) == false)
                        {
                            responseDoc = JToken.Parse(responseContent);
                        }

                        if (responseDoc != null && responseDoc.Type != JTokenType.Null)
                        {
                            Transformation transformationInstance = new Transformation();
                            result.Transformation = transformationInstance;

                            JToken nameValue = responseDoc["name"];
                            if (nameValue != null && nameValue.Type != JTokenType.Null)
                            {
                                string nameInstance = ((string)nameValue);
                                transformationInstance.Name = nameInstance;
                            }

                            JToken propertiesValue = responseDoc["properties"];
                            if (propertiesValue != null && propertiesValue.Type != JTokenType.Null)
                            {
                                TransformationProperties propertiesInstance = new TransformationProperties();
                                transformationInstance.Properties = propertiesInstance;

                                JToken etagValue = propertiesValue["etag"];
                                if (etagValue != null && etagValue.Type != JTokenType.Null)
                                {
                                    string etagInstance = ((string)etagValue);
                                    propertiesInstance.Etag = etagInstance;
                                }

                                JToken streamingUnitsValue = propertiesValue["streamingUnits"];
                                if (streamingUnitsValue != null && streamingUnitsValue.Type != JTokenType.Null)
                                {
                                    int streamingUnitsInstance = ((int)streamingUnitsValue);
                                    propertiesInstance.StreamingUnits = streamingUnitsInstance;
                                }

                                JToken queryValue = propertiesValue["query"];
                                if (queryValue != null && queryValue.Type != JTokenType.Null)
                                {
                                    string queryInstance = ((string)queryValue);
                                    propertiesInstance.Query = queryInstance;
                                }
                            }
                        }
                    }
                    result.StatusCode = statusCode;
                    if (httpResponse.Headers.Contains("Date"))
                    {
                        result.Date = DateTime.Parse(httpResponse.Headers.GetValues("Date").FirstOrDefault(), CultureInfo.InvariantCulture);
                    }
                    if (httpResponse.Headers.Contains("ETag"))
                    {
                        result.Transformation.Properties.Etag = httpResponse.Headers.GetValues("ETag").FirstOrDefault();
                    }
                    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();
                }
            }
        }