Example #1
0
        /// <summary>
        /// Resolve URI UriTemplate with base URI and different types of arguments.
        /// </summary>
        /// <param name="baseUri">Base URI for resolving relative URI templates.</param>
        /// <param name="template">The URI UriTemplate to resolve.</param>
        /// <param name="parameters">Parameters for resolving URI UriTemplate (can be IDictionary<string, string>, NameValueCollection or
        /// any object where property names are used to match parameter names.</param>
        /// <returns></returns>
        public static Uri BindTemplate(Uri baseUri, UriTemplate template, object parameters = null)
        {
            if (baseUri == null)
            {
                throw new InvalidOperationException("It is not possible to bind relative URL templates without a base URL. Make sure session and/or service has been created with a base URL.");
            }
            Condition.Requires(template, "template").IsNotNull();

            if (parameters == null)
            {
                Dictionary <string, string> emptyParameters = new Dictionary <string, string>();
                return(template.BindByName(baseUri, emptyParameters));
            }
            else if (parameters is IDictionary <string, string> dp)
            {
                return(template.BindByName(baseUri, dp));
            }
            else if (parameters is NameValueCollection nvp)
            {
                IDictionary <string, string> dictParameters = nvp.Cast <string>().ToDictionary(p => p, p => nvp[p]);
                return(template.BindByName(baseUri, dictParameters));
            }
            else
            {
                Dictionary <string, string> parameterDictionary = DictionaryConverter.ConvertObjectPropertiesToDictionary(parameters);
                return(template.BindByName(baseUri, parameterDictionary));
            }
        }
        public void CanConvertSimpleObjectsToDictionary()
        {
            // Arrange
            object o1 = new { A = 10, B = "Train", C = true };
            object o2 = new SimpleObject {
                X = 10, Y = "Train", Z = true
            };

            // Act
            Dictionary <string, string> d1 = DictionaryConverter.ConvertObjectPropertiesToDictionary(o1);
            Dictionary <string, string> d2 = DictionaryConverter.ConvertObjectPropertiesToDictionary(o2);

            // Assert
            Assert.IsNotNull(d1);
            Assert.IsNotNull(d2);
            Assert.AreEqual(3, d1.Count);
            Assert.AreEqual(4, d2.Count);

            Assert.AreEqual("10", d1["A"]);
            Assert.AreEqual("Train", d1["B"]);
            Assert.AreEqual("True", d1["C"]);
            Assert.AreEqual("10", d2["X"]);
            Assert.AreEqual("Train", d2["Y"]);
            Assert.AreEqual("True", d2["Z"]);
        }
        public void WhenConvertingNullItReturnsEmptyDictionary()
        {
            // Arrange

            // Act
            Dictionary <string, string> d = DictionaryConverter.ConvertObjectPropertiesToDictionary(null);

            // Assert
            Assert.IsNotNull(d);
            Assert.AreEqual(0, d.Count);
        }
        public void WhenConvertingDictionaryItReturnsIt()
        {
            // Arrange
            Dictionary <string, string> d1 = new Dictionary <string, string>();

            d1["W"] = "Window";

            // Act
            Dictionary <string, string> d2 = DictionaryConverter.ConvertObjectPropertiesToDictionary(d1);

            // Assert
            Assert.IsNotNull(d2);
            Assert.AreEqual(1, d2.Count);
            Assert.AreEqual("Window", d2["W"]);
        }
Example #5
0
        /// <summary>
        /// Add query parameters to URL while keeping exiting parameters already specified in the URL.
        /// </summary>
        /// <remarks>This method respects repeated keys, such that adding "x=3&amp;x=4" to "x=1&amp;x=2" yields "x=1&amp;x=2&amp;x=3&amp;x=4".</remarks>
        /// <param name="url"></param>
        /// <param name="parameters">Either IDictionary&lt;string,string&gt;, NameValueCollection or any other
        /// class where the public properties are added as query parameters.</param>
        /// <returns>New Uri with added parameters</returns>
        public static Uri AddQueryParameters(this Uri url, object parameters)
        {
            if (parameters == null)
            {
                return(url);
            }

            Condition.Requires(url, "url").IsNotNull();

            NameValueCollection paramColl = UrlUtility.ParseQueryString(url.Query);

            if (parameters is IDictionary <string, string> )
            {
                foreach (KeyValuePair <string, string> p in (IDictionary <string, string>)parameters)
                {
                    paramColl.Add(p.Key, p.Value);
                }
            }
            else if (parameters is NameValueCollection)
            {
                NameValueCollection pcol = (NameValueCollection)parameters;
                paramColl.Add(pcol);
                //foreach (string key in pcol.AllKeys)
                //  paramColl.Add(key, pcol[key]);
            }
            else
            {
                Dictionary <string, string> parameterDictionary = DictionaryConverter.ConvertObjectPropertiesToDictionary(parameters);
                foreach (KeyValuePair <string, string> p in parameterDictionary)
                {
                    paramColl.Add(p.Key, p.Value);
                }
            }

            string q = paramColl.ToString();

            if (string.IsNullOrWhiteSpace(q))
            {
                return(url);
            }
            else
            {
                return(new Uri(url.GetLeftPart(UriPartial.Path) + "?" + q));
            }
        }
 private static Uri BindTemplate(Uri baseUri, UriTemplate template, object parameters = null)
 {
     if (parameters == null)
     {
         Dictionary <string, string> emptyParameters = new Dictionary <string, string>();
         return(template.BindByName(baseUri, emptyParameters));
     }
     else if (parameters is IDictionary <string, string> )
     {
         return(template.BindByName(baseUri, (IDictionary <string, string>)parameters));
     }
     else if (parameters is NameValueCollection)
     {
         return(template.BindByName(baseUri, (NameValueCollection)parameters));
     }
     else
     {
         Dictionary <string, string> parameterDictionary = DictionaryConverter.ConvertObjectPropertiesToDictionary(parameters);
         return(template.BindByName(baseUri, parameterDictionary));
     }
 }