Added test button to Plex. That's fixed #9

pull/13/head
tidusjar 8 years ago
parent fd36df091d
commit d0bfe95402

@ -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);
}
}

@ -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

@ -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> 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; }
}
}

@ -25,7 +25,7 @@
// ************************************************************************/
#endregion
namespace PlexRequests.Api.Models
namespace PlexRequests.Api.Models.Plex
{
public class PlexUserRequest
{

@ -47,6 +47,7 @@
<Compile Include="Plex\PlexError.cs" />
<Compile Include="Plex\PlexFriends.cs" />
<Compile Include="Plex\PlexSearch.cs" />
<Compile Include="Plex\PlexStatus.cs" />
<Compile Include="Plex\PlexUserRequest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Sonarr\SonarrAddSeries.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<PlexStatus>(request, uri);
return users;
}
}
}

@ -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;

@ -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<AuthenticationSettings> 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<AuthenticationSettings> 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<PlexSettings>();
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. <strong>Exception Message:</strong> {e.Message}";
if (e.InnerException != null)
{
message = $"Could not connect to Plex, please check your settings. <strong>Exception Message:</strong> {e.InnerException.Message}";
}
return Response.AsJson(new JsonResponseModel { Result = false, Message = message });
}
}
}
}

@ -29,6 +29,11 @@
<input type="text" class="form-control form-control-custom " id="portNumber" name="Port" placeholder="Port Number" value="@port">
</div>
</div>
<div class="form-group">
<div>
<button id="testPlex" type="submit" class="btn btn-primary-outline">Test Connectivity</button>
</div>
</div>
<div class="form-group">
<div>
@ -40,3 +45,32 @@
</div>
<script>
$(function() {
$('#testPlex').click(function (e) {
e.preventDefault();
var $form = $("#mainForm");
$.ajax({
type: $form.prop("method"),
url: "/test/plex",
data: $form.serialize(),
dataType: "json",
success: function (response) {
console.log(response);
if (response.result === true) {
generateNotify(response.message, "success");
$('#authToken').val(response.authToken);
} else {
generateNotify(response.message, "warning");
}
},
error: function (e) {
console.log(e);
generateNotify("Something went wrong!", "danger");
}
});
});
});
</script>
Loading…
Cancel
Save