diff options
Diffstat (limited to 'src/SMAPI.Web/Framework/Clients/GitHub')
6 files changed, 107 insertions, 43 deletions
diff --git a/src/SMAPI.Web/Framework/Clients/GitHub/GitAsset.cs b/src/SMAPI.Web/Framework/Clients/GitHub/GitAsset.cs index 39ebf94e..dbce9368 100644 --- a/src/SMAPI.Web/Framework/Clients/GitHub/GitAsset.cs +++ b/src/SMAPI.Web/Framework/Clients/GitHub/GitAsset.cs @@ -1,5 +1,3 @@ -#nullable disable - using Newtonsoft.Json; namespace StardewModdingAPI.Web.Framework.Clients.GitHub @@ -7,16 +5,34 @@ namespace StardewModdingAPI.Web.Framework.Clients.GitHub /// <summary>A GitHub download attached to a release.</summary> internal class GitAsset { + /********* + ** Accessors + *********/ /// <summary>The file name.</summary> [JsonProperty("name")] - public string FileName { get; set; } + public string FileName { get; } /// <summary>The file content type.</summary> [JsonProperty("content_type")] - public string ContentType { get; set; } + public string ContentType { get; } /// <summary>The download URL.</summary> [JsonProperty("browser_download_url")] - public string DownloadUrl { get; set; } + public string DownloadUrl { get; } + + + /********* + ** Public methods + *********/ + /// <summary>Construct an instance.</summary> + /// <param name="fileName">The file name.</param> + /// <param name="contentType">The file content type.</param> + /// <param name="downloadUrl">The download URL.</param> + public GitAsset(string fileName, string contentType, string downloadUrl) + { + this.FileName = fileName; + this.ContentType = contentType; + this.DownloadUrl = downloadUrl; + } } } diff --git a/src/SMAPI.Web/Framework/Clients/GitHub/GitHubClient.cs b/src/SMAPI.Web/Framework/Clients/GitHub/GitHubClient.cs index 0e68e2c2..785979a5 100644 --- a/src/SMAPI.Web/Framework/Clients/GitHub/GitHubClient.cs +++ b/src/SMAPI.Web/Framework/Clients/GitHub/GitHubClient.cs @@ -1,5 +1,3 @@ -#nullable disable - using System; using System.Linq; using System.Net; @@ -35,26 +33,26 @@ namespace StardewModdingAPI.Web.Framework.Clients.GitHub /// <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 userAgent, string acceptHeader, string username, string password) + public GitHubClient(string baseUrl, string userAgent, string acceptHeader, string? username, string? password) { this.Client = new FluentClient(baseUrl) .SetUserAgent(userAgent) .AddDefault(req => req.WithHeader("Accept", acceptHeader)); if (!string.IsNullOrWhiteSpace(username)) - this.Client = this.Client.SetBasicAuthentication(username, password); + 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) + public async Task<GitRepo?> GetRepositoryAsync(string repo) { this.AssertKeyFormat(repo); try { return await this.Client .GetAsync($"repos/{repo}") - .As<GitRepo>(); + .As<GitRepo?>(); } catch (ApiException ex) when (ex.Status == HttpStatusCode.NotFound) { @@ -66,7 +64,7 @@ namespace StardewModdingAPI.Web.Framework.Clients.GitHub /// <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) + public async Task<GitRelease?> GetLatestReleaseAsync(string repo, bool includePrerelease = false) { this.AssertKeyFormat(repo); try @@ -81,7 +79,7 @@ namespace StardewModdingAPI.Web.Framework.Clients.GitHub return await this.Client .GetAsync($"repos/{repo}/releases/latest") - .As<GitRelease>(); + .As<GitRelease?>(); } catch (ApiException ex) when (ex.Status == HttpStatusCode.NotFound) { @@ -91,7 +89,7 @@ namespace StardewModdingAPI.Web.Framework.Clients.GitHub /// <summary>Get update check info about a mod.</summary> /// <param name="id">The mod ID.</param> - public async Task<IModPage> GetModData(string id) + public async Task<IModPage?> GetModData(string id) { IModPage page = new GenericModPage(this.SiteKey, id); @@ -99,15 +97,15 @@ namespace StardewModdingAPI.Web.Framework.Clients.GitHub return page.SetError(RemoteModStatus.DoesNotExist, $"The value '{id}' isn't a valid GitHub mod ID, must be a username and project name like 'Pathoschild/SMAPI'."); // fetch repo info - GitRepo repository = await this.GetRepositoryAsync(id); + GitRepo? repository = await this.GetRepositoryAsync(id); if (repository == null) return page.SetError(RemoteModStatus.DoesNotExist, "Found no GitHub repository for this ID."); string name = repository.FullName; string url = $"{repository.WebUrl}/releases"; // get releases - GitRelease latest; - GitRelease preview; + GitRelease? latest; + GitRelease? preview; { // get latest release (whether preview or stable) latest = await this.GetLatestReleaseAsync(id, includePrerelease: true); @@ -118,7 +116,7 @@ namespace StardewModdingAPI.Web.Framework.Clients.GitHub preview = null; if (latest.IsPrerelease) { - GitRelease release = await this.GetLatestReleaseAsync(id, includePrerelease: false); + GitRelease? release = await this.GetLatestReleaseAsync(id, includePrerelease: false); if (release != null) { preview = latest; @@ -129,8 +127,8 @@ namespace StardewModdingAPI.Web.Framework.Clients.GitHub // get downloads IModDownload[] downloads = new[] { latest, preview } - .Where(release => release != null) - .Select(release => (IModDownload)new GenericModDownload(release.Name, release.Body, release.Tag)) + .Where(release => release is not null) + .Select(release => (IModDownload)new GenericModDownload(release!.Name, release.Body, release.Tag)) .ToArray(); // return info @@ -140,7 +138,7 @@ namespace StardewModdingAPI.Web.Framework.Clients.GitHub /// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary> public void Dispose() { - this.Client?.Dispose(); + this.Client.Dispose(); } diff --git a/src/SMAPI.Web/Framework/Clients/GitHub/GitLicense.cs b/src/SMAPI.Web/Framework/Clients/GitHub/GitLicense.cs index 275c775a..24d6c3c5 100644 --- a/src/SMAPI.Web/Framework/Clients/GitHub/GitLicense.cs +++ b/src/SMAPI.Web/Framework/Clients/GitHub/GitLicense.cs @@ -1,5 +1,3 @@ -#nullable disable - using Newtonsoft.Json; namespace StardewModdingAPI.Web.Framework.Clients.GitHub @@ -7,16 +5,34 @@ namespace StardewModdingAPI.Web.Framework.Clients.GitHub /// <summary>The license info for a GitHub project.</summary> internal class GitLicense { + /********* + ** Accessors + *********/ /// <summary>The license display name.</summary> [JsonProperty("name")] - public string Name { get; set; } + public string Name { get; } /// <summary>The SPDX ID for the license.</summary> [JsonProperty("spdx_id")] - public string SpdxId { get; set; } + public string SpdxId { get; } /// <summary>The URL for the license info.</summary> [JsonProperty("url")] - public string Url { get; set; } + public string Url { get; } + + + /********* + ** Public methods + *********/ + /// <summary>Construct an instance.</summary> + /// <param name="name">The license display name.</param> + /// <param name="spdxId">The SPDX ID for the license.</param> + /// <param name="url">The URL for the license info.</param> + public GitLicense(string name, string spdxId, string url) + { + this.Name = name; + this.SpdxId = spdxId; + this.Url = url; + } } } diff --git a/src/SMAPI.Web/Framework/Clients/GitHub/GitRelease.cs b/src/SMAPI.Web/Framework/Clients/GitHub/GitRelease.cs index 383775d2..9de6f020 100644 --- a/src/SMAPI.Web/Framework/Clients/GitHub/GitRelease.cs +++ b/src/SMAPI.Web/Framework/Clients/GitHub/GitRelease.cs @@ -1,5 +1,4 @@ -#nullable disable - +using System; using Newtonsoft.Json; namespace StardewModdingAPI.Web.Framework.Clients.GitHub @@ -12,24 +11,45 @@ namespace StardewModdingAPI.Web.Framework.Clients.GitHub *********/ /// <summary>The display name.</summary> [JsonProperty("name")] - public string Name { get; set; } + public string Name { get; } /// <summary>The semantic version string.</summary> [JsonProperty("tag_name")] - public string Tag { get; set; } + public string Tag { get; } /// <summary>The Markdown description for the release.</summary> - public string Body { get; set; } + public string Body { get; internal set; } /// <summary>Whether this is a draft version.</summary> [JsonProperty("draft")] - public bool IsDraft { get; set; } + public bool IsDraft { get; } /// <summary>Whether this is a prerelease version.</summary> [JsonProperty("prerelease")] - public bool IsPrerelease { get; set; } + public bool IsPrerelease { get; } /// <summary>The attached files.</summary> - public GitAsset[] Assets { get; set; } + public GitAsset[] Assets { get; } + + + /********* + ** Public methods + *********/ + /// <summary>Construct an instance.</summary> + /// <param name="name">The display name.</param> + /// <param name="tag">The semantic version string.</param> + /// <param name="body">The Markdown description for the release.</param> + /// <param name="isDraft">Whether this is a draft version.</param> + /// <param name="isPrerelease">Whether this is a prerelease version.</param> + /// <param name="assets">The attached files.</param> + public GitRelease(string name, string tag, string? body, bool isDraft, bool isPrerelease, GitAsset[]? assets) + { + this.Name = name; + this.Tag = tag; + this.Body = body ?? string.Empty; + this.IsDraft = isDraft; + this.IsPrerelease = isPrerelease; + this.Assets = assets ?? Array.Empty<GitAsset>(); + } } } diff --git a/src/SMAPI.Web/Framework/Clients/GitHub/GitRepo.cs b/src/SMAPI.Web/Framework/Clients/GitHub/GitRepo.cs index 5b5ce6a6..879b5e49 100644 --- a/src/SMAPI.Web/Framework/Clients/GitHub/GitRepo.cs +++ b/src/SMAPI.Web/Framework/Clients/GitHub/GitRepo.cs @@ -1,5 +1,3 @@ -#nullable disable - using Newtonsoft.Json; namespace StardewModdingAPI.Web.Framework.Clients.GitHub @@ -7,16 +5,34 @@ namespace StardewModdingAPI.Web.Framework.Clients.GitHub /// <summary>Basic metadata about a GitHub project.</summary> internal class GitRepo { + /********* + ** Accessors + *********/ /// <summary>The full repository name, including the owner.</summary> [JsonProperty("full_name")] - public string FullName { get; set; } + public string FullName { get; } /// <summary>The URL to the repository web page, if any.</summary> [JsonProperty("html_url")] - public string WebUrl { get; set; } + public string? WebUrl { get; } /// <summary>The code license, if any.</summary> [JsonProperty("license")] - public GitLicense License { get; set; } + public GitLicense? License { get; } + + + /********* + ** Public methods + *********/ + /// <summary>Construct an instance.</summary> + /// <param name="fullName">The full repository name, including the owner.</param> + /// <param name="webUrl">The URL to the repository web page, if any.</param> + /// <param name="license">The code license, if any.</param> + public GitRepo(string fullName, string? webUrl, GitLicense? license) + { + this.FullName = fullName; + this.WebUrl = webUrl; + this.License = license; + } } } diff --git a/src/SMAPI.Web/Framework/Clients/GitHub/IGitHubClient.cs b/src/SMAPI.Web/Framework/Clients/GitHub/IGitHubClient.cs index e1961416..886e32d3 100644 --- a/src/SMAPI.Web/Framework/Clients/GitHub/IGitHubClient.cs +++ b/src/SMAPI.Web/Framework/Clients/GitHub/IGitHubClient.cs @@ -1,5 +1,3 @@ -#nullable disable - using System; using System.Threading.Tasks; @@ -14,12 +12,12 @@ namespace StardewModdingAPI.Web.Framework.Clients.GitHub /// <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); + 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> /// <returns>Returns the release if found, else <c>null</c>.</returns> - Task<GitRelease> GetLatestReleaseAsync(string repo, bool includePrerelease = false); + Task<GitRelease?> GetLatestReleaseAsync(string repo, bool includePrerelease = false); } } |