diff options
| author | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2019-09-13 18:24:54 -0400 |
|---|---|---|
| committer | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2019-09-13 18:24:54 -0400 |
| commit | 6521df7b131924835eb797251c1e956fae0d6e13 (patch) | |
| tree | b704dc64b6b6fef72615bac8950d5eff3c80ea89 /src/StardewModdingAPI.Toolkit/Framework/Clients/WebApi/WebApiClient.cs | |
| parent | e22a54212182d0adc443ac95bc791e83c90f7e10 (diff) | |
| parent | b7b8b001c5c2dc5d2c9fc1347532ca29368c2325 (diff) | |
| download | SMAPI-6521df7b131924835eb797251c1e956fae0d6e13.tar.gz SMAPI-6521df7b131924835eb797251c1e956fae0d6e13.tar.bz2 SMAPI-6521df7b131924835eb797251c1e956fae0d6e13.zip | |
Merge branch 'develop' into stable
Diffstat (limited to 'src/StardewModdingAPI.Toolkit/Framework/Clients/WebApi/WebApiClient.cs')
| -rw-r--r-- | src/StardewModdingAPI.Toolkit/Framework/Clients/WebApi/WebApiClient.cs | 73 |
1 files changed, 0 insertions, 73 deletions
diff --git a/src/StardewModdingAPI.Toolkit/Framework/Clients/WebApi/WebApiClient.cs b/src/StardewModdingAPI.Toolkit/Framework/Clients/WebApi/WebApiClient.cs deleted file mode 100644 index 7c3df384..00000000 --- a/src/StardewModdingAPI.Toolkit/Framework/Clients/WebApi/WebApiClient.cs +++ /dev/null @@ -1,73 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Net; -using Newtonsoft.Json; -using StardewModdingAPI.Toolkit.Serialisation; - -namespace StardewModdingAPI.Toolkit.Framework.Clients.WebApi -{ - /// <summary>Provides methods for interacting with the SMAPI web API.</summary> - public class WebApiClient - { - /********* - ** Fields - *********/ - /// <summary>The base URL for the web API.</summary> - private readonly Uri BaseUrl; - - /// <summary>The API version number.</summary> - private readonly ISemanticVersion Version; - - /// <summary>The JSON serializer settings to use.</summary> - private readonly JsonSerializerSettings JsonSettings = new JsonHelper().JsonSettings; - - - /********* - ** Public methods - *********/ - /// <summary>Construct an instance.</summary> - /// <param name="baseUrl">The base URL for the web API.</param> - /// <param name="version">The web API version.</param> - public WebApiClient(string baseUrl, ISemanticVersion version) - { - this.BaseUrl = new Uri(baseUrl); - this.Version = version; - } - - /// <summary>Get metadata about a set of mods from the web API.</summary> - /// <param name="mods">The mod keys for which to fetch the latest version.</param> - /// <param name="includeExtendedMetadata">Whether to include extended metadata for each mod.</param> - public IDictionary<string, ModEntryModel> GetModInfo(ModSearchEntryModel[] mods, bool includeExtendedMetadata = false) - { - return this.Post<ModSearchModel, ModEntryModel[]>( - $"v{this.Version}/mods", - new ModSearchModel(mods, includeExtendedMetadata) - ).ToDictionary(p => p.ID); - } - - - /********* - ** Private methods - *********/ - /// <summary>Fetch the response from the backend API.</summary> - /// <typeparam name="TBody">The body content type.</typeparam> - /// <typeparam name="TResult">The expected response type.</typeparam> - /// <param name="url">The request URL, optionally excluding the base URL.</param> - /// <param name="content">The body content to post.</param> - private TResult Post<TBody, TResult>(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<TResult>(response, this.JsonSettings); - } - } - } -} |
