// TODO: Use continuation token.
        public virtual Uri CreateRequestUri(string template, NameValueCollection parameters = null, Query query = null, ContinuationToken continuationToken = null)
        {
            var uriTemplate = new UriTemplate(template, true);

            if (parameters != null)
            {
                // Add parameters one by one to avoid mismatch parameters count errors.
                foreach (var key in parameters.AllKeys)
                {
                    uriTemplate = uriTemplate.AddParameter(key, parameters[key]);
                }
            }

            var resolvedUri = uriTemplate.Resolve();

            // Build complete URI.
            Uri completeUri = new Uri(RuntimeEndpoint, resolvedUri);

            var uriBuilder = new UriBuilder(completeUri.AbsoluteUri)
            {
                Query = query.Coalesce().QueryString
            };

            return(uriBuilder.Uri);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Binds the <see cref="UriTemplate" />
        /// to the specified <c>params</c> by position.
        /// </summary>
        /// <param name="template">The template.</param>
        /// <param name="baseUri">The base URI.</param>
        /// <param name="values">The values.</param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException">template - The expected URI template is not here.</exception>
        /// <exception cref="FormatException"></exception>
        public static Uri BindByPosition(this UriTemplate template, Uri baseUri, params string[] values)
        {
            if (template == null)
            {
                throw new ArgumentNullException("template", "The expected URI template is not here.");
            }

            var keys = template.GetParameterNames();

            for (int i = 0; i < keys.Count(); i++)
            {
                template.AddParameter(keys.ElementAt(i), values.ElementAtOrDefault(i));
            }

            var resolved = template.Resolve();

            if (baseUri != null)
            {
                return(new UriBuilder(baseUri).WithPath(resolved).Uri);
            }
            else
            {
                var isAbsolute = Uri.IsWellFormedUriString(resolved, UriKind.Absolute);
                var isRelative = Uri.IsWellFormedUriString(resolved, UriKind.Relative);
                if (!isAbsolute && !isRelative)
                {
                    throw new FormatException($"The resolved URI template, {resolved}, is in an unknown format.");
                }
                return(isAbsolute ? new Uri(resolved, UriKind.Absolute) : new Uri(resolved, UriKind.Relative));
            }
        }
        public async Task <ApiId[]> GetManagedApis()
        {
            // https://management.azure.com/subscriptions/83e6374a-dfa5-428b-82ef-eab6c6bdd383/providers/Microsoft.Web/locations/brazilsouth/managedApis?api-version=2015-08-01-preview"

            const string template   = "/subscriptions/{subscriptionId}/providers/Microsoft.Web/locations/{location}/managedApis/?api-version={apiVersion}";
            var          parameters = new Dictionary <string, string>();

            parameters["subscriptionId"] = subscriptionId;
            parameters["location"]       = location;
            parameters["apiVersion"]     = ApiVersion;


            var uriTemplate = new UriTemplate(template, true);

            // Add parameters one by one to avoid mismatch parameters count errors.
            foreach (var key in parameters)
            {
                uriTemplate = uriTemplate.AddParameter(key.Key, key.Value);
            }

            var resolvedUri = uriTemplate.Resolve();

            // Build complete URI.
            Uri url = new Uri(_managementEndpointUri, resolvedUri);

            var json = await SendAsync <JObject>(HttpMethod.Get, url);

            var apis = json["value"].ToObject <ArmEnvelope <JToken>[]>();

            return(Array.ConvertAll(apis, x => new ApiId
            {
                Id = x.Id,
                Name = x.Name
            }));
        }
Ejemplo n.º 4
0
        public void Can_generate_a_path_with_anonymous_complex_route_properties()
        {
            string       expected      = _baseUrl + "foo/1?bar.abc=abc&bar.def=def";
            const string routeName     = "foo.show";
            const string routeTemplate = "foo/{id}";

            string template = TestHelper.CreateATemplateGenerator(_baseUrl, routeName, routeTemplate)
                              .Generate(routeName, new { Id = 1, Bar = new { Abc = "abc", Def = "def" } });
            var uriTemplate = new UriTemplate(template);

            uriTemplate.AddParameters(new { id = 1 });
            uriTemplate.AddParameter("bar.abc", "abc");
            uriTemplate.AddParameter("bar.def", "def");

            string actual = uriTemplate.Resolve();

            Assert.Equal(expected, actual);
        }
Ejemplo n.º 5
0
 public HttpRequestMessage CreateRequest()
 {
     return(new HttpRequestMessage()
     {
         RequestUri = new Uri(_Template
                              .AddParameter("fromlanguage", FromLanguage)
                              .AddParameter("tolanguage", ToLanguage)
                              .AddParameter("fromphrase", FromPhrase)
                              .Resolve())
     });
 }
Ejemplo n.º 6
0
        public void Can_generate_a_fully_qualified_path()
        {
            string       expected      = _baseUrl + "foo/1";
            const string routeName     = "foo.show";
            const string routeTemplate = "foo/{id}";

            string template = TestHelper.CreateATemplateGenerator(_baseUrl, routeName, routeTemplate)
                              .Generate(routeName);
            var uriTemplate = new UriTemplate(template);

            uriTemplate.AddParameter("id", 1);

            string actual = uriTemplate.Resolve();

            Assert.Equal(expected, actual);
        }
Ejemplo n.º 7
0
        public void Can_generate_two_optional_path_items_template()
        {
            string       expected      = _baseUrl + "foo/2";
            const string routeName     = "foo.one.two";
            const string routeTemplate = "foo/{one}/{two}";

            string template = TestHelper.CreateATemplateGenerator(_baseUrl, routeName, routeTemplate,
                                                                  routeDefaults: new { one = RouteParameter.Optional, two = RouteParameter.Optional })
                              .Generate(routeName, new { two = 2 });
            var uriTemplate = new UriTemplate(template);

            uriTemplate.AddParameter("two", "2");

            string actual = uriTemplate.Resolve();

            Assert.Equal(expected, actual);
        }
        private Uri BuildUri(Uri baseUri, string template, IDictionary <string, string> parameters)
        {
            var uriTemplate = new UriTemplate(template, true);

            // Add parameters one by one to avoid mismatch parameters count errors.
            foreach (var key in parameters)
            {
                uriTemplate = uriTemplate.AddParameter(key.Key, key.Value);
            }

            var resolvedUri = uriTemplate.Resolve();

            // Build complete URI.
            Uri completeUri = new Uri(baseUri, resolvedUri);

            return(completeUri);
        }
Ejemplo n.º 9
0
        public static Uri BindByName(this UriTemplate template, Uri baseUri, IDictionary <string, string> parameters)
        {
            IDictionary <string, string> unusedParameters = new Dictionary <string, string>(parameters);

            foreach (var p in template.GetParameterNames())
            {
                if (parameters.TryGetValue(p, out string v))
                {
                    template = template.AddParameter(p, v);
                    unusedParameters.Remove(p);
                }
            }

            var url = new Uri(baseUri, template.Resolve());

            url = url.AddQueryParameters(unusedParameters);

            return(url);
        }