From 99ab77b926a5fb3e569e2955eb577e1a2211adfd Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Fri, 19 Jul 2019 15:33:09 +0900 Subject: [PATCH] Add PaginatedWebRequest to handle request pagination --- .../API/Requests/GetUserBeatmapsRequest.cs | 11 +++---- .../GetUserMostPlayedBeatmapsRequest.cs | 9 ++---- .../GetUserRecentActivitiesRequest.cs | 9 ++---- .../API/Requests/GetUserScoresRequest.cs | 10 ++----- .../API/Requests/PaginatedAPIRequest.cs | 30 +++++++++++++++++++ 5 files changed, 43 insertions(+), 26 deletions(-) create mode 100644 osu.Game/Online/API/Requests/PaginatedAPIRequest.cs diff --git a/osu.Game/Online/API/Requests/GetUserBeatmapsRequest.cs b/osu.Game/Online/API/Requests/GetUserBeatmapsRequest.cs index 57005181ca..978d0915fb 100644 --- a/osu.Game/Online/API/Requests/GetUserBeatmapsRequest.cs +++ b/osu.Game/Online/API/Requests/GetUserBeatmapsRequest.cs @@ -7,23 +7,20 @@ using osu.Game.Online.API.Requests.Responses; namespace osu.Game.Online.API.Requests { - public class GetUserBeatmapsRequest : APIRequest> + public class GetUserBeatmapsRequest : PaginatedAPIRequest> { 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 diff --git a/osu.Game/Online/API/Requests/GetUserMostPlayedBeatmapsRequest.cs b/osu.Game/Online/API/Requests/GetUserMostPlayedBeatmapsRequest.cs index fccb27a86c..a363d127d8 100644 --- a/osu.Game/Online/API/Requests/GetUserMostPlayedBeatmapsRequest.cs +++ b/osu.Game/Online/API/Requests/GetUserMostPlayedBeatmapsRequest.cs @@ -6,19 +6,16 @@ using osu.Game.Online.API.Requests.Responses; namespace osu.Game.Online.API.Requests { - public class GetUserMostPlayedBeatmapsRequest : APIRequest> + public class GetUserMostPlayedBeatmapsRequest : PaginatedAPIRequest> { 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"; } } diff --git a/osu.Game/Online/API/Requests/GetUserRecentActivitiesRequest.cs b/osu.Game/Online/API/Requests/GetUserRecentActivitiesRequest.cs index d066636911..5675e877b5 100644 --- a/osu.Game/Online/API/Requests/GetUserRecentActivitiesRequest.cs +++ b/osu.Game/Online/API/Requests/GetUserRecentActivitiesRequest.cs @@ -6,20 +6,17 @@ using osu.Game.Online.API.Requests.Responses; namespace osu.Game.Online.API.Requests { - public class GetUserRecentActivitiesRequest : APIRequest> + public class GetUserRecentActivitiesRequest : PaginatedAPIRequest> { 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 diff --git a/osu.Game/Online/API/Requests/GetUserScoresRequest.cs b/osu.Game/Online/API/Requests/GetUserScoresRequest.cs index 0e2f7ec417..03a11b68ac 100644 --- a/osu.Game/Online/API/Requests/GetUserScoresRequest.cs +++ b/osu.Game/Online/API/Requests/GetUserScoresRequest.cs @@ -6,23 +6,19 @@ using osu.Game.Online.API.Requests.Responses; namespace osu.Game.Online.API.Requests { - public class GetUserScoresRequest : APIRequest> + public class GetUserScoresRequest : PaginatedAPIRequest> { 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 diff --git a/osu.Game/Online/API/Requests/PaginatedAPIRequest.cs b/osu.Game/Online/API/Requests/PaginatedAPIRequest.cs new file mode 100644 index 0000000000..45360612bd --- /dev/null +++ b/osu.Game/Online/API/Requests/PaginatedAPIRequest.cs @@ -0,0 +1,30 @@ +// Copyright (c) ppy Pty Ltd . 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 : APIRequest + { + 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; + } + } +}