2012-07-31 18:29:07 +02:00
using System ;
using System.Collections.Generic ;
using System.IO ;
2012-08-03 17:54:05 +02:00
using System.Linq ;
2012-07-31 18:29:07 +02:00
using System.Threading.Tasks ;
using MediaBrowser.Model.Configuration ;
2012-08-17 15:16:50 +02:00
using MediaBrowser.Model.DTO ;
2012-07-31 18:29:07 +02:00
using MediaBrowser.Model.Entities ;
using MediaBrowser.Model.Users ;
namespace MediaBrowser.ApiInteraction
{
2012-08-15 15:20:29 +02:00
public class ApiClient : IDisposable
2012-07-31 18:29:07 +02:00
{
2012-08-15 15:20:29 +02:00
/// <summary>
/// Gets or sets the server host name (myserver or 192.168.x.x)
/// </summary>
public string ServerHostName { get ; set ; }
2012-07-31 18:29:07 +02:00
2012-08-15 15:20:29 +02:00
/// <summary>
/// Gets or sets the port number used by the API
/// </summary>
public int ServerApiPort { get ; set ; }
2012-07-31 18:29:07 +02:00
2012-08-15 15:20:29 +02:00
/// <summary>
/// Gets the current api url based on hostname and port.
/// </summary>
protected string ApiUrl
2012-08-01 03:53:40 +02:00
{
2012-08-15 15:20:29 +02:00
get
{
return string . Format ( "http://{0}:{1}/mediabrowser/api" , ServerHostName , ServerApiPort ) ;
}
2012-08-01 03:53:40 +02:00
}
2012-08-15 15:20:29 +02:00
public IHttpClient HttpClient { get ; set ; }
public IJsonSerializer JsonSerializer { get ; set ; }
2012-07-31 18:29:07 +02:00
/// <summary>
/// Gets an image url that can be used to download an image from the api
/// </summary>
/// <param name="itemId">The Id of the item</param>
/// <param name="imageType">The type of image requested</param>
/// <param name="imageIndex">The image index, if there are multiple. Currently only applies to backdrops. Supply null or 0 for first backdrop.</param>
/// <param name="width">Use if a fixed width is required. Aspect ratio will be preserved.</param>
/// <param name="height">Use if a fixed height is required. Aspect ratio will be preserved.</param>
/// <param name="maxWidth">Use if a max width is required. Aspect ratio will be preserved.</param>
/// <param name="maxHeight">Use if a max height is required. Aspect ratio will be preserved.</param>
/// <param name="quality">Quality level, from 0-100. Currently only applies to JPG. The default value should suffice.</param>
2012-08-06 15:21:26 +02:00
public string GetImageUrl ( Guid itemId , ImageType imageType , int? imageIndex = null , int? width = null , int? height = null , int? maxWidth = null , int? maxHeight = null , int? quality = null )
2012-07-31 18:29:07 +02:00
{
string url = ApiUrl + "/image" ;
url + = "?id=" + itemId . ToString ( ) ;
url + = "&type=" + imageType . ToString ( ) ;
if ( imageIndex . HasValue )
{
url + = "&index=" + imageIndex ;
}
if ( width . HasValue )
{
url + = "&width=" + width ;
}
if ( height . HasValue )
{
url + = "&height=" + height ;
}
if ( maxWidth . HasValue )
{
url + = "&maxWidth=" + maxWidth ;
}
if ( maxHeight . HasValue )
{
url + = "&maxHeight=" + maxHeight ;
}
if ( quality . HasValue )
{
url + = "&quality=" + quality ;
}
return url ;
}
2012-08-03 17:54:05 +02:00
/// <summary>
/// This is a helper to get a list of backdrop url's from a given ApiBaseItemWrapper. If the actual item does not have any backdrops it will return backdrops from the first parent that does.
/// </summary>
/// <param name="itemWrapper">A given item.</param>
/// <param name="width">Use if a fixed width is required. Aspect ratio will be preserved.</param>
/// <param name="height">Use if a fixed height is required. Aspect ratio will be preserved.</param>
/// <param name="maxWidth">Use if a max width is required. Aspect ratio will be preserved.</param>
/// <param name="maxHeight">Use if a max height is required. Aspect ratio will be preserved.</param>
/// <param name="quality">Quality level, from 0-100. Currently only applies to JPG. The default value should suffice.</param>
2012-08-17 15:16:50 +02:00
public IEnumerable < string > GetBackdropImageUrls ( BaseItemWrapper < ApiBaseItem > itemWrapper , int? width = null , int? height = null , int? maxWidth = null , int? maxHeight = null , int? quality = null )
2012-08-03 17:54:05 +02:00
{
Guid ? backdropItemId = null ;
int backdropCount = 0 ;
if ( itemWrapper . Item . BackdropImagePaths = = null | | ! itemWrapper . Item . BackdropImagePaths . Any ( ) )
{
backdropItemId = itemWrapper . ParentBackdropItemId ;
backdropCount = itemWrapper . ParentBackdropCount ? ? 0 ;
}
else
{
backdropItemId = itemWrapper . Item . Id ;
backdropCount = itemWrapper . Item . BackdropImagePaths . Count ( ) ;
}
if ( backdropItemId = = null )
{
return new string [ ] { } ;
}
List < string > files = new List < string > ( ) ;
for ( int i = 0 ; i < backdropCount ; i + + )
{
files . Add ( GetImageUrl ( backdropItemId . Value , ImageType . Backdrop , i , width , height , maxWidth , maxHeight , quality ) ) ;
}
return files ;
}
/// <summary>
/// This is a helper to get the logo image url from a given ApiBaseItemWrapper. If the actual item does not have a logo, it will return the logo from the first parent that does, or null.
/// </summary>
/// <param name="itemWrapper">A given item.</param>
/// <param name="width">Use if a fixed width is required. Aspect ratio will be preserved.</param>
/// <param name="height">Use if a fixed height is required. Aspect ratio will be preserved.</param>
/// <param name="maxWidth">Use if a max width is required. Aspect ratio will be preserved.</param>
/// <param name="maxHeight">Use if a max height is required. Aspect ratio will be preserved.</param>
/// <param name="quality">Quality level, from 0-100. Currently only applies to JPG. The default value should suffice.</param>
2012-08-17 15:16:50 +02:00
public string GetLogoImageUrl ( BaseItemWrapper < ApiBaseItem > itemWrapper , int? width = null , int? height = null , int? maxWidth = null , int? maxHeight = null , int? quality = null )
2012-08-03 17:54:05 +02:00
{
Guid ? logoItemId = ! string . IsNullOrEmpty ( itemWrapper . Item . LogoImagePath ) ? itemWrapper . Item . Id : itemWrapper . ParentLogoItemId ;
if ( logoItemId . HasValue )
{
return GetImageUrl ( logoItemId . Value , ImageType . Logo , null , width , height , maxWidth , maxHeight , quality ) ;
}
return null ;
}
2012-08-06 15:21:26 +02:00
2012-07-31 18:29:07 +02:00
/// <summary>
/// Gets an image stream based on a url
/// </summary>
2012-07-31 18:30:38 +02:00
public async Task < Stream > GetImageStreamAsync ( string url )
2012-07-31 18:29:07 +02:00
{
2012-08-01 03:53:40 +02:00
return await HttpClient . GetStreamAsync ( url ) ;
2012-07-31 18:29:07 +02:00
}
/// <summary>
/// Gets a BaseItem
/// </summary>
2012-08-17 15:16:50 +02:00
public async Task < BaseItemWrapper < ApiBaseItem > > GetItemAsync ( Guid id , Guid userId )
2012-07-31 18:29:07 +02:00
{
string url = ApiUrl + "/item?userId=" + userId . ToString ( ) ;
if ( id ! = Guid . Empty )
{
url + = "&id=" + id . ToString ( ) ;
}
using ( Stream stream = await HttpClient . GetStreamAsync ( url ) )
{
2012-08-17 15:16:50 +02:00
return JsonSerializer . DeserializeFromStream < BaseItemWrapper < ApiBaseItem > > ( stream ) ;
2012-07-31 18:29:07 +02:00
}
}
/// <summary>
/// Gets all Users
/// </summary>
public async Task < IEnumerable < User > > GetAllUsersAsync ( )
{
string url = ApiUrl + "/users" ;
using ( Stream stream = await HttpClient . GetStreamAsync ( url ) )
{
2012-08-01 03:53:40 +02:00
return JsonSerializer . DeserializeFromStream < IEnumerable < User > > ( stream ) ;
2012-07-31 18:29:07 +02:00
}
}
/// <summary>
/// Gets all Genres
/// </summary>
public async Task < IEnumerable < CategoryInfo < Genre > > > GetAllGenresAsync ( Guid userId )
{
string url = ApiUrl + "/genres?userId=" + userId . ToString ( ) ;
using ( Stream stream = await HttpClient . GetStreamAsync ( url ) )
{
2012-08-01 11:25:13 +02:00
return JsonSerializer . DeserializeFromStream < IEnumerable < CategoryInfo < Genre > > > ( stream ) ;
2012-07-31 18:29:07 +02:00
}
}
2012-08-14 18:06:46 +02:00
/// <summary>
/// Gets all Years
/// </summary>
public async Task < IEnumerable < CategoryInfo < Year > > > GetAllYearsAsync ( Guid userId )
{
string url = ApiUrl + "/years?userId=" + userId . ToString ( ) ;
using ( Stream stream = await HttpClient . GetStreamAsync ( url ) )
{
return JsonSerializer . DeserializeFromStream < IEnumerable < CategoryInfo < Year > > > ( stream ) ;
}
}
/// <summary>
2012-08-14 21:06:25 +02:00
/// Gets all items that contain a given Year
2012-08-14 18:06:46 +02:00
/// </summary>
2012-08-17 15:16:50 +02:00
public async Task < IEnumerable < BaseItemWrapper < ApiBaseItem > > > GetItemsWithYearAsync ( string name , Guid userId )
2012-08-14 18:06:46 +02:00
{
string url = ApiUrl + "/itemswithyear?userId=" + userId . ToString ( ) + "&name=" + name ;
using ( Stream stream = await HttpClient . GetStreamAsync ( url ) )
{
2012-08-17 15:16:50 +02:00
return JsonSerializer . DeserializeFromStream < IEnumerable < BaseItemWrapper < ApiBaseItem > > > ( stream ) ;
2012-08-14 18:06:46 +02:00
}
}
2012-07-31 18:29:07 +02:00
/// <summary>
2012-08-14 21:06:25 +02:00
/// Gets all items that contain a given Genre
2012-07-31 18:29:07 +02:00
/// </summary>
2012-08-17 15:16:50 +02:00
public async Task < IEnumerable < BaseItemWrapper < ApiBaseItem > > > GetItemsWithGenreAsync ( string name , Guid userId )
2012-07-31 18:29:07 +02:00
{
2012-08-14 18:06:46 +02:00
string url = ApiUrl + "/itemswithgenre?userId=" + userId . ToString ( ) + "&name=" + name ;
2012-07-31 18:29:07 +02:00
using ( Stream stream = await HttpClient . GetStreamAsync ( url ) )
{
2012-08-17 15:16:50 +02:00
return JsonSerializer . DeserializeFromStream < IEnumerable < BaseItemWrapper < ApiBaseItem > > > ( stream ) ;
2012-07-31 18:29:07 +02:00
}
}
2012-08-14 21:06:25 +02:00
/// <summary>
/// Gets all items that contain a given Person
/// </summary>
2012-08-17 15:16:50 +02:00
public async Task < IEnumerable < BaseItemWrapper < ApiBaseItem > > > GetItemsWithPersonAsync ( string name , PersonType ? personType , Guid userId )
2012-08-14 21:06:25 +02:00
{
2012-08-14 21:27:35 +02:00
string url = ApiUrl + "/itemswithperson?userId=" + userId . ToString ( ) + "&name=" + name ;
2012-08-14 21:06:25 +02:00
if ( personType . HasValue )
{
url + = "&persontype=" + personType . Value . ToString ( ) ;
}
using ( Stream stream = await HttpClient . GetStreamAsync ( url ) )
{
2012-08-17 15:16:50 +02:00
return JsonSerializer . DeserializeFromStream < IEnumerable < BaseItemWrapper < ApiBaseItem > > > ( stream ) ;
2012-08-14 21:06:25 +02:00
}
}
2012-07-31 18:29:07 +02:00
/// <summary>
/// Gets all studious
/// </summary>
public async Task < IEnumerable < CategoryInfo < Studio > > > GetAllStudiosAsync ( Guid userId )
{
string url = ApiUrl + "/studios?userId=" + userId . ToString ( ) ;
using ( Stream stream = await HttpClient . GetStreamAsync ( url ) )
{
2012-08-01 11:25:13 +02:00
return JsonSerializer . DeserializeFromStream < IEnumerable < CategoryInfo < Studio > > > ( stream ) ;
2012-07-31 18:29:07 +02:00
}
}
/// <summary>
/// Gets the current personalized configuration
/// </summary>
public async Task < UserConfiguration > GetUserConfigurationAsync ( Guid userId )
{
string url = ApiUrl + "/userconfiguration?userId=" + userId . ToString ( ) ;
using ( Stream stream = await HttpClient . GetStreamAsync ( url ) )
{
2012-08-01 11:25:13 +02:00
return JsonSerializer . DeserializeFromStream < UserConfiguration > ( stream ) ;
2012-07-31 18:29:07 +02:00
}
}
/// <summary>
2012-08-14 21:06:25 +02:00
/// Gets all items that contain a given Studio
2012-07-31 18:29:07 +02:00
/// </summary>
2012-08-17 15:16:50 +02:00
public async Task < IEnumerable < BaseItemWrapper < ApiBaseItem > > > GetItemsWithStudioAsync ( string name , Guid userId )
2012-07-31 18:29:07 +02:00
{
2012-08-14 18:06:46 +02:00
string url = ApiUrl + "/itemswithstudio?userId=" + userId . ToString ( ) + "&name=" + name ;
2012-07-31 18:29:07 +02:00
using ( Stream stream = await HttpClient . GetStreamAsync ( url ) )
{
2012-08-17 15:16:50 +02:00
return JsonSerializer . DeserializeFromStream < IEnumerable < BaseItemWrapper < ApiBaseItem > > > ( stream ) ;
2012-07-31 18:29:07 +02:00
}
}
2012-08-15 15:20:29 +02:00
public void Dispose ( )
{
HttpClient . Dispose ( ) ;
}
2012-07-31 18:29:07 +02:00
}
}