Skip to content

Commit e2d457e

Browse files
committed
Fix pagination logic
1 parent 1acfa89 commit e2d457e

4 files changed

Lines changed: 28 additions & 19 deletions

File tree

src/Server/Helpers/Http/Client/IHttpClientProxy.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Diagnostics.CodeAnalysis;
22
using System.Net;
3+
using Gommon;
34

45
namespace Ryujinx.Systems.Update.Server.Helpers.Http;
56

@@ -14,10 +15,13 @@ public interface IHttpClientProxy
1415
/// <returns></returns>
1516
/// <remarks>Do no call. Use an overload.</remarks>
1617
protected Task<HttpResponseMessage> SendAsync(string actualCaller, HttpRequestMessage request, HttpCompletionOption? option = null, CancellationToken? token = null);
17-
18+
1819
public Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, HttpCompletionOption? option = null,
1920
CancellationToken? token = null)
2021
=> SendAsync(nameof(SendAsync), request, option, token);
22+
23+
public PaginatedEndpoint<T> Paginate<T>(Func<PaginatedEndpoint<T>.BuilderApi, PaginatedEndpoint<T>.BuilderApi> builder)
24+
=> PaginatedEndpoint<T>.Builder(this).Into(builder);
2125

2226
#region Convenience overloads for SendAsync
2327

src/Server/Helpers/Http/Endpoint/PaginatedEndpoint.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ private PaginatedEndpoint(IHttpClientProxy client,
3434
if (returned.TryGetFirst(predicate, out var matched))
3535
return matched;
3636

37-
if (!response.Headers.GetValues("x-total-pages").ToString().TryParse<int>(out var pageCount) || pageCount > 1)
37+
if ((response.Headers.GetValues("x-total-pages").FirstOrDefault()?.TryParse<int>(out var pageCount) ?? false) && pageCount > 1)
3838
{
3939
currentPage++;
4040
do
@@ -74,7 +74,7 @@ private PaginatedEndpoint(IHttpClientProxy client,
7474
if (returned.Length > 0)
7575
return returned[0];
7676

77-
if (!response.Headers.GetValues("x-total-pages").ToString().TryParse<int>(out var pageCount) || pageCount > 1)
77+
if ((response.Headers.GetValues("x-total-pages").FirstOrDefault()?.TryParse<int>(out var pageCount) ?? false) && pageCount > 1)
7878
{
7979
currentPage++;
8080
do
@@ -112,7 +112,7 @@ private PaginatedEndpoint(IHttpClientProxy client,
112112

113113
IEnumerable<T> accumulated = await _parsePage(response.Content);
114114

115-
if (!response.Headers.GetValues("x-total-pages").ToString().TryParse<int>(out var pageCount) || pageCount > 1)
115+
if ((response.Headers.GetValues("x-total-pages").FirstOrDefault()?.TryParse<int>(out var pageCount) ?? false) && pageCount > 1)
116116
{
117117
currentPage++;
118118
do
@@ -147,8 +147,8 @@ private PaginatedEndpoint(IHttpClientProxy client,
147147
}
148148

149149
IEnumerable<T> accumulated = await _parsePage(response.Content);
150-
151-
if (!response.Headers.GetValues("x-total-pages").ToString().TryParse<int>(out var pageCount) || pageCount > 1)
150+
151+
if ((response.Headers.GetValues("x-total-pages").FirstOrDefault()?.TryParse<int>(out var pageCount) ?? false) && pageCount > 1)
152152
{
153153
currentPage++;
154154
do
Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System.Net.Http.Headers;
2-
using System.Text.Json;
1+
using System.Text.Json;
32
using System.Text.Json.Serialization.Metadata;
43
using NGitLab;
54
using Ryujinx.Systems.Update.Server.Helpers.Http;
@@ -10,7 +9,7 @@ public class GitLabService
109
{
1110
private static readonly GitLabReleaseJsonResponseSerializerContext ReleaseSerializerContext =
1211
new();
13-
12+
1413
private readonly IHttpClientProxy _http;
1514
public GitLabClient Client { get; }
1615

@@ -19,15 +18,16 @@ public class GitLabService
1918
public GitLabService(IConfiguration config, ILogger<GitLabService> logger, DefaultHttpClientProxy httpClient)
2019
{
2120
_logger = logger;
22-
21+
2322
var gitlabSection = config.GetSection("GitLab");
24-
23+
2524
if (!gitlabSection.Exists())
26-
throw new Exception($"The '{gitlabSection.Key}' section does not exist in your appsettings.json. You need to provide an 'Endpoint', 'AccessToken', and optionally 'RefreshIntervalMinutes' values.");
25+
throw new Exception(
26+
$"The '{gitlabSection.Key}' section does not exist in your appsettings.json. You need to provide an 'Endpoint', 'AccessToken', and optionally 'RefreshIntervalMinutes' values.");
2727

2828
var host = gitlabSection.GetValue<string>("Endpoint")!.TrimEnd('/');
2929
var accessToken = gitlabSection.GetValue<string>("AccessToken");
30-
30+
3131
Client = new GitLabClient(host, accessToken);
3232
_http = httpClient;
3333
}
@@ -41,8 +41,8 @@ public GitLabService(IConfiguration config, ILogger<GitLabService> logger, Defau
4141

4242
return JsonSerializer.Deserialize(contentString, typeInfo);
4343
}
44-
45-
public Task<GitLabReleaseJsonResponse?> GetLatestReleaseAsync(long projectId)
44+
45+
public Task<GitLabReleaseJsonResponse?> GetLatestReleaseAsync(long projectId)
4646
=> GetReleaseAsync(projectId, "permalink/latest");
4747

4848
public async Task<GitLabReleaseJsonResponse?> GetReleaseAsync(long projectId, string tagName) =>
@@ -51,9 +51,14 @@ await _http.GetAsync($"api/v4/projects/{projectId}/releases/{tagName}"),
5151
ReleaseSerializerContext.GitLabReleaseJsonResponse
5252
);
5353

54-
public PaginatedEndpoint<GitLabReleaseJsonResponse> GetReleasesAsync(long projectId)
55-
=> PaginatedEndpoint<GitLabReleaseJsonResponse>.Builder(_http)
54+
public PaginatedEndpoint<GitLabReleaseJsonResponse> PageReleases(long projectId)
55+
=> _http.Paginate<GitLabReleaseJsonResponse>(builder => builder
5656
.WithBaseUrl($"api/v4/projects/{projectId}/releases")
57+
.WithPerPageCount(100)
5758
.WithJsonContentParser(ReleaseSerializerContext.IEnumerableGitLabReleaseJsonResponse)
58-
.WithQueryStringParameters(QueryParams.Sort("desc"), QueryParams.OrderBy("created_at"));
59+
.WithQueryStringParameters(
60+
QueryParams.Sort("desc"),
61+
QueryParams.OrderBy("created_at")
62+
)
63+
);
5964
}

src/Server/Services/GitLab/VersionCache.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public async Task RefreshAsync()
128128

129129
var sw = Stopwatch.StartNew();
130130

131-
var releases = await _gl.GetReleasesAsync(_cachedProject.Value.Id)
131+
var releases = await _gl.PageReleases(_cachedProject.Value.Id)
132132
.GetAllAsync(onNonSuccess:
133133
code => _logger.LogError(
134134
"One of the pagination requests to get all releases returned a non-success status code: {code}",

0 commit comments

Comments
 (0)