summaryrefslogtreecommitdiff
path: root/src/SMAPI.Web/Framework
diff options
context:
space:
mode:
Diffstat (limited to 'src/SMAPI.Web/Framework')
-rw-r--r--src/SMAPI.Web/Framework/Caching/Mods/CachedMod.cs12
-rw-r--r--src/SMAPI.Web/Framework/Clients/GitHub/GitHubClient.cs39
-rw-r--r--src/SMAPI.Web/Framework/Clients/GitHub/GitLicense.cs20
-rw-r--r--src/SMAPI.Web/Framework/Clients/GitHub/GitRepo.cs20
-rw-r--r--src/SMAPI.Web/Framework/Clients/GitHub/IGitHubClient.cs5
-rw-r--r--src/SMAPI.Web/Framework/ConfigModels/ApiClientsConfig.cs6
-rw-r--r--src/SMAPI.Web/Framework/ModRepositories/ChucklefishRepository.cs12
-rw-r--r--src/SMAPI.Web/Framework/ModRepositories/GitHubRepository.cs24
-rw-r--r--src/SMAPI.Web/Framework/ModRepositories/ModDropRepository.cs10
-rw-r--r--src/SMAPI.Web/Framework/ModRepositories/ModInfoModel.cs42
-rw-r--r--src/SMAPI.Web/Framework/ModRepositories/NexusRepository.cs8
11 files changed, 150 insertions, 48 deletions
diff --git a/src/SMAPI.Web/Framework/Caching/Mods/CachedMod.cs b/src/SMAPI.Web/Framework/Caching/Mods/CachedMod.cs
index fe8a7a1f..96eca847 100644
--- a/src/SMAPI.Web/Framework/Caching/Mods/CachedMod.cs
+++ b/src/SMAPI.Web/Framework/Caching/Mods/CachedMod.cs
@@ -58,6 +58,12 @@ namespace StardewModdingAPI.Web.Framework.Caching.Mods
/// <summary>The URL for the mod page.</summary>
public string Url { get; set; }
+ /// <summary>The license URL, if available.</summary>
+ public string LicenseUrl { get; set; }
+
+ /// <summary>The license name, if available.</summary>
+ public string LicenseName { get; set; }
+
/*********
** Accessors
@@ -86,12 +92,16 @@ namespace StardewModdingAPI.Web.Framework.Caching.Mods
this.MainVersion = mod.Version;
this.PreviewVersion = mod.PreviewVersion;
this.Url = mod.Url;
+ this.LicenseUrl = mod.LicenseUrl;
+ this.LicenseName = mod.LicenseName;
}
/// <summary>Get the API model for the cached data.</summary>
public ModInfoModel GetModel()
{
- return new ModInfoModel(name: this.Name, version: this.MainVersion, previewVersion: this.PreviewVersion, url: this.Url).WithError(this.FetchStatus, this.FetchError);
+ return new ModInfoModel(name: this.Name, version: this.MainVersion, url: this.Url, previewVersion: this.PreviewVersion)
+ .SetLicense(this.LicenseUrl, this.LicenseName)
+ .SetError(this.FetchStatus, this.FetchError);
}
}
}
diff --git a/src/SMAPI.Web/Framework/Clients/GitHub/GitHubClient.cs b/src/SMAPI.Web/Framework/Clients/GitHub/GitHubClient.cs
index 22950db9..84c20957 100644
--- a/src/SMAPI.Web/Framework/Clients/GitHub/GitHubClient.cs
+++ b/src/SMAPI.Web/Framework/Clients/GitHub/GitHubClient.cs
@@ -12,12 +12,6 @@ namespace StardewModdingAPI.Web.Framework.Clients.GitHub
/*********
** Fields
*********/
- /// <summary>The URL for a GitHub API query for the latest stable release, excluding the base URL, where {0} is the organisation and project name.</summary>
- private readonly string StableReleaseUrlFormat;
-
- /// <summary>The URL for a GitHub API query for the latest release (including prerelease), excluding the base URL, where {0} is the organisation and project name.</summary>
- private readonly string AnyReleaseUrlFormat;
-
/// <summary>The underlying HTTP client.</summary>
private readonly IClient Client;
@@ -27,17 +21,12 @@ namespace StardewModdingAPI.Web.Framework.Clients.GitHub
*********/
/// <summary>Construct an instance.</summary>
/// <param name="baseUrl">The base URL for the GitHub API.</param>
- /// <param name="stableReleaseUrlFormat">The URL for a GitHub API query for the latest stable release, excluding the <paramref name="baseUrl"/>, where {0} is the organisation and project name.</param>
- /// <param name="anyReleaseUrlFormat">The URL for a GitHub API query for the latest release (including prerelease), excluding the <paramref name="baseUrl"/>, where {0} is the organisation and project name.</param>
/// <param name="userAgent">The user agent for the API client.</param>
/// <param name="acceptHeader">The Accept header value expected by the GitHub API.</param>
/// <param name="username">The username with which to authenticate to the GitHub API.</param>
/// <param name="password">The password with which to authenticate to the GitHub API.</param>
- public GitHubClient(string baseUrl, string stableReleaseUrlFormat, string anyReleaseUrlFormat, string userAgent, string acceptHeader, string username, string password)
+ public GitHubClient(string baseUrl, string userAgent, string acceptHeader, string username, string password)
{
- this.StableReleaseUrlFormat = stableReleaseUrlFormat;
- this.AnyReleaseUrlFormat = anyReleaseUrlFormat;
-
this.Client = new FluentClient(baseUrl)
.SetUserAgent(userAgent)
.AddDefault(req => req.WithHeader("Accept", acceptHeader));
@@ -45,25 +34,43 @@ namespace StardewModdingAPI.Web.Framework.Clients.GitHub
this.Client = this.Client.SetBasicAuthentication(username, password);
}
+ /// <summary>Get basic metadata for a GitHub repository, if available.</summary>
+ /// <param name="repo">The repository key (like <c>Pathoschild/SMAPI</c>).</param>
+ /// <returns>Returns the repository info if it exists, else <c>null</c>.</returns>
+ public async Task<GitRepo> GetRepositoryAsync(string repo)
+ {
+ this.AssertKeyFormat(repo);
+ try
+ {
+ return await this.Client
+ .GetAsync($"repos/{repo}")
+ .As<GitRepo>();
+ }
+ catch (ApiException ex) when (ex.Status == HttpStatusCode.NotFound)
+ {
+ return null;
+ }
+ }
+
/// <summary>Get the latest release for a GitHub repository.</summary>
/// <param name="repo">The repository key (like <c>Pathoschild/SMAPI</c>).</param>
/// <param name="includePrerelease">Whether to return a prerelease version if it's latest.</param>
/// <returns>Returns the release if found, else <c>null</c>.</returns>
public async Task<GitRelease> GetLatestReleaseAsync(string repo, bool includePrerelease = false)
{
- this.AssetKeyFormat(repo);
+ this.AssertKeyFormat(repo);
try
{
if (includePrerelease)
{
GitRelease[] results = await this.Client
- .GetAsync(string.Format(this.AnyReleaseUrlFormat, repo))
+ .GetAsync($"repos/{repo}/releases?per_page=2") // allow for draft release (only visible if GitHub repo is owned by same account as the update check credentials)
.AsArray<GitRelease>();
return results.FirstOrDefault(p => !p.IsDraft);
}
return await this.Client
- .GetAsync(string.Format(this.StableReleaseUrlFormat, repo))
+ .GetAsync($"repos/{repo}/releases/latest")
.As<GitRelease>();
}
catch (ApiException ex) when (ex.Status == HttpStatusCode.NotFound)
@@ -85,7 +92,7 @@ namespace StardewModdingAPI.Web.Framework.Clients.GitHub
/// <summary>Assert that a repository key is formatted correctly.</summary>
/// <param name="repo">The repository key (like <c>Pathoschild/SMAPI</c>).</param>
/// <exception cref="ArgumentException">The repository key is invalid.</exception>
- private void AssetKeyFormat(string repo)
+ private void AssertKeyFormat(string repo)
{
if (repo == null || !repo.Contains("/") || repo.IndexOf("/", StringComparison.InvariantCultureIgnoreCase) != repo.LastIndexOf("/", StringComparison.InvariantCultureIgnoreCase))
throw new ArgumentException($"The value '{repo}' isn't a valid GitHub repository key, must be a username and project name like 'Pathoschild/SMAPI'.", nameof(repo));
diff --git a/src/SMAPI.Web/Framework/Clients/GitHub/GitLicense.cs b/src/SMAPI.Web/Framework/Clients/GitHub/GitLicense.cs
new file mode 100644
index 00000000..736efbe6
--- /dev/null
+++ b/src/SMAPI.Web/Framework/Clients/GitHub/GitLicense.cs
@@ -0,0 +1,20 @@
+using Newtonsoft.Json;
+
+namespace StardewModdingAPI.Web.Framework.Clients.GitHub
+{
+ /// <summary>The license info for a GitHub project.</summary>
+ internal class GitLicense
+ {
+ /// <summary>The license display name.</summary>
+ [JsonProperty("name")]
+ public string Name { get; set; }
+
+ /// <summary>The SPDX ID for the license.</summary>
+ [JsonProperty("spdx_id")]
+ public string SpdxId { get; set; }
+
+ /// <summary>The URL for the license info.</summary>
+ [JsonProperty("url")]
+ public string Url { get; set; }
+ }
+}
diff --git a/src/SMAPI.Web/Framework/Clients/GitHub/GitRepo.cs b/src/SMAPI.Web/Framework/Clients/GitHub/GitRepo.cs
new file mode 100644
index 00000000..7d80576e
--- /dev/null
+++ b/src/SMAPI.Web/Framework/Clients/GitHub/GitRepo.cs
@@ -0,0 +1,20 @@
+using Newtonsoft.Json;
+
+namespace StardewModdingAPI.Web.Framework.Clients.GitHub
+{
+ /// <summary>Basic metadata about a GitHub project.</summary>
+ internal class GitRepo
+ {
+ /// <summary>The full repository name, including the owner.</summary>
+ [JsonProperty("full_name")]
+ public string FullName { get; set; }
+
+ /// <summary>The URL to the repository web page, if any.</summary>
+ [JsonProperty("html_url")]
+ public string WebUrl { get; set; }
+
+ /// <summary>The code license, if any.</summary>
+ [JsonProperty("license")]
+ public GitLicense License { get; set; }
+ }
+}
diff --git a/src/SMAPI.Web/Framework/Clients/GitHub/IGitHubClient.cs b/src/SMAPI.Web/Framework/Clients/GitHub/IGitHubClient.cs
index 9519c26f..a34f03bd 100644
--- a/src/SMAPI.Web/Framework/Clients/GitHub/IGitHubClient.cs
+++ b/src/SMAPI.Web/Framework/Clients/GitHub/IGitHubClient.cs
@@ -9,6 +9,11 @@ namespace StardewModdingAPI.Web.Framework.Clients.GitHub
/*********
** Methods
*********/
+ /// <summary>Get basic metadata for a GitHub repository, if available.</summary>
+ /// <param name="repo">The repository key (like <c>Pathoschild/SMAPI</c>).</param>
+ /// <returns>Returns the repository info if it exists, else <c>null</c>.</returns>
+ Task<GitRepo> GetRepositoryAsync(string repo);
+
/// <summary>Get the latest release for a GitHub repository.</summary>
/// <param name="repo">The repository key (like <c>Pathoschild/SMAPI</c>).</param>
/// <param name="includePrerelease">Whether to return a prerelease version if it's latest.</param>
diff --git a/src/SMAPI.Web/Framework/ConfigModels/ApiClientsConfig.cs b/src/SMAPI.Web/Framework/ConfigModels/ApiClientsConfig.cs
index d2e9a2fe..a0a1f42a 100644
--- a/src/SMAPI.Web/Framework/ConfigModels/ApiClientsConfig.cs
+++ b/src/SMAPI.Web/Framework/ConfigModels/ApiClientsConfig.cs
@@ -29,12 +29,6 @@ namespace StardewModdingAPI.Web.Framework.ConfigModels
/// <summary>The base URL for the GitHub API.</summary>
public string GitHubBaseUrl { get; set; }
- /// <summary>The URL for a GitHub API query for the latest stable release, excluding the <see cref="GitHubBaseUrl"/>, where {0} is the organisation and project name.</summary>
- public string GitHubStableReleaseUrlFormat { get; set; }
-
- /// <summary>The URL for a GitHub API query for the latest release (including prerelease), excluding the <see cref="GitHubBaseUrl"/>, where {0} is the organisation and project name.</summary>
- public string GitHubAnyReleaseUrlFormat { get; set; }
-
/// <summary>The Accept header value expected by the GitHub API.</summary>
public string GitHubAcceptHeader { get; set; }
diff --git a/src/SMAPI.Web/Framework/ModRepositories/ChucklefishRepository.cs b/src/SMAPI.Web/Framework/ModRepositories/ChucklefishRepository.cs
index 04c80dd2..c14fb45d 100644
--- a/src/SMAPI.Web/Framework/ModRepositories/ChucklefishRepository.cs
+++ b/src/SMAPI.Web/Framework/ModRepositories/ChucklefishRepository.cs
@@ -32,21 +32,19 @@ namespace StardewModdingAPI.Web.Framework.ModRepositories
{
// validate ID format
if (!uint.TryParse(id, out uint realID))
- return new ModInfoModel().WithError(RemoteModStatus.DoesNotExist, $"The value '{id}' isn't a valid Chucklefish mod ID, must be an integer ID.");
+ return new ModInfoModel().SetError(RemoteModStatus.DoesNotExist, $"The value '{id}' isn't a valid Chucklefish mod ID, must be an integer ID.");
// fetch info
try
{
var mod = await this.Client.GetModAsync(realID);
- if (mod == null)
- return new ModInfoModel().WithError(RemoteModStatus.DoesNotExist, "Found no Chucklefish mod with this ID.");
-
- // create model
- return new ModInfoModel(name: mod.Name, version: this.NormaliseVersion(mod.Version), url: mod.Url);
+ return mod != null
+ ? new ModInfoModel(name: mod.Name, version: this.NormaliseVersion(mod.Version), url: mod.Url)
+ : new ModInfoModel().SetError(RemoteModStatus.DoesNotExist, "Found no Chucklefish mod with this ID.");
}
catch (Exception ex)
{
- return new ModInfoModel().WithError(RemoteModStatus.TemporaryError, ex.ToString());
+ return new ModInfoModel().SetError(RemoteModStatus.TemporaryError, ex.ToString());
}
}
diff --git a/src/SMAPI.Web/Framework/ModRepositories/GitHubRepository.cs b/src/SMAPI.Web/Framework/ModRepositories/GitHubRepository.cs
index 614e00c2..0e254b39 100644
--- a/src/SMAPI.Web/Framework/ModRepositories/GitHubRepository.cs
+++ b/src/SMAPI.Web/Framework/ModRepositories/GitHubRepository.cs
@@ -30,36 +30,46 @@ namespace StardewModdingAPI.Web.Framework.ModRepositories
/// <param name="id">The mod ID in this repository.</param>
public override async Task<ModInfoModel> GetModInfoAsync(string id)
{
+ ModInfoModel result = new ModInfoModel().SetBasicInfo(id, $"https://github.com/{id}/releases");
+
// validate ID format
if (!id.Contains("/") || id.IndexOf("/", StringComparison.InvariantCultureIgnoreCase) != id.LastIndexOf("/", StringComparison.InvariantCultureIgnoreCase))
- return new ModInfoModel().WithError(RemoteModStatus.DoesNotExist, $"The value '{id}' isn't a valid GitHub mod ID, must be a username and project name like 'Pathoschild/LookupAnything'.");
+ return result.SetError(RemoteModStatus.DoesNotExist, $"The value '{id}' isn't a valid GitHub mod ID, must be a username and project name like 'Pathoschild/LookupAnything'.");
// fetch info
try
{
+ // fetch repo info
+ GitRepo repository = await this.Client.GetRepositoryAsync(id);
+ if (repository == null)
+ return result.SetError(RemoteModStatus.DoesNotExist, "Found no GitHub repository for this ID.");
+ result
+ .SetBasicInfo(repository.FullName, $"{repository.WebUrl}/releases")
+ .SetLicense(url: repository.License?.Url, name: repository.License?.Name);
+
// get latest release (whether preview or stable)
GitRelease latest = await this.Client.GetLatestReleaseAsync(id, includePrerelease: true);
if (latest == null)
- return new ModInfoModel().WithError(RemoteModStatus.DoesNotExist, "Found no GitHub release for this ID.");
+ return result.SetError(RemoteModStatus.DoesNotExist, "Found no GitHub release for this ID.");
// split stable/prerelease if applicable
GitRelease preview = null;
if (latest.IsPrerelease)
{
- GitRelease result = await this.Client.GetLatestReleaseAsync(id, includePrerelease: false);
- if (result != null)
+ GitRelease release = await this.Client.GetLatestReleaseAsync(id, includePrerelease: false);
+ if (release != null)
{
preview = latest;
- latest = result;
+ latest = release;
}
}
// return data
- return new ModInfoModel(name: id, version: this.NormaliseVersion(latest.Tag), previewVersion: this.NormaliseVersion(preview?.Tag), url: $"https://github.com/{id}/releases");
+ return result.SetVersions(version: this.NormaliseVersion(latest.Tag), previewVersion: this.NormaliseVersion(preview?.Tag));
}
catch (Exception ex)
{
- return new ModInfoModel().WithError(RemoteModStatus.TemporaryError, ex.ToString());
+ return result.SetError(RemoteModStatus.TemporaryError, ex.ToString());
}
}
diff --git a/src/SMAPI.Web/Framework/ModRepositories/ModDropRepository.cs b/src/SMAPI.Web/Framework/ModRepositories/ModDropRepository.cs
index 5703b34e..62142668 100644
--- a/src/SMAPI.Web/Framework/ModRepositories/ModDropRepository.cs
+++ b/src/SMAPI.Web/Framework/ModRepositories/ModDropRepository.cs
@@ -32,19 +32,19 @@ namespace StardewModdingAPI.Web.Framework.ModRepositories
{
// validate ID format
if (!long.TryParse(id, out long modDropID))
- return new ModInfoModel().WithError(RemoteModStatus.DoesNotExist, $"The value '{id}' isn't a valid ModDrop mod ID, must be an integer ID.");
+ return new ModInfoModel().SetError(RemoteModStatus.DoesNotExist, $"The value '{id}' isn't a valid ModDrop mod ID, must be an integer ID.");
// fetch info
try
{
ModDropMod mod = await this.Client.GetModAsync(modDropID);
- if (mod == null)
- return new ModInfoModel().WithError(RemoteModStatus.DoesNotExist, "Found no ModDrop mod with this ID.");
- return new ModInfoModel(name: mod.Name, version: mod.LatestDefaultVersion?.ToString(), previewVersion: mod.LatestOptionalVersion?.ToString(), url: mod.Url);
+ return mod != null
+ ? new ModInfoModel(name: mod.Name, version: mod.LatestDefaultVersion?.ToString(), previewVersion: mod.LatestOptionalVersion?.ToString(), url: mod.Url)
+ : new ModInfoModel().SetError(RemoteModStatus.DoesNotExist, "Found no ModDrop mod with this ID.");
}
catch (Exception ex)
{
- return new ModInfoModel().WithError(RemoteModStatus.TemporaryError, ex.ToString());
+ return new ModInfoModel().SetError(RemoteModStatus.TemporaryError, ex.ToString());
}
}
diff --git a/src/SMAPI.Web/Framework/ModRepositories/ModInfoModel.cs b/src/SMAPI.Web/Framework/ModRepositories/ModInfoModel.cs
index 15e6c213..46b98860 100644
--- a/src/SMAPI.Web/Framework/ModRepositories/ModInfoModel.cs
+++ b/src/SMAPI.Web/Framework/ModRepositories/ModInfoModel.cs
@@ -18,6 +18,12 @@ namespace StardewModdingAPI.Web.Framework.ModRepositories
/// <summary>The mod's web URL.</summary>
public string Url { get; set; }
+ /// <summary>The license URL, if available.</summary>
+ public string LicenseUrl { get; set; }
+
+ /// <summary>The license name, if available.</summary>
+ public string LicenseName { get; set; }
+
/// <summary>The mod availability status on the remote site.</summary>
public RemoteModStatus Status { get; set; } = RemoteModStatus.Ok;
@@ -38,16 +44,48 @@ namespace StardewModdingAPI.Web.Framework.ModRepositories
/// <param name="url">The mod's web URL.</param>
public ModInfoModel(string name, string version, string url, string previewVersion = null)
{
+ this
+ .SetBasicInfo(name, url)
+ .SetVersions(version, previewVersion);
+ }
+
+ /// <summary>Set the basic mod info.</summary>
+ /// <param name="name">The mod name.</param>
+ /// <param name="url">The mod's web URL.</param>
+ public ModInfoModel SetBasicInfo(string name, string url)
+ {
this.Name = name;
+ this.Url = url;
+
+ return this;
+ }
+
+ /// <summary>Set the mod version info.</summary>
+ /// <param name="version">The semantic version for the mod's latest release.</param>
+ /// <param name="previewVersion">The semantic version for the mod's latest preview release, if available and different from <see cref="Version"/>.</param>
+ public ModInfoModel SetVersions(string version, string previewVersion = null)
+ {
this.Version = version;
this.PreviewVersion = previewVersion;
- this.Url = url;
+
+ return this;
+ }
+
+ /// <summary>Set the license info, if available.</summary>
+ /// <param name="url">The license URL.</param>
+ /// <param name="name">The license name.</param>
+ public ModInfoModel SetLicense(string url, string name)
+ {
+ this.LicenseUrl = url;
+ this.LicenseName = name;
+
+ return this;
}
/// <summary>Set a mod error.</summary>
/// <param name="status">The mod availability status on the remote site.</param>
/// <param name="error">The error message indicating why the mod is invalid (if applicable).</param>
- public ModInfoModel WithError(RemoteModStatus status, string error)
+ public ModInfoModel SetError(RemoteModStatus status, string error)
{
this.Status = status;
this.Error = error;
diff --git a/src/SMAPI.Web/Framework/ModRepositories/NexusRepository.cs b/src/SMAPI.Web/Framework/ModRepositories/NexusRepository.cs
index a4ae61eb..b4791f56 100644
--- a/src/SMAPI.Web/Framework/ModRepositories/NexusRepository.cs
+++ b/src/SMAPI.Web/Framework/ModRepositories/NexusRepository.cs
@@ -32,27 +32,27 @@ namespace StardewModdingAPI.Web.Framework.ModRepositories
{
// validate ID format
if (!uint.TryParse(id, out uint nexusID))
- return new ModInfoModel().WithError(RemoteModStatus.DoesNotExist, $"The value '{id}' isn't a valid Nexus mod ID, must be an integer ID.");
+ return new ModInfoModel().SetError(RemoteModStatus.DoesNotExist, $"The value '{id}' isn't a valid Nexus mod ID, must be an integer ID.");
// fetch info
try
{
NexusMod mod = await this.Client.GetModAsync(nexusID);
if (mod == null)
- return new ModInfoModel().WithError(RemoteModStatus.DoesNotExist, "Found no Nexus mod with this ID.");
+ return new ModInfoModel().SetError(RemoteModStatus.DoesNotExist, "Found no Nexus mod with this ID.");
if (mod.Error != null)
{
RemoteModStatus remoteStatus = mod.Status == NexusModStatus.Hidden || mod.Status == NexusModStatus.NotPublished
? RemoteModStatus.DoesNotExist
: RemoteModStatus.TemporaryError;
- return new ModInfoModel().WithError(remoteStatus, mod.Error);
+ return new ModInfoModel().SetError(remoteStatus, mod.Error);
}
return new ModInfoModel(name: mod.Name, version: this.NormaliseVersion(mod.Version), previewVersion: mod.LatestFileVersion?.ToString(), url: mod.Url);
}
catch (Exception ex)
{
- return new ModInfoModel().WithError(RemoteModStatus.TemporaryError, ex.ToString());
+ return new ModInfoModel().SetError(RemoteModStatus.TemporaryError, ex.ToString());
}
}