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.
public class WebApiClient
{
/*********
** Properties
*********/
/// The base URL for the web API.
private readonly Uri BaseUrl;
/// The API version number.
private readonly ISemanticVersion Version;
/*********
** Public methods
*********/
/// Construct an instance.
/// The base URL for the web API.
/// The web API version.
public WebApiClient(string baseUrl, ISemanticVersion version)
{
this.BaseUrl = new Uri(baseUrl);
this.Version = version;
}
/// Get the latest SMAPI version.
/// The mod keys for which to fetch the latest version.
public IDictionary GetModInfo(params string[] modKeys)
{
return this.Post>(
$"v{this.Version}/mods",
new ModSearchModel(modKeys, allowInvalidVersions: true)
);
}
/// 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
*********/
/// Fetch the response from the backend API.
/// The body content type.
/// The expected response type.
/// The request URL, optionally excluding the base URL.
/// The body content to post.
private TResult Post(string url, TBody content)
{
// note: avoid HttpClient for Mac compatibility
using (WebClient client = new WebClient())
{
Uri fullUrl = new Uri(this.BaseUrl, url);
string data = JsonConvert.SerializeObject(content);
client.Headers["Content-Type"] = "application/json";
client.Headers["User-Agent"] = $"SMAPI/{this.Version}";
string response = client.UploadString(fullUrl, data);
return JsonConvert.DeserializeObject(response);
}
}
}
}