From 570b19ca7a16dc6af901a8869f178a8196147fb0 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Tue, 5 Jun 2018 21:38:24 -0400 Subject: tweak client for reuse in toolkit (#532) --- .../Framework/Clients/WebApi/WebApiClient.cs | 34 +++++++++++++++++++--- 1 file changed, 30 insertions(+), 4 deletions(-) (limited to 'src/StardewModdingAPI.Toolkit') diff --git a/src/StardewModdingAPI.Toolkit/Framework/Clients/WebApi/WebApiClient.cs b/src/StardewModdingAPI.Toolkit/Framework/Clients/WebApi/WebApiClient.cs index 277fbeeb..d94b0259 100644 --- a/src/StardewModdingAPI.Toolkit/Framework/Clients/WebApi/WebApiClient.cs +++ b/src/StardewModdingAPI.Toolkit/Framework/Clients/WebApi/WebApiClient.cs @@ -1,12 +1,13 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Net; using Newtonsoft.Json; namespace StardewModdingAPI.Toolkit.Framework.Clients.WebApi { /// Provides methods for interacting with the SMAPI web API. - internal class WebApiClient + public class WebApiClient { /********* ** Properties @@ -26,9 +27,6 @@ namespace StardewModdingAPI.Toolkit.Framework.Clients.WebApi /// The web API version. public WebApiClient(string baseUrl, ISemanticVersion version) { -#if !SMAPI_FOR_WINDOWS - baseUrl = baseUrl.Replace("https://", "http://"); // workaround for OpenSSL issues with the game's bundled Mono on Linux/Mac -#endif this.BaseUrl = new Uri(baseUrl); this.Version = version; } @@ -43,6 +41,34 @@ namespace StardewModdingAPI.Toolkit.Framework.Clients.WebApi ); } + /// Get the latest version for a mod. + /// The update keys to search. + public ISemanticVersion GetLatestVersion(string[] updateKeys) + { + if (!updateKeys.Any()) + return null; + + // fetch update results + ModInfoModel[] results = this + .GetModInfo(updateKeys) + .Values + .Where(p => p.Error == null) + .ToArray(); + if (!results.Any()) + return null; + + ISemanticVersion latest = null; + foreach (ModInfoModel result in results) + { + if (!SemanticVersion.TryParse(result.PreviewVersion ?? result.Version, out ISemanticVersion cur)) + continue; + + if (latest == null || cur.IsNewerThan(latest)) + latest = cur; + } + return latest; + } + /********* ** Private methods -- cgit