Ejemplo n.º 1
0
        /// <summary>
        /// Generate code to build the URL from a url expression and method parameters
        /// </summary>
        /// <param name="variableName">The variable to store the url in.</param>
        /// <returns></returns>
        public virtual string BuildUrl(string variableName)
        {
            var builder = new IndentedStringBuilder();

            foreach (var pathParameter in ParameterTemplateModels.Where(p => p.Location == ParameterLocation.Path))
            {
                builder.AppendLine("{0} = {0}.Replace(\"{{{1}}}\", Uri.EscapeDataString({2}));",
                                   variableName,
                                   pathParameter.SerializedName,
                                   pathParameter.Type.ToString(ClientReference, pathParameter.Name));
            }
            if (ParameterTemplateModels.Any(p => p.Location == ParameterLocation.Query))
            {
                builder.AppendLine("List<string> queryParameters = new List<string>();");
                foreach (var queryParameter in ParameterTemplateModels
                         .Where(p => p.Location == ParameterLocation.Query))
                {
                    builder.AppendLine("if ({0} != null)", queryParameter.Name)
                    .AppendLine("{").Indent()
                    .AppendLine("queryParameters.Add(string.Format(\"{0}={{0}}\", Uri.EscapeDataString({1})));",
                                queryParameter.SerializedName, queryParameter.GetFormattedReferenceValue(ClientReference)).Outdent()
                    .AppendLine("}");
                }

                builder.AppendLine("if (queryParameters.Count > 0)")
                .AppendLine("{").Indent()
                .AppendLine("{0} += \"?\" + string.Join(\"&\", queryParameters);", variableName).Outdent()
                .AppendLine("}");
            }

            return(builder.ToString());
        }
Ejemplo n.º 2
0
        private void AddQueryParametersToUri(string variableName, IndentedStringBuilder builder)
        {
            builder.AppendLine("List<string> queryParameters = new List<string>();");
            if (ParameterTemplateModels.Any(p => p.Location == ParameterLocation.Query))
            {
                foreach (var queryParameter in ParameterTemplateModels
                         .Where(p => p.Location == ParameterLocation.Query))
                {
                    string queryParametersAddString =
                        "queryParameters.Add(string.Format(\"{0}={{0}}\", Uri.EscapeDataString({1})));";

                    if (queryParameter.SerializedName.Equals("$filter", StringComparison.OrdinalIgnoreCase) &&
                        queryParameter.Type is CompositeType &&
                        queryParameter.Location == ParameterLocation.Query)
                    {
                        queryParametersAddString =
                            "queryParameters.Add(string.Format(\"{0}={{0}}\", FilterString.Generate(filter)));";
                    }
                    else if (queryParameter.Extensions.ContainsKey(AzureCodeGenerator.SkipUrlEncodingExtension))
                    {
                        queryParametersAddString = "queryParameters.Add(string.Format(\"{0}={{0}}\", {1}));";
                    }

                    builder.AppendLine("if ({0} != null)", queryParameter.Name)
                    .AppendLine("{").Indent()
                    .AppendLine(queryParametersAddString,
                                queryParameter.SerializedName, queryParameter.GetFormattedReferenceValue(ClientReference))
                    .Outdent()
                    .AppendLine("}");
                }
            }

            builder.AppendLine("if (queryParameters.Count > 0)")
            .AppendLine("{").Indent()
            .AppendLine("{0} += \"?\" + string.Join(\"&\", queryParameters);", variableName).Outdent()
            .AppendLine("}");
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Detremines whether the Uri will have any query string
 /// </summary>
 /// <returns>True if a query string is possible given the method parameters, otherwise false</returns>
 protected virtual bool HasQueryParameters()
 {
     return(ParameterTemplateModels.Any(p => p.Location == ParameterLocation.Query));
 }