using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Pathoschild.Http.Client;
using StardewModdingAPI.Toolkit.Serialization;
using StardewModdingAPI.Toolkit.Utilities;
namespace StardewModdingAPI.Toolkit.Framework.Clients.WebApi
{
/// Provides methods for interacting with the SMAPI web API.
public class WebApiClient : IDisposable
{
/*********
** Fields
*********/
/// The API version number.
private readonly ISemanticVersion Version;
/// The underlying HTTP client.
private readonly IClient Client;
/*********
** Public methods
*********/
/// Construct an instance.
/// The base URL for the web API.
/// The web API version.
public WebApiClient(string baseUrl, ISemanticVersion version)
{
this.Version = version;
this.Client = new FluentClient(baseUrl)
.SetUserAgent($"SMAPI/{version}");
this.Client.Formatters.JsonFormatter.SerializerSettings = JsonHelper.CreateDefaultSettings();
}
/// Get metadata about a set of mods from the web API.
/// The mod keys for which to fetch the latest version.
/// The SMAPI version installed by the player. If this is null, the API won't provide a recommended update.
/// The Stardew Valley version installed by the player.
/// The OS on which the player plays.
/// Whether to include extended metadata for each mod.
public async Task> GetModInfoAsync(ModSearchEntryModel[] mods, ISemanticVersion apiVersion, ISemanticVersion gameVersion, Platform platform, bool includeExtendedMetadata = false)
{
ModEntryModel[] result = await this.Client
.PostAsync(
$"v{this.Version}/mods",
new ModSearchModel(mods, apiVersion, gameVersion, platform, includeExtendedMetadata)
)
.As();
return result.ToDictionary(p => p.ID);
}
///
public void Dispose()
{
this.Client.Dispose();
}
}
}