Skip to content

Commit

Permalink
Added a retry handler into the solution. We can now retry failed api …
Browse files Browse the repository at this point in the history
…requests.

this should resolve #202 and start on #208
  • Loading branch information
TidusJar committed May 13, 2016
1 parent f9205fc commit adeeb78
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 8 deletions.
58 changes: 51 additions & 7 deletions PlexRequests.Api/PlexApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
// 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
using System;

Expand Down Expand Up @@ -67,7 +70,14 @@ public PlexAuthentication SignIn(string username, string password)
request.AddJsonBody(userModel);

var api = new ApiRequest();
return api.Execute<PlexAuthentication>(request, new Uri("https://plex.tv/users/sign_in.json"));

var policy = RetryHandler.RetryAndWaitPolicy (new TimeSpan[] {
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)
Expand All @@ -80,7 +90,13 @@ public PlexFriends GetUsers(string authToken)
AddHeaders(ref request, authToken);

var api = new ApiRequest();
var users = api.ExecuteXml<PlexFriends>(request, new Uri("https://plex.tv/pms/friends/all"));
var policy = RetryHandler.RetryAndWaitPolicy (new TimeSpan[] {
TimeSpan.FromSeconds (2),
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;
}
Expand All @@ -104,7 +120,13 @@ public PlexSearch SearchContent(string authToken, string searchTerm, Uri plexFul
AddHeaders(ref request, authToken);

var api = new ApiRequest();
var search = api.ExecuteXml<PlexSearch>(request, plexFullHost);
var policy = RetryHandler.RetryAndWaitPolicy (new TimeSpan[] {
TimeSpan.FromSeconds (2),
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;
}
Expand All @@ -117,9 +139,14 @@ public PlexStatus GetStatus(string authToken, Uri uri)
};

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 = api.ExecuteXml<PlexStatus>(request, uri);
var users = (PlexStatus)policy.Execute(() => api.ExecuteXml<PlexStatus>(request, uri));

return users;
}
Expand All @@ -133,8 +160,15 @@ public PlexAccount GetAccount(string 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 GetAccount for Plex, Retrying: {0}", timespan));


var api = new ApiRequest();
var account = api.ExecuteXml<PlexAccount>(request, new Uri("https://plex.tv/users/account"));
var account = (PlexAccount)policy.Execute(() => api.ExecuteXml<PlexAccount>(request, new Uri("https://plex.tv/users/account")));

return account;
}
Expand All @@ -152,8 +186,13 @@ public PlexLibraries GetLibrarySections(string authToken, Uri plexFullHost)
var api = new ApiRequest();
try
{
var policy = RetryHandler.RetryAndWaitPolicy (new TimeSpan[] {
TimeSpan.FromSeconds (5),
TimeSpan.FromSeconds(10),
TimeSpan.FromSeconds(30)
}, (exception, timespan) => Log.Error (exception, "Exception when calling GetLibrarySections for Plex, Retrying {0}", timespan));

return api.ExecuteXml<PlexLibraries>(request, plexFullHost);
return (PlexLibraries)policy.Execute(() => api.ExecuteXml<PlexLibraries>(request, plexFullHost));
}
catch (ApiRequestException)
{
Expand All @@ -176,8 +215,13 @@ public PlexSearch GetLibrary(string authToken, Uri plexFullHost, string libraryI
var api = new ApiRequest();
try
{
var policy = RetryHandler.RetryAndWaitPolicy (new TimeSpan[] {
TimeSpan.FromSeconds (5),
TimeSpan.FromSeconds(10),
TimeSpan.FromSeconds(30)
}, (exception, timespan) => Log.Error (exception, "Exception when calling GetLibrary for Plex, Retrying {0}", timespan));

return api.ExecuteXml<PlexSearch>(request, plexFullHost);
return (PlexSearch)policy.Execute(() => api.ExecuteXml<PlexSearch>(request, plexFullHost));
}
catch (ApiRequestException)
{
Expand Down
4 changes: 4 additions & 0 deletions PlexRequests.Api/PlexRequests.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@
<Reference Include="TMDbLib, Version=0.9.0.0, Culture=neutral, PublicKeyToken=null">
<HintPath>..\packages\TMDbLib.0.9.0.0-alpha\lib\net45\TMDbLib.dll</HintPath>
</Reference>
<Reference Include="Polly">
<HintPath>..\packages\Polly-Signed.4.2.0\lib\net45\Polly.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="ApiRequest.cs" />
Expand All @@ -81,6 +84,7 @@
<Compile Include="TheTvDbApi.cs" />
<Compile Include="TvMazeApi.cs" />
<Compile Include="TvMazeBase.cs" />
<Compile Include="RetryHandler.cs" />
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
Expand Down
34 changes: 34 additions & 0 deletions PlexRequests.Api/RetryHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using Polly.Retry;
using Polly;

namespace PlexRequests.Api
{
public static class RetryHandler
{
public static RetryPolicy RetryAndWaitPolicy(TimeSpan[] TimeSpan, Action action)
{
var policy = Policy.Handle<Exception> ()
.WaitAndRetry(TimeSpan, (exception, timeSpan) => action());

return policy;
}

public static RetryPolicy RetryAndWaitPolicy(TimeSpan[] TimeSpan)
{
var policy = Policy.Handle<Exception> ()
.WaitAndRetry(TimeSpan);

return policy;
}

public static RetryPolicy RetryAndWaitPolicy(TimeSpan[] TimeSpan, Action<Exception, TimeSpan> action)
{
var policy = Policy.Handle<Exception> ()
.WaitAndRetry(TimeSpan, (exception, timeSpan) => action(exception, timeSpan));

return policy;
}
}
}

1 change: 1 addition & 0 deletions PlexRequests.Api/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<package id="Nancy" version="1.4.3" targetFramework="net452" />
<package id="Newtonsoft.Json" version="8.0.2" targetFramework="net452" />
<package id="NLog" version="4.2.3" targetFramework="net452" />
<package id="Polly-Signed" version="4.2.0" targetFramework="net45" />
<package id="RestSharp" version="105.2.3" targetFramework="net452" requireReinstallation="True" />
<package id="TMDbLib" version="0.9.0.0-alpha" targetFramework="net452" />
</packages>
1 change: 0 additions & 1 deletion PlexRequests.UI/Validators/SonarrValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public SonarrValidator()
RuleFor(request => request.Ip).NotEmpty().WithMessage("You must specify a IP/Host name.");
RuleFor(request => request.Port).NotEmpty().WithMessage("You must specify a Port.");
RuleFor(request => request.QualityProfile).NotEmpty().WithMessage("You must specify a Quality Profile.");
RuleFor(request => request.RootPath).NotEmpty().WithMessage("You must specify a Root Path.");
}
}
}

0 comments on commit adeeb78

Please sign in to comment.