diff options
author | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2022-05-18 19:23:26 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2022-05-18 19:23:26 -0400 |
commit | 3a161a30a7faa2d69ebe08938cd68f43921b4a81 (patch) | |
tree | 35e25eb7ca0132cd42421fc282b1633073fa8dc9 /src/SMAPI.Web/Framework | |
parent | 5731b015a0c548ac72e0d7ce9c4153aa52da3562 (diff) | |
download | SMAPI-3a161a30a7faa2d69ebe08938cd68f43921b4a81.tar.gz SMAPI-3a161a30a7faa2d69ebe08938cd68f43921b4a81.tar.bz2 SMAPI-3a161a30a7faa2d69ebe08938cd68f43921b4a81.zip |
update for the new CurseForge API
Diffstat (limited to 'src/SMAPI.Web/Framework')
5 files changed, 42 insertions, 42 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 |