diff --git a/PlexRequests.Api.Interfaces/IPlexApi.cs b/PlexRequests.Api.Interfaces/IPlexApi.cs index 59fffb5d5..473bf3237 100644 --- a/PlexRequests.Api.Interfaces/IPlexApi.cs +++ b/PlexRequests.Api.Interfaces/IPlexApi.cs @@ -36,6 +36,7 @@ namespace PlexRequests.Api.Interfaces PlexAuthentication SignIn(string username, string password); PlexFriends GetUsers(string authToken); PlexSearch SearchContent(string authToken, string searchTerm, Uri plexFullHost); + PlexStatus GetStatus(string authToken, Uri uri); } } \ No newline at end of file diff --git a/PlexRequests.Api.Models/Plex/PlexSearch.cs b/PlexRequests.Api.Models/Plex/PlexSearch.cs index 28a72e6f4..1d19b18bd 100644 --- a/PlexRequests.Api.Models/Plex/PlexSearch.cs +++ b/PlexRequests.Api.Models/Plex/PlexSearch.cs @@ -24,11 +24,10 @@ // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // ************************************************************************/ #endregion - using System.Collections.Generic; using System.Xml.Serialization; -namespace PlexRequests.Api.Models +namespace PlexRequests.Api.Models.Plex { [XmlRoot(ElementName = "Part")] public class Part diff --git a/PlexRequests.Api.Models/Plex/PlexStatus.cs b/PlexRequests.Api.Models/Plex/PlexStatus.cs new file mode 100644 index 000000000..e6f1e64bb --- /dev/null +++ b/PlexRequests.Api.Models/Plex/PlexStatus.cs @@ -0,0 +1,89 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Serialization; + +namespace PlexRequests.Api.Models.Plex +{ + [XmlRoot(ElementName = "Directory")] + public class Directory + { + [XmlAttribute(AttributeName = "count")] + public string Count { get; set; } + [XmlAttribute(AttributeName = "key")] + public string Key { get; set; } + [XmlAttribute(AttributeName = "title")] + public string Title { get; set; } + } + + [XmlRoot(ElementName = "MediaContainer")] + public class PlexStatus + { + [XmlElement(ElementName = "Directory")] + public List Directory { get; set; } + [XmlAttribute(AttributeName = "size")] + public string Size { get; set; } + [XmlAttribute(AttributeName = "allowCameraUpload")] + public string AllowCameraUpload { get; set; } + [XmlAttribute(AttributeName = "allowChannelAccess")] + public string AllowChannelAccess { get; set; } + [XmlAttribute(AttributeName = "allowMediaDeletion")] + public string AllowMediaDeletion { get; set; } + [XmlAttribute(AttributeName = "allowSync")] + public string AllowSync { get; set; } + [XmlAttribute(AttributeName = "backgroundProcessing")] + public string BackgroundProcessing { get; set; } + [XmlAttribute(AttributeName = "certificate")] + public string Certificate { get; set; } + [XmlAttribute(AttributeName = "companionProxy")] + public string CompanionProxy { get; set; } + [XmlAttribute(AttributeName = "friendlyName")] + public string FriendlyName { get; set; } + [XmlAttribute(AttributeName = "machineIdentifier")] + public string MachineIdentifier { get; set; } + [XmlAttribute(AttributeName = "multiuser")] + public string Multiuser { get; set; } + [XmlAttribute(AttributeName = "myPlex")] + public string MyPlex { get; set; } + [XmlAttribute(AttributeName = "myPlexMappingState")] + public string MyPlexMappingState { get; set; } + [XmlAttribute(AttributeName = "myPlexSigninState")] + public string MyPlexSigninState { get; set; } + [XmlAttribute(AttributeName = "myPlexSubscription")] + public string MyPlexSubscription { get; set; } + [XmlAttribute(AttributeName = "myPlexUsername")] + public string MyPlexUsername { get; set; } + [XmlAttribute(AttributeName = "platform")] + public string Platform { get; set; } + [XmlAttribute(AttributeName = "platformVersion")] + public string PlatformVersion { get; set; } + [XmlAttribute(AttributeName = "requestParametersInCookie")] + public string RequestParametersInCookie { get; set; } + [XmlAttribute(AttributeName = "sync")] + public string Sync { get; set; } + [XmlAttribute(AttributeName = "transcoderActiveVideoSessions")] + public string TranscoderActiveVideoSessions { get; set; } + [XmlAttribute(AttributeName = "transcoderAudio")] + public string TranscoderAudio { get; set; } + [XmlAttribute(AttributeName = "transcoderLyrics")] + public string TranscoderLyrics { get; set; } + [XmlAttribute(AttributeName = "transcoderPhoto")] + public string TranscoderPhoto { get; set; } + [XmlAttribute(AttributeName = "transcoderSubtitles")] + public string TranscoderSubtitles { get; set; } + [XmlAttribute(AttributeName = "transcoderVideo")] + public string TranscoderVideo { get; set; } + [XmlAttribute(AttributeName = "transcoderVideoBitrates")] + public string TranscoderVideoBitrates { get; set; } + [XmlAttribute(AttributeName = "transcoderVideoQualities")] + public string TranscoderVideoQualities { get; set; } + [XmlAttribute(AttributeName = "transcoderVideoResolutions")] + public string TranscoderVideoResolutions { get; set; } + [XmlAttribute(AttributeName = "updatedAt")] + public string UpdatedAt { get; set; } + [XmlAttribute(AttributeName = "version")] + public string Version { get; set; } + } +} diff --git a/PlexRequests.Api.Models/Plex/PlexUserRequest.cs b/PlexRequests.Api.Models/Plex/PlexUserRequest.cs index eaa517000..a466740e4 100644 --- a/PlexRequests.Api.Models/Plex/PlexUserRequest.cs +++ b/PlexRequests.Api.Models/Plex/PlexUserRequest.cs @@ -25,7 +25,7 @@ // ************************************************************************/ #endregion -namespace PlexRequests.Api.Models +namespace PlexRequests.Api.Models.Plex { public class PlexUserRequest { diff --git a/PlexRequests.Api.Models/PlexRequests.Api.Models.csproj b/PlexRequests.Api.Models/PlexRequests.Api.Models.csproj index fd5200e72..2255291fe 100644 --- a/PlexRequests.Api.Models/PlexRequests.Api.Models.csproj +++ b/PlexRequests.Api.Models/PlexRequests.Api.Models.csproj @@ -47,6 +47,7 @@ + diff --git a/PlexRequests.Api/PlexApi.cs b/PlexRequests.Api/PlexApi.cs index cf31ce53e..2308910b7 100644 --- a/PlexRequests.Api/PlexApi.cs +++ b/PlexRequests.Api/PlexApi.cs @@ -115,6 +115,25 @@ namespace PlexRequests.Api return search; } + + public PlexStatus GetStatus(string authToken, Uri uri) + { + var request = new RestRequest + { + Method = Method.GET, + }; + + request.AddHeader("X-Plex-Client-Identifier", "Test213"); + request.AddHeader("X-Plex-Product", "Request Plex"); + request.AddHeader("X-Plex-Version", Version); + request.AddHeader("X-Plex-Token", authToken); + request.AddHeader("Content-Type", "application/xml"); + + var api = new ApiRequest(); + var users = api.ExecuteXml(request, uri); + + return users; + } } } diff --git a/PlexRequests.Services.Tests/PlexAvailabilityCheckerTests.cs b/PlexRequests.Services.Tests/PlexAvailabilityCheckerTests.cs index 44f960c87..ed70fcf7d 100644 --- a/PlexRequests.Services.Tests/PlexAvailabilityCheckerTests.cs +++ b/PlexRequests.Services.Tests/PlexAvailabilityCheckerTests.cs @@ -33,6 +33,7 @@ using NUnit.Framework; using PlexRequests.Api.Interfaces; using PlexRequests.Api.Models; +using PlexRequests.Api.Models.Plex; using PlexRequests.Core; using PlexRequests.Core.SettingModels; using PlexRequests.Helpers.Exceptions; diff --git a/PlexRequests.UI/Modules/ApplicationTesterModule.cs b/PlexRequests.UI/Modules/ApplicationTesterModule.cs index eeeef9cfc..40d118f9e 100644 --- a/PlexRequests.UI/Modules/ApplicationTesterModule.cs +++ b/PlexRequests.UI/Modules/ApplicationTesterModule.cs @@ -33,6 +33,7 @@ using Nancy.Security; using NLog; using PlexRequests.Api.Interfaces; +using PlexRequests.Core; using PlexRequests.Core.SettingModels; using PlexRequests.UI.Models; @@ -41,22 +42,27 @@ namespace PlexRequests.UI.Modules public class ApplicationTesterModule : BaseModule { - public ApplicationTesterModule(ICouchPotatoApi cpApi, ISonarrApi sonarrApi) : base("test") + public ApplicationTesterModule(ICouchPotatoApi cpApi, ISonarrApi sonarrApi, IPlexApi plexApi, + ISettingsService authSettings) : base("test") { this.RequiresAuthentication(); CpApi = cpApi; SonarrApi = sonarrApi; - + PlexApi = plexApi; + AuthSettings = authSettings; Post["/cp"] = _ => CouchPotatoTest(); Post["/sonarr"] = _ => SonarrTest(); + Post["/plex"] = _ => PlexTest(); } private static readonly Logger Log = LogManager.GetCurrentClassLogger(); - private ISonarrApi SonarrApi { get; set; } + private ISonarrApi SonarrApi { get; } private ICouchPotatoApi CpApi { get; } + private IPlexApi PlexApi { get; } + private ISettingsService AuthSettings { get; } private Response CouchPotatoTest() { @@ -105,5 +111,34 @@ namespace PlexRequests.UI.Modules return Response.AsJson(new JsonResponseModel { Result = false, Message = message }); } } + + private Response PlexTest() + { + var plexSettings = this.Bind(); + var settings = AuthSettings.GetSettings(); + if (settings?.PlexAuthToken == null) + { + return Response.AsJson(new JsonResponseModel { Result = false, Message = "Plex is not setup yet, we need " }); + } + try + { + var status = PlexApi.GetStatus(settings.PlexAuthToken, plexSettings.FullUri); + return status != null + ? Response.AsJson(new JsonResponseModel { Result = true, Message = "Connected to Plex successfully!" }) + : Response.AsJson(new JsonResponseModel { Result = false, Message = "Could not connect to Plex, 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 Plex's status: "); + Log.Warn(e); + var message = $"Could not connect to Plex, please check your settings. Exception Message: {e.Message}"; + if (e.InnerException != null) + { + message = $"Could not connect to Plex, 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/Views/Admin/Plex.cshtml b/PlexRequests.UI/Views/Admin/Plex.cshtml index f3e55e9db..3e8632bb8 100644 --- a/PlexRequests.UI/Views/Admin/Plex.cshtml +++ b/PlexRequests.UI/Views/Admin/Plex.cshtml @@ -29,6 +29,11 @@ +
+
+ +
+
@@ -40,3 +45,32 @@
+ \ No newline at end of file