Add PaginatedWebRequest to handle request pagination

This commit is contained in:
smoogipoo 2019-07-19 15:33:09 +09:00
parent 9458bca58f
commit 99ab77b926
5 changed files with 43 additions and 26 deletions

View File

@ -7,23 +7,20 @@ using osu.Game.Online.API.Requests.Responses;
namespace osu.Game.Online.API.Requests
{
public class GetUserBeatmapsRequest : APIRequest<List<APIBeatmapSet>>
public class GetUserBeatmapsRequest : PaginatedAPIRequest<List<APIBeatmapSet>>
{
private readonly long userId;
private readonly int offset;
private readonly int limit;
private readonly BeatmapSetType type;
public GetUserBeatmapsRequest(long userId, BeatmapSetType type, int offset = 0, int limit = 6)
: base(offset, limit)
{
this.userId = userId;
this.offset = offset;
this.limit = limit;
this.type = type;
}
// ReSharper disable once ImpureMethodCallOnReadonlyValueField
protected override string Target => $@"users/{userId}/beatmapsets/{type.ToString().Underscore()}?offset={offset}&limit={limit}";
protected override string Target => $@"users/{userId}/beatmapsets/{type.ToString().Underscore()}";
}
public enum BeatmapSetType

View File

@ -6,19 +6,16 @@ using osu.Game.Online.API.Requests.Responses;
namespace osu.Game.Online.API.Requests
{
public class GetUserMostPlayedBeatmapsRequest : APIRequest<List<APIUserMostPlayedBeatmap>>
public class GetUserMostPlayedBeatmapsRequest : PaginatedAPIRequest<List<APIUserMostPlayedBeatmap>>
{
private readonly long userId;
private readonly int offset;
private readonly int limit;
public GetUserMostPlayedBeatmapsRequest(long userId, int offset = 0, int limit = 5)
: base(offset, limit)
{
this.userId = userId;
this.offset = offset;
this.limit = limit;
}
protected override string Target => $@"users/{userId}/beatmapsets/most_played?offset={offset}&limit={limit}";
protected override string Target => $@"users/{userId}/beatmapsets/most_played";
}
}

View File

@ -6,20 +6,17 @@ using osu.Game.Online.API.Requests.Responses;
namespace osu.Game.Online.API.Requests
{
public class GetUserRecentActivitiesRequest : APIRequest<List<APIRecentActivity>>
public class GetUserRecentActivitiesRequest : PaginatedAPIRequest<List<APIRecentActivity>>
{
private readonly long userId;
private readonly int offset;
private readonly int limit;
public GetUserRecentActivitiesRequest(long userId, int offset = 0, int limit = 5)
: base(offset, limit)
{
this.userId = userId;
this.offset = offset;
this.limit = limit;
}
protected override string Target => $"users/{userId}/recent_activity?offset={offset}&limit={limit}";
protected override string Target => $"users/{userId}/recent_activity";
}
public enum RecentActivityType

View File

@ -6,23 +6,19 @@ using osu.Game.Online.API.Requests.Responses;
namespace osu.Game.Online.API.Requests
{
public class GetUserScoresRequest : APIRequest<List<APILegacyScoreInfo>>
public class GetUserScoresRequest : PaginatedAPIRequest<List<APILegacyScoreInfo>>
{
private readonly long userId;
private readonly ScoreType type;
private readonly int offset;
private readonly int limit;
public GetUserScoresRequest(long userId, ScoreType type, int offset = 0, int limit = 5)
: base(offset, limit)
{
this.userId = userId;
this.type = type;
this.offset = offset;
this.limit = limit;
}
// ReSharper disable once ImpureMethodCallOnReadonlyValueField
protected override string Target => $@"users/{userId}/scores/{type.ToString().ToLowerInvariant()}?offset={offset}&limit={limit}";
protected override string Target => $@"users/{userId}/scores/{type.ToString().ToLowerInvariant()}";
}
public enum ScoreType

View File

@ -0,0 +1,30 @@
// 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.
using System.Globalization;
using osu.Framework.IO.Network;
namespace osu.Game.Online.API.Requests
{
public abstract class PaginatedAPIRequest<T> : APIRequest<T>
{
private readonly int offset;
private readonly int limit;
protected PaginatedAPIRequest(int offset, int limit)
{
this.offset = offset;
this.limit = limit;
}
protected override WebRequest CreateWebRequest()
{
var req = base.CreateWebRequest();
req.AddParameter("offset", offset.ToString(CultureInfo.InvariantCulture));
req.AddParameter("limit", limit.ToString(CultureInfo.InvariantCulture));
return req;
}
}
}