@ -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 )
{
{