update for the new CurseForge API

This commit is contained in:
Jesse Plamondon-Willard 2022-05-18 19:23:26 -04:00
parent 5731b015a0
commit 3a161a30a7
No known key found for this signature in database
GPG Key ID: CF8B1456B3E29F49
8 changed files with 50 additions and 44 deletions

View File

@ -1,6 +1,10 @@
← [README](README.md)
# Release notes
## Upcoming release
* For players:
* Fixed CurseForge update checks for the new CurseForge API.
## 3.14.4
Released 15 May 2022 for Stardew Valley 1.5.6 or later.

View File

@ -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>

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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

View File

@ -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(

View File

@ -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",