summaryrefslogtreecommitdiff
path: root/src/SMAPI.Web/Framework/Clients/ModDrop
diff options
context:
space:
mode:
Diffstat (limited to 'src/SMAPI.Web/Framework/Clients/ModDrop')
-rw-r--r--src/SMAPI.Web/Framework/Clients/ModDrop/IModDropClient.cs17
-rw-r--r--src/SMAPI.Web/Framework/Clients/ModDrop/ModDropClient.cs96
-rw-r--r--src/SMAPI.Web/Framework/Clients/ModDrop/ModDropMod.cs24
-rw-r--r--src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/FileDataModel.cs21
-rw-r--r--src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/ModDataModel.cs15
-rw-r--r--src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/ModListModel.cs11
-rw-r--r--src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/ModModel.cs12
7 files changed, 196 insertions, 0 deletions
diff --git a/src/SMAPI.Web/Framework/Clients/ModDrop/IModDropClient.cs b/src/SMAPI.Web/Framework/Clients/ModDrop/IModDropClient.cs
new file mode 100644
index 00000000..3ede46e2
--- /dev/null
+++ b/src/SMAPI.Web/Framework/Clients/ModDrop/IModDropClient.cs
@@ -0,0 +1,17 @@
+using System;
+using System.Threading.Tasks;
+
+namespace StardewModdingAPI.Web.Framework.Clients.ModDrop
+{
+ /// <summary>An HTTP client for fetching mod metadata from the ModDrop API.</summary>
+ internal interface IModDropClient : IDisposable
+ {
+ /*********
+ ** Methods
+ *********/
+ /// <summary>Get metadata about a mod.</summary>
+ /// <param name="id">The ModDrop mod ID.</param>
+ /// <returns>Returns the mod info if found, else <c>null</c>.</returns>
+ Task<ModDropMod> GetModAsync(long id);
+ }
+}
diff --git a/src/SMAPI.Web/Framework/Clients/ModDrop/ModDropClient.cs b/src/SMAPI.Web/Framework/Clients/ModDrop/ModDropClient.cs
new file mode 100644
index 00000000..19b0b24d
--- /dev/null
+++ b/src/SMAPI.Web/Framework/Clients/ModDrop/ModDropClient.cs
@@ -0,0 +1,96 @@
+using System.Threading.Tasks;
+using Pathoschild.Http.Client;
+using StardewModdingAPI.Toolkit;
+using StardewModdingAPI.Web.Framework.Clients.ModDrop.ResponseModels;
+
+namespace StardewModdingAPI.Web.Framework.Clients.ModDrop
+{
+ /// <summary>An HTTP client for fetching mod metadata from the ModDrop API.</summary>
+ internal class ModDropClient : IModDropClient
+ {
+ /*********
+ ** Properties
+ *********/
+ /// <summary>The underlying HTTP client.</summary>
+ private readonly IClient Client;
+
+ /// <summary>The URL for a ModDrop mod page for the user, where {0} is the mod ID.</summary>
+ private readonly string ModUrlFormat;
+
+
+ /*********
+ ** Public methods
+ *********/
+ /// <summary>Construct an instance.</summary>
+ /// <param name="userAgent">The user agent for the API client.</param>
+ /// <param name="apiUrl">The base URL for the ModDrop API.</param>
+ /// <param name="modUrlFormat">The URL for a ModDrop mod page for the user, where {0} is the mod ID.</param>
+ public ModDropClient(string userAgent, string apiUrl, string modUrlFormat)
+ {
+ this.Client = new FluentClient(apiUrl).SetUserAgent(userAgent);
+ this.ModUrlFormat = modUrlFormat;
+ }
+
+ /// <summary>Get metadata about a mod.</summary>
+ /// <param name="id">The ModDrop mod ID.</param>
+ /// <returns>Returns the mod info if found, else <c>null</c>.</returns>
+ public async Task<ModDropMod> GetModAsync(long id)
+ {
+ // get raw data
+ ModListModel response = await this.Client
+ .PostAsync("")
+ .WithBody(new
+ {
+ ModIDs = new[] { id },
+ Files = true,
+ Mods = true
+ })
+ .As<ModListModel>();
+ ModModel mod = response.Mods[id];
+ if (mod.Mod?.Title == null || mod.Mod.ErrorCode.HasValue)
+ return null;
+
+ // get latest versions
+ ISemanticVersion latest = null;
+ ISemanticVersion optional = null;
+ foreach (FileDataModel file in mod.Files)
+ {
+ if (file.IsOld || file.IsDeleted || file.IsHidden)
+ continue;
+
+ if (!SemanticVersion.TryParse(file.Version, out ISemanticVersion version))
+ continue;
+
+ if (file.IsDefault)
+ {
+ if (latest == null || version.IsNewerThan(latest))
+ latest = version;
+ }
+ else if (optional == null || version.IsNewerThan(optional))
+ optional = version;
+ }
+ if (latest == null)
+ {
+ latest = optional;
+ optional = null;
+ }
+ if (optional != null && latest.IsNewerThan(optional))
+ optional = null;
+
+ // generate result
+ return new ModDropMod
+ {
+ Name = mod.Mod?.Title,
+ LatestDefaultVersion = latest,
+ LatestOptionalVersion = optional,
+ Url = string.Format(this.ModUrlFormat, id)
+ };
+ }
+
+ /// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
+ public void Dispose()
+ {
+ this.Client?.Dispose();
+ }
+ }
+}
diff --git a/src/SMAPI.Web/Framework/Clients/ModDrop/ModDropMod.cs b/src/SMAPI.Web/Framework/Clients/ModDrop/ModDropMod.cs
new file mode 100644
index 00000000..291fb353
--- /dev/null
+++ b/src/SMAPI.Web/Framework/Clients/ModDrop/ModDropMod.cs
@@ -0,0 +1,24 @@
+namespace StardewModdingAPI.Web.Framework.Clients.ModDrop
+{
+ /// <summary>Mod metadata from the ModDrop API.</summary>
+ internal class ModDropMod
+ {
+ /*********
+ ** Accessors
+ *********/
+ /// <summary>The mod name.</summary>
+ public string Name { get; set; }
+
+ /// <summary>The latest default file version.</summary>
+ public ISemanticVersion LatestDefaultVersion { get; set; }
+
+ /// <summary>The latest optional file version.</summary>
+ public ISemanticVersion LatestOptionalVersion { get; set; }
+
+ /// <summary>The mod's web URL.</summary>
+ public string Url { get; set; }
+
+ /// <summary>A user-friendly error which indicates why fetching the mod info failed (if applicable).</summary>
+ public string Error { get; set; }
+ }
+}
diff --git a/src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/FileDataModel.cs b/src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/FileDataModel.cs
new file mode 100644
index 00000000..fa84b287
--- /dev/null
+++ b/src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/FileDataModel.cs
@@ -0,0 +1,21 @@
+namespace StardewModdingAPI.Web.Framework.Clients.ModDrop.ResponseModels
+{
+ /// <summary>Metadata from the ModDrop API about a mod file.</summary>
+ public class FileDataModel
+ {
+ /// <summary>Whether the file is deleted.</summary>
+ public bool IsDeleted { get; set; }
+
+ /// <summary>Whether the file is hidden from users.</summary>
+ public bool IsHidden { get; set; }
+
+ /// <summary>Whether this is the default file for the mod.</summary>
+ public bool IsDefault { get; set; }
+
+ /// <summary>Whether this is an archived file.</summary>
+ public bool IsOld { get; set; }
+
+ /// <summary>The file version.</summary>
+ public string Version { get; set; }
+ }
+}
diff --git a/src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/ModDataModel.cs b/src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/ModDataModel.cs
new file mode 100644
index 00000000..cfdd6a4e
--- /dev/null
+++ b/src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/ModDataModel.cs
@@ -0,0 +1,15 @@
+namespace StardewModdingAPI.Web.Framework.Clients.ModDrop.ResponseModels
+{
+ /// <summary>Metadata about a mod from the ModDrop API.</summary>
+ public class ModDataModel
+ {
+ /// <summary>The mod's unique ID on ModDrop.</summary>
+ public int ID { get; set; }
+
+ /// <summary>The error code, if any.</summary>
+ public int? ErrorCode { get; set; }
+
+ /// <summary>The mod name.</summary>
+ public string Title { get; set; }
+ }
+}
diff --git a/src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/ModListModel.cs b/src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/ModListModel.cs
new file mode 100644
index 00000000..7f692ca1
--- /dev/null
+++ b/src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/ModListModel.cs
@@ -0,0 +1,11 @@
+using System.Collections.Generic;
+
+namespace StardewModdingAPI.Web.Framework.Clients.ModDrop.ResponseModels
+{
+ /// <summary>A list of mods from the ModDrop API.</summary>
+ public class ModListModel
+ {
+ /// <summary>The mod data.</summary>
+ public IDictionary<long, ModModel> Mods { get; set; }
+ }
+}
diff --git a/src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/ModModel.cs b/src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/ModModel.cs
new file mode 100644
index 00000000..9f4b2c6f
--- /dev/null
+++ b/src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/ModModel.cs
@@ -0,0 +1,12 @@
+namespace StardewModdingAPI.Web.Framework.Clients.ModDrop.ResponseModels
+{
+ /// <summary>An entry in a mod list from the ModDrop API.</summary>
+ public class ModModel
+ {
+ /// <summary>The available file downloads.</summary>
+ public FileDataModel[] Files { get; set; }
+
+ /// <summary>The mod metadata.</summary>
+ public ModDataModel Mod { get; set; }
+ }
+}