diff --git a/PlexRequests.Api.Interfaces/ICouchPotatoApi.cs b/PlexRequests.Api.Interfaces/ICouchPotatoApi.cs index a7b17d61c..bcfdb3dfd 100644 --- a/PlexRequests.Api.Interfaces/ICouchPotatoApi.cs +++ b/PlexRequests.Api.Interfaces/ICouchPotatoApi.cs @@ -27,11 +27,13 @@ using System; +using PlexRequests.Api.Models.Movie; + namespace PlexRequests.Api.Interfaces { public interface ICouchPotatoApi { bool AddMovie(string imdbid, string apiKey, string title, Uri baseUrl); - + CouchPotatoStatus GetStatus(Uri url, string apiKey); } } \ No newline at end of file diff --git a/PlexRequests.Api.Models/Movie/CouchPotatoStatus.cs b/PlexRequests.Api.Models/Movie/CouchPotatoStatus.cs new file mode 100644 index 000000000..7f20a7596 --- /dev/null +++ b/PlexRequests.Api.Models/Movie/CouchPotatoStatus.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PlexRequests.Api.Models.Movie +{ + public class CouchPotatoStatus + { + public bool success { get; set; } + } +} diff --git a/PlexRequests.Api.Models/PlexRequests.Api.Models.csproj b/PlexRequests.Api.Models/PlexRequests.Api.Models.csproj index 79683c5fe..c7d8aaeef 100644 --- a/PlexRequests.Api.Models/PlexRequests.Api.Models.csproj +++ b/PlexRequests.Api.Models/PlexRequests.Api.Models.csproj @@ -42,6 +42,7 @@ + diff --git a/PlexRequests.Api/CouchPotatoApi.cs b/PlexRequests.Api/CouchPotatoApi.cs index 0f8e91fcd..bdb238ee2 100644 --- a/PlexRequests.Api/CouchPotatoApi.cs +++ b/PlexRequests.Api/CouchPotatoApi.cs @@ -30,6 +30,8 @@ using Newtonsoft.Json.Linq; using NLog; using PlexRequests.Api.Interfaces; +using PlexRequests.Api.Models.Movie; + using RestSharp; namespace PlexRequests.Api @@ -71,5 +73,25 @@ namespace PlexRequests.Api } return false; } + + /// + /// Gets the status. + /// + /// The URL. + /// The API key. + /// + public CouchPotatoStatus GetStatus(Uri url, string apiKey) + { + Log.Trace("Getting CP Status, ApiKey = {0}", apiKey); + var request = new RestRequest + { + Resource = "api/{apikey}/app.available/", + Method = Method.GET + }; + + request.AddUrlSegment("apikey", apiKey); + + return Api.Execute(request,url); + } } } \ No newline at end of file diff --git a/PlexRequests.UI/Modules/AdminModule.cs b/PlexRequests.UI/Modules/AdminModule.cs index 184bdcc85..7ac0bba95 100644 --- a/PlexRequests.UI/Modules/AdminModule.cs +++ b/PlexRequests.UI/Modules/AdminModule.cs @@ -199,6 +199,8 @@ namespace PlexRequests.UI.Modules return View["CouchPotato", model]; } + + private Response SaveCouchPotato() { var couchPotatoSettings = this.Bind(); diff --git a/PlexRequests.UI/Modules/ApplicationTesterModule.cs b/PlexRequests.UI/Modules/ApplicationTesterModule.cs new file mode 100644 index 000000000..b979952bf --- /dev/null +++ b/PlexRequests.UI/Modules/ApplicationTesterModule.cs @@ -0,0 +1,89 @@ +#region Copyright +// /************************************************************************ +// Copyright (c) 2016 Jamie Rees +// File: ApplicationTesterModule.cs +// Created By: Jamie Rees +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// ************************************************************************/ +#endregion +using System; +using System.Collections.Generic; +using System.Linq; + +using Nancy; +using Nancy.ModelBinding; +using Nancy.Security; + +using NLog; + +using PlexRequests.Api; +using PlexRequests.Api.Interfaces; +using PlexRequests.Core; +using PlexRequests.Core.SettingModels; +using PlexRequests.Store; +using PlexRequests.UI.Models; + +namespace PlexRequests.UI.Modules +{ + public class ApplicationTesterModule : BaseModule + { + + public ApplicationTesterModule(ICouchPotatoApi cpApi, ISonarrApi sonarrApi) : base("test") + { + this.RequiresAuthentication(); + + CpApi = cpApi; + SonarrApi = sonarrApi; + + + Post["/cp"] = _ => CouchPotatoTest(); + + } + + private static readonly Logger Log = LogManager.GetCurrentClassLogger(); + private ISonarrApi SonarrApi { get; set; } + private ICouchPotatoApi CpApi { get; } + + private Response CouchPotatoTest() + { + var couchPotatoSettings = this.Bind(); + try + { + var status = CpApi.GetStatus(couchPotatoSettings.FullUri, couchPotatoSettings.ApiKey); + return status.success + ? Response.AsJson(new JsonResponseModel { Result = true, Message = "Connected to CouchPotato successfully!" }) + : Response.AsJson(new JsonResponseModel { Result = false, Message = "Could not connect to CouchPotato, please check your settings." }); + + } + catch (ApplicationException e) // Exceptions are expected if we cannot connect so we will just log and swallow them. + { + Log.Warn("Exception thrown when attempting to get CP's status: "); + Log.Warn(e); + var message = $"Could not connect to CouchPotato, please check your settings. Exception Message: {e.Message}"; + if (e.InnerException != null) + { + message = $"Could not connect to CouchPotato, please check your settings. Exception Message: {e.InnerException.Message}"; + } + return Response.AsJson(new JsonResponseModel { Result = false, Message = message }); + } + } + } +} \ No newline at end of file diff --git a/PlexRequests.UI/PlexRequests.UI.csproj b/PlexRequests.UI/PlexRequests.UI.csproj index 036f695a7..901540af6 100644 --- a/PlexRequests.UI/PlexRequests.UI.csproj +++ b/PlexRequests.UI/PlexRequests.UI.csproj @@ -166,6 +166,7 @@ + diff --git a/PlexRequests.UI/Views/Admin/CouchPotato.cshtml b/PlexRequests.UI/Views/Admin/CouchPotato.cshtml index 9d32f6871..a9ae695c0 100644 --- a/PlexRequests.UI/Views/Admin/CouchPotato.cshtml +++ b/PlexRequests.UI/Views/Admin/CouchPotato.cshtml @@ -29,7 +29,7 @@ - +
@@ -38,7 +38,14 @@
- + +
+
+ +
+
+ +
@@ -51,3 +58,37 @@
+ + \ No newline at end of file diff --git a/PlexRequests.UI/Views/Admin/EmailNotifications.cshtml b/PlexRequests.UI/Views/Admin/EmailNotifications.cshtml index d0560a863..90c17038d 100644 --- a/PlexRequests.UI/Views/Admin/EmailNotifications.cshtml +++ b/PlexRequests.UI/Views/Admin/EmailNotifications.cshtml @@ -90,3 +90,4 @@ +