Skip to content

Commit 3135ec6

Browse files
committed
✨ dynamic model for KitsuPagination
1 parent cb9e580 commit 3135ec6

File tree

3 files changed

+73
-3
lines changed

3 files changed

+73
-3
lines changed

Biyori/API/Kitsu/Kitsu.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace Biyori.API.Kitsu
1010

1111
public class Kitsu
1212
{
13-
private const string BaseURL = "https://kitsu.io/api/edge";
13+
public static readonly string BaseURL = "https://kitsu.io/api/edge";
1414
private RestClient getClient()
1515
{
1616
var client = new RestClient(BaseURL);
@@ -31,7 +31,6 @@ public Kitsu()
3131
/// <param name="Password">Password or API Key</param>
3232
public void InitializeUserClient(string loginUser, string Password)
3333
{
34-
3534
}
3635
public async Task<KitsuPaginationModel<KitsuDataModel>> GetAnimeByBulkId(params int[] animeId)
3736
{

Biyori/API/Kitsu/KitsuPaginationModel.cs

+59-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
using Newtonsoft.Json;
1+
using Biyori.Lib.Util;
2+
using Newtonsoft.Json;
3+
using RestSharp;
4+
using RestSharp.Extensions;
25
using System;
36
using System.Collections.Generic;
47
using System.Linq;
@@ -11,10 +14,65 @@ public class KitsuPaginationModel<T> where T : KitsuDataModel
1114
{
1215
[JsonProperty("meta")]
1316
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; }
1427
[JsonProperty("data")]
1528
public IEnumerable<T> Data { get; set; } = new List<T>();
1629
[JsonProperty("links")]
1730
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+
}
1876
}
1977
public class KitsuDataModel { }
2078
public class KitsuMetaModel

Biyori/Lib/Util/Object.Extensions.cs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Biyori.Lib.Util
8+
{
9+
public static class ObjectExtensions
10+
{
11+
public static T ToType<T>(this object obj) => (T)Convert.ChangeType(obj, typeof(T));
12+
}
13+
}

0 commit comments

Comments
 (0)