Skip to content

Commit

Permalink
Finished #208 and #202
Browse files Browse the repository at this point in the history
  • Loading branch information
TidusJar committed May 13, 2016
1 parent adeeb78 commit e993788
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
<Reference Include="RestSharp, Version=105.2.3.0, Culture=neutral, PublicKeyToken=null">
<HintPath>..\packages\RestSharp.105.2.3\lib\net452\RestSharp.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="IApiRequest.cs" />
Expand Down
31 changes: 27 additions & 4 deletions PlexRequests.Api/CouchPotatoApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,13 @@ public CouchPotatoApi()
request.AddUrlSegment("imdbid", imdbid);
request.AddUrlSegment("title", title);

var obj = Api.ExecuteJson<JObject>(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<JObject>(request, baseUrl));
Log.Trace("CP movie Add result count {0}", obj.Count);

if (obj.Count > 0)
Expand Down Expand Up @@ -99,8 +105,13 @@ public CouchPotatoStatus GetStatus(Uri url, string apiKey)
};

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<CouchPotatoStatus>(request,url);
return (CouchPotatoStatus)policy.Execute( () => Api.Execute<CouchPotatoStatus>(request,url));
}

public CouchPotatoProfiles GetProfiles(Uri url, string apiKey)
Expand All @@ -114,7 +125,13 @@ public CouchPotatoProfiles GetProfiles(Uri url, string apiKey)

request.AddUrlSegment("apikey", apiKey);

return Api.Execute<CouchPotatoProfiles>(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<CouchPotatoProfiles>(request,url));
}

public CouchPotatoMovies GetMovies(Uri baseUrl, string apiKey, string[] status)
Expand All @@ -128,7 +145,13 @@ public CouchPotatoMovies GetMovies(Uri baseUrl, string apiKey, string[] status)
request.AddUrlSegment("status", string.Join(",", status));
try
{
return Api.Execute<CouchPotatoMovies>(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<CouchPotatoMovies>(request,baseUrl));
}
catch (Exception e) // Request error is already logged in the ApiRequest class
{
Expand Down
60 changes: 47 additions & 13 deletions PlexRequests.Api/SickrageApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -82,9 +83,14 @@ public async Task<SickRageTvAdd> AddSeries(int tvdbId, int seasonCount, int[] se
request.AddQueryParameter("initial", quality);
}

Log.Trace("Entering `Execute<SickRageTvAdd>`");
var obj = Api.Execute<SickRageTvAdd>(request, baseUrl);
Log.Trace("Exiting `Execute<SickRageTvAdd>`");
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<SickRageTvAdd>(request, baseUrl));
Log.Trace("obj Result:");
Log.Trace(obj.DumpJson());

Expand All @@ -95,7 +101,6 @@ public async Task<SickRageTvAdd> AddSeries(int tvdbId, int seasonCount, int[] se

var seasonIncrement = 0;
var seasonList = new SickRageSeasonList();
Log.Trace("while (seasonIncrement < seasonCount) where seasonCount = {0}", seasonCount);
try
{
while (seasonIncrement < seasonCount)
Expand Down Expand Up @@ -132,7 +137,8 @@ public async Task<SickRageTvAdd> AddSeries(int tvdbId, int seasonCount, int[] se
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());
}
Expand All @@ -157,8 +163,17 @@ public SickRagePing Ping(string apiKey, Uri baseUrl)
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<SickRagePing>(request, baseUrl);
var obj = policy.Execute( () => Api.ExecuteJson<SickRagePing>(request, baseUrl));

return obj;
}
Expand All @@ -176,9 +191,14 @@ public SickRageSeasonList VerifyShowHasLoaded(int tvdbId, string apiKey, Uri bas

try
{
Log.Trace("Entering `ExecuteJson<SickRageSeasonList>`");
var obj = Api.ExecuteJson<SickRageSeasonList>(request, baseUrl);
Log.Trace("Exited `ExecuteJson<SickRageSeasonList>`");
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<SickRageSeasonList>(request, baseUrl));
return obj;
}
catch (Exception e)
Expand All @@ -203,10 +223,16 @@ public async Task<SickRageTvAdd> AddSeason(int tvdbId, int season, string apiKey
await Task.Run(() => Thread.Sleep(2000));
return await Task.Run(() =>
{
Log.Trace("Entering `Execute<SickRageTvAdd>` in a new `Task<T>`");
var result = Api.Execute<SickRageTvAdd>(request, baseUrl);

Log.Trace("Exiting `Execute<SickRageTvAdd>` and yeilding `Task<T>` 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<SickRageTvAdd>(request, baseUrl));

return result;
}).ConfigureAwait(false);
}
Expand All @@ -225,7 +251,15 @@ public async Task<SickrageShows> GetShows(string apiKey, Uri baseUrl)
{
try
{
return Api.Execute<SickrageShows>(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<SickrageShows>(request, baseUrl));

}
catch (ApiRequestException)
{
Expand Down
34 changes: 28 additions & 6 deletions PlexRequests.Api/SonarrApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,13 @@ public List<SonarrProfile> GetProfiles(string apiKey, Uri baseUrl)
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<List<SonarrProfile>>(request, baseUrl);
var obj = policy.Execute(() => Api.ExecuteJson<List<SonarrProfile>>(request, baseUrl));

return obj;
}
Expand Down Expand Up @@ -102,7 +107,13 @@ public SonarrAddSeries AddSeries(int tvdbId, string title, int qualityId, bool s
SonarrAddSeries result;
try
{
result = Api.ExecuteJson<SonarrAddSeries>(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<SonarrAddSeries>(request, baseUrl));
}
catch (JsonSerializationException jse)
{
Expand All @@ -121,7 +132,13 @@ public SystemStatus SystemStatus(string apiKey, Uri baseUrl)
var request = new RestRequest { Resource = "/api/system/status", Method = Method.GET };
request.AddHeader("X-Api-Key", apiKey);

var obj = Api.ExecuteJson<SystemStatus>(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<SystemStatus>(request, baseUrl));

return obj;
}
Expand All @@ -132,9 +149,14 @@ public List<Series> GetSeries(string apiKey, Uri baseUrl)
request.AddHeader("X-Api-Key", apiKey);
try
{

return Api.Execute<List<Series>>(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<List<Series>>(request, baseUrl));
}
catch (ApiRequestException)
{
Log.Error("There has been an API exception when getting the Sonarr Series");
Expand Down

0 comments on commit e993788

Please sign in to comment.