From 83bc6264e44d9b385db819c15892da316955471f Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Tue, 26 Sep 2017 20:46:25 -0400 Subject: simplify API fetch code --- src/StardewModdingAPI/Framework/WebApiClient.cs | 36 ++++++++----------------- src/StardewModdingAPI/Program.cs | 7 ++--- 2 files changed, 13 insertions(+), 30 deletions(-) (limited to 'src/StardewModdingAPI') 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 /// Get the latest SMAPI version. /// The mod keys for which to fetch the latest version. - public async Task> GetModInfoAsync(params string[] modKeys) + public IDictionary GetModInfo(params string[] modKeys) { - return await this.PostAsync>( + return this.Post>( $"v{this.Version}/mods", new ModSearchModel(modKeys) ); @@ -56,31 +53,20 @@ namespace StardewModdingAPI.Framework /// The expected response type. /// The request URL, optionally excluding the base URL. /// The body content to post. - private async Task PostAsync(string url, TBody content) + private TResult Post(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(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(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(); } -- cgit