From af463d0dd79693fa900415c8d0965bce33bc3bd3 Mon Sep 17 00:00:00 2001 From: smcpeck Date: Mon, 27 Feb 2017 20:03:26 -0600 Subject: [PATCH 01/11] Added some logging around API calls. --- Ombi.Api/ApiRequest.cs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Ombi.Api/ApiRequest.cs b/Ombi.Api/ApiRequest.cs index 2de72101d..470f591ee 100644 --- a/Ombi.Api/ApiRequest.cs +++ b/Ombi.Api/ApiRequest.cs @@ -84,8 +84,10 @@ namespace Ombi.Api public T ExecuteXml(IRestRequest request, Uri baseUri) where T : class { var client = new RestClient { BaseUrl = baseUri }; - var response = client.Execute(request); + Log.Trace("Request made to " + client.BaseUrl + " with details of " + request.ToString()); + Log.Trace("Api Content Response:"); + Log.Trace(response.Content); if (response.ErrorException != null) { @@ -95,12 +97,14 @@ namespace Ombi.Api } var result = DeserializeXml(response.Content); - return result;} + return result; + } public T ExecuteJson(IRestRequest request, Uri baseUri) where T : new() { var client = new RestClient { BaseUrl = baseUri }; var response = client.Execute(request); + Log.Trace("Request made to " + client.BaseUrl + " with details of " + request.ToString()); Log.Trace("Api Content Response:"); Log.Trace(response.Content); if (response.ErrorException != null) From 652650dacc6f682abb4746c1e075e95d1b900ac9 Mon Sep 17 00:00:00 2001 From: smcpeck Date: Mon, 27 Feb 2017 21:16:21 -0600 Subject: [PATCH 02/11] Added a missing `await` for an HP AddArtist call. Added some more Trace logging. --- Ombi.Api/ApiRequest.cs | 15 +++------------ Ombi.Core/HeadphonesSender.cs | 8 ++++---- 2 files changed, 7 insertions(+), 16 deletions(-) diff --git a/Ombi.Api/ApiRequest.cs b/Ombi.Api/ApiRequest.cs index 470f591ee..32b2f060f 100644 --- a/Ombi.Api/ApiRequest.cs +++ b/Ombi.Api/ApiRequest.cs @@ -58,9 +58,7 @@ namespace Ombi.Api var client = new RestClient { BaseUrl = baseUri }; var response = client.Execute(request); - Log.Trace("Api Content Response:"); - Log.Trace(response.Content); - + Log.Trace($"Request made to {client.BaseUrl} with details of {request.ToString()}. The response was {response.Content}"); if (response.ErrorException != null) { @@ -85,9 +83,7 @@ namespace Ombi.Api { var client = new RestClient { BaseUrl = baseUri }; var response = client.Execute(request); - Log.Trace("Request made to " + client.BaseUrl + " with details of " + request.ToString()); - Log.Trace("Api Content Response:"); - Log.Trace(response.Content); + Log.Trace($"Request made to {client.BaseUrl} with details of {request.ToString()}. The response was {response.Content}"); if (response.ErrorException != null) { @@ -104,9 +100,7 @@ namespace Ombi.Api { var client = new RestClient { BaseUrl = baseUri }; var response = client.Execute(request); - Log.Trace("Request made to " + client.BaseUrl + " with details of " + request.ToString()); - Log.Trace("Api Content Response:"); - Log.Trace(response.Content); + Log.Trace($"Request made to {client.BaseUrl} with details of {request.ToString()}. The response was {response.Content}"); if (response.ErrorException != null) { Log.Error(response.ErrorException); @@ -114,10 +108,7 @@ namespace Ombi.Api throw new ApiRequestException(message, response.ErrorException); } - Log.Trace("Deserialzing Object"); var json = JsonConvert.DeserializeObject(response.Content, _settings); - Log.Trace("Finished Deserialzing Object"); - return json; } diff --git a/Ombi.Core/HeadphonesSender.cs b/Ombi.Core/HeadphonesSender.cs index 043ea4e2a..f652eb56e 100644 --- a/Ombi.Core/HeadphonesSender.cs +++ b/Ombi.Core/HeadphonesSender.cs @@ -90,8 +90,8 @@ namespace Ombi.Core var artistExists = index.Any(x => x.ArtistID == request.ArtistId); if (!artistExists) { - var artistAdd = Api.AddArtist(Settings.ApiKey, Settings.FullUri, request.ArtistId); - Log.Info("Artist add result : {0}", artistAdd); + var artistAdd = await Api.AddArtist(Settings.ApiKey, Settings.FullUri, request.ArtistId); + Log.Info("Artist add result for {1}: {0}", artistAdd, request.ArtistName); } var counter = 0; @@ -116,13 +116,13 @@ namespace Ombi.Core { Thread.Sleep(WaitTime); counter++; - Log.Trace("Artist status {1}. Counter = {0}", counter, artistStatus); + Log.Trace("Artist {2} status {1}. Counter = {0}", counter, artistStatus, request.ArtistName); index = await Api.GetIndex(Settings.ApiKey, Settings.FullUri); artistStatus = index.Where(x => x.ArtistID == request.ArtistId).Select(x => x.Status).FirstOrDefault(); if (counter > CounterMax) { Log.Trace("Artist status is still not active. Counter = {0}. Returning false", counter); - Log.Warn("The artist status is still not Active. We have waited long enough, seems to be a big delay in headphones."); + Log.Warn($"The artist status for {request.ArtistName} is still not Active. We have waited long enough, seems to be a big delay in headphones."); return false; } } From e57cdf8c6f833df817fd8f944505d29113e1cbfe Mon Sep 17 00:00:00 2001 From: smcpeck Date: Mon, 27 Feb 2017 21:16:21 -0600 Subject: [PATCH 03/11] Added a missing `await` for an HP AddArtist call. Added some more Trace logging. --- Ombi.Api/ApiRequest.cs | 15 +++------------ Ombi.Core/HeadphonesSender.cs | 8 ++++---- 2 files changed, 7 insertions(+), 16 deletions(-) diff --git a/Ombi.Api/ApiRequest.cs b/Ombi.Api/ApiRequest.cs index 470f591ee..32b2f060f 100644 --- a/Ombi.Api/ApiRequest.cs +++ b/Ombi.Api/ApiRequest.cs @@ -58,9 +58,7 @@ namespace Ombi.Api var client = new RestClient { BaseUrl = baseUri }; var response = client.Execute(request); - Log.Trace("Api Content Response:"); - Log.Trace(response.Content); - + Log.Trace($"Request made to {client.BaseUrl} with details of {request.ToString()}. The response was {response.Content}"); if (response.ErrorException != null) { @@ -85,9 +83,7 @@ namespace Ombi.Api { var client = new RestClient { BaseUrl = baseUri }; var response = client.Execute(request); - Log.Trace("Request made to " + client.BaseUrl + " with details of " + request.ToString()); - Log.Trace("Api Content Response:"); - Log.Trace(response.Content); + Log.Trace($"Request made to {client.BaseUrl} with details of {request.ToString()}. The response was {response.Content}"); if (response.ErrorException != null) { @@ -104,9 +100,7 @@ namespace Ombi.Api { var client = new RestClient { BaseUrl = baseUri }; var response = client.Execute(request); - Log.Trace("Request made to " + client.BaseUrl + " with details of " + request.ToString()); - Log.Trace("Api Content Response:"); - Log.Trace(response.Content); + Log.Trace($"Request made to {client.BaseUrl} with details of {request.ToString()}. The response was {response.Content}"); if (response.ErrorException != null) { Log.Error(response.ErrorException); @@ -114,10 +108,7 @@ namespace Ombi.Api throw new ApiRequestException(message, response.ErrorException); } - Log.Trace("Deserialzing Object"); var json = JsonConvert.DeserializeObject(response.Content, _settings); - Log.Trace("Finished Deserialzing Object"); - return json; } diff --git a/Ombi.Core/HeadphonesSender.cs b/Ombi.Core/HeadphonesSender.cs index 043ea4e2a..f652eb56e 100644 --- a/Ombi.Core/HeadphonesSender.cs +++ b/Ombi.Core/HeadphonesSender.cs @@ -90,8 +90,8 @@ namespace Ombi.Core var artistExists = index.Any(x => x.ArtistID == request.ArtistId); if (!artistExists) { - var artistAdd = Api.AddArtist(Settings.ApiKey, Settings.FullUri, request.ArtistId); - Log.Info("Artist add result : {0}", artistAdd); + var artistAdd = await Api.AddArtist(Settings.ApiKey, Settings.FullUri, request.ArtistId); + Log.Info("Artist add result for {1}: {0}", artistAdd, request.ArtistName); } var counter = 0; @@ -116,13 +116,13 @@ namespace Ombi.Core { Thread.Sleep(WaitTime); counter++; - Log.Trace("Artist status {1}. Counter = {0}", counter, artistStatus); + Log.Trace("Artist {2} status {1}. Counter = {0}", counter, artistStatus, request.ArtistName); index = await Api.GetIndex(Settings.ApiKey, Settings.FullUri); artistStatus = index.Where(x => x.ArtistID == request.ArtistId).Select(x => x.Status).FirstOrDefault(); if (counter > CounterMax) { Log.Trace("Artist status is still not active. Counter = {0}. Returning false", counter); - Log.Warn("The artist status is still not Active. We have waited long enough, seems to be a big delay in headphones."); + Log.Warn($"The artist status for {request.ArtistName} is still not Active. We have waited long enough, seems to be a big delay in headphones."); return false; } } From 231a525109f8f01025e6168f9fd0f66dcb72afac Mon Sep 17 00:00:00 2001 From: smcpeck Date: Mon, 27 Feb 2017 22:14:42 -0600 Subject: [PATCH 04/11] Only deserialize response payload in ApiRequest when StatusCode == 200. Will a default return value in other cases cause other issues? --- Ombi.Api/ApiRequest.cs | 25 +++++++++++++++---------- Ombi.Core/HeadphonesSender.cs | 4 ++-- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/Ombi.Api/ApiRequest.cs b/Ombi.Api/ApiRequest.cs index 32b2f060f..65d2455fd 100644 --- a/Ombi.Api/ApiRequest.cs +++ b/Ombi.Api/ApiRequest.cs @@ -56,9 +56,8 @@ namespace Ombi.Api public T Execute(IRestRequest request, Uri baseUri) where T : new() { var client = new RestClient { BaseUrl = baseUri }; - var response = client.Execute(request); - Log.Trace($"Request made to {client.BaseUrl} with details of {request.ToString()}. The response was {response.Content}"); + Log.Trace($"Request made to {client.BaseUrl} with status code {response.StatusCode}. The response was {response.Content}"); if (response.ErrorException != null) { @@ -67,15 +66,16 @@ namespace Ombi.Api throw new ApiRequestException(message, response.ErrorException); } - return response.Data; + if (response.StatusCode != HttpStatusCode.OK) + return default(T); + else + return response.Data; } public IRestResponse Execute(IRestRequest request, Uri baseUri) { var client = new RestClient { BaseUrl = baseUri }; - var response = client.Execute(request); - return response; } @@ -83,7 +83,7 @@ namespace Ombi.Api { var client = new RestClient { BaseUrl = baseUri }; var response = client.Execute(request); - Log.Trace($"Request made to {client.BaseUrl} with details of {request.ToString()}. The response was {response.Content}"); + Log.Trace($"Request made to {client.BaseUrl} with status code {response.StatusCode}. The response was {response.Content}"); if (response.ErrorException != null) { @@ -92,7 +92,9 @@ namespace Ombi.Api throw new ApiRequestException(message, response.ErrorException); } - var result = DeserializeXml(response.Content); + T result = default(T); + if (response.StatusCode == HttpStatusCode.OK) + result = DeserializeXml(response.Content); return result; } @@ -100,7 +102,8 @@ namespace Ombi.Api { var client = new RestClient { BaseUrl = baseUri }; var response = client.Execute(request); - Log.Trace($"Request made to {client.BaseUrl} with details of {request.ToString()}. The response was {response.Content}"); + Log.Trace($"Request made to {client.BaseUrl} with status code {response.StatusCode}. The response was {response.Content}"); + if (response.ErrorException != null) { Log.Error(response.ErrorException); @@ -108,8 +111,10 @@ namespace Ombi.Api throw new ApiRequestException(message, response.ErrorException); } - var json = JsonConvert.DeserializeObject(response.Content, _settings); - return json; + T result = default(T); + if (response.StatusCode == HttpStatusCode.OK) + result = JsonConvert.DeserializeObject(response.Content, _settings); + return result; } private T DeserializeXml(string input) diff --git a/Ombi.Core/HeadphonesSender.cs b/Ombi.Core/HeadphonesSender.cs index f652eb56e..7fb971c9e 100644 --- a/Ombi.Core/HeadphonesSender.cs +++ b/Ombi.Core/HeadphonesSender.cs @@ -79,7 +79,7 @@ namespace Ombi.Core request.Approved = true; // Update the record - var updated = RequestService.UpdateRequest(request); + bool updated = RequestService.UpdateRequest(request); return updated; } @@ -90,7 +90,7 @@ namespace Ombi.Core var artistExists = index.Any(x => x.ArtistID == request.ArtistId); if (!artistExists) { - var artistAdd = await Api.AddArtist(Settings.ApiKey, Settings.FullUri, request.ArtistId); + bool artistAdd = await Api.AddArtist(Settings.ApiKey, Settings.FullUri, request.ArtistId); Log.Info("Artist add result for {1}: {0}", artistAdd, request.ArtistName); } From ec973917b4f12fa1da4341e9a6f09eb1947fed78 Mon Sep 17 00:00:00 2001 From: smcpeck Date: Mon, 27 Feb 2017 23:30:03 -0600 Subject: [PATCH 05/11] Tweaked ApiRequest behavior on non-200 responses; think it was breaking login. :-" --- Ombi.Api/ApiRequest.cs | 20 +++++++++++--------- Ombi.UI/Modules/UserLoginModule.cs | 2 -- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/Ombi.Api/ApiRequest.cs b/Ombi.Api/ApiRequest.cs index 65d2455fd..9566d2be0 100644 --- a/Ombi.Api/ApiRequest.cs +++ b/Ombi.Api/ApiRequest.cs @@ -66,10 +66,11 @@ namespace Ombi.Api throw new ApiRequestException(message, response.ErrorException); } - if (response.StatusCode != HttpStatusCode.OK) - return default(T); - else + if (response.StatusCode == HttpStatusCode.OK) return response.Data; + else + throw new Exception($"Got StatusCode={response.StatusCode} for {baseUri}."); + } public IRestResponse Execute(IRestRequest request, Uri baseUri) @@ -92,10 +93,11 @@ namespace Ombi.Api throw new ApiRequestException(message, response.ErrorException); } - T result = default(T); if (response.StatusCode == HttpStatusCode.OK) - result = DeserializeXml(response.Content); - return result; + return DeserializeXml(response.Content); + else + throw new Exception($"Got StatusCode={response.StatusCode} for {baseUri}."); + } public T ExecuteJson(IRestRequest request, Uri baseUri) where T : new() @@ -111,10 +113,10 @@ namespace Ombi.Api throw new ApiRequestException(message, response.ErrorException); } - T result = default(T); if (response.StatusCode == HttpStatusCode.OK) - result = JsonConvert.DeserializeObject(response.Content, _settings); - return result; + return JsonConvert.DeserializeObject(response.Content, _settings); + else + throw new Exception($"Got StatusCode={response.StatusCode} for {baseUri}."); } private T DeserializeXml(string input) diff --git a/Ombi.UI/Modules/UserLoginModule.cs b/Ombi.UI/Modules/UserLoginModule.cs index 1fd82cb2e..1f9250ad9 100644 --- a/Ombi.UI/Modules/UserLoginModule.cs +++ b/Ombi.UI/Modules/UserLoginModule.cs @@ -373,7 +373,6 @@ namespace Ombi.UI.Modules return Response.AsJson(new { result = false, message = Resources.UI.UserLogin_IncorrectUserPass }); } - var m = await AuthenticationSetup(userId, username, dateTimeOffset, loginGuid, isOwner, plexSettings.Enable, embySettings.Enable); var landingSettings = await LandingPageSettings.GetSettingsAsync(); @@ -397,7 +396,6 @@ namespace Ombi.UI.Modules return CustomModuleExtensions.LoginAndRedirect(this, m.LoginGuid, null, retVal.ToString()); } return Response.AsJson(new { result = true, url = retVal.ToString() }); - } private async Task LoginUser() From 3ee4ae34d572b20a9a926c5a8b6d6ba86115a565 Mon Sep 17 00:00:00 2001 From: smcpeck Date: Mon, 27 Feb 2017 23:46:06 -0600 Subject: [PATCH 06/11] Reverted (for now) non-200 response handling; added some extra logging. --- Ombi.Api/ApiRequest.cs | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/Ombi.Api/ApiRequest.cs b/Ombi.Api/ApiRequest.cs index 9566d2be0..3abe469f2 100644 --- a/Ombi.Api/ApiRequest.cs +++ b/Ombi.Api/ApiRequest.cs @@ -63,13 +63,14 @@ namespace Ombi.Api { var message = "Error retrieving response. Check inner details for more info."; Log.Error(response.ErrorException); + Log.Error(response.ErrorException.InnerException); throw new ApiRequestException(message, response.ErrorException); } - if (response.StatusCode == HttpStatusCode.OK) + //if (response.StatusCode == HttpStatusCode.OK) return response.Data; - else - throw new Exception($"Got StatusCode={response.StatusCode} for {baseUri}."); + //else + // throw new Exception($"Got StatusCode={response.StatusCode} for {baseUri}."); } @@ -89,14 +90,15 @@ namespace Ombi.Api if (response.ErrorException != null) { Log.Error(response.ErrorException); + Log.Error(response.ErrorException.InnerException); var message = "Error retrieving response. Check inner details for more info."; throw new ApiRequestException(message, response.ErrorException); } - if (response.StatusCode == HttpStatusCode.OK) + //if (response.StatusCode == HttpStatusCode.OK) return DeserializeXml(response.Content); - else - throw new Exception($"Got StatusCode={response.StatusCode} for {baseUri}."); + //else + // throw new Exception($"Got StatusCode={response.StatusCode} for {baseUri}."); } @@ -109,14 +111,15 @@ namespace Ombi.Api if (response.ErrorException != null) { Log.Error(response.ErrorException); + Log.Error(response.ErrorException.InnerException); var message = "Error retrieving response. Check inner details for more info."; throw new ApiRequestException(message, response.ErrorException); } - if (response.StatusCode == HttpStatusCode.OK) + //if (response.StatusCode == HttpStatusCode.OK) return JsonConvert.DeserializeObject(response.Content, _settings); - else - throw new Exception($"Got StatusCode={response.StatusCode} for {baseUri}."); + //else + // throw new Exception($"Got StatusCode={response.StatusCode} for {baseUri}."); } private T DeserializeXml(string input) From b5fc7692d647e25f450f4e8daddd04064198c0cc Mon Sep 17 00:00:00 2001 From: smcpeck Date: Tue, 28 Feb 2017 20:48:00 -0600 Subject: [PATCH 07/11] Restore checking of HTTP StatusCode on ApiRequests; remove checking of response.ErrorException --- Ombi.Api/ApiRequest.cs | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/Ombi.Api/ApiRequest.cs b/Ombi.Api/ApiRequest.cs index 3abe469f2..bb4dabb0a 100644 --- a/Ombi.Api/ApiRequest.cs +++ b/Ombi.Api/ApiRequest.cs @@ -57,8 +57,9 @@ namespace Ombi.Api { var client = new RestClient { BaseUrl = baseUri }; var response = client.Execute(request); - Log.Trace($"Request made to {client.BaseUrl} with status code {response.StatusCode}. The response was {response.Content}"); + Log.Trace($"Request made to {response.ResponseUri} with status code {response.StatusCode}. The response was {response.Content}"); + /** if (response.ErrorException != null) { var message = "Error retrieving response. Check inner details for more info."; @@ -66,11 +67,12 @@ namespace Ombi.Api Log.Error(response.ErrorException.InnerException); throw new ApiRequestException(message, response.ErrorException); } + **/ - //if (response.StatusCode == HttpStatusCode.OK) + if (response.StatusCode == HttpStatusCode.OK) return response.Data; - //else - // throw new Exception($"Got StatusCode={response.StatusCode} for {baseUri}."); + else + throw new ApiRequestException($"Got StatusCode={response.StatusCode} for {response.ResponseUri}."); } @@ -85,8 +87,9 @@ namespace Ombi.Api { var client = new RestClient { BaseUrl = baseUri }; var response = client.Execute(request); - Log.Trace($"Request made to {client.BaseUrl} with status code {response.StatusCode}. The response was {response.Content}"); + Log.Trace($"Request made to {response.ResponseUri} with status code {response.StatusCode}. The response was {response.Content}"); + /** if (response.ErrorException != null) { Log.Error(response.ErrorException); @@ -94,11 +97,12 @@ namespace Ombi.Api var message = "Error retrieving response. Check inner details for more info."; throw new ApiRequestException(message, response.ErrorException); } + **/ - //if (response.StatusCode == HttpStatusCode.OK) + if (response.StatusCode == HttpStatusCode.OK) return DeserializeXml(response.Content); - //else - // throw new Exception($"Got StatusCode={response.StatusCode} for {baseUri}."); + else + throw new ApiRequestException($"Got StatusCode={response.StatusCode} for {response.ResponseUri}."); } @@ -106,8 +110,9 @@ namespace Ombi.Api { var client = new RestClient { BaseUrl = baseUri }; var response = client.Execute(request); - Log.Trace($"Request made to {client.BaseUrl} with status code {response.StatusCode}. The response was {response.Content}"); + Log.Trace($"Request made to {response.ResponseUri} with status code {response.StatusCode}. The response was {response.Content}"); + /** if (response.ErrorException != null) { Log.Error(response.ErrorException); @@ -115,11 +120,12 @@ namespace Ombi.Api var message = "Error retrieving response. Check inner details for more info."; throw new ApiRequestException(message, response.ErrorException); } + **/ - //if (response.StatusCode == HttpStatusCode.OK) + if (response.StatusCode == HttpStatusCode.OK) return JsonConvert.DeserializeObject(response.Content, _settings); - //else - // throw new Exception($"Got StatusCode={response.StatusCode} for {baseUri}."); + else + throw new ApiRequestException($"Got StatusCode={response.StatusCode} for {response.ResponseUri}."); } private T DeserializeXml(string input) From f0358c1f5e376e140791f9c3da682b1fb623a3e0 Mon Sep 17 00:00:00 2001 From: smcpeck Date: Tue, 28 Feb 2017 20:51:28 -0600 Subject: [PATCH 08/11] Remove all the polling/retry logic around HP requests. This was a problem do to not properly awaiting the initial AddArtist API call being sent to HP. Also fix SetAlbumStatus to use ReleaseId instead of MusicBrainsId (same fix previously applied to AddArtist). --- Ombi.Core/HeadphonesSender.cs | 77 +++-------------------------------- 1 file changed, 5 insertions(+), 72 deletions(-) diff --git a/Ombi.Core/HeadphonesSender.cs b/Ombi.Core/HeadphonesSender.cs index 7fb971c9e..c96fefa06 100644 --- a/Ombi.Core/HeadphonesSender.cs +++ b/Ombi.Core/HeadphonesSender.cs @@ -88,87 +88,20 @@ namespace Ombi.Core { var index = await Api.GetIndex(Settings.ApiKey, Settings.FullUri); var artistExists = index.Any(x => x.ArtistID == request.ArtistId); + bool artistAdd = false; if (!artistExists) { - bool artistAdd = await Api.AddArtist(Settings.ApiKey, Settings.FullUri, request.ArtistId); + artistAdd = await Api.AddArtist(Settings.ApiKey, Settings.FullUri, request.ArtistId); Log.Info("Artist add result for {1}: {0}", artistAdd, request.ArtistName); } - var counter = 0; - while (index.All(x => x.ArtistID != request.ArtistId)) - { - Thread.Sleep(WaitTime); - counter++; - Log.Trace("Artist is still not present in the index. Counter = {0}", counter); - index = await Api.GetIndex(Settings.ApiKey, Settings.FullUri); - - if (counter > CounterMax) - { - Log.Trace("Artist is still not present in the index. Counter = {0}. Returning false", counter); - Log.Warn("We have tried adding the artist but it seems they are still not in headphones."); - return false; - } - } - - counter = 0; - var artistStatus = index.Where(x => x.ArtistID == request.ArtistId).Select(x => x.Status).FirstOrDefault(); - while (artistStatus != "Active") - { - Thread.Sleep(WaitTime); - counter++; - Log.Trace("Artist {2} status {1}. Counter = {0}", counter, artistStatus, request.ArtistName); - index = await Api.GetIndex(Settings.ApiKey, Settings.FullUri); - artistStatus = index.Where(x => x.ArtistID == request.ArtistId).Select(x => x.Status).FirstOrDefault(); - if (counter > CounterMax) - { - Log.Trace("Artist status is still not active. Counter = {0}. Returning false", counter); - Log.Warn($"The artist status for {request.ArtistName} is still not Active. We have waited long enough, seems to be a big delay in headphones."); - return false; - } - } - - var addedArtist = index.FirstOrDefault(x => x.ArtistID == request.ArtistId); - var artistName = addedArtist?.ArtistName ?? string.Empty; - counter = 0; - while (artistName.Contains("Fetch failed")) - { - Thread.Sleep(WaitTime); - await Api.RefreshArtist(Settings.ApiKey, Settings.FullUri, request.ArtistId); - - index = await Api.GetIndex(Settings.ApiKey, Settings.FullUri); - - artistName = index?.FirstOrDefault(x => x.ArtistID == request.ArtistId)?.ArtistName ?? string.Empty; - counter++; - if (counter > CounterMax) - { - Log.Trace("Artist fetch has failed. Counter = {0}. Returning false", counter); - Log.Warn("Artist in headphones fetch has failed, we have tried refreshing the artist but no luck."); - return false; - } - } - - return true; + return artistExists || artistAdd; } private async Task SetAlbumStatus(RequestedModel request) { - var counter = 0; - var setStatus = await Api.QueueAlbum(Settings.ApiKey, Settings.FullUri, request.MusicBrainzId); - - while (!setStatus) - { - Thread.Sleep(WaitTime); - counter++; - Log.Trace("Setting Album status. Counter = {0}", counter); - setStatus = await Api.QueueAlbum(Settings.ApiKey, Settings.FullUri, request.MusicBrainzId); - if (counter > CounterMax) - { - Log.Trace("Album status is still not active. Counter = {0}. Returning false", counter); - Log.Warn("We tried to se the status for the album but headphones didn't want to snatch it."); - return false; - } - } - return true; + bool setStatus = await Api.QueueAlbum(Settings.ApiKey, Settings.FullUri, request.ReleaseId); + return setStatus; } } } \ No newline at end of file From b7bfc2fcc0fe089decf037885b03883ad72d73df Mon Sep 17 00:00:00 2001 From: smcpeck Date: Tue, 28 Feb 2017 20:52:43 -0600 Subject: [PATCH 09/11] Move local user login to be the first thing checked; renamed old Api variable to PlexApi now that Emby is in play. --- Ombi.UI/Modules/UserLoginModule.cs | 38 +++++++++++++----------------- 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/Ombi.UI/Modules/UserLoginModule.cs b/Ombi.UI/Modules/UserLoginModule.cs index 1f9250ad9..cd234ca57 100644 --- a/Ombi.UI/Modules/UserLoginModule.cs +++ b/Ombi.UI/Modules/UserLoginModule.cs @@ -65,7 +65,7 @@ namespace Ombi.UI.Modules AuthService = auth; LandingPageSettings = lp; Analytics = a; - Api = api; + PlexApi = api; PlexSettings = plexSettings; Linker = linker; UserLogins = userLogins; @@ -126,7 +126,7 @@ namespace Ombi.UI.Modules private ISettingsService LandingPageSettings { get; } private ISettingsService PlexSettings { get; } private ISettingsService EmbySettings { get; } - private IPlexApi Api { get; } + private IPlexApi PlexApi { get; } private IEmbyApi EmbyApi { get; } private IResourceLinker Linker { get; } private IAnalytics Analytics { get; } @@ -301,12 +301,18 @@ namespace Ombi.UI.Modules var plexSettings = await PlexSettings.GetSettingsAsync(); var embySettings = await EmbySettings.GetSettingsAsync(); - if (plexSettings.Enable) + // attempt local login first as it has the least amount of overhead + userId = CustomUserMapper.ValidateUser(username, password)?.ToString(); + if (userId != null) + { + authenticated = true; + } + else if (userId == null && plexSettings.Enable) { if (settings.UserAuthentication) // Authenticate with Plex { Log.Debug("Need to auth and also provide pass"); - var signedIn = (PlexAuthentication) Api.SignIn(username, password); + var signedIn = (PlexAuthentication) PlexApi.SignIn(username, password); if (signedIn.user?.authentication_token != null) { Log.Debug("Correct credentials, checking if the user is account owner or in the friends list"); @@ -325,9 +331,9 @@ namespace Ombi.UI.Modules } } } - if (embySettings.Enable) + else if (userId == null && embySettings.Enable) { - if (settings.UserAuthentication) // Authenticate with Plex + if (settings.UserAuthentication) // Authenticate with Emby { Log.Debug("Need to auth and also provide pass"); EmbyUser signedIn = null; @@ -358,16 +364,6 @@ namespace Ombi.UI.Modules } } - if (string.IsNullOrEmpty(userId)) - { - // Local user? - userId = CustomUserMapper.ValidateUser(username, password)?.ToString(); - if (userId != null) - { - authenticated = true; - } - } - if (!authenticated) { return Response.AsJson(new { result = false, message = Resources.UI.UserLogin_IncorrectUserPass }); @@ -440,7 +436,7 @@ namespace Ombi.UI.Modules if (settings.UserAuthentication && settings.UsePassword) // Authenticate with Plex { Log.Debug("Need to auth and also provide pass"); - var signedIn = (PlexAuthentication)Api.SignIn(username, password); + var signedIn = (PlexAuthentication)PlexApi.SignIn(username, password); if (signedIn.user?.authentication_token != null) { Log.Debug("Correct credentials, checking if the user is account owner or in the friends list"); @@ -711,7 +707,7 @@ namespace Ombi.UI.Modules private bool CheckIfUserIsOwner(string authToken, string userName) { - var userAccount = Api.GetAccount(authToken); + var userAccount = PlexApi.GetAccount(authToken); if (userAccount == null) { return false; @@ -721,7 +717,7 @@ namespace Ombi.UI.Modules private string GetOwnerId(string authToken, string userName) { - var userAccount = Api.GetAccount(authToken); + var userAccount = PlexApi.GetAccount(authToken); if (userAccount == null) { return string.Empty; @@ -731,7 +727,7 @@ namespace Ombi.UI.Modules private bool CheckIfUserIsInPlexFriends(string username, string authToken) { - var users = Api.GetUsers(authToken); + var users = PlexApi.GetUsers(authToken); var allUsers = users?.User?.Where(x => !string.IsNullOrEmpty(x.Title)); return allUsers != null && allUsers.Any(x => x.Title.Equals(username, StringComparison.CurrentCultureIgnoreCase)); } @@ -769,7 +765,7 @@ namespace Ombi.UI.Modules private string GetUserIdIsInPlexFriends(string username, string authToken) { - var users = Api.GetUsers(authToken); + var users = PlexApi.GetUsers(authToken); var allUsers = users?.User?.Where(x => !string.IsNullOrEmpty(x.Title)); return allUsers?.Where(x => x.Title.Equals(username, StringComparison.CurrentCultureIgnoreCase)).Select(x => x.Id).FirstOrDefault(); } From f400412568fe69dd07482da6b17c4a4a7d1cda8a Mon Sep 17 00:00:00 2001 From: smcpeck Date: Tue, 28 Feb 2017 20:53:29 -0600 Subject: [PATCH 10/11] Normalized spacing/tabs --- Ombi.UI/Program.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Ombi.UI/Program.cs b/Ombi.UI/Program.cs index b454550c5..2dc1ef5a8 100644 --- a/Ombi.UI/Program.cs +++ b/Ombi.UI/Program.cs @@ -75,7 +75,7 @@ namespace Ombi.UI var s = new Setup(); var cn = s.SetupDb(baseUrl); s.CacheQualityProfiles(); - ConfigureTargets(cn); + ConfigureTargets(cn); SetupLogging(); if (port == -1 || port == 3579) @@ -110,7 +110,7 @@ namespace Ombi.UI { Log.Info("This is not Mono"); Console.WriteLine("Press any key to exit"); - Console.ReadLine(); + Console.ReadLine(); } } } From e46b621a2ddc7e3374d867460cab7aa10e7421e2 Mon Sep 17 00:00:00 2001 From: Shaun McPeck Date: Wed, 1 Mar 2017 00:35:46 -0600 Subject: [PATCH 11/11] Needed to treat a 201 as success, too. + removed some commented out code. --- Ombi.Api/ApiRequest.cs | 36 +++--------------------------------- 1 file changed, 3 insertions(+), 33 deletions(-) diff --git a/Ombi.Api/ApiRequest.cs b/Ombi.Api/ApiRequest.cs index bb4dabb0a..c399eae45 100644 --- a/Ombi.Api/ApiRequest.cs +++ b/Ombi.Api/ApiRequest.cs @@ -59,17 +59,7 @@ namespace Ombi.Api var response = client.Execute(request); Log.Trace($"Request made to {response.ResponseUri} with status code {response.StatusCode}. The response was {response.Content}"); - /** - if (response.ErrorException != null) - { - var message = "Error retrieving response. Check inner details for more info."; - Log.Error(response.ErrorException); - Log.Error(response.ErrorException.InnerException); - throw new ApiRequestException(message, response.ErrorException); - } - **/ - - if (response.StatusCode == HttpStatusCode.OK) + if (response.StatusCode == HttpStatusCode.OK || response.StatusCode == HttpStatusCode.Created) return response.Data; else throw new ApiRequestException($"Got StatusCode={response.StatusCode} for {response.ResponseUri}."); @@ -89,17 +79,7 @@ namespace Ombi.Api var response = client.Execute(request); Log.Trace($"Request made to {response.ResponseUri} with status code {response.StatusCode}. The response was {response.Content}"); - /** - if (response.ErrorException != null) - { - Log.Error(response.ErrorException); - Log.Error(response.ErrorException.InnerException); - var message = "Error retrieving response. Check inner details for more info."; - throw new ApiRequestException(message, response.ErrorException); - } - **/ - - if (response.StatusCode == HttpStatusCode.OK) + if (response.StatusCode == HttpStatusCode.OK || response.StatusCode == HttpStatusCode.Created) return DeserializeXml(response.Content); else throw new ApiRequestException($"Got StatusCode={response.StatusCode} for {response.ResponseUri}."); @@ -112,17 +92,7 @@ namespace Ombi.Api var response = client.Execute(request); Log.Trace($"Request made to {response.ResponseUri} with status code {response.StatusCode}. The response was {response.Content}"); - /** - if (response.ErrorException != null) - { - Log.Error(response.ErrorException); - Log.Error(response.ErrorException.InnerException); - var message = "Error retrieving response. Check inner details for more info."; - throw new ApiRequestException(message, response.ErrorException); - } - **/ - - if (response.StatusCode == HttpStatusCode.OK) + if (response.StatusCode == HttpStatusCode.OK || response.StatusCode == HttpStatusCode.Created) return JsonConvert.DeserializeObject(response.Content, _settings); else throw new ApiRequestException($"Got StatusCode={response.StatusCode} for {response.ResponseUri}.");