1
- using Newtonsoft . Json ;
1
+ using Biyori . Lib . Util ;
2
+ using Newtonsoft . Json ;
3
+ using RestSharp ;
4
+ using RestSharp . Extensions ;
2
5
using System ;
3
6
using System . Collections . Generic ;
4
7
using System . Linq ;
@@ -11,10 +14,65 @@ public class KitsuPaginationModel<T> where T : KitsuDataModel
11
14
{
12
15
[ JsonProperty ( "meta" ) ]
13
16
public KitsuMetaModel Meta { get ; set ; }
17
+ [ JsonIgnore ]
18
+ public int Count { get => this . Meta ? . Count ?? 0 ; }
19
+ [ JsonIgnore ]
20
+ private int Limit { get => ( new RestRequest ( this . Links . NextPage . UrlDecode ( ) ) . Parameters . FirstOrDefault ( x => x . Name == "page[limit]" ) ? . Value . ToType < int > ( ) ?? 0 ) ; }
21
+ [ JsonIgnore ]
22
+ private int Offset { get => ( new RestRequest ( this . Links . NextPage . UrlDecode ( ) ) . Parameters . FirstOrDefault ( x => x . Name == "page[offset]" ) ? . Value . ToType < int > ( ) ?? 0 ) ; }
23
+ [ JsonIgnore ]
24
+ public int Pages { get => Links != null ? Count / Limit : 0 ; }
25
+ [ JsonIgnore ]
26
+ public int CurrentPage { get => Pages == 0 ? 1 : Offset / Limit ; }
14
27
[ JsonProperty ( "data" ) ]
15
28
public IEnumerable < T > Data { get ; set ; } = new List < T > ( ) ;
16
29
[ JsonProperty ( "links" ) ]
17
30
public KitsuLinkModel Links { get ; set ; }
31
+
32
+ public async Task < KitsuPaginationModel < T > > NextPage ( )
33
+ {
34
+ if ( this . Links ? . NextPage == null )
35
+ return null ;
36
+
37
+ var client = this . getClient ( ) ;
38
+ var rr = new RestRequest ( new Uri ( this . Links . NextPage , UriKind . Absolute ) ) ;
39
+ return ( await client . ExecuteTaskAsync < KitsuPaginationModel < T > > ( rr ) ) ? . Data ;
40
+ }
41
+ public async Task < KitsuPaginationModel < T > > PreviousPage ( )
42
+ {
43
+ if ( this . Links ? . PreviousPage == null )
44
+ return null ;
45
+
46
+ var client = this . getClient ( ) ;
47
+ var rr = new RestRequest ( new Uri ( this . Links . PreviousPage , UriKind . Absolute ) ) ;
48
+ return ( await client . ExecuteTaskAsync < KitsuPaginationModel < T > > ( rr ) ) ? . Data ;
49
+ }
50
+ public async Task < KitsuPaginationModel < T > > FirstPage ( )
51
+ {
52
+ if ( this . Links ? . FirstPage == null )
53
+ return null ;
54
+
55
+ var client = this . getClient ( ) ;
56
+ var rr = new RestRequest ( new Uri ( this . Links . FirstPage , UriKind . Absolute ) ) ;
57
+ return ( await client . ExecuteTaskAsync < KitsuPaginationModel < T > > ( rr ) ) ? . Data ;
58
+ }
59
+ public async Task < KitsuPaginationModel < T > > LastPage ( )
60
+ {
61
+ if ( this . Links ? . LastPage == null )
62
+ return null ;
63
+
64
+ var client = this . getClient ( ) ;
65
+ var rr = new RestRequest ( new Uri ( this . Links . LastPage , UriKind . Absolute ) ) ;
66
+ return ( await client . ExecuteTaskAsync < KitsuPaginationModel < T > > ( rr ) ) ? . Data ;
67
+ }
68
+ private RestClient getClient ( )
69
+ {
70
+ var client = new RestClient ( Kitsu . BaseURL ) ;
71
+ client
72
+ . AddDefaultHeader ( "Accept" , "application/vnd.api+json" )
73
+ . AddDefaultHeader ( "Content-Type" , "application/vnd.api+json" ) ;
74
+ return client ;
75
+ }
18
76
}
19
77
public class KitsuDataModel { }
20
78
public class KitsuMetaModel
0 commit comments