diff options
author | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-09-26 20:46:25 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-09-26 20:46:25 -0400 |
commit | 83bc6264e44d9b385db819c15892da316955471f (patch) | |
tree | da770298f9d93c590e79ef961c26efbd087da62f | |
parent | b67c0602c651d0ec973d527dbcfdb5f47ac35624 (diff) | |
download | SMAPI-83bc6264e44d9b385db819c15892da316955471f.tar.gz SMAPI-83bc6264e44d9b385db819c15892da316955471f.tar.bz2 SMAPI-83bc6264e44d9b385db819c15892da316955471f.zip |
simplify API fetch code
-rw-r--r-- | src/StardewModdingAPI/Framework/WebApiClient.cs | 36 | ||||
-rw-r--r-- | src/StardewModdingAPI/Program.cs | 7 |
2 files changed, 13 insertions, 30 deletions
diff --git a/src/StardewModdingAPI/Framework/WebApiClient.cs b/src/StardewModdingAPI/Framework/WebApiClient.cs index 8f0b403d..f3c7de28 100644 --- a/src/StardewModdingAPI/Framework/WebApiClient.cs +++ b/src/StardewModdingAPI/Framework/WebApiClient.cs @@ -1,9 +1,6 @@ using System; using System.Collections.Generic; -using System.IO; using System.Net; -using System.Text; -using System.Threading.Tasks; using Newtonsoft.Json; using StardewModdingAPI.Models; @@ -39,9 +36,9 @@ namespace StardewModdingAPI.Framework /// <summary>Get the latest SMAPI version.</summary> /// <param name="modKeys">The mod keys for which to fetch the latest version.</param> - public async Task<IDictionary<string, ModInfoModel>> GetModInfoAsync(params string[] modKeys) + public IDictionary<string, ModInfoModel> GetModInfo(params string[] modKeys) { - return await this.PostAsync<ModSearchModel, Dictionary<string, ModInfoModel>>( + return this.Post<ModSearchModel, Dictionary<string, ModInfoModel>>( $"v{this.Version}/mods", new ModSearchModel(modKeys) ); @@ -56,31 +53,20 @@ namespace StardewModdingAPI.Framework /// <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 async Task<TResult> PostAsync<TBody, TResult>(string url, TBody content) + private TResult Post<TBody, TResult>(string url, TBody content) { /*** ** Note: avoid HttpClient for Mac compatibility. ***/ - - // serialise content - byte[] data = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(content)); - - // build request - HttpWebRequest request = WebRequest.CreateHttp(new Uri(this.BaseUrl, url).ToString()); - request.Method = "POST"; - request.UserAgent = $"SMAPI/{this.Version}"; - request.ContentType = "application/json"; - request.ContentLength = data.Length; - using (Stream bodyStream = request.GetRequestStream()) - bodyStream.Write(data, 0, data.Length); - - // fetch data - using (WebResponse response = await request.GetResponseAsync()) - using (Stream responseStream = response.GetResponseStream()) - using (StreamReader reader = new StreamReader(responseStream)) + using (WebClient client = new WebClient()) { - string responseText = reader.ReadToEnd(); - return JsonConvert.DeserializeObject<TResult>(responseText); + 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); } } } diff --git a/src/StardewModdingAPI/Program.cs b/src/StardewModdingAPI/Program.cs index ba8c7847..e7f6f458 100644 --- a/src/StardewModdingAPI/Program.cs +++ b/src/StardewModdingAPI/Program.cs @@ -497,7 +497,7 @@ namespace StardewModdingAPI { this.Monitor.Log("Checking for SMAPI update...", LogLevel.Trace); - ModInfoModel response = client.GetModInfoAsync($"GitHub:{this.Settings.GitHubProjectName}").Result.Single().Value; + ModInfoModel response = client.GetModInfo($"GitHub:{this.Settings.GitHubProjectName}").Single().Value; if (response.Error != null) { this.Monitor.Log("Couldn't check for a new version of SMAPI. This won't affect your game, but you may not be notified of new versions if this keeps happening.", LogLevel.Warn); @@ -549,7 +549,7 @@ namespace StardewModdingAPI this.Monitor.Log($"Checking for updates to {modsByKey.Keys.Count} keys...", LogLevel.Trace); var results = ( - from entry in client.GetModInfoAsync(modsByKey.Keys.ToArray()).Result + from entry in client.GetModInfo(modsByKey.Keys.ToArray()) from mod in modsByKey[entry.Key] orderby mod.DisplayName select new { entry.Key, Mod = mod, Info = entry.Value } @@ -595,9 +595,6 @@ namespace StardewModdingAPI { this.Monitor.Log($"Couldn't check for new mod versions:\n{ex.GetLogSummary()}", LogLevel.Trace); } - - - }).Start(); } |