summaryrefslogtreecommitdiff
path: root/src/SMAPI.Web
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2022-05-18 19:23:26 -0400
committerJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2022-05-18 19:23:26 -0400
commit3a161a30a7faa2d69ebe08938cd68f43921b4a81 (patch)
tree35e25eb7ca0132cd42421fc282b1633073fa8dc9 /src/SMAPI.Web
parent5731b015a0c548ac72e0d7ce9c4153aa52da3562 (diff)
downloadSMAPI-3a161a30a7faa2d69ebe08938cd68f43921b4a81.tar.gz
SMAPI-3a161a30a7faa2d69ebe08938cd68f43921b4a81.tar.bz2
SMAPI-3a161a30a7faa2d69ebe08938cd68f43921b4a81.zip
update for the new CurseForge API
Diffstat (limited to 'src/SMAPI.Web')
-rw-r--r--src/SMAPI.Web/Framework/Clients/CurseForge/CurseForgeClient.cs25
-rw-r--r--src/SMAPI.Web/Framework/Clients/CurseForge/ResponseModels/ModLinksModel.cs7
-rw-r--r--src/SMAPI.Web/Framework/Clients/CurseForge/ResponseModels/ModModel.cs41
-rw-r--r--src/SMAPI.Web/Framework/Clients/CurseForge/ResponseModels/ResponseModel.cs8
-rw-r--r--src/SMAPI.Web/Framework/ConfigModels/ApiClientsConfig.cs3
-rw-r--r--src/SMAPI.Web/Startup.cs3
-rw-r--r--src/SMAPI.Web/appsettings.json3
7 files changed, 46 insertions, 44 deletions
diff --git a/src/SMAPI.Web/Framework/Clients/CurseForge/CurseForgeClient.cs b/src/SMAPI.Web/Framework/Clients/CurseForge/CurseForgeClient.cs
index d351b42d..9b4f2580 100644
--- a/src/SMAPI.Web/Framework/Clients/CurseForge/CurseForgeClient.cs
+++ b/src/SMAPI.Web/Framework/Clients/CurseForge/CurseForgeClient.cs
@@ -1,4 +1,5 @@
using System.Collections.Generic;
+using System.Net;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Pathoschild.Http.Client;
@@ -33,9 +34,12 @@ namespace StardewModdingAPI.Web.Framework.Clients.CurseForge
/// <summary>Construct an instance.</summary>
/// <param name="userAgent">The user agent for the API client.</param>
/// <param name="apiUrl">The base URL for the CurseForge API.</param>
- public CurseForgeClient(string userAgent, string apiUrl)
+ /// <param name="apiKey">The API authentication key.</param>
+ public CurseForgeClient(string userAgent, string apiUrl, string apiKey)
{
- this.Client = new FluentClient(apiUrl).SetUserAgent(userAgent);
+ this.Client = new FluentClient(apiUrl)
+ .SetUserAgent(userAgent)
+ .AddDefault(request => request.WithHeader("x-api-key", apiKey));
}
/// <summary>Get update check info about a mod.</summary>
@@ -49,11 +53,18 @@ namespace StardewModdingAPI.Web.Framework.Clients.CurseForge
return page.SetError(RemoteModStatus.DoesNotExist, $"The value '{id}' isn't a valid CurseForge mod ID, must be an integer ID.");
// get raw data
- ModModel? mod = await this.Client
- .GetAsync($"addon/{parsedId}")
- .As<ModModel?>();
- if (mod == null)
+ ModModel? mod;
+ try
+ {
+ ResponseModel<ModModel> response = await this.Client
+ .GetAsync($"mods/{parsedId}")
+ .As<ResponseModel<ModModel>>();
+ mod = response.Data;
+ }
+ catch (ApiException ex) when (ex.Status == HttpStatusCode.NotFound)
+ {
return page.SetError(RemoteModStatus.DoesNotExist, "Found no CurseForge mod with this ID.");
+ }
// get downloads
List<IModDownload> downloads = new List<IModDownload>();
@@ -65,7 +76,7 @@ namespace StardewModdingAPI.Web.Framework.Clients.CurseForge
}
// return info
- return page.SetInfo(name: mod.Name, version: null, url: mod.WebsiteUrl, downloads: downloads);
+ return page.SetInfo(name: mod.Name, version: null, url: mod.Links.WebsiteUrl, downloads: downloads);
}
/// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
diff --git a/src/SMAPI.Web/Framework/Clients/CurseForge/ResponseModels/ModLinksModel.cs b/src/SMAPI.Web/Framework/Clients/CurseForge/ResponseModels/ModLinksModel.cs
new file mode 100644
index 00000000..2f9abe4f
--- /dev/null
+++ b/src/SMAPI.Web/Framework/Clients/CurseForge/ResponseModels/ModLinksModel.cs
@@ -0,0 +1,7 @@
+namespace StardewModdingAPI.Web.Framework.Clients.CurseForge.ResponseModels
+{
+ /// <summary>A list of links for a mod.</summary>
+ /// <param name="WebsiteUrl">The URL for the CurseForge mod page.</param>
+ /// <param name="SourceUrl">The URL for the mod's source code, if any.</param>
+ public record ModLinksModel(string WebsiteUrl, string? SourceUrl);
+}
diff --git a/src/SMAPI.Web/Framework/Clients/CurseForge/ResponseModels/ModModel.cs b/src/SMAPI.Web/Framework/Clients/CurseForge/ResponseModels/ModModel.cs
index fd7796f2..7018be54 100644
--- a/src/SMAPI.Web/Framework/Clients/CurseForge/ResponseModels/ModModel.cs
+++ b/src/SMAPI.Web/Framework/Clients/CurseForge/ResponseModels/ModModel.cs
@@ -1,38 +1,9 @@
namespace StardewModdingAPI.Web.Framework.Clients.CurseForge.ResponseModels
{
- /// <summary>An mod from the CurseForge API.</summary>
- public class ModModel
- {
- /*********
- ** Accessors
- *********/
- /// <summary>The mod's unique ID on CurseForge.</summary>
- public int ID { get; }
-
- /// <summary>The mod name.</summary>
- public string Name { get; }
-
- /// <summary>The web URL for the mod page.</summary>
- public string WebsiteUrl { get; }
-
- /// <summary>The available file downloads.</summary>
- public ModFileModel[] LatestFiles { get; }
-
-
- /*********
- ** Public methods
- *********/
- /// <summary>Construct an instance.</summary>
- /// <param name="id">The mod's unique ID on CurseForge.</param>
- /// <param name="name">The mod name.</param>
- /// <param name="websiteUrl">The web URL for the mod page.</param>
- /// <param name="latestFiles">The available file downloads.</param>
- public ModModel(int id, string name, string websiteUrl, ModFileModel[] latestFiles)
- {
- this.ID = id;
- this.Name = name;
- this.WebsiteUrl = websiteUrl;
- this.LatestFiles = latestFiles;
- }
- }
+ /// <summary>A mod from the CurseForge API.</summary>
+ /// <param name="Id">The mod's unique ID on CurseForge.</param>
+ /// <param name="Name">The mod name.</param>
+ /// <param name="LatestFiles">The available file downloads.</param>
+ /// <param name="Links">The URLs for this mod.</param>
+ public record ModModel(int Id, string Name, ModFileModel[] LatestFiles, ModLinksModel Links);
}
diff --git a/src/SMAPI.Web/Framework/Clients/CurseForge/ResponseModels/ResponseModel.cs b/src/SMAPI.Web/Framework/Clients/CurseForge/ResponseModels/ResponseModel.cs
new file mode 100644
index 00000000..4d538a93
--- /dev/null
+++ b/src/SMAPI.Web/Framework/Clients/CurseForge/ResponseModels/ResponseModel.cs
@@ -0,0 +1,8 @@
+using Newtonsoft.Json;
+
+namespace StardewModdingAPI.Web.Framework.Clients.CurseForge.ResponseModels
+{
+ /// <summary>A response from the CurseForge API.</summary>
+ /// <param name="Data">The data returned by the API.</param>
+ public record ResponseModel<TData>(TData Data);
+}
diff --git a/src/SMAPI.Web/Framework/ConfigModels/ApiClientsConfig.cs b/src/SMAPI.Web/Framework/ConfigModels/ApiClientsConfig.cs
index b582b2b0..ebb3618a 100644
--- a/src/SMAPI.Web/Framework/ConfigModels/ApiClientsConfig.cs
+++ b/src/SMAPI.Web/Framework/ConfigModels/ApiClientsConfig.cs
@@ -42,6 +42,9 @@ namespace StardewModdingAPI.Web.Framework.ConfigModels
/// <summary>The base URL for the CurseForge API.</summary>
public string CurseForgeBaseUrl { get; set; } = null!;
+ /// <summary>The API authentication key for the CurseForge API.</summary>
+ public string CurseForgeApiKey { get; set; } = null!;
+
/****
** GitHub
diff --git a/src/SMAPI.Web/Startup.cs b/src/SMAPI.Web/Startup.cs
index 2693aa90..9980d00c 100644
--- a/src/SMAPI.Web/Startup.cs
+++ b/src/SMAPI.Web/Startup.cs
@@ -111,7 +111,8 @@ namespace StardewModdingAPI.Web
services.AddSingleton<ICurseForgeClient>(new CurseForgeClient(
userAgent: userAgent,
- apiUrl: api.CurseForgeBaseUrl
+ apiUrl: api.CurseForgeBaseUrl,
+ apiKey: api.CurseForgeApiKey
));
services.AddSingleton<IGitHubClient>(new GitHubClient(
diff --git a/src/SMAPI.Web/appsettings.json b/src/SMAPI.Web/appsettings.json
index 1231f824..10fcbe9c 100644
--- a/src/SMAPI.Web/appsettings.json
+++ b/src/SMAPI.Web/appsettings.json
@@ -31,7 +31,8 @@
"ChucklefishBaseUrl": "https://community.playstarbound.com",
"ChucklefishModPageUrlFormat": "resources/{0}",
- "CurseForgeBaseUrl": "https://addons-ecs.forgesvc.net/api/v2/",
+ "CurseForgeBaseUrl": "https://api.curseforge.com/v1/",
+ "CurseForgeApiKey": null,
"GitHubBaseUrl": "https://api.github.com",
"GitHubAcceptHeader": "application/vnd.github.v3+json",