mirror of
https://github.com/osukey/osukey.git
synced 2025-08-03 14:46:38 +09:00
Introduce Pagination
and simplify paginated API requests
This commit is contained in:
@ -13,8 +13,8 @@ namespace osu.Game.Online.API.Requests
|
||||
|
||||
private readonly BeatmapSetType type;
|
||||
|
||||
public GetUserBeatmapsRequest(long userId, BeatmapSetType type, int page, int itemsPerPage, int initialItems)
|
||||
: base(page, itemsPerPage, initialItems)
|
||||
public GetUserBeatmapsRequest(long userId, BeatmapSetType type, Pagination pagination)
|
||||
: base(pagination)
|
||||
{
|
||||
this.userId = userId;
|
||||
this.type = type;
|
||||
|
@ -10,8 +10,8 @@ namespace osu.Game.Online.API.Requests
|
||||
{
|
||||
private readonly long userId;
|
||||
|
||||
public GetUserKudosuHistoryRequest(long userId, int page, int itemsPerPage, int initialItems)
|
||||
: base(page, itemsPerPage, initialItems)
|
||||
public GetUserKudosuHistoryRequest(long userId, Pagination pagination)
|
||||
: base(pagination)
|
||||
{
|
||||
this.userId = userId;
|
||||
}
|
||||
|
@ -10,8 +10,8 @@ namespace osu.Game.Online.API.Requests
|
||||
{
|
||||
private readonly long userId;
|
||||
|
||||
public GetUserMostPlayedBeatmapsRequest(long userId, int page, int itemsPerPage, int initialItems)
|
||||
: base(page, itemsPerPage, initialItems)
|
||||
public GetUserMostPlayedBeatmapsRequest(long userId, Pagination pagination)
|
||||
: base(pagination)
|
||||
{
|
||||
this.userId = userId;
|
||||
}
|
||||
|
@ -10,8 +10,8 @@ namespace osu.Game.Online.API.Requests
|
||||
{
|
||||
private readonly long userId;
|
||||
|
||||
public GetUserRecentActivitiesRequest(long userId, int page, int itemsPerPage, int initialItems)
|
||||
: base(page, itemsPerPage, initialItems)
|
||||
public GetUserRecentActivitiesRequest(long userId, Pagination pagination)
|
||||
: base(pagination)
|
||||
{
|
||||
this.userId = userId;
|
||||
}
|
||||
|
@ -14,8 +14,8 @@ namespace osu.Game.Online.API.Requests
|
||||
private readonly ScoreType type;
|
||||
private readonly RulesetInfo ruleset;
|
||||
|
||||
public GetUserScoresRequest(long userId, ScoreType type, int page, int itemsPerPage, int initialItems, RulesetInfo ruleset = null)
|
||||
: base(page, itemsPerPage, initialItems)
|
||||
public GetUserScoresRequest(long userId, ScoreType type, Pagination pagination, RulesetInfo ruleset = null)
|
||||
: base(pagination)
|
||||
{
|
||||
this.userId = userId;
|
||||
this.type = type;
|
||||
|
@ -8,28 +8,19 @@ namespace osu.Game.Online.API.Requests
|
||||
{
|
||||
public abstract class PaginatedAPIRequest<T> : APIRequest<T> where T : class
|
||||
{
|
||||
private readonly int page;
|
||||
private readonly int initialItems;
|
||||
private readonly int itemsPerPage;
|
||||
private readonly Pagination pagination;
|
||||
|
||||
protected PaginatedAPIRequest(int page, int itemsPerPage, int initialItems)
|
||||
protected PaginatedAPIRequest(Pagination pagination)
|
||||
{
|
||||
this.page = page;
|
||||
this.initialItems = initialItems;
|
||||
this.itemsPerPage = itemsPerPage;
|
||||
this.pagination = pagination;
|
||||
}
|
||||
|
||||
protected override WebRequest CreateWebRequest()
|
||||
{
|
||||
var req = base.CreateWebRequest();
|
||||
|
||||
if (page == 0)
|
||||
req.AddParameter("limit", initialItems.ToString(CultureInfo.InvariantCulture));
|
||||
else
|
||||
{
|
||||
req.AddParameter("offset", (initialItems + (page - 1) * itemsPerPage).ToString(CultureInfo.InvariantCulture));
|
||||
req.AddParameter("limit", itemsPerPage.ToString(CultureInfo.InvariantCulture));
|
||||
}
|
||||
req.AddParameter("offset", pagination.Offset.ToString(CultureInfo.InvariantCulture));
|
||||
req.AddParameter("limit", pagination.Limit.ToString(CultureInfo.InvariantCulture));
|
||||
|
||||
return req;
|
||||
}
|
||||
|
38
osu.Game/Online/API/Requests/Pagination.cs
Normal file
38
osu.Game/Online/API/Requests/Pagination.cs
Normal file
@ -0,0 +1,38 @@
|
||||
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
||||
// See the LICENCE file in the repository root for full licence text.
|
||||
|
||||
namespace osu.Game.Online.API.Requests
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a pagination data used for <see cref="PaginatedAPIRequest{T}"/>.
|
||||
/// </summary>
|
||||
public readonly struct Pagination
|
||||
{
|
||||
/// <summary>
|
||||
/// The starting point of the request.
|
||||
/// </summary>
|
||||
public int Offset { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The maximum number of items to return in this request.
|
||||
/// </summary>
|
||||
public int Limit { get; }
|
||||
|
||||
public Pagination(int offset, int limit)
|
||||
{
|
||||
Offset = offset;
|
||||
Limit = limit;
|
||||
}
|
||||
|
||||
public Pagination(int limit)
|
||||
: this(0, limit)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a <see cref="Pagination"/> of the next number of items defined by <paramref name="limit"/> after this.
|
||||
/// </summary>
|
||||
/// <param name="limit">The limit of the next pagination.</param>
|
||||
public Pagination TakeNext(int limit) => new Pagination(Offset + Limit, limit);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user