-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPageCountingResolver.cs
63 lines (55 loc) · 2.17 KB
/
PageCountingResolver.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using System.Linq;
using System.Net.Http;
using Akka.Util;
using Arcane.Framework.Sources.RestApi.Services.PageResolvers.Base;
namespace Arcane.Framework.Sources.RestApi.Services.PageResolvers;
/// <summary>
/// Page offset resolver for numeric page pointers.
/// </summary>
internal sealed class PageCountingResolver : PageResolverBase<int?>
{
private readonly string[] totalPagesPropertyKeyChain;
private int? totalPages;
/// <summary>
/// Page offset resolver for numeric page pointers.
/// </summary>
/// <param name="totalPagesPropertyKeyChain">Optional property key chain for resolver property value like total pages or token value.</param>
public PageCountingResolver(string[] totalPagesPropertyKeyChain)
{
this.totalPagesPropertyKeyChain = totalPagesPropertyKeyChain;
this.totalPages = null;
}
/// <inheritdoc cref="PageResolverBase{TPagePointer}.Next"/>
public override bool Next(Option<HttpResponseMessage> apiResponse)
{
if (!apiResponse.IsEmpty)
{
// read total pages from the first response
this.totalPages ??= this.totalPagesPropertyKeyChain
.Aggregate(this.GetResponse(apiResponse), (je, property) => je.GetProperty(property)).GetInt32();
// check if we are starting to list pages, or are in the process already, or have finished
switch (this.pagePointer)
{
case null:
this.pagePointer = 1;
return true;
case var value when value < this.totalPages:
this.pagePointer += 1;
return true;
default:
this.pagePointer = null;
this.totalPages = null;
return false;
}
}
if (!this.pagePointer.HasValue)
{
this.pagePointer = 1;
return true;
}
// next got called without overfilling the total pages, but empty response was received - maybe page count was incorrect - thus do a full reset
this.pagePointer = null;
this.totalPages = null;
return false;
}
}