using System; using System.Linq; using System.Net; using System.Threading.Tasks; using Pathoschild.Http.Client; namespace StardewModdingAPI.Web.Framework.Clients.GitHub { /// An HTTP client for fetching metadata from GitHub. internal class GitHubClient : IGitHubClient { /********* ** Properties *********/ /// The URL for a GitHub API query for the latest stable release, excluding the base URL, where {0} is the organisation and project name. private readonly string StableReleaseUrlFormat; /// 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. private readonly string AnyReleaseUrlFormat; /// The underlying HTTP client. private readonly IClient Client; /********* ** Public methods *********/ /// Construct an instance. /// The base URL for the GitHub API. /// The URL for a GitHub API query for the latest stable release, excluding the , where {0} is the organisation and project name. /// The URL for a GitHub API query for the latest release (including prerelease), excluding the , where {0} is the organisation and project name. /// The user agent for the API client. /// The Accept header value expected by the GitHub API. /// The username with which to authenticate to the GitHub API. /// The password with which to authenticate to the GitHub API. public GitHubClient(string baseUrl, string stableReleaseUrlFormat, string anyReleaseUrlFormat, 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)); if (!string.IsNullOrWhiteSpace(username)) this.Client = this.Client.SetBasicAuthentication(username, password); } /// Get the latest release for a GitHub repository. /// The repository key (like Pathoschild/SMAPI). /// Whether to return a prerelease version if it's latest. /// Returns the release if found, else null. public async Task GetLatestReleaseAsync(string repo, bool includePrerelease = false) { this.AssetKeyFormat(repo); try { if (includePrerelease) { GitRelease[] results = await this.Client .GetAsync(string.Format(this.AnyReleaseUrlFormat, repo)) .AsArray(); return results.FirstOrDefault(p => !p.IsDraft); } return await this.Client .GetAsync(string.Format(this.StableReleaseUrlFormat, repo)) .As(); } catch (ApiException ex) when (ex.Status == HttpStatusCode.NotFound) { return null; } } /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. public void Dispose() { this.Client?.Dispose(); } /********* ** Private methods *********/ /// Assert that a repository key is formatted correctly. /// The repository key (like Pathoschild/SMAPI). /// The repository key is invalid. private void AssetKeyFormat(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)); } } }