diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/StardewModdingAPI.Web/Controllers/CheckController.cs | 66 | ||||
-rw-r--r-- | src/StardewModdingAPI.Web/Models/IModModel.cs | 12 | ||||
-rw-r--r-- | src/StardewModdingAPI.Web/Models/ModGenericModel.cs | 27 | ||||
-rw-r--r-- | src/StardewModdingAPI.Web/Models/NexusResponseModel.cs | 41 | ||||
-rw-r--r-- | src/StardewModdingAPI.Web/Program.cs | 27 | ||||
-rw-r--r-- | src/StardewModdingAPI.Web/Properties/launchSettings.json | 29 | ||||
-rw-r--r-- | src/StardewModdingAPI.Web/StardewModdingAPI.Web.csproj | 27 | ||||
-rw-r--r-- | src/StardewModdingAPI.Web/Startup.cs | 52 | ||||
-rw-r--r-- | src/StardewModdingAPI.Web/appsettings.Development.json | 10 | ||||
-rw-r--r-- | src/StardewModdingAPI.Web/appsettings.json | 8 | ||||
-rw-r--r-- | src/StardewModdingAPI.sln | 2 |
11 files changed, 300 insertions, 1 deletions
diff --git a/src/StardewModdingAPI.Web/Controllers/CheckController.cs b/src/StardewModdingAPI.Web/Controllers/CheckController.cs new file mode 100644 index 00000000..8ab4611b --- /dev/null +++ b/src/StardewModdingAPI.Web/Controllers/CheckController.cs @@ -0,0 +1,66 @@ +using System; +using System.Collections.Generic; +using System.Net.Http; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using Newtonsoft.Json; +using StardewModdingAPI.Web.Models; + +namespace StardewModdingAPI.Web.Controllers +{ + /// <summary>Provides an API to perform mod update checks.</summary> + [Produces("application/json")] + [Route("api/check")] + public class CheckController : Controller + { + /********* + ** Public methods + *********/ + /// <summary>Fetch version metadata for the given mods.</summary> + /// <param name="mods">The mods for which to fetch update metadata.</param> + [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 }); + } + } + } +} diff --git a/src/StardewModdingAPI.Web/Models/IModModel.cs b/src/StardewModdingAPI.Web/Models/IModModel.cs new file mode 100644 index 00000000..2eadcaec --- /dev/null +++ b/src/StardewModdingAPI.Web/Models/IModModel.cs @@ -0,0 +1,12 @@ +namespace StardewModdingAPI.Web.Models +{ + /// <summary>A mod metadata response which provides a method to extract generic info.</summary> + internal interface IModModel + { + /********* + ** Public methods + *********/ + /// <summary>Get basic mod metadata.</summary> + ModGenericModel ModInfo(); + } +} diff --git a/src/StardewModdingAPI.Web/Models/ModGenericModel.cs b/src/StardewModdingAPI.Web/Models/ModGenericModel.cs new file mode 100644 index 00000000..208af416 --- /dev/null +++ b/src/StardewModdingAPI.Web/Models/ModGenericModel.cs @@ -0,0 +1,27 @@ +namespace StardewModdingAPI.Web.Models +{ + /// <summary>Generic metadata about a mod.</summary> + public class ModGenericModel + { + /********* + ** Accessors + *********/ + /// <summary>The unique mod ID.</summary> + public int ID { get; set; } + + /// <summary>The mod name.</summary> + public string Name { get; set; } + + /// <summary>The mod's vendor ID.</summary> + public string Vendor { get; set; } + + /// <summary>The mod's semantic version number.</summary> + public string Version { get; set; } + + /// <summary>The mod's web URL.</summary> + public string Url { get; set; } + + /// <summary>Whether the mod is valid.</summary> + public bool Valid { get; set; } = true; + } +} diff --git a/src/StardewModdingAPI.Web/Models/NexusResponseModel.cs b/src/StardewModdingAPI.Web/Models/NexusResponseModel.cs new file mode 100644 index 00000000..ae5c691c --- /dev/null +++ b/src/StardewModdingAPI.Web/Models/NexusResponseModel.cs @@ -0,0 +1,41 @@ +using Newtonsoft.Json; + +namespace StardewModdingAPI.Web.Models +{ + /// <summary>A mod metadata response from Nexus Mods.</summary> + public class NexusResponseModel : IModModel + { + /********* + ** Accessors + *********/ + /// <summary>The unique mod ID.</summary> + public int ID { get; set; } + + /// <summary>The mod name.</summary> + public string Name { get; set; } + + /// <summary>The mod's semantic version number.</summary> + public string Version { get; set; } + + /// <summary>The mod's web URL.</summary> + [JsonProperty("mod_page_uri")] + public string Url { get; set; } + + + /********* + ** Public methods + *********/ + /// <summary>Get basic mod metadata.</summary> + public ModGenericModel ModInfo() + { + return new ModGenericModel + { + ID = this.ID, + Version = this.Version, + Name = this.Name, + Url = this.Url, + Vendor = "Nexus" + }; + } + } +} diff --git a/src/StardewModdingAPI.Web/Program.cs b/src/StardewModdingAPI.Web/Program.cs new file mode 100644 index 00000000..5e258acc --- /dev/null +++ b/src/StardewModdingAPI.Web/Program.cs @@ -0,0 +1,27 @@ +using System.IO; +using Microsoft.AspNetCore.Hosting; + +namespace StardewModdingAPI.Web +{ + /// <summary>The main app entry point.</summary> + public class Program + { + /********* + ** Public methods + *********/ + /// <summary>The main app entry point.</summary> + /// <param name="args">The command-line arguments.</param> + public static void Main(string[] args) + { + // configure web server + new WebHostBuilder() + .UseKestrel() + .UseContentRoot(Directory.GetCurrentDirectory()) + .UseIISIntegration() + .UseStartup<Startup>() + .UseApplicationInsights() + .Build() + .Run(); + } + } +} diff --git a/src/StardewModdingAPI.Web/Properties/launchSettings.json b/src/StardewModdingAPI.Web/Properties/launchSettings.json new file mode 100644 index 00000000..c15134dc --- /dev/null +++ b/src/StardewModdingAPI.Web/Properties/launchSettings.json @@ -0,0 +1,29 @@ +{ + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:59482/", + "sslPort": 0 + } + }, + "profiles": { + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "api/check", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "Dewdrop": { + "commandName": "Project", + "launchBrowser": true, + "launchUrl": "api/check", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + }, + "applicationUrl": "http://localhost:59483" + } + } +} diff --git a/src/StardewModdingAPI.Web/StardewModdingAPI.Web.csproj b/src/StardewModdingAPI.Web/StardewModdingAPI.Web.csproj new file mode 100644 index 00000000..fa1d88eb --- /dev/null +++ b/src/StardewModdingAPI.Web/StardewModdingAPI.Web.csproj @@ -0,0 +1,27 @@ +<Project Sdk="Microsoft.NET.Sdk.Web"> + + <PropertyGroup> + <TargetFramework>netcoreapp1.1</TargetFramework> + <PackageTargetFallback>portable-net45+win8</PackageTargetFallback> + </PropertyGroup> + + <ItemGroup> + <Folder Include="wwwroot\" /> + </ItemGroup> + <ItemGroup> + <PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.0.0" /> + <PackageReference Include="Microsoft.AspNetCore" Version="1.1.2" /> + <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.3" /> + <PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.1.2" /> + <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="1.1.2" /> + <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="1.1.2" /> + <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer.Design" Version="1.1.2" /> + <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.2" /> + <PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="1.1.2" /> + <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="1.1.1" /> + </ItemGroup> + <ItemGroup> + <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="1.0.1" /> + </ItemGroup> + +</Project> diff --git a/src/StardewModdingAPI.Web/Startup.cs b/src/StardewModdingAPI.Web/Startup.cs new file mode 100644 index 00000000..c7a5e8fe --- /dev/null +++ b/src/StardewModdingAPI.Web/Startup.cs @@ -0,0 +1,52 @@ +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; + +namespace StardewModdingAPI.Web +{ + /// <summary>The web app startup configuration.</summary> + public class Startup + { + /********* + ** Accessors + *********/ + /// <summary>The web app configuration.</summary> + public IConfigurationRoot Configuration { get; } + + + /********* + ** Public methods + *********/ + /// <summary>Construct an instance.</summary> + /// <param name="env">The hosting environment.</param> + public Startup(IHostingEnvironment env) + { + this.Configuration = new ConfigurationBuilder() + .SetBasePath(env.ContentRootPath) + .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) + .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) + .AddEnvironmentVariables() + .Build(); + } + + /// <summary>The method called by the runtime to add services to the container.</summary> + /// <param name="services">The service injection container.</param> + public void ConfigureServices(IServiceCollection services) + { + services.AddMvc(); + } + + /// <summary>The method called by the runtime to configure the HTTP request pipeline.</summary> + /// <param name="app">The application builder.</param> + /// <param name="env">The hosting environment.</param> + /// <param name="loggerFactory">The logger factory.</param> + public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) + { + loggerFactory.AddConsole(this.Configuration.GetSection("Logging")); + loggerFactory.AddDebug(); + app.UseMvc(); + } + } +} diff --git a/src/StardewModdingAPI.Web/appsettings.Development.json b/src/StardewModdingAPI.Web/appsettings.Development.json new file mode 100644 index 00000000..fa8ce71a --- /dev/null +++ b/src/StardewModdingAPI.Web/appsettings.Development.json @@ -0,0 +1,10 @@ +{ + "Logging": { + "IncludeScopes": false, + "LogLevel": { + "Default": "Debug", + "System": "Information", + "Microsoft": "Information" + } + } +} diff --git a/src/StardewModdingAPI.Web/appsettings.json b/src/StardewModdingAPI.Web/appsettings.json new file mode 100644 index 00000000..5fff67ba --- /dev/null +++ b/src/StardewModdingAPI.Web/appsettings.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "IncludeScopes": false, + "LogLevel": { + "Default": "Warning" + } + } +} diff --git a/src/StardewModdingAPI.sln b/src/StardewModdingAPI.sln index 3cf129c0..031e2d5f 100644 --- a/src/StardewModdingAPI.sln +++ b/src/StardewModdingAPI.sln @@ -29,7 +29,7 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StardewModdingAPI.Installer EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StardewModdingAPI.Tests", "StardewModdingAPI.Tests\StardewModdingAPI.Tests.csproj", "{36CCB19E-92EB-48C7-9615-98EEFD45109B}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Dewdrop", "..\Dewdrop\Dewdrop.csproj", "{A308F679-51A3-4006-92D5-BAEC7EBD01A1}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StardewModdingAPI.Web", "StardewModdingAPI.Web\StardewModdingAPI.Web.csproj", "{A308F679-51A3-4006-92D5-BAEC7EBD01A1}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution |