@ -11,352 +11,17 @@ using MediaBrowser.Model.Weather;
namespace MediaBrowser.ApiInteraction
{
public class ApiClient : IDisposable
public class ApiClient : BaseApiClient
{
public ApiClient ( HttpClientHandler handler )
: base ( )
{
handler . AutomaticDecompression = DecompressionMethods . Deflate ;
HttpClient = new HttpClient ( handler ) ;
DataSerializer . Configure ( ) ;
}
/// <summary>
/// Gets or sets the server host name (myserver or 192.168.x.x)
/// </summary>
public string ServerHostName { get ; set ; }
/// <summary>
/// Gets or sets the port number used by the API
/// </summary>
public int ServerApiPort { get ; set ; }
/// <summary>
/// Gets the current api url based on hostname and port.
/// </summary>
protected string ApiUrl
{
get
{
return string . Format ( "http://{0}:{1}/mediabrowser/api" , ServerHostName , ServerApiPort ) ;
}
}
/// <summary>
/// Gets the data format to request from the server
/// </summary>
private SerializationFormats SerializationFormat
{
get
{
return ApiInteraction . SerializationFormats . Protobuf ;
}
}
public HttpClient HttpClient { get ; private set ; }
/// <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>
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 )
{
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 ;
}
/// <summary>
/// Gets an image url that can be used to download an image from the api
/// </summary>
/// <param name="userId">The Id of the user</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>
public string GetUserImageUrl ( Guid userId , int? width = null , int? height = null , int? maxWidth = null , int? maxHeight = null , int? quality = null )
{
string url = ApiUrl + "/image" ;
url + = "?userId=" + userId . ToString ( ) ;
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 ;
}
/// <summary>
/// Gets an image url that can be used to download an image from the api
/// </summary>
/// <param name="name">The name of the person</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>
public string GetPersonImageUrl ( string name , int? width = null , int? height = null , int? maxWidth = null , int? maxHeight = null , int? quality = null )
{
string url = ApiUrl + "/image" ;
url + = "?personname=" + name ;
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 ;
}
/// <summary>
/// Gets an image url that can be used to download an image from the api
/// </summary>
/// <param name="year">The year</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>
public string GetYearImageUrl ( int year , int? width = null , int? height = null , int? maxWidth = null , int? maxHeight = null , int? quality = null )
{
string url = ApiUrl + "/image" ;
url + = "?year=" + year ;
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 ;
}
/// <summary>
/// Gets an image url that can be used to download an image from the api
/// </summary>
/// <param name="name">The name of the genre</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>
public string GetGenreImageUrl ( string name , int? width = null , int? height = null , int? maxWidth = null , int? maxHeight = null , int? quality = null )
{
string url = ApiUrl + "/image" ;
url + = "?genre=" + name ;
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 ;
}
/// <summary>
/// Gets an image url that can be used to download an image from the api
/// </summary>
/// <param name="name">The name of the studio</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>
public string GetStudioImageUrl ( string name , int? width = null , int? height = null , int? maxWidth = null , int? maxHeight = null , int? quality = null )
{
string url = ApiUrl + "/image" ;
url + = "?studio=" + name ;
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 ;
}
/// <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="item">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>
public string [ ] GetBackdropImageUrls ( DTOBaseItem item , int? width = null , int? height = null , int? maxWidth = null , int? maxHeight = null , int? quality = null )
{
Guid ? backdropItemId = null ;
int backdropCount = 0 ;
if ( item . BackdropCount = = 0 )
{
backdropItemId = item . ParentBackdropItemId ;
backdropCount = item . ParentBackdropCount ? ? 0 ;
}
else
{
backdropItemId = item . Id ;
backdropCount = item . BackdropCount ;
}
if ( backdropItemId = = null )
{
return new string [ ] { } ;
}
string [ ] files = new string [ backdropCount ] ;
for ( int i = 0 ; i < backdropCount ; i + + )
{
files [ i ] = 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="item">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>
public string GetLogoImageUrl ( DTOBaseItem item , int? width = null , int? height = null , int? maxWidth = null , int? maxHeight = null , int? quality = null )
{
Guid ? logoItemId = item . HasLogo ? item . Id : item . ParentLogoItemId ;
if ( logoItemId . HasValue )
{
return GetImageUrl ( logoItemId . Value , ImageType . Logo , null , width , height , maxWidth , maxHeight , quality ) ;
}
return null ;
}
private HttpClient HttpClient { get ; set ; }
/// <summary>
/// Gets an image stream based on a url
@ -397,6 +62,35 @@ namespace MediaBrowser.ApiInteraction
}
}
public void GetAllUsersAsync ( Action < DTOUser [ ] > callback )
{
string url = ApiUrl + "/users" ;
HttpWebRequest request = HttpWebRequest . CreateHttp ( url ) ;
request . BeginGetResponse ( new AsyncCallback ( result = >
{
HttpWebResponse response = ( result . AsyncState as HttpWebRequest ) . EndGetResponse ( result ) as HttpWebResponse ;
Stream stream = response . GetResponseStream ( ) ;
} ) , request ) ;
}
public void GetData < T > ( string url , SerializationFormats serializationFormat , Action < T > callback )
{
HttpWebRequest request = HttpWebRequest . CreateHttp ( url ) ;
//request.AutomaticDecompression = DecompressionMethods.Deflate;
request . BeginGetResponse ( new AsyncCallback ( result = >
{
HttpWebResponse response = ( result . AsyncState as HttpWebRequest ) . EndGetResponse ( result ) as HttpWebResponse ;
Stream stream = response . GetResponseStream ( ) ;
} ) , request ) ;
}
/// <summary>
/// Gets all Genres
/// </summary>
@ -694,11 +388,6 @@ namespace MediaBrowser.ApiInteraction
return GetStreamAsync ( url ) ;
}
private T DeserializeFromStream < T > ( Stream stream )
{
return DataSerializer . DeserializeFromStream < T > ( stream , SerializationFormat ) ;
}
/// <summary>
/// This is just a helper around HttpClient
/// </summary>
@ -707,7 +396,7 @@ namespace MediaBrowser.ApiInteraction
return HttpClient . GetStreamAsync ( url ) ;
}
public void Dispose ( )
public override void Dispose ( )
{
HttpClient . Dispose ( ) ;
}