Example #1
0
        // some page routines are not in the paged format
        static IDictionary <string, object> _InvokeFlatPaged(string path, bool sendLang, int minPage, int maxPage, IDictionary <string, object> args, Func <object, object> fixupResultItem, Func <object, object> fixupError)
        {
            var result     = new JsonObject();
            var hasResults = false;

            if (0 > minPage)
            {
                throw new ArgumentOutOfRangeException(nameof(minPage));
            }
            if (0 > maxPage)
            {
                throw new ArgumentOutOfRangeException(nameof(maxPage));
            }
            if (maxPage < minPage)
            {
                throw new ArgumentException("The parameter " + nameof(minPage) + "must be less than or equal to " + nameof(maxPage), nameof(minPage));
            }
            if (null == args)
            {
                args = new JsonObject();
            }
            var args2 = new JsonObject();

            JsonObject.CopyTo(args, args2);

            for (var i = minPage; 1000 > i; ++i)
            {
                var thisHasResults = false;
                // the server expects the pages to be one based
                if (0 != i)
                {
                    args2["page"] = i + 1;
                }
                var d = Tmdb._Invoke(path, sendLang, args2, null, null, fixupError, null) as IDictionary <string, object>;
                if (null != d)
                {
                    object o;
                    // we don't know the name of the field
                    // so fetch the first field that's a list
                    foreach (var field in d)
                    {
                        var ll = field.Value as IList <object>;
                        if (null != ll)
                        {
                            o              = ll;
                            hasResults     = true;
                            thisHasResults = true;
                            if (null != fixupResultItem)
                            {
                                o = fixupResultItem(o);
                            }
                            result.Add(i.ToString(), o);
                            break;
                        }
                    }
                }
                if (maxPage <= i || !thisHasResults)
                {
                    break;
                }
            }
            if (hasResults)
            {
                return(result);
            }
            return(null);
        }
Example #2
0
        static IDictionary <string, object> _InvokePaged(string path, bool sendLang, int minPage, int maxPage, IDictionary <string, object> args, Func <object, object> fixupResultItem, Func <object, object> fixupError)
        {
            var result     = new JsonObject();
            var hasResults = false;

            if (0 > minPage)
            {
                throw new ArgumentOutOfRangeException(nameof(minPage));
            }
            if (0 > maxPage)
            {
                throw new ArgumentOutOfRangeException(nameof(maxPage));
            }
            if (maxPage < minPage)
            {
                throw new ArgumentException("The parameter " + nameof(minPage) + "must be less than or equal to " + nameof(maxPage), nameof(minPage));
            }
            if (null == args)
            {
                args = new JsonObject();
            }
            var args2 = new JsonObject();

            JsonObject.CopyTo(args, args2);

            for (var i = minPage; 1000 > i; ++i)
            {
                // the server expects the pages to be one based
                if (0 != i)
                {
                    args2["page"] = i + 1;
                }
                var d = Tmdb._Invoke(path, sendLang, args2, null, null, fixupError, null) as IDictionary <string, object>;
                if (null != d)
                {
                    object o;
                    if (minPage == i)
                    {
                        if (0 > maxPage)
                        {
                            if (d.TryGetValue("total_pages", out o) && o is int)
                            {
                                maxPage = ((int)o) - 1;
                            }
                            else
                            {
                                maxPage = minPage;
                            }
                        }
                        else
                        {
                            if (d.TryGetValue("total_pages", out o) && o is int)
                            {
                                var p = ((int)o) - 1;
                                if (0 > p)
                                {
                                    p = 0;
                                }
                                if (-1 != p)
                                {
                                    if (maxPage > p)
                                    {
                                        maxPage = p;
                                    }
                                }
                            }
                        }
                    }
                    if (d.TryGetValue("results", out o))
                    {
                        hasResults = true;
                        if (null != fixupResultItem)
                        {
                            o = fixupResultItem(o);
                        }
                        result.Add((i + 1).ToString(), o);
                    }
                }
                if (maxPage <= i)
                {
                    break;
                }
            }
            if (hasResults)
            {
                return(result);
            }
            return(null);
        }