diff options
author | James Stine <leon.blade@gmail.com> | 2017-08-13 17:44:00 -0400 |
---|---|---|
committer | James Stine <leon.blade@gmail.com> | 2017-08-13 17:44:00 -0400 |
commit | 06ed54b2c6c2e1f49d6975a34ea36f685d7f7c49 (patch) | |
tree | fea1bb6a927ca84009657430134cea8a2028cc09 /Dewdrop/Controllers | |
download | SMAPI-06ed54b2c6c2e1f49d6975a34ea36f685d7f7c49.tar.gz SMAPI-06ed54b2c6c2e1f49d6975a34ea36f685d7f7c49.tar.bz2 SMAPI-06ed54b2c6c2e1f49d6975a34ea36f685d7f7c49.zip |
First commit.
Diffstat (limited to 'Dewdrop/Controllers')
-rw-r--r-- | Dewdrop/Controllers/CheckController.cs | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/Dewdrop/Controllers/CheckController.cs b/Dewdrop/Controllers/CheckController.cs new file mode 100644 index 00000000..f3cdd364 --- /dev/null +++ b/Dewdrop/Controllers/CheckController.cs @@ -0,0 +1,60 @@ +using System; +using System.Net.Http; +using Microsoft.AspNetCore.Mvc; +using System.Threading.Tasks; +using Newtonsoft.Json; +using System.Collections.Generic; +using Dewdrop.Models; + +namespace Dewdrop.Controllers +{ + [Produces("application/json")] + [Route("api/check")] + public class CheckController : Controller + { + [HttpPost] + public async Task<string> Post([FromBody] NexusResponseModel[] mods) + { + using (var client = new HttpClient()) + { + // the return array of mods + var modList = new List<ModGenericModel>(); + + foreach (var mod in mods) + { + try + { + // create request with HttpRequestMessage + var request = new HttpRequestMessage(HttpMethod.Get, new Uri($"http://www.nexusmods.com/stardewvalley/mods/{mod.Id}")); + + // add the Nexus Client useragent to get JSON response from the site + request.Headers.UserAgent.ParseAdd("Nexus Client v0.63.15"); + + // send the request out + var response = await client.SendAsync(request); + // ensure the response is valid (throws exception) + response.EnsureSuccessStatusCode(); + + // get the JSON string of the response + var stringResponse = await response.Content.ReadAsStringAsync(); + + // create the mod data from the JSON string + var modData = JsonConvert.DeserializeObject<NexusResponseModel>(stringResponse); + + // add to the list of mods + modList.Add(modData.ModInfo()); + } + catch (Exception ex) + { + var modData = mod.ModInfo(); + modData.Valid = false; + + modList.Add(modData); + } + } + + return JsonConvert.SerializeObject(modList, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); + } + } + } +}
\ No newline at end of file |