using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
using MediaBrowser.Model.Configuration;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Users;
namespace MediaBrowser.ApiInteraction
{
public class ApiClient : BaseClient
{
public IJsonSerializer JsonSerializer { get; set; }
public ApiClient()
: base()
{
}
public ApiClient(HttpClientHandler handler)
: base(handler)
{
}
///
/// 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, int? width, int? height, int? maxWidth, int? maxHeight, int? quality)
{
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;
}
///
/// 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 a Genre
///
public async Task> GetGenreAsync(string name, Guid userId)
{
string url = ApiUrl + "/genre?userId=" + userId.ToString() + "&name=" + name;
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 the current personalized configuration
///
public async Task GetUserConfigurationAsync(Guid userId)
{
string url = ApiUrl + "/userconfiguration?userId=" + userId.ToString();
using (Stream stream = await HttpClient.GetStreamAsync(url))
{
return JsonSerializer.DeserializeFromStream(stream);
}
}
///
/// Gets a Studio
///
public async Task> GetStudioAsync(string name, Guid userId)
{
string url = ApiUrl + "/studio?userId=" + userId.ToString() + "&name=" + name;
using (Stream stream = await HttpClient.GetStreamAsync(url))
{
return JsonSerializer.DeserializeFromStream>(stream);
}
}
}
}