using System; using System.Collections.Generic; using System.Linq; using System.Net; using Newtonsoft.Json; 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 { /********* ** Fields *********/ /// The base URL for the web API. private readonly Uri BaseUrl; /// The API version number. private readonly ISemanticVersion Version; /// The JSON serializer settings to use. private readonly JsonSerializerSettings JsonSettings = new JsonHelper().JsonSettings; /********* ** 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 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 IDictionary GetModInfo(ModSearchEntryModel[] mods, ISemanticVersion apiVersion, ISemanticVersion gameVersion, Platform platform, bool includeExtendedMetadata = false) { return this.Post( $"v{this.Version}/mods", new ModSearchModel(mods, apiVersion, gameVersion, platform, includeExtendedMetadata) ).ToDictionary(p => p.ID); } /********* ** 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, this.JsonSettings); } } }