public async Task <IList <T> > PaginateAll <T, TNext>(
            IPaginatable <T, TNext> firstPage, Func <TNext, IPaginatable <T, TNext> > mapper, IAPIConnector connector
            )
        {
            Ensure.ArgumentNotNull(firstPage, nameof(firstPage));
            Ensure.ArgumentNotNull(mapper, nameof(mapper));
            Ensure.ArgumentNotNull(connector, nameof(connector));

            var page    = firstPage;
            var results = new List <T>();

            if (page.Items != null)
            {
                results.AddRange(page.Items);
            }
            while (page.Next != null && await ShouldContinue(results, page).ConfigureAwait(false))
            {
                var next = await connector.Get <TNext>(new Uri(page.Next, UriKind.Absolute)).ConfigureAwait(false);

                page = mapper(next);
                if (page.Items != null)
                {
                    results.AddRange(page.Items);
                }
            }

            return(results);
        }
Esempio n. 2
0
        public async IAsyncEnumerable <T> Paginate <T, TNext>(
            IPaginatable <T, TNext> firstPage,
            Func <TNext, IPaginatable <T, TNext> > mapper,
            IAPIConnector connector,
            [EnumeratorCancellation] CancellationToken cancel = default)
        {
            Ensure.ArgumentNotNull(firstPage, nameof(firstPage));
            Ensure.ArgumentNotNull(mapper, nameof(mapper));
            Ensure.ArgumentNotNull(connector, nameof(connector));

            var page = firstPage;

            foreach (var item in page.Items)
            {
                yield return(item);
            }
            while (page.Next != null)
            {
                var next = await connector.Get <TNext>(new Uri(page.Next, UriKind.Absolute)).ConfigureAwait(false);

                page = mapper(next);
                foreach (var item in page.Items)
                {
                    yield return(item);
                }
            }
        }
Esempio n. 3
0
        private async Task <ConfigurationStep> AskForConfirmation(IPaginatable element)
        {
            var deletionConfirmationStep = new ConfigurationStep("Do you really want to do that?", interactive, context, ConfigurationStep.StepType.Reaction, null);
            var message = await context.Channel.SendMessageAsync($"delete: { element.display() }");

            deletionConfirmationStep.MessagesToRemoveOnNextProgression.Add(message);
            var deniedStep = new ReactionAction(new Emoji("✅"));

            deniedStep.Action = async(ConfigurationStep a) =>
            {
                a.Result = actionOnIndex(element);
                await Task.CompletedTask;
            };

            var confirmStep = new ReactionAction(new Emoji("❌"));

            confirmStep.Action = async(ConfigurationStep a) =>
            {
                a.Result = step;
                await Task.CompletedTask;
            };

            deletionConfirmationStep.Actions.Add(confirmStep);
            deletionConfirmationStep.Actions.Add(deniedStep);

            return(deletionConfirmationStep);
        }
Esempio n. 4
0
        public static IQueryable <T> Paginate <T>(this IQueryable <T> query, IPaginatable request)
        {
            try
            {
                if (request.Page.HasValue)
                {
                    if (request.PageSize.HasValue)
                    {
                        query = query
                                .Skip(request.Page.Value * request.PageSize.Value);
                    }
                }

                if (request.PageSize.HasValue)
                {
                    query = query
                            .Take(request.PageSize.Value);
                }

                return(query);
            }
            catch (Exception e)
            {
                throw new Exception("Was an exception in Paginate", e);
            }
        }
        public async IAsyncEnumerable <T> Paginate <T>(
            IPaginatable <T> firstPage,
            IAPIConnector connector,
            [EnumeratorCancellation] CancellationToken cancel = default)
        {
            Ensure.ArgumentNotNull(firstPage, nameof(firstPage));
            Ensure.ArgumentNotNull(connector, nameof(connector));
            if (firstPage.Items == null)
            {
                throw new ArgumentException("The first page has to contain an Items list!", nameof(firstPage));
            }

            var page = firstPage;

            foreach (var item in page.Items)
            {
                yield return(item);
            }
            while (page.Next != null)
            {
                page = await connector.Get <Paging <T> >(new Uri(page.Next, UriKind.Absolute)).ConfigureAwait(false);

                foreach (var item in page.Items !)
                {
                    yield return(item);
                }
            }
        }
 /// <summary>
 /// Paginate through pages by using IAsyncEnumerable, introduced in C# 8
 /// The default paginator will fetch all available resources without a delay between requests.
 /// This can drain your request limit quite fast, so consider using a custom paginator with delays.
 /// </summary>
 /// <param name="firstPage">A first page, will be included in the output list!</param>
 /// <param name="paginator">Optional. If not supplied, DefaultPaginator will be used</param>
 /// <param name="cancellationToken">An optional Cancellation Token</param>
 /// <typeparam name="T">The Paging-Type</typeparam>
 /// <returns>An iterable IAsyncEnumerable</returns>
 public IAsyncEnumerable <T> Paginate <T>(
     IPaginatable <T> firstPage,
     IPaginator?paginator = null,
     CancellationToken cancellationToken = default
     )
 {
     return((paginator ?? DefaultPaginator).Paginate(firstPage, _apiConnector, cancellationToken));
 }
 /// <summary>
 /// Fetches all pages and returns them grouped in a list.
 /// Some responses (e.g search response) have the pagination nested in a JSON Property.
 /// To workaround this limitation, the mapper is required and needs to point to the correct next pagination.
 /// The default paginator will fetch all available resources without a delay between requests.
 /// This can drain your request limit quite fast, so consider using a custom paginator with delays.
 /// </summary>
 /// <param name="firstPage">A first page, will be included in the output list!</param>
 /// <param name="mapper">A function which maps response objects to the next paging object</param>
 /// <param name="paginator">Optional. If not supplied, DefaultPaginator will be used</param>
 /// <typeparam name="T">The Paging-Type</typeparam>
 /// <typeparam name="TNext">The Response-Type</typeparam>
 /// <returns>A list containing all fetched pages</returns>
 public Task <IList <T> > PaginateAll <T, TNext>(
     IPaginatable <T, TNext> firstPage,
     Func <TNext, IPaginatable <T, TNext> > mapper,
     IPaginator?paginator = null
     )
 {
     return((paginator ?? DefaultPaginator).PaginateAll(firstPage, mapper, _apiConnector));
 }
 /// <summary>
 /// Paginate through pages by using IAsyncEnumerable, introduced in C# 8
 /// Some responses (e.g search response) have the pagination nested in a JSON Property.
 /// To workaround this limitation, the mapper is required and needs to point to the correct next pagination.
 /// The default paginator will fetch all available resources without a delay between requests.
 /// This can drain your request limit quite fast, so consider using a custom paginator with delays.
 /// </summary>
 /// <param name="firstPage">A first page, will be included in the output list!</param>
 /// <param name="mapper">A function which maps response objects to the next paging object</param>
 /// <param name="paginator">Optional. If not supplied, DefaultPaginator will be used</param>
 /// <param name="cancellationToken">An optional Cancellation Token</param>
 /// <typeparam name="T">The Paging-Type</typeparam>
 /// <typeparam name="TNext">The Response-Type</typeparam>
 /// <returns></returns>
 public IAsyncEnumerable <T> Paginate <T, TNext>(
     IPaginatable <T, TNext> firstPage,
     Func <TNext, IPaginatable <T, TNext> > mapper,
     IPaginator?paginator = null,
     CancellationToken cancellationToken = default
     )
 {
     return((paginator ?? DefaultPaginator).Paginate(firstPage, mapper, _apiConnector, cancellationToken));
 }
Esempio n. 9
0
 internal BatchMigration(IPaginatable <T> dataSource,
                         IDataDestination <T> dataDestination,
                         int batchSize,
                         bool failOnSingleWriteError)
 {
     _dataSource             = dataSource;
     _dataDestination        = dataDestination;
     _batchSize              = batchSize;
     _failOnSingleWriteError = failOnSingleWriteError;
 }
Esempio n. 10
0
        public void Print(IPaginatable paginatable, Size pageSize)
        {
            var document = paginatable.ToFixedDocument(pageSize);

            var printServer = new LocalPrintServer();
            var queue       = printServer.DefaultPrintQueue;

            queue.DefaultPrintTicket.PageMediaSize =
                new PageMediaSize(pageSize.Width, pageSize.Height);

            var writer = PrintQueue.CreateXpsDocumentWriter(queue);

            writer.Write(document);
        }
Esempio n. 11
0
        public async Task <IList <T> > PaginateAll <T>(IPaginatable <T> firstPage, IAPIConnector connector)
        {
            var page    = firstPage;
            var results = new List <T>();

            results.AddRange(firstPage.Items);

            while (page.Next != null && await this.ShouldContinue(results, page).ConfigureAwait(false))
            {
                page = await connector.Get <Paging <T> >(new Uri(page.Next, UriKind.Absolute)).ConfigureAwait(false);

                results.AddRange(page.Items);
                Thread.Sleep(this.delay);
            }

            return(results);
        }
Esempio n. 12
0
        public async Task <IList <T> > PaginateAll <T>(IPaginatable <T> firstPage, IAPIConnector connector)
        {
            Ensure.ArgumentNotNull(firstPage, nameof(firstPage));
            Ensure.ArgumentNotNull(connector, nameof(connector));

            var page    = firstPage;
            var results = new List <T>();

            results.AddRange(firstPage.Items);
            while (page.Next != null && await ShouldContinue(results, page).ConfigureAwait(false))
            {
                page = await connector.Get <Paging <T> >(new Uri(page.Next, UriKind.Absolute)).ConfigureAwait(false);

                results.AddRange(page.Items);
            }

            return(results);
        }
Esempio n. 13
0
        /*
         * Wrapper to get Page object from Spotify
         */
        public IList <SimplePlaylist> Page(IPaginatable <SimplePlaylist> page)
        {
            int i = 0;

            do
            {
                try
                {
                    var task = spotify.PaginateAll(page);
                    task.Wait();
                    return(task.Result);
                }
                catch (AggregateException ex)
                {
                    if (ex.InnerException.GetType() == typeof(APIUnauthorizedException))
                    {
                        GuaranteeLogin();
                    }
                    else if (ex.InnerException.GetType() == typeof(APITooManyRequestsException))
                    {
                        Logger.Log("Retrieve paging failed: Too many requests. " + ex.InnerException.Message);
                        i = -1;
                    }
                    else if (ex.InnerException.GetType() == typeof(APIException))
                    {
                        Logger.Log("Retrieve paging failed: " + ((APIException)ex.InnerException).Response?.StatusCode + ex.InnerException.Message);
                        i = -1;
                    }
                    else
                    {
                        Logger.Log("Retrieve paging failed: " + ex.InnerException.Message);
                        i = -1;
                    }
                }
            } while (i == 0);
            return(null);
        }
Esempio n. 14
0
        ToFixedDocument(this IPaginatable paginatable, Size pageSize)
        {
            var document = new FixedDocument();

            foreach (var content in paginatable.Paginate(pageSize))
            {
                var presenter =
                    new ContentPresenter()
                {
                    Content = content,
                    Width   = pageSize.Width,
                    Height  = pageSize.Height,
                };

                var page =
                    new FixedPage()
                {
                    Width  = pageSize.Width,
                    Height = pageSize.Height,
                };
                page.Children.Add(presenter);

                // この3つを行わないと DataGrid がページ全体に展開せず、潰れた状態になる。
                // これらが実際に何をするかは余裕があったら調べたい。
                page.Measure(pageSize);
                page.Arrange(new Rect(new Point(0, 0), pageSize));
                page.UpdateLayout();

                var pageContent = new PageContent()
                {
                    Child = page
                };
                document.Pages.Add(pageContent);
            }

            return(document);
        }
Esempio n. 15
0
 Task <IList <T> > ISpotifyClient.PaginateAll <T, TNext>(IPaginatable <T, TNext> firstPage, Func <TNext, IPaginatable <T, TNext> > mapper, IPaginator paginator)
 {
     throw new NotImplementedException();
 }
Esempio n. 16
0
 Task <IList <T> > ISpotifyClient.PaginateAll <T>(IPaginatable <T> firstPage, IPaginator paginator)
 {
     throw new NotImplementedException();
 }
Esempio n. 17
0
 public Task <TNext> NextPage <T, TNext>(IPaginatable <T, TNext> paginatable)
 {
     throw new NotImplementedException();
 }
Esempio n. 18
0
 /// <summary>
 /// Fetches all pages and returns them grouped in a list.
 /// The default paginator will fetch all available resources without a delay between requests.
 /// This can drain your request limit quite fast, so consider using a custom paginator with delays.
 /// </summary>
 /// <param name="firstPage">The first page, will be included in the output list!</param>
 /// <param name="paginator">Optional. If not supplied, DefaultPaginator will be used</param>
 /// <typeparam name="T">The Paging-Type</typeparam>
 /// <returns>A list containing all fetched pages</returns>
 public Task <IList <T> > PaginateAll <T>(IPaginatable <T> firstPage, IPaginator?paginator = null)
 {
     return((paginator ?? DefaultPaginator).PaginateAll(firstPage, _apiConnector));
 }
 protected virtual Task <bool> ShouldContinue <T, TNext>(List <T> results, IPaginatable <T, TNext> page)
 {
     return(Task.FromResult(true));
 }