45 CancellationToken cancellationToken)
48 throw new ArgumentNullException(nameof(route));
50 var routeFormatter = $
"{route}?page={{0}}";
52 if (paginationSettings !=
null)
55 return new List<TModel>();
57 throw new ArgumentOutOfRangeException(nameof(paginationSettings),
"RetrieveCount cannot be less than 0!");
60 if (paginationSettings.
PageSize.HasValue)
63 pageSize = paginationSettings.
PageSize.Value;
64 routeFormatter += $
"&pageSize={pageSize}";
67 if (paginationSettings.
Offset.HasValue)
69 if (paginationSettings.
Offset.Value < 0)
70 throw new ArgumentOutOfRangeException(nameof(paginationSettings),
"Offset cannot be less than 0!");
72 pageSize ??= paginationSettings.
Offset.Value;
73 currentPage = (paginationSettings.Offset.Value / pageSize.Value) + 1;
77 ValueTask<PaginatedResponse<TModel>> GetPage() => instanceId.HasValue
79 String.Format(CultureInfo.InvariantCulture, routeFormatter, currentPage),
83 String.Format(CultureInfo.InvariantCulture, routeFormatter, currentPage),
86 var firstPage = await GetPage().ConfigureAwait(
false);
88 var totalAvailable = firstPage.TotalPages * firstPage.
PageSize;
89 var maximumItems = paginationSettings?.
RetrieveCount.HasValue ==
true
90 ? Math.Min(paginationSettings.
RetrieveCount!.Value, totalAvailable)
93 var results =
new List<TModel>(maximumItems);
94 var currentResults = firstPage;
99 currentResults = await GetPage().ConfigureAwait(
false);
101 if (currentResults.Content ==
null)
104 IEnumerable<TModel> rangeToAdd = currentResults.Content;
105 if (paginationSettings?.Offset.HasValue ==
true)
107 rangeToAdd = rangeToAdd
108 .Skip(paginationSettings.
Offset!.Value % currentResults.PageSize);
111 var itemsAvailableInPage = rangeToAdd.Count();
112 var itemsStillRequired = maximumItems - results.Count;
113 if (itemsAvailableInPage > itemsStillRequired)
114 rangeToAdd = rangeToAdd
115 .Take(itemsStillRequired);
117 results.AddRange(rangeToAdd);
120 while (results.Count < maximumItems && currentPage <= currentResults.TotalPages);