Skip to content

Commit 2870405

Browse files
authored
Merge pull request #58 from Zechiax/update_restclient
Update restclient
2 parents 8618147 + c15ee17 commit 2870405

File tree

4 files changed

+43
-27
lines changed

4 files changed

+43
-27
lines changed

Asterion/Asterion.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
3030
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="7.0.0" />
3131
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="7.0.0" />
32-
<PackageReference Include="Modrinth.RestClient" Version="2.5.6" />
32+
<PackageReference Include="Modrinth.RestClient" Version="3.0.0-rc.1" />
3333
<PackageReference Include="Serilog" Version="2.12.0" />
3434
<PackageReference Include="Serilog.AspNetCore" Version="6.1.0" />
3535
<PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />

Asterion/Services/MessageHandler.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
using Discord.WebSocket;
77
using Microsoft.Extensions.DependencyInjection;
88
using Microsoft.Extensions.Logging;
9-
using Polly;
10-
using Asterion.Modules;
119

1210
namespace Asterion.Services;
1311

Asterion/Services/Modrinth/ModrinthService.Api.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public partial class ModrinthService
2626
Project? p;
2727
try
2828
{
29-
p = await _api.GetProjectAsync(slugOrId);
29+
p = await _api.Project.GetAsync(slugOrId);
3030
}
3131
catch (Exception e)
3232
{
@@ -44,7 +44,7 @@ public partial class ModrinthService
4444
{
4545
try
4646
{
47-
var searchResponse = await _api.GetProjectVersionListAsync(slugOrId);
47+
var searchResponse = await _api.Version.GetProjectVersionListAsync(slugOrId);
4848
return searchResponse;
4949
}
5050
catch (Exception e)
@@ -59,7 +59,7 @@ public partial class ModrinthService
5959
{
6060
try
6161
{
62-
var searchResponse = await _api.GetMultipleVersionsAsync(versions);
62+
var searchResponse = await _api.Version.GetMultipleAsync(versions);
6363
return searchResponse;
6464
}
6565
catch (Exception e)
@@ -83,7 +83,7 @@ public partial class ModrinthService
8383
try
8484
{
8585
_logger.LogDebug("Team members for project ID {ProjectId} are not in cache", projectId);
86-
var team = await _api.GetProjectTeamMembersByProjectAsync(projectId);
86+
var team = await _api.Team.GetProjectTeamAsync(projectId);
8787

8888
_cache.Set($"project-team-members:{projectId}", team, absoluteExpirationRelativeToNow: TimeSpan.FromMinutes(30));
8989
_logger.LogDebug("Saving team members for project ID {ProjectId} to cache", projectId);
@@ -102,7 +102,7 @@ public partial class ModrinthService
102102
{
103103
try
104104
{
105-
var searchResponse = await _api.SearchProjectsAsync(query);
105+
var searchResponse = await _api.Project.SearchAsync(query);
106106
return searchResponse;
107107
}
108108
catch (Exception e)
@@ -117,7 +117,7 @@ public partial class ModrinthService
117117
{
118118
try
119119
{
120-
var searchResponse = await _api.GetMultipleProjectsAsync(projectIds);
120+
var searchResponse = await _api.Project.GetMultipleAsync(projectIds);
121121
return searchResponse;
122122
}
123123
catch (Exception e)
@@ -153,7 +153,7 @@ public partial class ModrinthService
153153
try
154154
{
155155
_logger.LogDebug("Game versions not in cache, fetching from api...");
156-
var gameVersions = await _api.GetGameVersionsAsync();
156+
var gameVersions = await _api.Tag.GetGameVersionsAsync();
157157

158158
_cache.Set("gameVersions", gameVersions, absoluteExpirationRelativeToNow: TimeSpan.FromHours(12));
159159

Asterion/Services/Modrinth/ModrinthService.cs

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
using Microsoft.Extensions.Logging;
1313
using Modrinth.RestClient;
1414
using Modrinth.RestClient.Models;
15-
using RestEase;
1615
using Asterion.Extensions;
16+
using Flurl.Http;
1717
using Version = Modrinth.RestClient.Models.Version;
1818
using Timer = System.Timers.Timer;
1919

@@ -22,7 +22,7 @@ namespace Asterion.Services.Modrinth;
2222
public partial class ModrinthService
2323
{
2424
private readonly BackgroundWorker _updateWorker;
25-
private readonly IModrinthApi _api;
25+
private readonly IModrinthClient _api;
2626
private readonly ILogger _logger;
2727
private readonly IMemoryCache _cache;
2828
private readonly MemoryCacheEntryOptions _cacheEntryOptions;
@@ -33,7 +33,7 @@ public partial class ModrinthService
3333
public ModrinthService(IServiceProvider serviceProvider, IHttpClientFactory httpClientFactory)
3434
{
3535
_httpClientFactory = httpClientFactory;
36-
_api = ModrinthApi.NewClient(userAgent: "Asterion");
36+
_api = new ModrinthClient(userAgent: "Zechiax/Asterion");
3737
_logger = serviceProvider.GetRequiredService<ILogger<ModrinthService>>();
3838
_cache = serviceProvider.GetRequiredService<IMemoryCache>();
3939
_dataService = serviceProvider.GetRequiredService<IDataService>();
@@ -87,17 +87,35 @@ private async void CheckUpdates(object? sender, DoWorkEventArgs e)
8787

8888
var versions = apiProjects.SelectMany(p => p.Versions).ToArray();
8989

90+
91+
const int splitBy = 500;
9092
_logger.LogDebug("Getting multiple versions ({Count}) from Modrinth", versions.Length);
9193

92-
var apiVersions = await GetMultipleVersionsAsync(versions);
93-
94-
if (apiVersions is null)
94+
// Make multiple requests to get all versions - we don't want to get 1500+ versions in one request
95+
// We make sure to split the requests into chunks of 500 versions
96+
var apiVersions = new List<Version>();
97+
// Split the array into chunks of 500, we use ArraySegment
98+
var versionChunks = System.Array.Empty<ArraySegment<string>>();
99+
100+
for (var i = 0; i < versions.Length; i += splitBy)
95101
{
96-
_logger.LogWarning("Could not get information from API, update search interrupted");
97-
return;
102+
_logger.LogDebug("Appending versions {Start} to {End}", i, Math.Min(splitBy, versions.Length - i));
103+
versionChunks = versionChunks.Append(new ArraySegment<string>(versions, i, Math.Min(splitBy, versions.Length - i))).ToArray();
104+
}
105+
106+
foreach (var chunk in versionChunks)
107+
{
108+
_logger.LogDebug("Getting versions {Start} to {End}", chunk.Offset, chunk.Offset + chunk.Count);
109+
var versionsChunk = await GetMultipleVersionsAsync(chunk);
110+
if (versionsChunk is null)
111+
{
112+
_logger.LogWarning("Could not get information from API, update search interrupted");
113+
return;
114+
}
115+
apiVersions.AddRange(versionsChunk);
98116
}
99117

100-
_logger.LogDebug("Got {Count} versions", apiVersions.Length);
118+
_logger.LogDebug("Got {Count} versions", apiVersions.Count);
101119

102120
foreach (var project in apiProjects)
103121
{
@@ -270,14 +288,14 @@ public async Task<SearchResult<ProjectDto>> FindProject(string query)
270288
var projectFoundById = false;
271289
try
272290
{
273-
project = await _api.GetProjectAsync(query);
291+
project = await _api.Project.GetAsync(query);
274292

275-
searchResponse = await _api.SearchProjectsAsync(query);
293+
searchResponse = await _api.Project.SearchAsync(query);
276294
// Won't be set if exception is thrown
277295
projectFoundById = true;
278296
}
279297
// Not found status code is returned when requested project was not found
280-
catch (ApiException e) when (e.StatusCode == HttpStatusCode.NotFound)
298+
catch (FlurlHttpException e) when (e.Call.Response.ResponseMessage.StatusCode == HttpStatusCode.NotFound)
281299
{
282300
// Project not found by slug or id
283301
_logger.LogDebug("Project query '{Query}' not found with ID or slug", query);
@@ -304,7 +322,7 @@ public async Task<SearchResult<ProjectDto>> FindProject(string query)
304322

305323
try
306324
{
307-
searchResponse = await _api.SearchProjectsAsync(query);
325+
searchResponse = await _api.Project.SearchAsync(query);
308326

309327
// No search results
310328
if (searchResponse.TotalHits <= 0)
@@ -313,7 +331,7 @@ public async Task<SearchResult<ProjectDto>> FindProject(string query)
313331
}
314332

315333
// Return first result
316-
project = await _api.GetProjectAsync(searchResponse.Hits[0].ProjectId);
334+
project = await _api.Project.GetAsync(searchResponse.Hits[0].ProjectId);
317335

318336
var result = new SearchResult<ProjectDto>(new ProjectDto
319337
{
@@ -354,10 +372,10 @@ public async Task<SearchResult<UserDto>> FindUser(string query)
354372

355373
try
356374
{
357-
user = await _api.GetUserAsync(query);
375+
user = await _api.User.GetAsync(query);
358376
_logger.LogDebug("User query '{Query}' found", query);
359377
}
360-
catch (ApiException e) when (e.StatusCode == HttpStatusCode.NotFound)
378+
catch (FlurlHttpException e) when (e.Call.Response.ResponseMessage.StatusCode == HttpStatusCode.NotFound)
361379
{
362380
// Project not found by slug or id
363381
_logger.LogDebug("User not found '{Query}'", query);
@@ -371,7 +389,7 @@ public async Task<SearchResult<UserDto>> FindUser(string query)
371389
// User can't be null from here
372390
try
373391
{
374-
var projects = await _api.GetUsersProjectsByUserIdAsync(user.Id);
392+
var projects = await _api.User.GetProjectsAsync(user.Id);
375393

376394
var searchResult = new SearchResult<UserDto>(new UserDto
377395
{

0 commit comments

Comments
 (0)