Created
December 8, 2015 20:53
-
-
Save Mozu-CS/628b1befea2c961e7770 to your computer and use it in GitHub Desktop.
A Mozu extension method for ProductResource to retrieve all products from a master catalog
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace MozuExtensions | |
{ | |
public static class MozuExtensions | |
{ | |
/// <summary> | |
/// Retrieves all products from the master catalog using the tenant that was supplied when Mozu.Api.Resources.Commerce.Catalog.Admin.ProductResource was created. | |
/// <para /> | |
/// Accepts an optional pageSize parameter -- pageSize is set to 200 by default. | |
/// </summary> | |
/// <param name="productResource"></param> | |
/// <param name="pageSize"></param> | |
/// <returns> | |
/// A Task that completes to a List with members of type Mozu.Api.Contracts.ProductAdmin.Product | |
/// </returns> | |
public async static Task<List<Mozu.Api.Contracts.ProductAdmin.Product>> GetAllProducts(this Mozu.Api.Resources.Commerce.Catalog.Admin.ProductResource productResource, int pageSize = 200) | |
{ | |
var productCollectionsTaskList = new List<Task<Mozu.Api.Contracts.ProductAdmin.ProductCollection>>(); | |
var productsList = new List<Mozu.Api.Contracts.ProductAdmin.Product>(); | |
var totalProductCount = 0; | |
var startIndex = 0; | |
var productCollection = await productResource.GetProductsAsync(pageSize: pageSize, startIndex: startIndex); | |
totalProductCount = productCollection.TotalCount; | |
startIndex += pageSize; | |
productsList.AddRange(productCollection.Items); | |
while (totalProductCount > startIndex) | |
{ | |
productCollectionsTaskList.Add(productResource.GetProductsAsync(pageSize: pageSize, startIndex: startIndex)); | |
startIndex += pageSize; | |
} | |
while(productCollectionsTaskList.Count > 0) | |
{ | |
var finishedTask = await Task.WhenAny(productCollectionsTaskList); | |
productCollectionsTaskList.Remove(finishedTask); | |
productsList.AddRange((await finishedTask).Items); | |
} | |
return productsList; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment