Esempio n. 1
0
        private string ResolveUrlSegments(string path, List <object> segments)
        {
            if (segments == null)
            {
                throw new ArgumentNullException("segments");
            }

            // Support alternate client authorities here

            if (path.Equals("search"))
            {
                _client.Authority   = Globals.SearchAPIAuthority;
                _client.VersionPath = null;
            }
            else
            {
                _client.Authority   = Globals.RestAPIAuthority;
                _client.VersionPath = "1";
            }

            for (var i = 0; i < segments.Count; i++)
            {
                // Currently only trends takes DateTimes
                if (segments[i] is DateTime)
                {
                    segments[i] = ((DateTime)segments[i]).ToString("yyyy-MM-dd");
                }

                if (typeof(IEnumerable).IsAssignableFrom(segments[i].GetType()) && !(segments[i].GetType() == typeof(string)))
                {
                    ResolveEnumerableUrlSegments(segments, i);
                }
            }

            path = PathHelpers.ReplaceUriTemplateTokens(segments, path);

            PathHelpers.EscapeDataContainingUrlSegments(segments);

            if (IncludeEntities)// && !path.Contains("/lists"))
            {
                segments.Add(segments.Count() > 1 ? "&include_entities=" : "?include_entities=");
                segments.Add("1");
            }

            segments.Insert(0, path);

            return(string.Concat(segments.ToArray()).ToString(CultureInfo.InvariantCulture));
        }
Esempio n. 2
0
        private string ResolveUrlSegments(string path, List <object> segments)
        {
            if (segments == null)
            {
                throw new ArgumentNullException("segments");
            }

            var cleansed = new List <object>();

            for (var i = 0; i < segments.Count; i++)
            {
                if (i == 0)
                {
                    cleansed.Add(segments[i]);
                }
                if (i > 0 && i % 2 == 0)
                {
                    var key   = segments[i - 1];
                    var value = segments[i];
                    if (value != null)
                    {
                        if (cleansed.Count == 1 && key is string)
                        {
                            var keyString = key.ToString();
                            if (keyString.StartsWith("&"))
                            {
                                key = "?" + keyString.Substring(1);
                            }
                        }
                        cleansed.Add(key);
                        cleansed.Add(value);
                    }
                }
            }
            segments = cleansed;

            for (var i = 0; i < segments.Count; i++)
            {
                if (segments[i] is DateTime)
                {
                    segments[i] = ((DateTime)segments[i]).ToString("yyyy-MM-dd");
                }

                if (segments[i] is bool)
                {
                    var flag = (bool)segments[i];
                    segments[i] = flag ? "1" : "0";
                }

                if (segments[i] is double)
                {
                    segments[i] = ((double)segments[i]).ToString(CultureInfo.InvariantCulture);
                }

                if (segments[i] is decimal)
                {
                    segments[i] = ((decimal)segments[i]).ToString(CultureInfo.InvariantCulture);
                }

                if (segments[i] is float)
                {
                    segments[i] = ((float)segments[i]).ToString(CultureInfo.InvariantCulture);
                }

                if (segments[i] is IEnumerable && !(segments[i] is string))
                {
                    ResolveEnumerableUrlSegments(segments, i);
                }
            }

            path = PathHelpers.ReplaceUriTemplateTokens(segments, path);

            PathHelpers.EscapeDataContainingUrlSegments(segments);

            const string includeEntities = "include_entities";
            const string includeRetweets = "include_rts";

            if (IncludeEntities && !IsKeyAlreadySet(segments, includeEntities))
            {
                segments.Add(segments.Count() > 1 ? "&" + includeEntities + "=" : "?" + includeEntities + "=");
                segments.Add("1");
            }
            if (IncludeRetweets && !IsKeyAlreadySet(segments, includeRetweets))
            {
                segments.Add(segments.Count() > 1 ? "&" + includeRetweets + "=" : "?" + includeRetweets + "=");
                segments.Add("1");
            }

            segments.Insert(0, path);
#if !WINRT
            return(string.Concat(segments.ToArray()).ToString(CultureInfo.InvariantCulture));
#else
            return(string.Concat(segments.ToArray()));
#endif
        }