tweak client for reuse in toolkit (#532)

This commit is contained in:
Jesse Plamondon-Willard 2018-06-05 21:38:24 -04:00
parent 172862db29
commit 570b19ca7a
2 changed files with 35 additions and 5 deletions

View File

@ -577,7 +577,11 @@ namespace StardewModdingAPI
new Thread(() =>
{
// create client
WebApiClient client = new WebApiClient(this.Settings.WebApiBaseUrl, Constants.ApiVersionForToolkit);
string url = this.Settings.WebApiBaseUrl;
#if !SMAPI_FOR_WINDOWS
url = url.Replace("https://", "http://"); // workaround for OpenSSL issues with the game's bundled Mono on Linux/Mac
#endif
WebApiClient client = new WebApiClient(url, Constants.ApiVersionForToolkit);
this.Monitor.Log("Checking for updates...", LogLevel.Trace);
// check SMAPI version

View File

@ -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
{
/// <summary>Provides methods for interacting with the SMAPI web API.</summary>
internal class WebApiClient
public class WebApiClient
{
/*********
** Properties
@ -26,9 +27,6 @@ namespace StardewModdingAPI.Toolkit.Framework.Clients.WebApi
/// <param name="version">The web API version.</param>
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
);
}
/// <summary>Get the latest version for a mod.</summary>
/// <param name="updateKeys">The update keys to search.</param>
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