2012-07-31 16:29:07 +00:00
using System ;
using System.Collections.Generic ;
using System.IO ;
2012-08-18 08:22:54 +00:00
using System.Net ;
using System.Net.Http ;
2012-07-31 16:29:07 +00:00
using System.Threading.Tasks ;
2012-09-02 13:45:02 +00:00
using MediaBrowser.Model.Configuration ;
2012-08-17 13:16:50 +00:00
using MediaBrowser.Model.DTO ;
2012-07-31 16:29:07 +00:00
using MediaBrowser.Model.Entities ;
2012-09-02 05:30:25 +00:00
using MediaBrowser.Model.Weather ;
2012-07-31 16:29:07 +00:00
namespace MediaBrowser.ApiInteraction
{
2012-08-15 13:20:29 +00:00
public class ApiClient : IDisposable
2012-07-31 16:29:07 +00:00
{
2012-08-18 08:22:54 +00:00
public ApiClient ( HttpClientHandler handler )
{
handler . AutomaticDecompression = DecompressionMethods . Deflate ;
HttpClient = new HttpClient ( handler ) ;
}
2012-08-15 13:20:29 +00:00
/// <summary>
/// Gets or sets the server host name (myserver or 192.168.x.x)
/// </summary>
public string ServerHostName { get ; set ; }
2012-07-31 16:29:07 +00:00
2012-08-15 13:20:29 +00:00
/// <summary>
/// Gets or sets the port number used by the API
/// </summary>
public int ServerApiPort { get ; set ; }
2012-07-31 16:29:07 +00:00
2012-08-15 13:20:29 +00:00
/// <summary>
/// Gets the current api url based on hostname and port.
/// </summary>
protected string ApiUrl
2012-08-01 01:53:40 +00:00
{
2012-08-15 13:20:29 +00:00
get
{
return string . Format ( "http://{0}:{1}/mediabrowser/api" , ServerHostName , ServerApiPort ) ;
}
2012-08-01 01:53:40 +00:00
}
2012-08-22 13:05:25 +00:00
/// <summary>
2012-08-29 12:21:56 +00:00
/// Gets the data format to request from the server
2012-08-22 13:05:25 +00:00
/// </summary>
2012-08-29 12:21:56 +00:00
private SerializationFormat SerializationFormat
{
get
{
// First try Protobuf since it has the best performance
if ( DataSerializer . CanDeserializeProtobuf )
{
return ApiInteraction . SerializationFormat . Protobuf ;
}
// Next best is jsv
if ( DataSerializer . CanDeserializeJsv )
{
return ApiInteraction . SerializationFormat . Jsv ;
}
return ApiInteraction . SerializationFormat . Json ;
}
}
2012-08-22 13:05:25 +00:00
2012-08-18 08:22:54 +00:00
public HttpClient HttpClient { get ; private set ; }
2012-08-22 13:05:25 +00:00
public IDataSerializer DataSerializer { get ; set ; }
2012-08-15 13:20:29 +00:00
2012-07-31 16:29:07 +00: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 13:21:26 +00: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 16:29:07 +00: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-25 20:02:53 +00:00
/// <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 ;
}
2012-08-29 12:21:56 +00:00
2012-08-03 15:54:05 +00: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>
2012-08-18 08:22:54 +00:00
/// <param name="item">A given item.</param>
2012-08-03 15:54:05 +00:00
/// <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-18 08:22:54 +00:00
public IEnumerable < string > GetBackdropImageUrls ( DTOBaseItem item , int? width = null , int? height = null , int? maxWidth = null , int? maxHeight = null , int? quality = null )
2012-08-03 15:54:05 +00:00
{
Guid ? backdropItemId = null ;
int backdropCount = 0 ;
2012-08-18 08:22:54 +00:00
if ( item . BackdropCount = = 0 )
2012-08-03 15:54:05 +00:00
{
2012-08-18 08:22:54 +00:00
backdropItemId = item . ParentBackdropItemId ;
backdropCount = item . ParentBackdropCount ? ? 0 ;
2012-08-03 15:54:05 +00:00
}
else
{
2012-08-18 08:22:54 +00:00
backdropItemId = item . Id ;
backdropCount = item . BackdropCount ;
2012-08-03 15:54:05 +00:00
}
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>
2012-08-18 08:22:54 +00:00
/// <param name="item">A given item.</param>
2012-08-03 15:54:05 +00:00
/// <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-18 08:22:54 +00:00
public string GetLogoImageUrl ( DTOBaseItem item , int? width = null , int? height = null , int? maxWidth = null , int? maxHeight = null , int? quality = null )
2012-08-03 15:54:05 +00:00
{
2012-08-18 08:22:54 +00:00
Guid ? logoItemId = item . HasLogo ? item . Id : item . ParentLogoItemId ;
2012-08-03 15:54:05 +00:00
if ( logoItemId . HasValue )
{
return GetImageUrl ( logoItemId . Value , ImageType . Logo , null , width , height , maxWidth , maxHeight , quality ) ;
}
return null ;
}
2012-08-06 13:21:26 +00:00
2012-07-31 16:29:07 +00:00
/// <summary>
/// Gets an image stream based on a url
/// </summary>
2012-08-22 02:50:59 +00:00
public Task < Stream > GetImageStreamAsync ( string url )
2012-07-31 16:29:07 +00:00
{
2012-08-22 12:56:44 +00:00
return GetStreamAsync ( url ) ;
2012-07-31 16:29:07 +00:00
}
/// <summary>
/// Gets a BaseItem
/// </summary>
2012-08-18 08:22:54 +00:00
public async Task < DTOBaseItem > GetItemAsync ( Guid id , Guid userId )
2012-07-31 16:29:07 +00:00
{
string url = ApiUrl + "/item?userId=" + userId . ToString ( ) ;
if ( id ! = Guid . Empty )
{
url + = "&id=" + id . ToString ( ) ;
}
2012-08-22 13:05:25 +00:00
using ( Stream stream = await GetSerializedStreamAsync ( url ) . ConfigureAwait ( false ) )
2012-07-31 16:29:07 +00:00
{
2012-08-29 12:21:56 +00:00
return DeserializeFromStream < DTOBaseItem > ( stream ) ;
2012-07-31 16:29:07 +00:00
}
}
/// <summary>
/// Gets all Users
/// </summary>
2012-08-29 12:21:56 +00:00
public async Task < IEnumerable < DTOUser > > GetAllUsersAsync ( )
2012-07-31 16:29:07 +00:00
{
string url = ApiUrl + "/users" ;
2012-08-22 13:05:25 +00:00
using ( Stream stream = await GetSerializedStreamAsync ( url ) . ConfigureAwait ( false ) )
2012-07-31 16:29:07 +00:00
{
2012-08-29 12:21:56 +00:00
return DeserializeFromStream < DTOUser [ ] > ( stream ) ;
2012-07-31 16:29:07 +00:00
}
}
/// <summary>
/// Gets all Genres
/// </summary>
2012-08-23 12:50:41 +00:00
public async Task < IEnumerable < IBNItem > > GetAllGenresAsync ( Guid userId )
2012-07-31 16:29:07 +00:00
{
string url = ApiUrl + "/genres?userId=" + userId . ToString ( ) ;
2012-08-22 13:05:25 +00:00
using ( Stream stream = await GetSerializedStreamAsync ( url ) . ConfigureAwait ( false ) )
2012-07-31 16:29:07 +00:00
{
2012-08-29 12:21:56 +00:00
return DeserializeFromStream < IBNItem [ ] > ( stream ) ;
2012-07-31 16:29:07 +00:00
}
}
2012-08-14 16:06:46 +00:00
/// <summary>
/// Gets all Years
/// </summary>
2012-08-23 12:50:41 +00:00
public async Task < IEnumerable < IBNItem > > GetAllYearsAsync ( Guid userId )
2012-08-14 16:06:46 +00:00
{
string url = ApiUrl + "/years?userId=" + userId . ToString ( ) ;
2012-08-22 13:05:25 +00:00
using ( Stream stream = await GetSerializedStreamAsync ( url ) . ConfigureAwait ( false ) )
2012-08-14 16:06:46 +00:00
{
2012-08-29 12:21:56 +00:00
return DeserializeFromStream < IBNItem [ ] > ( stream ) ;
2012-08-14 16:06:46 +00:00
}
}
/// <summary>
2012-08-14 19:06:25 +00:00
/// Gets all items that contain a given Year
2012-08-14 16:06:46 +00:00
/// </summary>
2012-08-18 08:22:54 +00:00
public async Task < IEnumerable < DTOBaseItem > > GetItemsWithYearAsync ( string name , Guid userId )
2012-08-14 16:06:46 +00:00
{
2012-08-17 16:47:35 +00:00
string url = ApiUrl + "/itemlist?listtype=itemswithyear&userId=" + userId . ToString ( ) + "&name=" + name ;
2012-08-14 16:06:46 +00:00
2012-08-22 13:05:25 +00:00
using ( Stream stream = await GetSerializedStreamAsync ( url ) . ConfigureAwait ( false ) )
2012-08-14 16:06:46 +00:00
{
2012-08-29 12:21:56 +00:00
return DeserializeFromStream < DTOBaseItem [ ] > ( stream ) ;
2012-08-14 16:06:46 +00:00
}
}
2012-07-31 16:29:07 +00:00
/// <summary>
2012-08-14 19:06:25 +00:00
/// Gets all items that contain a given Genre
2012-07-31 16:29:07 +00:00
/// </summary>
2012-08-18 08:22:54 +00:00
public async Task < IEnumerable < DTOBaseItem > > GetItemsWithGenreAsync ( string name , Guid userId )
2012-07-31 16:29:07 +00:00
{
2012-08-17 16:47:35 +00:00
string url = ApiUrl + "/itemlist?listtype=itemswithgenre&userId=" + userId . ToString ( ) + "&name=" + name ;
2012-07-31 16:29:07 +00:00
2012-08-22 13:05:25 +00:00
using ( Stream stream = await GetSerializedStreamAsync ( url ) . ConfigureAwait ( false ) )
2012-07-31 16:29:07 +00:00
{
2012-08-29 12:21:56 +00:00
return DeserializeFromStream < DTOBaseItem [ ] > ( stream ) ;
2012-07-31 16:29:07 +00:00
}
}
2012-08-14 19:06:25 +00:00
/// <summary>
/// Gets all items that contain a given Person
/// </summary>
2012-08-18 08:22:54 +00:00
public async Task < IEnumerable < DTOBaseItem > > GetItemsWithPersonAsync ( string name , Guid userId )
2012-08-14 19:06:25 +00:00
{
2012-08-17 16:47:35 +00:00
string url = ApiUrl + "/itemlist?listtype=itemswithperson&userId=" + userId . ToString ( ) + "&name=" + name ;
2012-08-14 19:06:25 +00:00
2012-08-22 13:05:25 +00:00
using ( Stream stream = await GetSerializedStreamAsync ( url ) . ConfigureAwait ( false ) )
2012-08-14 19:06:25 +00:00
{
2012-08-29 12:21:56 +00:00
return DeserializeFromStream < DTOBaseItem [ ] > ( stream ) ;
2012-08-14 19:06:25 +00:00
}
2012-08-18 08:22:54 +00:00
}
2012-08-22 13:05:25 +00:00
2012-08-18 08:22:54 +00:00
/// <summary>
/// Gets all items that contain a given Person
/// </summary>
public async Task < IEnumerable < DTOBaseItem > > GetItemsWithPersonAsync ( string name , string personType , Guid userId )
{
string url = ApiUrl + "/itemlist?listtype=itemswithperson&userId=" + userId . ToString ( ) + "&name=" + name ;
url + = "&persontype=" + personType ;
2012-08-14 19:06:25 +00:00
2012-08-22 13:05:25 +00:00
using ( Stream stream = await GetSerializedStreamAsync ( url ) . ConfigureAwait ( false ) )
2012-08-14 19:06:25 +00:00
{
2012-08-29 12:21:56 +00:00
return DeserializeFromStream < DTOBaseItem [ ] > ( stream ) ;
2012-08-14 19:06:25 +00:00
}
}
2012-07-31 16:29:07 +00:00
/// <summary>
/// Gets all studious
/// </summary>
2012-08-23 12:50:41 +00:00
public async Task < IEnumerable < IBNItem > > GetAllStudiosAsync ( Guid userId )
2012-07-31 16:29:07 +00:00
{
string url = ApiUrl + "/studios?userId=" + userId . ToString ( ) ;
2012-08-22 13:05:25 +00:00
using ( Stream stream = await GetSerializedStreamAsync ( url ) . ConfigureAwait ( false ) )
2012-07-31 16:29:07 +00:00
{
2012-08-29 12:21:56 +00:00
return DeserializeFromStream < IBNItem [ ] > ( stream ) ;
2012-07-31 16:29:07 +00:00
}
}
/// <summary>
2012-08-14 19:06:25 +00:00
/// Gets all items that contain a given Studio
2012-07-31 16:29:07 +00:00
/// </summary>
2012-08-18 08:22:54 +00:00
public async Task < IEnumerable < DTOBaseItem > > GetItemsWithStudioAsync ( string name , Guid userId )
2012-07-31 16:29:07 +00:00
{
2012-08-17 16:47:35 +00:00
string url = ApiUrl + "/itemlist?listtype=itemswithstudio&userId=" + userId . ToString ( ) + "&name=" + name ;
2012-07-31 16:29:07 +00:00
2012-08-22 13:05:25 +00:00
using ( Stream stream = await GetSerializedStreamAsync ( url ) . ConfigureAwait ( false ) )
2012-07-31 16:29:07 +00:00
{
2012-08-29 12:21:56 +00:00
return DeserializeFromStream < DTOBaseItem [ ] > ( stream ) ;
2012-07-31 16:29:07 +00:00
}
}
2012-08-15 13:20:29 +00:00
2012-08-18 16:27:34 +00:00
/// <summary>
/// Gets a studio
/// </summary>
2012-08-23 12:50:41 +00:00
public async Task < IBNItem > GetStudioAsync ( Guid userId , string name )
2012-08-18 16:27:34 +00:00
{
string url = ApiUrl + "/studio?userId=" + userId . ToString ( ) + "&name=" + name ;
2012-08-22 13:05:25 +00:00
using ( Stream stream = await GetSerializedStreamAsync ( url ) . ConfigureAwait ( false ) )
2012-08-18 16:27:34 +00:00
{
2012-08-29 12:21:56 +00:00
return DeserializeFromStream < IBNItem > ( stream ) ;
2012-08-18 16:27:34 +00:00
}
}
/// <summary>
/// Gets a genre
/// </summary>
2012-08-23 12:50:41 +00:00
public async Task < IBNItem > GetGenreAsync ( Guid userId , string name )
2012-08-18 16:27:34 +00:00
{
string url = ApiUrl + "/genre?userId=" + userId . ToString ( ) + "&name=" + name ;
2012-08-22 13:05:25 +00:00
using ( Stream stream = await GetSerializedStreamAsync ( url ) . ConfigureAwait ( false ) )
2012-08-18 16:27:34 +00:00
{
2012-08-29 12:21:56 +00:00
return DeserializeFromStream < IBNItem > ( stream ) ;
2012-08-18 16:27:34 +00:00
}
}
/// <summary>
/// Gets a person
/// </summary>
2012-08-23 12:50:41 +00:00
public async Task < IBNItem > GetPersonAsync ( Guid userId , string name )
2012-08-18 16:27:34 +00:00
{
string url = ApiUrl + "/person?userId=" + userId . ToString ( ) + "&name=" + name ;
2012-08-22 13:05:25 +00:00
using ( Stream stream = await GetSerializedStreamAsync ( url ) . ConfigureAwait ( false ) )
2012-08-18 16:27:34 +00:00
{
2012-08-29 12:21:56 +00:00
return DeserializeFromStream < IBNItem > ( stream ) ;
2012-08-18 16:27:34 +00:00
}
}
/// <summary>
/// Gets a year
/// </summary>
2012-08-23 12:50:41 +00:00
public async Task < IBNItem > GetYearAsync ( Guid userId , int year )
2012-08-18 16:27:34 +00:00
{
string url = ApiUrl + "/year?userId=" + userId . ToString ( ) + "&year=" + year ;
2012-08-22 13:05:25 +00:00
using ( Stream stream = await GetSerializedStreamAsync ( url ) . ConfigureAwait ( false ) )
2012-08-18 16:27:34 +00:00
{
2012-08-29 12:21:56 +00:00
return DeserializeFromStream < IBNItem > ( stream ) ;
2012-08-18 16:27:34 +00:00
}
}
2012-09-03 18:15:07 +00:00
/// <summary>
/// Gets a list of plugins installed on the server
/// </summary>
public async Task < PluginInfo [ ] > GetInstalledPlugins ( )
{
string url = ApiUrl + "/plugins" ;
using ( Stream stream = await GetSerializedStreamAsync ( url ) . ConfigureAwait ( false ) )
{
return DeserializeFromStream < PluginInfo [ ] > ( stream ) ;
}
}
2012-09-02 05:30:25 +00:00
/// <summary>
/// Gets weather information for the default location as set in configuration
/// </summary>
2012-09-02 13:45:02 +00:00
public async Task < ServerConfiguration > GetServerConfigurationAsync ( )
{
string url = ApiUrl + "/ServerConfiguration" ;
2012-09-02 17:34:12 +00:00
using ( Stream stream = await GetSerializedStreamAsync ( url , ApiInteraction . SerializationFormat . Json ) . ConfigureAwait ( false ) )
2012-09-02 13:45:02 +00:00
{
2012-09-02 17:34:12 +00:00
return DeserializeFromStream < ServerConfiguration > ( stream , ApiInteraction . SerializationFormat . Json ) ;
2012-09-02 13:45:02 +00:00
}
}
2012-09-02 15:06:12 +00:00
/// <summary>
/// Gets weather information for the default location as set in configuration
/// </summary>
public async Task < DTOUser > GetDefaultUserAsync ( )
{
string url = ApiUrl + "/defaultuser" ;
using ( Stream stream = await GetSerializedStreamAsync ( url ) . ConfigureAwait ( false ) )
{
return DeserializeFromStream < DTOUser > ( stream ) ;
}
}
2012-09-02 13:45:02 +00:00
/// <summary>
/// Gets weather information for the default location as set in configuration
/// </summary>
public async Task < WeatherInfo > GetWeatherInfoAsync ( )
2012-09-02 05:30:25 +00:00
{
string url = ApiUrl + "/weather" ;
using ( Stream stream = await GetSerializedStreamAsync ( url ) . ConfigureAwait ( false ) )
{
return DeserializeFromStream < WeatherInfo > ( stream ) ;
}
}
/// <summary>
/// Gets weather information for a specific zip code
/// </summary>
2012-09-02 13:45:02 +00:00
public async Task < WeatherInfo > GetWeatherInfoAsync ( string zipCode )
2012-09-02 05:30:25 +00:00
{
string url = ApiUrl + "/weather?zipcode=" + zipCode ;
using ( Stream stream = await GetSerializedStreamAsync ( url ) . ConfigureAwait ( false ) )
{
return DeserializeFromStream < WeatherInfo > ( stream ) ;
}
}
2012-08-22 13:05:25 +00:00
/// <summary>
/// This is a helper around getting a stream from the server that contains serialized data
/// </summary>
private Task < Stream > GetSerializedStreamAsync ( string url )
2012-09-02 17:34:12 +00:00
{
return GetSerializedStreamAsync ( url , SerializationFormat ) ;
}
/// <summary>
/// This is a helper around getting a stream from the server that contains serialized data
/// </summary>
private Task < Stream > GetSerializedStreamAsync ( string url , SerializationFormat serializationFormat )
2012-08-22 12:56:44 +00:00
{
2012-08-22 13:05:25 +00:00
if ( url . IndexOf ( '?' ) = = - 1 )
{
2012-09-02 17:34:12 +00:00
url + = "?dataformat=" + serializationFormat . ToString ( ) . ToLower ( ) ;
2012-08-22 13:05:25 +00:00
}
else
{
2012-09-02 17:34:12 +00:00
url + = "&dataformat=" + serializationFormat . ToString ( ) . ToLower ( ) ;
2012-08-22 13:05:25 +00:00
}
2012-08-22 12:56:44 +00:00
return GetStreamAsync ( url ) ;
}
2012-09-02 17:34:12 +00:00
2012-08-29 12:21:56 +00:00
private T DeserializeFromStream < T > ( Stream stream )
{
return DeserializeFromStream < T > ( stream , SerializationFormat ) ;
}
private T DeserializeFromStream < T > ( Stream stream , SerializationFormat format )
{
if ( format = = ApiInteraction . SerializationFormat . Protobuf )
{
return DataSerializer . DeserializeProtobufFromStream < T > ( stream ) ;
}
if ( format = = ApiInteraction . SerializationFormat . Jsv )
{
return DataSerializer . DeserializeJsvFromStream < T > ( stream ) ;
}
return DataSerializer . DeserializeJsonFromStream < T > ( stream ) ;
}
2012-08-22 13:05:25 +00:00
/// <summary>
/// This is just a helper around HttpClient
/// </summary>
private Task < Stream > GetStreamAsync ( string url )
{
return HttpClient . GetStreamAsync ( url ) ;
}
2012-08-15 13:20:29 +00:00
public void Dispose ( )
{
HttpClient . Dispose ( ) ;
}
2012-07-31 16:29:07 +00:00
}
2012-08-22 13:05:25 +00:00
public enum SerializationFormat
{
Json ,
2012-08-29 12:21:56 +00:00
Jsv ,
Protobuf
2012-08-22 13:05:25 +00:00
}
2012-07-31 16:29:07 +00:00
}