/// <summary>
 /// Post parameters from multiple different parameter groups
 /// </summary>
 /// <param name='operations'>
 /// The operations group for this extension method.
 /// </param>
 /// <param name='firstParameterGroup'>
 /// Additional parameters for the operation
 /// </param>
 /// <param name='secondParameterGroup'>
 /// Additional parameters for the operation
 /// </param>
 public static void PostMultipleParameterGroups(this IParameterGroupingOperations operations, FirstParameterGroup firstParameterGroup = default(FirstParameterGroup), SecondParameterGroup secondParameterGroup = default(SecondParameterGroup))
 {
     Task.Factory.StartNew(s => ((IParameterGroupingOperations)s).PostMultipleParameterGroupsAsync(firstParameterGroup, secondParameterGroup), operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult();
 }
 /// <summary>
 /// Post parameters from multiple different parameter groups
 /// </summary>
 /// <param name='operations'>
 /// The operations group for this extension method.
 /// </param>
 /// <param name='firstParameterGroup'>
 /// Additional parameters for the operation
 /// </param>
 /// <param name='secondParameterGroup'>
 /// Additional parameters for the operation
 /// </param>
 /// <param name='cancellationToken'>
 /// The cancellation token.
 /// </param>
 public static async Task PostMultipleParameterGroupsAsync(this IParameterGroupingOperations operations, FirstParameterGroup firstParameterGroup = default(FirstParameterGroup), SecondParameterGroup secondParameterGroup = default(SecondParameterGroup), CancellationToken cancellationToken = default(CancellationToken))
 {
     await operations.PostMultipleParameterGroupsWithHttpMessagesAsync(firstParameterGroup, secondParameterGroup, null, cancellationToken).ConfigureAwait(false);
 }
Beispiel #3
0
        /// <summary>
        /// Post parameters from multiple different parameter groups
        /// </summary>
        /// <param name='firstParameterGroup'>
        /// Additional parameters for the operation
        /// </param>
        /// <param name='secondParameterGroup'>
        /// Additional parameters for the operation
        /// </param>
        /// <param name='customHeaders'>
        /// Headers that will be added to request.
        /// </param>
        /// <param name='cancellationToken'>
        /// The cancellation token.
        /// </param>
        public async Task <AzureOperationResponse> PostMultipleParameterGroupsWithHttpMessagesAsync(FirstParameterGroup firstParameterGroup = default(FirstParameterGroup), SecondParameterGroup secondParameterGroup = default(SecondParameterGroup), Dictionary <string, List <string> > customHeaders = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            var headerOne = (firstParameterGroup == null ? default(string) : firstParameterGroup.HeaderOne);
            var queryOne  = (firstParameterGroup == null ? default(int?) : firstParameterGroup.QueryOne);
            var headerTwo = (secondParameterGroup == null ? default(string) : secondParameterGroup.HeaderTwo);
            var queryTwo  = (secondParameterGroup == null ? default(int?) : secondParameterGroup.QueryTwo);
            // Tracing
            bool   shouldTrace  = ServiceClientTracing.IsEnabled;
            string invocationId = null;

            if (shouldTrace)
            {
                invocationId = ServiceClientTracing.NextInvocationId.ToString();
                Dictionary <string, object> tracingParameters = new Dictionary <string, object>();
                tracingParameters.Add("headerOne", headerOne);
                tracingParameters.Add("queryOne", queryOne);
                tracingParameters.Add("headerTwo", headerTwo);
                tracingParameters.Add("queryTwo", queryTwo);
                tracingParameters.Add("cancellationToken", cancellationToken);
                ServiceClientTracing.Enter(invocationId, this, "PostMultipleParameterGroups", tracingParameters);
            }
            // Construct URL
            var           baseUrl         = this.Client.BaseUri.AbsoluteUri;
            var           url             = new Uri(new Uri(baseUrl + (baseUrl.EndsWith("/") ? "" : "/")), "parameterGrouping/postMultipleParameterGroups").ToString();
            List <string> queryParameters = new List <string>();

            if (queryOne != null)
            {
                queryParameters.Add(string.Format("query-one={0}", Uri.EscapeDataString(JsonConvert.SerializeObject(queryOne, this.Client.SerializationSettings).Trim('"'))));
            }
            if (queryTwo != null)
            {
                queryParameters.Add(string.Format("query-two={0}", Uri.EscapeDataString(JsonConvert.SerializeObject(queryTwo, this.Client.SerializationSettings).Trim('"'))));
            }
            if (queryParameters.Count > 0)
            {
                url += "?" + string.Join("&", queryParameters);
            }
            // Create HTTP transport objects
            HttpRequestMessage httpRequest = new HttpRequestMessage();

            httpRequest.Method     = new HttpMethod("POST");
            httpRequest.RequestUri = new Uri(url);
            // Set Headers
            httpRequest.Headers.TryAddWithoutValidation("x-ms-client-request-id", Guid.NewGuid().ToString());
            if (this.Client.AcceptLanguage != null)
            {
                if (httpRequest.Headers.Contains("accept-language"))
                {
                    httpRequest.Headers.Remove("accept-language");
                }
                httpRequest.Headers.TryAddWithoutValidation("accept-language", this.Client.AcceptLanguage);
            }
            if (headerOne != null)
            {
                if (httpRequest.Headers.Contains("header-one"))
                {
                    httpRequest.Headers.Remove("header-one");
                }
                httpRequest.Headers.TryAddWithoutValidation("header-one", headerOne);
            }
            if (headerTwo != null)
            {
                if (httpRequest.Headers.Contains("header-two"))
                {
                    httpRequest.Headers.Remove("header-two");
                }
                httpRequest.Headers.TryAddWithoutValidation("header-two", headerTwo);
            }
            if (customHeaders != null)
            {
                foreach (var header in customHeaders)
                {
                    if (httpRequest.Headers.Contains(header.Key))
                    {
                        httpRequest.Headers.Remove(header.Key);
                    }
                    httpRequest.Headers.TryAddWithoutValidation(header.Key, header.Value);
                }
            }

            // Set Credentials
            if (this.Client.Credentials != null)
            {
                cancellationToken.ThrowIfCancellationRequested();
                await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false);
            }
            // Send Request
            if (shouldTrace)
            {
                ServiceClientTracing.SendRequest(invocationId, httpRequest);
            }
            cancellationToken.ThrowIfCancellationRequested();
            HttpResponseMessage httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);

            if (shouldTrace)
            {
                ServiceClientTracing.ReceiveResponse(invocationId, httpResponse);
            }
            HttpStatusCode statusCode = httpResponse.StatusCode;

            cancellationToken.ThrowIfCancellationRequested();
            if (!(statusCode == (HttpStatusCode)Enum.Parse(typeof(HttpStatusCode), "OK")))
            {
                var    ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", statusCode));
                string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

                Error errorBody = JsonConvert.DeserializeObject <Error>(responseContent, this.Client.DeserializationSettings);
                if (errorBody != null)
                {
                    ex.Body = errorBody;
                }
                ex.Request  = httpRequest;
                ex.Response = httpResponse;
                if (shouldTrace)
                {
                    ServiceClientTracing.Error(invocationId, ex);
                }
                throw ex;
            }
            // Create Result
            var result = new AzureOperationResponse();

            result.Request  = httpRequest;
            result.Response = httpResponse;
            if (httpResponse.Headers.Contains("x-ms-request-id"))
            {
                result.RequestId = httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault();
            }
            if (shouldTrace)
            {
                ServiceClientTracing.Exit(invocationId, result);
            }
            return(result);
        }
        public void ParameterGroupingTests()
        {
            const int    bodyParameter   = 1234;
            const string headerParameter = "header";
            const int    queryParameter  = 21;
            const string pathParameter   = "path";

            using (var client = new AutoRestParameterGroupingTestService(
                       Fixture.Uri,
                       new TokenCredentials(Guid.NewGuid().ToString())))
            {
                //Valid required parameters
                ParameterGroupingPostRequiredParameters requiredParameters = new ParameterGroupingPostRequiredParameters(bodyParameter, pathParameter)
                {
                    CustomHeader = headerParameter,
                    Query        = queryParameter
                };

                client.ParameterGrouping.PostRequired(requiredParameters);

                //Required parameters but null optional parameters
                requiredParameters = new ParameterGroupingPostRequiredParameters(bodyParameter, pathParameter);

                client.ParameterGrouping.PostRequired(requiredParameters);

                //Required parameters object is not null, but a required property of the object is
                requiredParameters = new ParameterGroupingPostRequiredParameters(null, pathParameter);

                Assert.Throws <ValidationException>(() => client.ParameterGrouping.PostRequired(requiredParameters));

                //null required parameters
                Assert.Throws <ValidationException>(() => client.ParameterGrouping.PostRequired(null));

                //Valid optional parameters
                ParameterGroupingPostOptionalParameters optionalParameters = new ParameterGroupingPostOptionalParameters()
                {
                    CustomHeader = headerParameter,
                    Query        = queryParameter
                };

                client.ParameterGrouping.PostOptional(optionalParameters);

                //null optional paramters
                client.ParameterGrouping.PostOptional(null);

                //Multiple grouped parameters
                FirstParameterGroup firstGroup = new FirstParameterGroup
                {
                    HeaderOne = headerParameter,
                    QueryOne  = queryParameter
                };
                SecondParameterGroup secondGroup = new SecondParameterGroup
                {
                    HeaderTwo = "header2",
                    QueryTwo  = 42
                };

                client.ParameterGrouping.PostMultipleParameterGroups(firstGroup, secondGroup);

                //Multiple grouped parameters -- some optional parameters omitted
                firstGroup = new FirstParameterGroup
                {
                    HeaderOne = headerParameter
                };
                secondGroup = new SecondParameterGroup
                {
                    QueryTwo = 42
                };

                client.ParameterGrouping.PostMultipleParameterGroups(firstGroup, secondGroup);
            }
        }
 /// <summary>
 /// Post parameters from multiple different parameter groups
 /// </summary>
 /// <param name='operations'>
 /// The operations group for this extension method.
 /// </param>
 /// <param name='firstParameterGroup'>
 /// Additional parameters for the operation
 /// </param>
 /// <param name='secondParameterGroup'>
 /// Additional parameters for the operation
 /// </param>
 public static void PostMultipleParameterGroups(this IParameterGroupingOperations operations, FirstParameterGroup firstParameterGroup = default(FirstParameterGroup), SecondParameterGroup secondParameterGroup = default(SecondParameterGroup))
 {
     Task.Factory.StartNew(s => ((IParameterGroupingOperations)s).PostMultipleParameterGroupsAsync(firstParameterGroup, secondParameterGroup), operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult();
 }
 /// <summary>
 /// Post parameters from multiple different parameter groups
 /// </summary>
 /// <param name='operations'>
 /// The operations group for this extension method.
 /// </param>
 /// <param name='firstParameterGroup'>
 /// Additional parameters for the operation
 /// </param>
 /// <param name='secondParameterGroup'>
 /// Additional parameters for the operation
 /// </param>
 /// <param name='cancellationToken'>
 /// The cancellation token.
 /// </param>
 public static async Task PostMultipleParameterGroupsAsync( this IParameterGroupingOperations operations, FirstParameterGroup firstParameterGroup = default(FirstParameterGroup), SecondParameterGroup secondParameterGroup = default(SecondParameterGroup), CancellationToken cancellationToken = default(CancellationToken))
 {
     await operations.PostMultipleParameterGroupsWithHttpMessagesAsync(firstParameterGroup, secondParameterGroup, null, cancellationToken).ConfigureAwait(false);
 }