From e9937884adb4d7aff48e154684ae3f0f5da162e0 Mon Sep 17 00:00:00 2001 From: TidusJar Date: Thu, 12 May 2016 23:06:17 -0400 Subject: [PATCH] Finished #208 and #202 --- .../PlexRequests.Api.Interfaces.csproj | 3 - PlexRequests.Api/CouchPotatoApi.cs | 31 ++++++++-- PlexRequests.Api/SickrageApi.cs | 60 +++++++++++++++---- PlexRequests.Api/SonarrApi.cs | 34 +++++++++-- 4 files changed, 102 insertions(+), 26 deletions(-) diff --git a/PlexRequests.Api.Interfaces/PlexRequests.Api.Interfaces.csproj b/PlexRequests.Api.Interfaces/PlexRequests.Api.Interfaces.csproj index 033b18133..9e7c8e613 100644 --- a/PlexRequests.Api.Interfaces/PlexRequests.Api.Interfaces.csproj +++ b/PlexRequests.Api.Interfaces/PlexRequests.Api.Interfaces.csproj @@ -39,9 +39,6 @@ - - ..\packages\RestSharp.105.2.3\lib\net452\RestSharp.dll - diff --git a/PlexRequests.Api/CouchPotatoApi.cs b/PlexRequests.Api/CouchPotatoApi.cs index 96d245cf9..e48cfd84d 100644 --- a/PlexRequests.Api/CouchPotatoApi.cs +++ b/PlexRequests.Api/CouchPotatoApi.cs @@ -62,7 +62,13 @@ namespace PlexRequests.Api request.AddUrlSegment("imdbid", imdbid); request.AddUrlSegment("title", title); - var obj = Api.ExecuteJson(request, baseUrl); + var policy = RetryHandler.RetryAndWaitPolicy (new TimeSpan[] { + TimeSpan.FromSeconds (2), + TimeSpan.FromSeconds(5), + TimeSpan.FromSeconds(10) + }, (exception, timespan) => Log.Error (exception, "Exception when calling AddMovie for CP, Retrying {0}", timespan)); + + var obj = (JObject)policy.Execute( () => Api.ExecuteJson(request, baseUrl)); Log.Trace("CP movie Add result count {0}", obj.Count); if (obj.Count > 0) @@ -99,8 +105,13 @@ namespace PlexRequests.Api }; request.AddUrlSegment("apikey", apiKey); + 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 CP, Retrying {0}", timespan)); - return Api.Execute(request,url); + return (CouchPotatoStatus)policy.Execute( () => Api.Execute(request,url)); } public CouchPotatoProfiles GetProfiles(Uri url, string apiKey) @@ -114,7 +125,13 @@ namespace PlexRequests.Api request.AddUrlSegment("apikey", apiKey); - return Api.Execute(request, url); + var policy = RetryHandler.RetryAndWaitPolicy (new TimeSpan[] { + TimeSpan.FromSeconds (2), + 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(request,url)); } public CouchPotatoMovies GetMovies(Uri baseUrl, string apiKey, string[] status) @@ -128,7 +145,13 @@ namespace PlexRequests.Api request.AddUrlSegment("status", string.Join(",", status)); try { - return Api.Execute(request, baseUrl); + var policy = RetryHandler.RetryAndWaitPolicy (new TimeSpan[] { + TimeSpan.FromSeconds (5), + TimeSpan.FromSeconds(10), + TimeSpan.FromSeconds(30) + }, (exception, timespan) => Log.Error (exception, "Exception when calling GetMovies for CP, Retrying {0}", timespan)); + + return (CouchPotatoMovies)policy.Execute( () => Api.Execute(request,baseUrl)); } catch (Exception e) // Request error is already logged in the ApiRequest class { diff --git a/PlexRequests.Api/SickrageApi.cs b/PlexRequests.Api/SickrageApi.cs index e600d5aec..72dc9d468 100644 --- a/PlexRequests.Api/SickrageApi.cs +++ b/PlexRequests.Api/SickrageApi.cs @@ -24,6 +24,7 @@ // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // ************************************************************************/ +using Polly; #endregion @@ -82,9 +83,14 @@ namespace PlexRequests.Api request.AddQueryParameter("initial", quality); } - Log.Trace("Entering `Execute`"); - var obj = Api.Execute(request, baseUrl); - Log.Trace("Exiting `Execute`"); + var policy = RetryHandler.RetryAndWaitPolicy (new 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(request, baseUrl)); Log.Trace("obj Result:"); Log.Trace(obj.DumpJson()); @@ -95,7 +101,6 @@ namespace PlexRequests.Api var seasonIncrement = 0; var seasonList = new SickRageSeasonList(); - Log.Trace("while (seasonIncrement < seasonCount) where seasonCount = {0}", seasonCount); try { while (seasonIncrement < seasonCount) @@ -132,7 +137,8 @@ namespace PlexRequests.Api foreach (var s in seasons) { Log.Trace("Adding season {0}", s); - var result = await AddSeason(tvdbId, s, apiKey, baseUrl); + + var result = await AddSeason(tvdbId, s, apiKey, baseUrl); Log.Trace("SickRage adding season results: "); Log.Trace(result.DumpJson()); } @@ -157,8 +163,17 @@ namespace PlexRequests.Api Method = Method.GET }; + + var policy = RetryHandler.RetryAndWaitPolicy (new TimeSpan[] { + TimeSpan.FromSeconds (2), + TimeSpan.FromSeconds(5), + TimeSpan.FromSeconds(10) + }, (exception, timespan) => Log.Error (exception, "Exception when calling Ping for SR, Retrying {0}", timespan)); + + + request.AddUrlSegment("apiKey", apiKey); - var obj = Api.ExecuteJson(request, baseUrl); + var obj = policy.Execute( () => Api.ExecuteJson(request, baseUrl)); return obj; } @@ -176,9 +191,14 @@ namespace PlexRequests.Api try { - Log.Trace("Entering `ExecuteJson`"); - var obj = Api.ExecuteJson(request, baseUrl); - Log.Trace("Exited `ExecuteJson`"); + var policy = RetryHandler.RetryAndWaitPolicy (new TimeSpan[] { + TimeSpan.FromSeconds (2), + 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(request, baseUrl)); return obj; } catch (Exception e) @@ -203,10 +223,16 @@ namespace PlexRequests.Api await Task.Run(() => Thread.Sleep(2000)); return await Task.Run(() => { - Log.Trace("Entering `Execute` in a new `Task`"); - var result = Api.Execute(request, baseUrl); - Log.Trace("Exiting `Execute` and yeilding `Task` result"); + var policy = RetryHandler.RetryAndWaitPolicy (new TimeSpan[] { + TimeSpan.FromSeconds (2), + 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(request, baseUrl)); + return result; }).ConfigureAwait(false); } @@ -225,7 +251,15 @@ namespace PlexRequests.Api { try { - return Api.Execute(request, baseUrl); + var policy = RetryHandler.RetryAndWaitPolicy (new TimeSpan[] { + TimeSpan.FromSeconds (5), + TimeSpan.FromSeconds(10), + TimeSpan.FromSeconds(30) + }, (exception, timespan) => Log.Error (exception, "Exception when calling GetShows for SR, Retrying {0}", timespan)); + + + return policy.Execute(() => Api.Execute(request, baseUrl)); + } catch (ApiRequestException) { diff --git a/PlexRequests.Api/SonarrApi.cs b/PlexRequests.Api/SonarrApi.cs index 3d160187a..7d26a8d5e 100644 --- a/PlexRequests.Api/SonarrApi.cs +++ b/PlexRequests.Api/SonarrApi.cs @@ -56,8 +56,13 @@ namespace PlexRequests.Api var request = new RestRequest { Resource = "/api/profile", Method = Method.GET }; request.AddHeader("X-Api-Key", apiKey); + var policy = RetryHandler.RetryAndWaitPolicy (new TimeSpan[] { + TimeSpan.FromSeconds (2), + TimeSpan.FromSeconds(5), + TimeSpan.FromSeconds(10) + }, (exception, timespan) => Log.Error (exception, "Exception when calling GetProfiles for Sonarr, Retrying {0}", timespan)); - var obj = Api.ExecuteJson>(request, baseUrl); + var obj = policy.Execute(() => Api.ExecuteJson>(request, baseUrl)); return obj; } @@ -102,7 +107,13 @@ namespace PlexRequests.Api SonarrAddSeries result; try { - result = Api.ExecuteJson(request, baseUrl); + var policy = RetryHandler.RetryAndWaitPolicy (new TimeSpan[] { + TimeSpan.FromSeconds (2), + TimeSpan.FromSeconds(5), + TimeSpan.FromSeconds(10) + }, (exception, timespan) => Log.Error (exception, "Exception when calling AddSeries for Sonarr, Retrying {0}", timespan)); + + result = policy.Execute(() => Api.ExecuteJson(request, baseUrl)); } catch (JsonSerializationException jse) { @@ -121,7 +132,13 @@ namespace PlexRequests.Api var request = new RestRequest { Resource = "/api/system/status", Method = Method.GET }; request.AddHeader("X-Api-Key", apiKey); - var obj = Api.ExecuteJson(request, baseUrl); + var policy = RetryHandler.RetryAndWaitPolicy (new TimeSpan[] { + TimeSpan.FromSeconds (2), + TimeSpan.FromSeconds(5), + TimeSpan.FromSeconds(10) + }, (exception, timespan) => Log.Error (exception, "Exception when calling SystemStatus for Sonarr, Retrying {0}", timespan)); + + var obj = policy.Execute(() => Api.ExecuteJson(request, baseUrl)); return obj; } @@ -132,9 +149,14 @@ namespace PlexRequests.Api request.AddHeader("X-Api-Key", apiKey); try { - - return Api.Execute>(request, baseUrl); - } + var policy = RetryHandler.RetryAndWaitPolicy (new TimeSpan[] { + TimeSpan.FromSeconds (5), + TimeSpan.FromSeconds(10), + TimeSpan.FromSeconds(30) + }, (exception, timespan) => Log.Error (exception, "Exception when calling GetSeries for Sonarr, Retrying {0}", timespan)); + + return policy.Execute(() => Api.ExecuteJson>(request, baseUrl)); + } catch (ApiRequestException) { Log.Error("There has been an API exception when getting the Sonarr Series");