using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using MediaBrowser.Model.DTO;
using MediaBrowser.Model.Entities;
namespace MediaBrowser.ApiInteraction
{
public class ApiClient : IDisposable
{
public ApiClient(HttpClientHandler handler)
{
handler.AutomaticDecompression = DecompressionMethods.Deflate;
HttpClient = new HttpClient(handler);
}
///
/// Gets or sets the server host name (myserver or 192.168.x.x)
///
public string ServerHostName { get; set; }
///
/// Gets or sets the port number used by the API
///
public int ServerApiPort { get; set; }
///
/// Gets the current api url based on hostname and port.
///
protected string ApiUrl
{
get
{
return string.Format("http://{0}:{1}/mediabrowser/api", ServerHostName, ServerApiPort);
}
}
public HttpClient HttpClient { get; private set; }
public IJsonSerializer JsonSerializer { get; set; }
///
/// Gets an image url that can be used to download an image from the api
///
/// The Id of the item
/// The type of image requested
/// The image index, if there are multiple. Currently only applies to backdrops. Supply null or 0 for first backdrop.
/// Use if a fixed width is required. Aspect ratio will be preserved.
/// Use if a fixed height is required. Aspect ratio will be preserved.
/// Use if a max width is required. Aspect ratio will be preserved.
/// Use if a max height is required. Aspect ratio will be preserved.
/// Quality level, from 0-100. Currently only applies to JPG. The default value should suffice.
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;
}
///
/// 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.
///
/// A given item.
/// Use if a fixed width is required. Aspect ratio will be preserved.
/// Use if a fixed height is required. Aspect ratio will be preserved.
/// Use if a max width is required. Aspect ratio will be preserved.
/// Use if a max height is required. Aspect ratio will be preserved.
/// Quality level, from 0-100. Currently only applies to JPG. The default value should suffice.
public IEnumerable 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[] { };
}
List files = new List();
for (int i = 0; i < backdropCount; i++)
{
files.Add(GetImageUrl(backdropItemId.Value, ImageType.Backdrop, i, width, height, maxWidth, maxHeight, quality));
}
return files;
}
///
/// 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.
///
/// A given item.
/// Use if a fixed width is required. Aspect ratio will be preserved.
/// Use if a fixed height is required. Aspect ratio will be preserved.
/// Use if a max width is required. Aspect ratio will be preserved.
/// Use if a max height is required. Aspect ratio will be preserved.
/// Quality level, from 0-100. Currently only applies to JPG. The default value should suffice.
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;
}
///
/// Gets an image stream based on a url
///
public async Task GetImageStreamAsync(string url)
{
return await HttpClient.GetStreamAsync(url);
}
///
/// Gets a BaseItem
///
public async Task GetItemAsync(Guid id, Guid userId)
{
string url = ApiUrl + "/item?userId=" + userId.ToString();
if (id != Guid.Empty)
{
url += "&id=" + id.ToString();
}
using (Stream stream = await HttpClient.GetStreamAsync(url))
{
return JsonSerializer.DeserializeFromStream(stream);
}
}
///
/// Gets all Users
///
public async Task> GetAllUsersAsync()
{
string url = ApiUrl + "/users";
using (Stream stream = await HttpClient.GetStreamAsync(url))
{
return JsonSerializer.DeserializeFromStream>(stream);
}
}
///
/// Gets all Genres
///
public async Task>> GetAllGenresAsync(Guid userId)
{
string url = ApiUrl + "/genres?userId=" + userId.ToString();
using (Stream stream = await HttpClient.GetStreamAsync(url))
{
return JsonSerializer.DeserializeFromStream>>(stream);
}
}
///
/// Gets all Years
///
public async Task>> GetAllYearsAsync(Guid userId)
{
string url = ApiUrl + "/years?userId=" + userId.ToString();
using (Stream stream = await HttpClient.GetStreamAsync(url))
{
return JsonSerializer.DeserializeFromStream>>(stream);
}
}
///
/// Gets all items that contain a given Year
///
public async Task> GetItemsWithYearAsync(string name, Guid userId)
{
string url = ApiUrl + "/itemlist?listtype=itemswithyear&userId=" + userId.ToString() + "&name=" + name;
using (Stream stream = await HttpClient.GetStreamAsync(url))
{
return JsonSerializer.DeserializeFromStream>(stream);
}
}
///
/// Gets all items that contain a given Genre
///
public async Task> GetItemsWithGenreAsync(string name, Guid userId)
{
string url = ApiUrl + "/itemlist?listtype=itemswithgenre&userId=" + userId.ToString() + "&name=" + name;
using (Stream stream = await HttpClient.GetStreamAsync(url))
{
return JsonSerializer.DeserializeFromStream>(stream);
}
}
///
/// Gets all items that contain a given Person
///
public async Task> GetItemsWithPersonAsync(string name, Guid userId)
{
string url = ApiUrl + "/itemlist?listtype=itemswithperson&userId=" + userId.ToString() + "&name=" + name;
using (Stream stream = await HttpClient.GetStreamAsync(url))
{
return JsonSerializer.DeserializeFromStream>(stream);
}
}
///
/// Gets all items that contain a given Person
///
public async Task> GetItemsWithPersonAsync(string name, string personType, Guid userId)
{
string url = ApiUrl + "/itemlist?listtype=itemswithperson&userId=" + userId.ToString() + "&name=" + name;
url += "&persontype=" + personType;
using (Stream stream = await HttpClient.GetStreamAsync(url))
{
return JsonSerializer.DeserializeFromStream>(stream);
}
}
///
/// Gets all studious
///
public async Task>> GetAllStudiosAsync(Guid userId)
{
string url = ApiUrl + "/studios?userId=" + userId.ToString();
using (Stream stream = await HttpClient.GetStreamAsync(url))
{
return JsonSerializer.DeserializeFromStream>>(stream);
}
}
///
/// Gets all items that contain a given Studio
///
public async Task> GetItemsWithStudioAsync(string name, Guid userId)
{
string url = ApiUrl + "/itemlist?listtype=itemswithstudio&userId=" + userId.ToString() + "&name=" + name;
using (Stream stream = await HttpClient.GetStreamAsync(url))
{
return JsonSerializer.DeserializeFromStream>(stream);
}
}
///
/// Gets a studio
///
public async Task> GetStudioAsync(Guid userId, string name)
{
string url = ApiUrl + "/studio?userId=" + userId.ToString() + "&name=" + name;
using (Stream stream = await HttpClient.GetStreamAsync(url))
{
return JsonSerializer.DeserializeFromStream>(stream);
}
}
///
/// Gets a genre
///
public async Task> GetGenreAsync(Guid userId, string name)
{
string url = ApiUrl + "/genre?userId=" + userId.ToString() + "&name=" + name;
using (Stream stream = await HttpClient.GetStreamAsync(url))
{
return JsonSerializer.DeserializeFromStream>(stream);
}
}
///
/// Gets a person
///
public async Task> GetPersonAsync(Guid userId, string name)
{
string url = ApiUrl + "/person?userId=" + userId.ToString() + "&name=" + name;
using (Stream stream = await HttpClient.GetStreamAsync(url))
{
return JsonSerializer.DeserializeFromStream>(stream);
}
}
///
/// Gets a year
///
public async Task> GetYearAsync(Guid userId, int year)
{
string url = ApiUrl + "/year?userId=" + userId.ToString() + "&year=" + year;
using (Stream stream = await HttpClient.GetStreamAsync(url))
{
return JsonSerializer.DeserializeFromStream>(stream);
}
}
public void Dispose()
{
HttpClient.Dispose();
}
}
}