- Improved the RetryHandler.

- Made the tester buttons on the settings pages a bit more robust and added an indication when it's testing (spinner)
pull/226/head
TidusJar 9 years ago
parent 96abba49f7
commit 741a4ae75c

@ -62,13 +62,12 @@ namespace PlexRequests.Api
request.AddUrlSegment("imdbid", imdbid); request.AddUrlSegment("imdbid", imdbid);
request.AddUrlSegment("title", title); request.AddUrlSegment("title", title);
var policy = RetryHandler.RetryAndWaitPolicy (new TimeSpan[] { var obj = RetryHandler.Execute<JObject>(() => Api.ExecuteJson<JObject> (request, baseUrl),new TimeSpan[] {
TimeSpan.FromSeconds (2), TimeSpan.FromSeconds (2),
TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(5),
TimeSpan.FromSeconds(10) TimeSpan.FromSeconds(10)},
}, (exception, timespan) => Log.Error (exception, "Exception when calling AddMovie for CP, Retrying {0}", timespan)); (exception, timespan) => Log.Error (exception, "Exception when calling AddMovie for CP, Retrying {0}", timespan));
var obj = (JObject)policy.Execute( () => Api.ExecuteJson<JObject>(request, baseUrl));
Log.Trace("CP movie Add result count {0}", obj.Count); Log.Trace("CP movie Add result count {0}", obj.Count);
if (obj.Count > 0) if (obj.Count > 0)
@ -105,13 +104,15 @@ namespace PlexRequests.Api
}; };
request.AddUrlSegment("apikey", apiKey); request.AddUrlSegment("apikey", apiKey);
var policy = RetryHandler.RetryAndWaitPolicy (new TimeSpan[] {
var obj = RetryHandler.Execute<CouchPotatoStatus>(() => Api.Execute<CouchPotatoStatus> (request, url),new TimeSpan[] {
TimeSpan.FromSeconds (2), TimeSpan.FromSeconds (2),
TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(5),
TimeSpan.FromSeconds(10) TimeSpan.FromSeconds(10)},
}, (exception, timespan) => Log.Error (exception, "Exception when calling GetStatus for CP, Retrying {0}", timespan)); (exception, timespan) => Log.Error (exception, "Exception when calling GetStatus for CP, Retrying {0}", timespan));
return (CouchPotatoStatus)policy.Execute( () => Api.Execute<CouchPotatoStatus>(request,url)); return obj;
} }
public CouchPotatoProfiles GetProfiles(Uri url, string apiKey) public CouchPotatoProfiles GetProfiles(Uri url, string apiKey)
@ -125,13 +126,10 @@ namespace PlexRequests.Api
request.AddUrlSegment("apikey", apiKey); request.AddUrlSegment("apikey", apiKey);
var policy = RetryHandler.RetryAndWaitPolicy (new TimeSpan[] { var obj = RetryHandler.Execute<CouchPotatoProfiles>(() => Api.Execute<CouchPotatoProfiles> (request, url),null,
TimeSpan.FromSeconds (2), (exception, timespan) => Log.Error (exception, "Exception when calling GetProfiles for CP, Retrying {0}", timespan));
TimeSpan.FromSeconds(5),
TimeSpan.FromSeconds(10)
}, (exception, timespan) => Log.Error (exception, "Exception when calling GetProfiles for CP, Retrying {0}", timespan));
return (CouchPotatoProfiles)policy.Execute( () => Api.Execute<CouchPotatoProfiles>(request,url)); return obj;
} }
public CouchPotatoMovies GetMovies(Uri baseUrl, string apiKey, string[] status) public CouchPotatoMovies GetMovies(Uri baseUrl, string apiKey, string[] status)
@ -145,13 +143,15 @@ namespace PlexRequests.Api
request.AddUrlSegment("status", string.Join(",", status)); request.AddUrlSegment("status", string.Join(",", status));
try try
{ {
var policy = RetryHandler.RetryAndWaitPolicy (new TimeSpan[] { var obj = RetryHandler.Execute<CouchPotatoMovies>(() => Api.Execute<CouchPotatoMovies> (request, baseUrl),
TimeSpan.FromSeconds (5), new TimeSpan[] {
TimeSpan.FromSeconds(10), TimeSpan.FromSeconds (5),
TimeSpan.FromSeconds(30) TimeSpan.FromSeconds(10),
}, (exception, timespan) => Log.Error (exception, "Exception when calling GetMovies for CP, Retrying {0}", timespan)); TimeSpan.FromSeconds(30)
},
return (CouchPotatoMovies)policy.Execute( () => Api.Execute<CouchPotatoMovies>(request,baseUrl)); (exception, timespan) => Log.Error (exception, "Exception when calling GetMovies for CP, Retrying {0}", timespan));
return obj;
} }
catch (Exception e) // Request error is already logged in the ApiRequest class catch (Exception e) // Request error is already logged in the ApiRequest class
{ {

@ -71,7 +71,7 @@ namespace PlexRequests.Api
return albumResult; return albumResult;
} }
catch (JsonSerializationException jse) catch (Exception jse)
{ {
Log.Error(jse); Log.Error(jse);
return false; // If there is no matching result we do not get returned a JSON string, it just returns "false". return false; // If there is no matching result we do not get returned a JSON string, it just returns "false".
@ -94,7 +94,7 @@ namespace PlexRequests.Api
return result; return result;
} }
catch (JsonSerializationException jse) catch (Exception jse)
{ {
Log.Error(jse); Log.Error(jse);
return new List<HeadphonesGetIndex>(); return new List<HeadphonesGetIndex>();

@ -47,6 +47,17 @@ namespace PlexRequests.Api
Version = AssemblyHelper.GetAssemblyVersion(); Version = AssemblyHelper.GetAssemblyVersion();
} }
public PlexApi (IApiRequest api)
{
Api = api;
}
private IApiRequest Api { get; }
private const string SignInUri = "https://plex.tv/users/sign_in.json";
private const string FriendsUri = "https://plex.tv/pms/friends/all";
private const string GetAccountUri = "https://plex.tv/users/account";
private static Logger Log = LogManager.GetCurrentClassLogger(); private static Logger Log = LogManager.GetCurrentClassLogger();
private static string Version { get; } private static string Version { get; }
@ -69,15 +80,11 @@ namespace PlexRequests.Api
request.AddJsonBody(userModel); request.AddJsonBody(userModel);
var api = new ApiRequest(); var obj = RetryHandler.Execute<PlexAuthentication>(() => Api.Execute<PlexAuthentication> (request, new Uri(SignInUri)),
null,
(exception, timespan) => Log.Error (exception, "Exception when calling SignIn for Plex, Retrying {0}", timespan));
var policy = RetryHandler.RetryAndWaitPolicy (new TimeSpan[] { return obj;
TimeSpan.FromSeconds (2),
TimeSpan.FromSeconds(5),
TimeSpan.FromSeconds(10)
}, (exception, timespan) => Log.Error (exception, "Exception when calling SignIn for Plex, Retrying {0}", timespan));
return (PlexAuthentication)policy.Execute(() => api.Execute<PlexAuthentication>(request, new Uri("https://plex.tv/users/sign_in.json")));
} }
public PlexFriends GetUsers(string authToken) public PlexFriends GetUsers(string authToken)
@ -89,14 +96,10 @@ namespace PlexRequests.Api
AddHeaders(ref request, authToken); AddHeaders(ref request, authToken);
var api = new ApiRequest(); var users = RetryHandler.Execute<PlexFriends>(() => Api.Execute<PlexFriends> (request, new Uri(FriendsUri)),
var policy = RetryHandler.RetryAndWaitPolicy (new TimeSpan[] { null,
TimeSpan.FromSeconds (2), (exception, timespan) => Log.Error (exception, "Exception when calling GetUsers for Plex, Retrying {0}", timespan));
TimeSpan.FromSeconds(5),
TimeSpan.FromSeconds(10)
}, (exception, timespan) => Log.Error (exception, "Exception when calling GetUsers for Plex, Retrying {0}", timespan));
var users = (PlexFriends)policy.Execute(() =>api.ExecuteXml<PlexFriends>(request, new Uri("https://plex.tv/pms/friends/all")));
return users; return users;
} }
@ -119,14 +122,9 @@ namespace PlexRequests.Api
request.AddUrlSegment("searchTerm", searchTerm); request.AddUrlSegment("searchTerm", searchTerm);
AddHeaders(ref request, authToken); AddHeaders(ref request, authToken);
var api = new ApiRequest(); var search = RetryHandler.Execute<PlexSearch>(() => Api.ExecuteXml<PlexSearch> (request, plexFullHost),
var policy = RetryHandler.RetryAndWaitPolicy (new TimeSpan[] { null,
TimeSpan.FromSeconds (2), (exception, timespan) => Log.Error (exception, "Exception when calling SearchContent for Plex, Retrying {0}", timespan));
TimeSpan.FromSeconds(5),
TimeSpan.FromSeconds(10)
}, (exception, timespan) => Log.Error (exception, "Exception when calling SearchContent for Plex, Retrying {0}", timespan));
var search = (PlexSearch)policy.Execute(() => api.ExecuteXml<PlexSearch>(request, plexFullHost));
return search; return search;
} }
@ -139,14 +137,10 @@ namespace PlexRequests.Api
}; };
AddHeaders(ref request, authToken); AddHeaders(ref request, authToken);
var policy = RetryHandler.RetryAndWaitPolicy (new TimeSpan[] {
TimeSpan.FromSeconds (2),
TimeSpan.FromSeconds(5),
TimeSpan.FromSeconds(10)
}, (exception, timespan) => Log.Error (exception, "Exception when calling GetStatus for Plex, Retrying {0}", timespan));
var api = new ApiRequest(); var users = RetryHandler.Execute<PlexStatus>(() => Api.ExecuteXml<PlexStatus> (request, uri),
var users = (PlexStatus)policy.Execute(() => api.ExecuteXml<PlexStatus>(request, uri)); null,
(exception, timespan) => Log.Error (exception, "Exception when calling GetStatus for Plex, Retrying {0}", timespan));
return users; return users;
} }
@ -160,15 +154,9 @@ namespace PlexRequests.Api
AddHeaders(ref request, authToken); AddHeaders(ref request, authToken);
var policy = RetryHandler.RetryAndWaitPolicy (new TimeSpan[] { var account = RetryHandler.Execute<PlexAccount>(() => Api.ExecuteXml<PlexAccount> (request, new Uri(GetAccountUri)),
TimeSpan.FromSeconds (2), null,
TimeSpan.FromSeconds(5), (exception, timespan) => Log.Error (exception, "Exception when calling GetAccount for Plex, Retrying {0}", timespan));
TimeSpan.FromSeconds(10)
}, (exception, timespan) => Log.Error (exception, "Exception when calling GetAccount for Plex, Retrying: {0}", timespan));
var api = new ApiRequest();
var account = (PlexAccount)policy.Execute(() => api.ExecuteXml<PlexAccount>(request, new Uri("https://plex.tv/users/account")));
return account; return account;
} }
@ -183,16 +171,17 @@ namespace PlexRequests.Api
AddHeaders(ref request, authToken); AddHeaders(ref request, authToken);
var api = new ApiRequest();
try try
{ {
var policy = RetryHandler.RetryAndWaitPolicy (new TimeSpan[] { var lib = RetryHandler.Execute<PlexLibraries>(() => Api.ExecuteXml<PlexLibraries> (request, plexFullHost),
TimeSpan.FromSeconds (5), new TimeSpan[] {
TimeSpan.FromSeconds(10), TimeSpan.FromSeconds (5),
TimeSpan.FromSeconds(30) TimeSpan.FromSeconds(10),
}, (exception, timespan) => Log.Error (exception, "Exception when calling GetLibrarySections for Plex, Retrying {0}", timespan)); TimeSpan.FromSeconds(30)
},
return (PlexLibraries)policy.Execute(() => api.ExecuteXml<PlexLibraries>(request, plexFullHost)); (exception, timespan) => Log.Error (exception, "Exception when calling GetLibrarySections for Plex, Retrying {0}", timespan));
return lib;
} }
catch (Exception e) catch (Exception e)
{ {
@ -212,16 +201,17 @@ namespace PlexRequests.Api
request.AddUrlSegment("libraryId", libraryId); request.AddUrlSegment("libraryId", libraryId);
AddHeaders(ref request, authToken); AddHeaders(ref request, authToken);
var api = new ApiRequest();
try try
{ {
var policy = RetryHandler.RetryAndWaitPolicy (new TimeSpan[] { var lib = RetryHandler.Execute<PlexSearch>(() => Api.ExecuteXml<PlexSearch> (request, plexFullHost),
TimeSpan.FromSeconds (5), new TimeSpan[] {
TimeSpan.FromSeconds(10), TimeSpan.FromSeconds (5),
TimeSpan.FromSeconds(30) TimeSpan.FromSeconds(10),
}, (exception, timespan) => Log.Error (exception, "Exception when calling GetLibrary for Plex, Retrying {0}", timespan)); TimeSpan.FromSeconds(30)
},
return (PlexSearch)policy.Execute(() => api.ExecuteXml<PlexSearch>(request, plexFullHost)); (exception, timespan) => Log.Error (exception, "Exception when calling GetLibrary for Plex, Retrying {0}", timespan));
return lib;
} }
catch (Exception e) catch (Exception e)
{ {

@ -1,34 +1,71 @@
using System; using System;
using Polly.Retry; using Polly.Retry;
using Polly; using Polly;
using System.Threading.Tasks;
namespace PlexRequests.Api namespace PlexRequests.Api
{ {
public static class RetryHandler public static class RetryHandler
{ {
public static RetryPolicy RetryAndWaitPolicy(TimeSpan[] TimeSpan, Action action)
private static TimeSpan[] DefaultTime = new TimeSpan[] {
TimeSpan.FromSeconds (2),
TimeSpan.FromSeconds(5),
TimeSpan.FromSeconds(10)};
public static RetryPolicy RetryAndWaitPolicy(TimeSpan[] timeSpan, Action action)
{ {
if(timeSpan == null)
{
timeSpan = DefaultTime;
}
var policy = Policy.Handle<Exception> () var policy = Policy.Handle<Exception> ()
.WaitAndRetry(TimeSpan, (exception, timeSpan) => action()); .WaitAndRetry(timeSpan, (e, ts) => action());
return policy; return policy;
} }
public static RetryPolicy RetryAndWaitPolicy(TimeSpan[] TimeSpan) public static RetryPolicy RetryAndWaitPolicy(TimeSpan[] timeSpan)
{ {
if(timeSpan == null)
{
timeSpan = DefaultTime;
}
var policy = Policy.Handle<Exception> () var policy = Policy.Handle<Exception> ()
.WaitAndRetry(TimeSpan); .WaitAndRetry(timeSpan);
return policy; return policy;
} }
public static RetryPolicy RetryAndWaitPolicy(TimeSpan[] TimeSpan, Action<Exception, TimeSpan> action) public static RetryPolicy RetryAndWaitPolicy(TimeSpan[] timeSpan, Action<Exception, TimeSpan> action)
{ {
if(timeSpan == null)
{
timeSpan = DefaultTime;
}
var policy = Policy.Handle<Exception> () var policy = Policy.Handle<Exception> ()
.WaitAndRetry(TimeSpan, (exception, timeSpan) => action(exception, timeSpan)); .WaitAndRetry(timeSpan, (exception, ts) => action(exception, ts));
return policy; return policy;
} }
public static T Execute<T>(Func<T> action, TimeSpan[] timeSpan)
{
var policy = RetryAndWaitPolicy (timeSpan);
return policy.Execute (action);
}
public static T Execute<T>(Func<T> func, TimeSpan[] timeSpan, Action<Exception, TimeSpan> action)
{
if(timeSpan == null)
{
timeSpan = DefaultTime;
}
var policy = RetryAndWaitPolicy (timeSpan, action);
return policy.Execute (func);
}
} }
} }

@ -83,11 +83,7 @@ namespace PlexRequests.Api
request.AddQueryParameter("initial", quality); request.AddQueryParameter("initial", quality);
} }
var policy = RetryHandler.RetryAndWaitPolicy (new TimeSpan[] { var policy = RetryHandler.RetryAndWaitPolicy (null,(exception, timespan) => Log.Error (exception, "Exception when calling AddSeries for SR, Retrying {0}", timespan));
TimeSpan.FromSeconds (2),
TimeSpan.FromSeconds(5),
TimeSpan.FromSeconds(10)
}, (exception, timespan) => Log.Error (exception, "Exception when calling AddSeries for SR, Retrying {0}", timespan));
var obj = policy.Execute( () => Api.Execute<SickRageTvAdd>(request, baseUrl)); var obj = policy.Execute( () => Api.Execute<SickRageTvAdd>(request, baseUrl));
@ -164,11 +160,7 @@ namespace PlexRequests.Api
}; };
var policy = RetryHandler.RetryAndWaitPolicy (new TimeSpan[] { var policy = RetryHandler.RetryAndWaitPolicy (null,(exception, timespan) => Log.Error (exception, "Exception when calling Ping for SR, Retrying {0}", timespan));
TimeSpan.FromSeconds (2),
TimeSpan.FromSeconds(5),
TimeSpan.FromSeconds(10)
}, (exception, timespan) => Log.Error (exception, "Exception when calling Ping for SR, Retrying {0}", timespan));
@ -191,11 +183,8 @@ namespace PlexRequests.Api
try try
{ {
var policy = RetryHandler.RetryAndWaitPolicy (new TimeSpan[] { var policy = RetryHandler.RetryAndWaitPolicy (null,(exception, timespan) =>
TimeSpan.FromSeconds (2), Log.Error (exception, "Exception when calling VerifyShowHasLoaded for SR, Retrying {0}", timespan));
TimeSpan.FromSeconds(5),
TimeSpan.FromSeconds(10)
}, (exception, timespan) => Log.Error (exception, "Exception when calling VerifyShowHasLoaded for SR, Retrying {0}", timespan));
var obj = policy.Execute( () => Api.ExecuteJson<SickRageSeasonList>(request, baseUrl)); var obj = policy.Execute( () => Api.ExecuteJson<SickRageSeasonList>(request, baseUrl));
@ -224,12 +213,8 @@ namespace PlexRequests.Api
return await Task.Run(() => return await Task.Run(() =>
{ {
var policy = RetryHandler.RetryAndWaitPolicy (new TimeSpan[] { var policy = RetryHandler.RetryAndWaitPolicy (null,(exception, timespan) =>
TimeSpan.FromSeconds (2), Log.Error (exception, "Exception when calling AddSeason for SR, Retrying {0}", timespan));
TimeSpan.FromSeconds(5),
TimeSpan.FromSeconds(10)
}, (exception, timespan) => Log.Error (exception, "Exception when calling AddSeason for SR, Retrying {0}", timespan));
var result = policy.Execute(() => Api.Execute<SickRageTvAdd>(request, baseUrl)); var result = policy.Execute(() => Api.Execute<SickRageTvAdd>(request, baseUrl));
@ -255,8 +240,8 @@ namespace PlexRequests.Api
TimeSpan.FromSeconds (5), TimeSpan.FromSeconds (5),
TimeSpan.FromSeconds(10), TimeSpan.FromSeconds(10),
TimeSpan.FromSeconds(30) TimeSpan.FromSeconds(30)
}, (exception, timespan) => Log.Error (exception, "Exception when calling GetShows for SR, Retrying {0}", timespan)); }, (exception, timespan) =>
Log.Error (exception, "Exception when calling GetShows for SR, Retrying {0}", timespan));
return policy.Execute(() => Api.Execute<SickrageShows>(request, baseUrl)); return policy.Execute(() => Api.Execute<SickrageShows>(request, baseUrl));

@ -157,10 +157,10 @@ namespace PlexRequests.Api
return policy.Execute(() => Api.ExecuteJson<List<Series>>(request, baseUrl)); return policy.Execute(() => Api.ExecuteJson<List<Series>>(request, baseUrl));
} }
catch (ApiRequestException) catch (Exception e)
{ {
Log.Error("There has been an API exception when getting the Sonarr Series"); Log.Error(e, "There has been an API exception when getting the Sonarr Series");
return null; return null;
} }
} }
} }

@ -23,6 +23,10 @@
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ************************************************************************/ // ************************************************************************/
using System.Net;
using PlexRequests.Helpers.Exceptions;
#endregion #endregion
using System.Collections.Generic; using System.Collections.Generic;
@ -273,18 +277,27 @@ namespace PlexRequests.UI.Modules
return Response.AsJson(string.Empty); return Response.AsJson(string.Empty);
} }
var users = PlexApi.GetUsers(token); try {
if (users == null) var users = PlexApi.GetUsers(token);
{ if (users == null)
return Response.AsJson(string.Empty); {
} return Response.AsJson(string.Empty);
if (users.User == null || users.User?.Length == 0) }
{ if (users.User == null || users.User?.Length == 0)
return Response.AsJson(string.Empty); {
} return Response.AsJson(string.Empty);
}
var usernames = users.User.Select(x => x.Title); var usernames = users.User.Select(x => x.Title);
return Response.AsJson(usernames); return Response.AsJson(new {Result = true, Users = usernames});
} catch (Exception ex) {
Log.Error (ex);
if (ex is WebException || ex is ApiRequestException) {
return Response.AsJson (new { Result = false, Message ="Could not load the user list! We have connectivity problems connecting to Plex, Please ensure we can access Plex.Tv, The error has been logged." });
}
return Response.AsJson (new { Result = false, Message = ex.Message});
}
} }
private Negotiator CouchPotato() private Negotiator CouchPotato()

@ -87,7 +87,7 @@ namespace PlexRequests.UI.Modules
: Response.AsJson(new JsonResponseModel { Result = false, Message = "Could not connect to CouchPotato, please check your settings." }); : 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. catch (Exception 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("Exception thrown when attempting to get CP's status: ");
Log.Warn(e); Log.Warn(e);
@ -116,7 +116,7 @@ namespace PlexRequests.UI.Modules
: Response.AsJson(new JsonResponseModel { Result = false, Message = "Could not connect to Sonarr, please check your settings." }); : Response.AsJson(new JsonResponseModel { Result = false, Message = "Could not connect to Sonarr, please check your settings." });
} }
catch (ApplicationException e) // Exceptions are expected, if we cannot connect so we will just log and swallow them. catch (Exception e) // Exceptions are expected, if we cannot connect so we will just log and swallow them.
{ {
Log.Warn("Exception thrown when attempting to get Sonarr's status: "); Log.Warn("Exception thrown when attempting to get Sonarr's status: ");
Log.Warn(e); Log.Warn(e);
@ -150,7 +150,7 @@ namespace PlexRequests.UI.Modules
: Response.AsJson(new JsonResponseModel { Result = false, Message = "Could not connect to Plex, please check your settings." }); : 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. catch (Exception 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("Exception thrown when attempting to get Plex's status: ");
Log.Warn(e); Log.Warn(e);
@ -179,7 +179,7 @@ namespace PlexRequests.UI.Modules
: Response.AsJson(new JsonResponseModel { Result = false, Message = "Could not connect to SickRage, please check your settings." }); : Response.AsJson(new JsonResponseModel { Result = false, Message = "Could not connect to SickRage, please check your settings." });
} }
catch (ApplicationException e) // Exceptions are expected, if we cannot connect so we will just log and swallow them. catch (Exception e) // Exceptions are expected, if we cannot connect so we will just log and swallow them.
{ {
Log.Warn("Exception thrown when attempting to get SickRage's status: "); Log.Warn("Exception thrown when attempting to get SickRage's status: ");
Log.Warn(e); Log.Warn(e);
@ -214,7 +214,7 @@ namespace PlexRequests.UI.Modules
} }
return Response.AsJson(new JsonResponseModel { Result = false, Message = "Could not connect to Headphones, please check your settings." }); return Response.AsJson(new JsonResponseModel { Result = false, Message = "Could not connect to Headphones, please check your settings." });
} }
catch (ApplicationException e) catch (Exception e)
{ {
Log.Warn("Exception thrown when attempting to get Headphones's status: "); Log.Warn("Exception thrown when attempting to get Headphones's status: ");
Log.Warn(e); Log.Warn(e);

@ -46,6 +46,7 @@ namespace PlexRequests.UI.Modules
{ {
{ {
dynamic model = new ExpandoObject(); dynamic model = new ExpandoObject();
model.Redirect = Request.Query.redirect.Value ?? string.Empty;
model.Errored = Request.Query.error.HasValue; model.Errored = Request.Query.error.HasValue;
var adminCreated = UserMapper.DoUsersExist(); var adminCreated = UserMapper.DoUsersExist();
model.AdminExists = adminCreated; model.AdminExists = adminCreated;
@ -61,6 +62,7 @@ namespace PlexRequests.UI.Modules
var username = (string)Request.Form.Username; var username = (string)Request.Form.Username;
var password = (string)Request.Form.Password; var password = (string)Request.Form.Password;
var dtOffset = (int)Request.Form.DateTimeOffset; var dtOffset = (int)Request.Form.DateTimeOffset;
var redirect = (string)Request.Form.Redirect;
var userId = UserMapper.ValidateUser(username, password); var userId = UserMapper.ValidateUser(username, password);
@ -75,12 +77,8 @@ namespace PlexRequests.UI.Modules
} }
Session[SessionKeys.UsernameKey] = username; Session[SessionKeys.UsernameKey] = username;
Session[SessionKeys.ClientDateTimeOffsetKey] = dtOffset; Session[SessionKeys.ClientDateTimeOffsetKey] = dtOffset;
if (!string.IsNullOrEmpty(BaseUrl))
{
return this.LoginAndRedirect(userId.Value, expiry, $"/{BaseUrl}"); return this.LoginAndRedirect(userId.Value, expiry, redirect);
}
return this.LoginAndRedirect(userId.Value, expiry);
}; };
Get["/register"] = x => Get["/register"] = x =>

@ -153,10 +153,17 @@
url = createBaseUrl(base, url); url = createBaseUrl(base, url);
$.ajax({ $.ajax({
type: "Get", type: "Get",
url: "getusers", url: url,
dataType: "json", dataType: "json",
success: function (response) { success: function (response) {
if (response.length > 1) {
$('#users').html("");
if(!response.result){
generateNotify(response.message,"danger");
$('#users').append("<option>Error!</option>");
return;
}
if (response.users.length > 1) {
$(response).each(function () { $(response).each(function () {
$('#users').append("<option>" + this + "</option>"); $('#users').append("<option>" + this + "</option>");
}); });
@ -167,6 +174,8 @@
error: function (e) { error: function (e) {
console.log(e); console.log(e);
generateNotify("Something went wrong!", "danger"); generateNotify("Something went wrong!", "danger");
$('#users').html("");
$('#users').append("<option>Error!</option>");
} }
}); });

@ -88,7 +88,7 @@
<div class="form-group"> <div class="form-group">
<div> <div>
<button id="testCp" type="submit" class="btn btn-primary-outline">Test Connectivity</button> <button id="testCp" type="submit" class="btn btn-primary-outline">Test Connectivity <div id="spinner"> </div></button>
</div> </div>
</div> </div>
@ -170,6 +170,8 @@
e.preventDefault(); e.preventDefault();
var $form = $("#mainForm"); var $form = $("#mainForm");
var url = createBaseUrl(baseUrl,"/test/cp"); var url = createBaseUrl(baseUrl,"/test/cp");
$('#spinner').attr("class", "fa fa-spinner fa-spin");
$.ajax({ $.ajax({
type: $form.prop("method"), type: $form.prop("method"),
url: url, url: url,
@ -178,15 +180,18 @@
success: function (response) { success: function (response) {
console.log(response); console.log(response);
if (response.result === true) { if (response.result === true) {
$('#spinner').attr("class", "fa fa-check");
generateNotify(response.message, "success"); generateNotify(response.message, "success");
$('#authToken').val(response.authToken); $('#authToken').val(response.authToken);
} else { } else {
generateNotify(response.message, "warning"); generateNotify(response.message, "warning");
$('#spinner').attr("class", "fa fa-times");
} }
}, },
error: function (e) { error: function (e) {
console.log(e); console.log(e);
generateNotify("Something went wrong!", "danger"); generateNotify("Something went wrong!", "danger");
$('#spinner').attr("class", "fa fa-times");
} }
}); });
}); });

@ -106,7 +106,7 @@
<div class="form-group"> <div class="form-group">
<div> <div>
<button id="testEmail" type="submit" class="btn btn-primary-outline">Test</button> <button id="testEmail" type="submit" class="btn btn-primary-outline">Test <div id="spinner"></div></button>
</div> </div>
</div> </div>
@ -150,7 +150,8 @@
}); });
}); });
$('#testEmail').click(function (e) { $('#testEmail').click(function (e) {
$('#spinner').attr("class", "fa fa-spinner fa-spin");
var url = createBaseUrl(base, '/admin/testemailnotification'); var url = createBaseUrl(base, '/admin/testemailnotification');
e.preventDefault(); e.preventDefault();
@ -167,13 +168,16 @@
dataType: "json", dataType: "json",
success: function (response) { success: function (response) {
if (response.result === true) { if (response.result === true) {
$('#spinner').attr("class", "fa fa-check");
generateNotify(response.message, "success"); generateNotify(response.message, "success");
} else { } else {
$('#spinner').attr("class", "fa fa-times");
generateNotify(response.message, "warning"); generateNotify(response.message, "warning");
} }
}, },
error: function (e) { error: function (e) {
console.log(e); console.log(e);
$('#spinner').attr("class", "fa fa-times");
generateNotify("Something went wrong!", "danger"); generateNotify("Something went wrong!", "danger");
} }
}); });

@ -75,7 +75,7 @@
<div class="form-group"> <div class="form-group">
<div> <div>
<button id="testHeadphones" type="submit" class="btn btn-primary-outline">Test Connectivity</button> <button id="testHeadphones" type="submit" class="btn btn-primary-outline">Test Connectivity <div id="spinner"></div></button>
</div> </div>
</div> </div>
@ -96,6 +96,8 @@
var base = '@Html.GetBaseUrl()'; var base = '@Html.GetBaseUrl()';
$('#testHeadphones').click(function (e) { $('#testHeadphones').click(function (e) {
$('#spinner').attr("class", "fa fa-spinner fa-spin");
e.preventDefault(); e.preventDefault();
var $form = $("#mainForm"); var $form = $("#mainForm");
var url = createBaseUrl(base, '/test/headphones'); var url = createBaseUrl(base, '/test/headphones');
@ -107,13 +109,16 @@
success: function (response) { success: function (response) {
console.log(response); console.log(response);
if (response.result === true) { if (response.result === true) {
$('#spinner').attr("class", "fa fa-check");
generateNotify(response.message, "success"); generateNotify(response.message, "success");
} else { } else {
$('#spinner').attr("class", "fa fa-times");
generateNotify(response.message, "warning"); generateNotify(response.message, "warning");
} }
}, },
error: function (e) { error: function (e) {
console.log(e); console.log(e);
$('#spinner').attr("class", "fa fa-times");
generateNotify("Something went wrong!", "danger"); generateNotify("Something went wrong!", "danger");
} }
}); });

@ -52,7 +52,7 @@
</div> </div>
<div class="form-group"> <div class="form-group">
<div> <div>
<button id="testPlex" type="submit" class="btn btn-primary-outline">Test Connectivity</button> <button id="testPlex" type="submit" class="btn btn-primary-outline">Test Connectivity <div id="spinner"></div></button>
</div> </div>
</div> </div>
@ -69,25 +69,34 @@
<script> <script>
$(function () { $(function () {
var base = '@Html.GetBaseUrl()'; var base = '@Html.GetBaseUrl()';
$('#testPlex').click(function (e) { $('#testPlex').click(function (e) {
e.preventDefault(); e.preventDefault();
var url = createBaseUrl(base, '/test/plex'); var url = createBaseUrl(base, '/test/plex');
var $form = $("#mainForm"); var $form = $("#mainForm");
$('#spinner').attr("class", "fa fa-spinner fa-spin");
$.ajax({ $.ajax({
type: $form.prop("method"), type: $form.prop("method"),
url: url, url: url,
data: $form.serialize(), data: $form.serialize(),
dataType: "json", dataType: "json",
success: function (response) { success: function (response) {
$('#spinner').attr("class", "");
console.log(response); console.log(response);
if (response.result === true) { if (response.result === true) {
generateNotify(response.message, "success"); generateNotify(response.message, "success");
$('#spinner').attr("class", "fa fa-check");
$('#authToken').val(response.authToken); $('#authToken').val(response.authToken);
} else { } else {
generateNotify(response.message, "warning"); generateNotify(response.message, "warning");
$('#spinner').attr("class", "fa fa-times");
} }
}, },
error: function (e) { error: function (e) {
$('#spinner').attr("class", "fa fa-times");
console.log(e); console.log(e);
generateNotify("Something went wrong!", "danger"); generateNotify("Something went wrong!", "danger");
} }

@ -39,7 +39,7 @@
<div class="form-group"> <div class="form-group">
<div> <div>
<button id="testPushbullet" type="submit" class="btn btn-primary-outline">Test</button> <button id="testPushbullet" type="submit" class="btn btn-primary-outline">Test <div id="spinner"></div></button>
</div> </div>
</div> </div>
@ -56,6 +56,8 @@
$(function () { $(function () {
var base = '@Html.GetBaseUrl()'; var base = '@Html.GetBaseUrl()';
$('#save').click(function (e) { $('#save').click(function (e) {
$('#spinner').attr("class", "fa fa-spinner fa-spin");
e.preventDefault(); e.preventDefault();
var $form = $("#mainForm"); var $form = $("#mainForm");
@ -67,12 +69,15 @@
success: function (response) { success: function (response) {
if (response.result === true) { if (response.result === true) {
generateNotify(response.message, "success"); generateNotify(response.message, "success");
$('#spinner').attr("class", "fa fa-check");
} else { } else {
generateNotify(response.message, "warning"); generateNotify(response.message, "warning");
$('#spinner').attr("class", "fa fa-times");
} }
}, },
error: function (e) { error: function (e) {
console.log(e); console.log(e);
$('#spinner').attr("class", "fa fa-times");
generateNotify("Something went wrong!", "danger"); generateNotify("Something went wrong!", "danger");
} }
}); });

@ -39,7 +39,7 @@
<div class="form-group"> <div class="form-group">
<div> <div>
<button id="testPushover" type="submit" class="btn btn-primary-outline">Test</button> <button id="testPushover" type="submit" class="btn btn-primary-outline">Test <div id="spinner"></div></button>
</div> </div>
</div> </div>
@ -59,6 +59,7 @@
$('#save').click(function (e) { $('#save').click(function (e) {
e.preventDefault(); e.preventDefault();
$('#spinner').attr("class", "fa fa-spinner fa-spin");
var $form = $("#mainForm"); var $form = $("#mainForm");
$.ajax({ $.ajax({
type: $form.prop("method"), type: $form.prop("method"),
@ -68,13 +69,16 @@
success: function (response) { success: function (response) {
if (response.result === true) { if (response.result === true) {
generateNotify(response.message, "success"); generateNotify(response.message, "success");
$('#spinner').attr("class", "fa fa-check");
} else { } else {
generateNotify(response.message, "warning"); generateNotify(response.message, "warning");
$('#spinner').attr("class", "fa fa-times");
} }
}, },
error: function (e) { error: function (e) {
console.log(e); console.log(e);
generateNotify("Something went wrong!", "danger"); generateNotify("Something went wrong!", "danger");
$('#spinner').attr("class", "fa fa-times");
} }
}); });
}); });

@ -86,7 +86,7 @@
<div class="form-group"> <div class="form-group">
<div> <div>
<button id="testSickRage" type="submit" class="btn btn-primary-outline">Test Connectivity</button> <button id="testSickRage" type="submit" class="btn btn-primary-outline">Test Connectivity <div id="spinner"></div></button>
</div> </div>
</div> </div>
@ -147,6 +147,8 @@
}); });
$('#testSickRage').click(function (e) { $('#testSickRage').click(function (e) {
$('#spinner').attr("class", "fa fa-spinner fa-spin");
e.preventDefault(); e.preventDefault();
var qualityProfile = $("#profiles option:selected").val(); var qualityProfile = $("#profiles option:selected").val();
@ -165,13 +167,16 @@
console.log(response); console.log(response);
if (response.result === true) { if (response.result === true) {
generateNotify(response.message, "success"); generateNotify(response.message, "success");
$('#spinner').attr("class", "fa fa-check");
} else { } else {
generateNotify(response.message, "warning"); generateNotify(response.message, "warning");
$('#spinner').attr("class", "fa fa-times");
} }
}, },
error: function (e) { error: function (e) {
console.log(e); console.log(e);
generateNotify("Something went wrong!", "danger"); generateNotify("Something went wrong!", "danger");
$('#spinner').attr("class", "fa fa-times");
} }
}); });
}); });

@ -73,7 +73,7 @@
</div> </div>
<div class="form-group"> <div class="form-group">
<div> <div>
<button type="submit" id="getProfiles" class="btn btn-primary-outline">Get Quality Profiles</button> <button type="submit" id="getProfiles" class="btn btn-primary-outline">Get Quality Profiles <div id="getSpinner"/></button>
</div> </div>
</div> </div>
<div class="form-group"> <div class="form-group">
@ -109,7 +109,7 @@
</div> </div>
<div class="form-group"> <div class="form-group">
<div> <div>
<button id="testSonarr" type="submit" class="btn btn-primary-outline">Test Connectivity</button> <button id="testSonarr" type="submit" class="btn btn-primary-outline">Test Connectivity <div id="spinner"/></button>
</div> </div>
</div> </div>
@ -200,17 +200,22 @@
}); });
$('#getProfiles').click(function (e) { $('#getProfiles').click(function (e) {
$('#getSpinner').attr("class", "fa fa-spinner fa-spin");
e.preventDefault(); e.preventDefault();
if (!$('#Ip').val()) { if (!$('#Ip').val()) {
generateNotify("Please enter a valid IP/Hostname.", "warning"); generateNotify("Please enter a valid IP/Hostname.", "warning");
$('#getSpinner').attr("class", "fa fa-times");
return; return;
} }
if (!$('#portNumber').val()) { if (!$('#portNumber').val()) {
generateNotify("Please enter a valid Port Number.", "warning"); generateNotify("Please enter a valid Port Number.", "warning");
$('#getSpinner').attr("class", "fa fa-times");
return; return;
} }
if (!$('#ApiKey').val()) { if (!$('#ApiKey').val()) {
generateNotify("Please enter a valid ApiKey.", "warning"); generateNotify("Please enter a valid ApiKey.", "warning");
$('#getSpinner').attr("class", "fa fa-times");
return; return;
} }
var $form = $("#mainForm"); var $form = $("#mainForm");
@ -221,11 +226,13 @@
dataType: "json", dataType: "json",
success: function (response) { success: function (response) {
response.forEach(function (result) { response.forEach(function (result) {
$('#getSpinner').attr("class", "fa fa-check");
$("#select").append("<option value='" + result.id + "'>" + result.name + "</option>"); $("#select").append("<option value='" + result.id + "'>" + result.name + "</option>");
}); });
}, },
error: function (e) { error: function (e) {
console.log(e); console.log(e);
$('#getSpinner').attr("class", "fa fa-times");
generateNotify("Something went wrong!", "danger"); generateNotify("Something went wrong!", "danger");
} }
}); });
@ -233,6 +240,8 @@
var base = '@Html.GetBaseUrl()'; var base = '@Html.GetBaseUrl()';
$('#testSonarr').click(function (e) { $('#testSonarr').click(function (e) {
$('#spinner').attr("class", "fa fa-spinner fa-spin");
e.preventDefault(); e.preventDefault();
var qualityProfile = $("#profiles option:selected").val(); var qualityProfile = $("#profiles option:selected").val();
@ -251,13 +260,16 @@
console.log(response); console.log(response);
if (response.result === true) { if (response.result === true) {
generateNotify(response.message, "success"); generateNotify(response.message, "success");
$('#spinner').attr("class", "fa fa-check");
} else { } else {
generateNotify(response.message, "warning"); generateNotify(response.message, "warning");
$('#spinner').attr("class", "fa fa-times");
} }
}, },
error: function (e) { error: function (e) {
console.log(e); console.log(e);
generateNotify("Something went wrong!", "danger"); generateNotify("Something went wrong!", "danger");
$('#spinner').attr("class", "fa fa-times");
} }
}); });
}); });

@ -16,6 +16,7 @@
<br/><br/> <br/><br/>
<input class="btn btn-success-outline" type="submit" value="Login"/> <input class="btn btn-success-outline" type="submit" value="Login"/>
<input type="hidden" id="DateTimeOffset" name="DateTimeOffset" /> <input type="hidden" id="DateTimeOffset" name="DateTimeOffset" />
<input type="hidden" id="redirect" name="redirect" value="@Model.Redirect"/>
</form> </form>
@if (!Model.AdminExists) @if (!Model.AdminExists)
{ {

@ -45,7 +45,8 @@
@if (!Context.CurrentUser.IsAuthenticated()) @if (!Context.CurrentUser.IsAuthenticated())
{ {
<li><a href="@url/login"><i class="fa fa-user"></i> Admin</a></li>
<li><a href="@url/login?redirect=@Context.Request.Path"><i class="fa fa-user"></i> Admin</a></li>
} }
else else
{ {

Loading…
Cancel
Save