nozzlegear / ShopifySharp

ShopifySharp is a .NET library that helps developers easily authenticate with and manage Shopify stores.
https://nozzlegear.com/shopify-development-handbook
MIT License
747 stars 309 forks source link

Product Service ListAsync - Not Paging #1041

Closed charnockandsandlin closed 6 months ago

charnockandsandlin commented 6 months ago

ShopifySharp v6.13.0

Hello, I am attempting to list all of my development store's active product (using a filter), however I keep getting the same first 50 results as if I'm not getting the next page. I noticed the following on the documentation for ListAsync:

/// <summary>
/// Gets a list of up to 250 of the shop's products.
/// </summary>
Task<ListResult<Product>> ListAsync(ListFilter<Product> filter, bool includePresentmentPrices = false, CancellationToken cancellationToken = default);

Will ListAsync only work for the first 250 products? I'll be attempting to pull in 50k+ results. Here is my sample code:


var service = new ProductService(store, access_token);

var filter = new ProductListFilter();
filter.Status = "active";

var products = new List<Product>();

bool hasNextPage = false;
ListResult<Product> result = null;

 do
 {
     try
     {
        result = await service.ListAsync(filter);
        products.AddRange(result.Items);            // Keep getting the same first 50 products over and over
        hasNextPage = result.HasNextPage;

        // Do work
     }
     catch (ShopifyRateLimitException ex)
     {
        // ...
        await Task.Delay((int)(ex.RetryAfterSeconds * 1000));
     }

 } while (result.GetNextPageFilter() != null);  // Also tried while hasNextPage; HasPreviousPage is always false

How can I page to the next result set?

Thank you for your time and for supporting this API!

charnockandsandlin commented 6 months ago

Fixed by doing the following

try
{
    do
    {
        result = await service.ListAsync(result.GetNextPageFilter());

        LogHelper.Log($"Working! Total results: {products.Count}; Elapsed time: {executionTime.Elapsed.TotalSeconds} seconds", LogLevel.Information);
        await Task.Delay(1000);

    } while (result.HasNextPage && (!ENABLE_MAX_RESULTS || products.Count < MAX_RESULTS));
}
catch (ShopifyRateLimitException ex)
{
    LogHelper.Log($"Shopify Timeout! Retry after: {ex.RetryAfterSeconds}", LogLevel.Warning);
    await Task.Delay((int)(ex.RetryAfterSeconds * 1000));
    // Continue iterating
    continue;
}
charnockandsandlin commented 6 months ago

Fixed