@ -15,6 +15,7 @@ namespace ErsatzTV.Infrastructure.Jellyfin;
@@ -15,6 +15,7 @@ namespace ErsatzTV.Infrastructure.Jellyfin;
public class JellyfinApiClient : IJellyfinApiClient
{
private readonly IFallbackMetadataProvider _f allbackMetadataProvider ;
private readonly IHttpClientFactory _ httpClientFactory ;
private readonly IJellyfinPathReplacementService _ jellyfinPathReplacementService ;
private readonly ILogger < JellyfinApiClient > _l ogger ;
private readonly IMemoryCache _ memoryCache ;
@ -23,11 +24,13 @@ public class JellyfinApiClient : IJellyfinApiClient
@@ -23,11 +24,13 @@ public class JellyfinApiClient : IJellyfinApiClient
IMemoryCache memoryCache ,
IJellyfinPathReplacementService jellyfinPathReplacementService ,
IFallbackMetadataProvider fallbackMetadataProvider ,
IHttpClientFactory httpClientFactory ,
ILogger < JellyfinApiClient > logger )
{
_ memoryCache = memoryCache ;
_ jellyfinPathReplacementService = jellyfinPathReplacementService ;
_f allbackMetadataProvider = fallbackMetadataProvider ;
_ httpClientFactory = httpClientFactory ;
_l ogger = logger ;
}
@ -37,7 +40,7 @@ public class JellyfinApiClient : IJellyfinApiClient
@@ -37,7 +40,7 @@ public class JellyfinApiClient : IJellyfinApiClient
{
try
{
IJellyfinApi service = RestService . For < IJellyfinApi > ( address ) ;
IJellyfinApi service = ServiceForAddress ( address ) ;
var cts = new CancellationTokenSource ( ) ;
cts . CancelAfter ( TimeSpan . FromSeconds ( 5 ) ) ;
return await service . GetSystemInformation ( apiKey , cts . Token )
@ -59,7 +62,7 @@ public class JellyfinApiClient : IJellyfinApiClient
@@ -59,7 +62,7 @@ public class JellyfinApiClient : IJellyfinApiClient
{
try
{
IJellyfinApi service = RestService . For < IJellyfinApi > ( address ) ;
IJellyfinApi service = ServiceForAddress ( address ) ;
List < JellyfinLibraryResponse > libraries = await service . GetLibraries ( apiKey ) ;
return libraries
. Map ( Project )
@ -189,7 +192,7 @@ public class JellyfinApiClient : IJellyfinApiClient
@@ -189,7 +192,7 @@ public class JellyfinApiClient : IJellyfinApiClient
{
try
{
IJellyfinApi service = RestService . For < IJellyfinApi > ( address ) ;
IJellyfinApi service = ServiceForAddress ( address ) ;
JellyfinPlaybackInfoResponse playbackInfo = await service . GetPlaybackInfo ( apiKey , itemId ) ;
Option < MediaVersion > maybeVersion = ProjectToMediaVersion ( playbackInfo ) ;
return maybeVersion . ToEither ( ( ) = > BaseError . New ( "Unable to locate Jellyfin statistics" ) ) ;
@ -209,7 +212,7 @@ public class JellyfinApiClient : IJellyfinApiClient
@@ -209,7 +212,7 @@ public class JellyfinApiClient : IJellyfinApiClient
{
try
{
IJellyfinApi service = RestService . For < IJellyfinApi > ( address ) ;
IJellyfinApi service = ServiceForAddress ( address ) ;
JellyfinLibraryItemsResponse itemsResponse = await service . GetShowLibraryItems (
apiKey ,
parentId : library . ItemId ,
@ -240,7 +243,7 @@ public class JellyfinApiClient : IJellyfinApiClient
@@ -240,7 +243,7 @@ public class JellyfinApiClient : IJellyfinApiClient
{
try
{
IJellyfinApi service = RestService . For < IJellyfinApi > ( address ) ;
IJellyfinApi service = ServiceForAddress ( address ) ;
JellyfinSearchHintsResponse searchResponse = await service . SearchHints (
apiKey ,
showTitle ,
@ -281,7 +284,7 @@ public class JellyfinApiClient : IJellyfinApiClient
@@ -281,7 +284,7 @@ public class JellyfinApiClient : IJellyfinApiClient
}
}
private static async IAsyncEnumerable < Tuple < TItem , int > > GetPagedLibraryItems < TItem > (
private async IAsyncEnumerable < Tuple < TItem , int > > GetPagedLibraryItems < TItem > (
string address ,
Option < JellyfinLibrary > maybeLibrary ,
int mediaSourceId ,
@ -289,7 +292,7 @@ public class JellyfinApiClient : IJellyfinApiClient
@@ -289,7 +292,7 @@ public class JellyfinApiClient : IJellyfinApiClient
Func < IJellyfinApi , string , int , int , Task < JellyfinLibraryItemsResponse > > getItems ,
Func < Option < JellyfinLibrary > , JellyfinLibraryItemResponse , Option < TItem > > mapper )
{
IJellyfinApi service = RestService . For < IJellyfinApi > ( address ) ;
IJellyfinApi service = ServiceForAddress ( address ) ;
const int PAGE_SIZE = 1 0 ;
@ -1043,4 +1046,11 @@ public class JellyfinApiClient : IJellyfinApiClient
@@ -1043,4 +1046,11 @@ public class JellyfinApiClient : IJellyfinApiClient
return version ;
} ) ;
}
private IJellyfinApi ServiceForAddress ( string address )
{
var client = _ httpClientFactory . CreateClient ( "RefitCustomClient" ) ;
client . BaseAddress = new Uri ( address ) ;
return RestService . For < IJellyfinApi > ( client ) ;
}
}