jellyfin/MediaBrowser.ApiInteraction/BaseHttpApiClient.cs

612 lines
22 KiB
C#
Raw Normal View History

2012-09-08 17:56:51 +00:00
using MediaBrowser.Model.Authentication;
using MediaBrowser.Model.Configuration;
using MediaBrowser.Model.DTO;
using MediaBrowser.Model.Weather;
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Threading.Tasks;
#if WINDOWS_PHONE
using SharpGIS;
#else
using System.Net.Http;
#endif
2012-09-08 17:56:51 +00:00
namespace MediaBrowser.ApiInteraction
{
/// <summary>
/// Provides api methods centered around an HttpClient
/// </summary>
public abstract class BaseHttpApiClient : BaseApiClient
{
#if WINDOWS_PHONE
public BaseHttpApiClient()
{
HttpClient = new GZipWebClient();
}
private WebClient HttpClient { get; set; }
#else
2012-09-11 19:37:14 +00:00
protected BaseHttpApiClient(HttpClientHandler handler)
2012-09-08 17:56:51 +00:00
: base()
{
handler.AutomaticDecompression = DecompressionMethods.Deflate;
HttpClient = new HttpClient(handler);
}
private HttpClient HttpClient { get; set; }
#endif
2012-09-08 17:56:51 +00:00
/// <summary>
/// Gets an image stream based on a url
/// </summary>
public Task<Stream> GetImageStreamAsync(string url)
{
return GetStreamAsync(url);
}
/// <summary>
/// Gets a BaseItem
/// </summary>
2012-09-11 18:20:12 +00:00
public async Task<DtoBaseItem> GetItemAsync(Guid id, Guid userId)
2012-09-08 17:56:51 +00:00
{
string url = ApiUrl + "/item?userId=" + userId.ToString();
if (id != Guid.Empty)
{
url += "&id=" + id.ToString();
}
using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
{
2012-09-11 18:20:12 +00:00
return DeserializeFromStream<DtoBaseItem>(stream);
2012-09-08 17:56:51 +00:00
}
}
/// <summary>
/// Gets all Users
/// </summary>
2012-09-11 18:20:12 +00:00
public async Task<DtoUser[]> GetAllUsersAsync()
2012-09-08 17:56:51 +00:00
{
string url = ApiUrl + "/users";
using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
{
2012-09-11 18:20:12 +00:00
return DeserializeFromStream<DtoUser[]>(stream);
2012-09-08 17:56:51 +00:00
}
}
/// <summary>
/// Gets all Genres
/// </summary>
2012-09-11 19:37:14 +00:00
public async Task<IbnItem[]> GetAllGenresAsync(Guid userId)
2012-09-08 17:56:51 +00:00
{
string url = ApiUrl + "/genres?userId=" + userId.ToString();
using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
{
2012-09-11 19:37:14 +00:00
return DeserializeFromStream<IbnItem[]>(stream);
2012-09-08 17:56:51 +00:00
}
}
/// <summary>
/// Gets in-progress items
/// </summary>
/// <param name="userId">The user id.</param>
/// <param name="folderId">(Optional) Specify a folder Id to localize the search to a specific folder.</param>
2012-09-11 18:20:12 +00:00
public async Task<DtoBaseItem[]> GetInProgressItemsItemsAsync(Guid userId, Guid? folderId = null)
2012-09-08 17:56:51 +00:00
{
string url = ApiUrl + "/itemlist?listtype=inprogressitems&userId=" + userId.ToString();
if (folderId.HasValue)
{
url += "&id=" + folderId.ToString();
}
using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
{
2012-09-11 18:20:12 +00:00
return DeserializeFromStream<DtoBaseItem[]>(stream);
2012-09-08 17:56:51 +00:00
}
}
/// <summary>
/// Gets recently added items
/// </summary>
/// <param name="userId">The user id.</param>
/// <param name="folderId">(Optional) Specify a folder Id to localize the search to a specific folder.</param>
2012-09-11 18:20:12 +00:00
public async Task<DtoBaseItem[]> GetRecentlyAddedItemsAsync(Guid userId, Guid? folderId = null)
2012-09-08 17:56:51 +00:00
{
string url = ApiUrl + "/itemlist?listtype=recentlyaddeditems&userId=" + userId.ToString();
if (folderId.HasValue)
{
url += "&id=" + folderId.ToString();
}
using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
{
2012-09-11 18:20:12 +00:00
return DeserializeFromStream<DtoBaseItem[]>(stream);
2012-09-08 17:56:51 +00:00
}
}
2012-09-09 18:32:51 +00:00
/// <summary>
/// Gets favorite items
/// </summary>
/// <param name="userId">The user id.</param>
/// <param name="folderId">(Optional) Specify a folder Id to localize the search to a specific folder.</param>
2012-09-11 18:20:12 +00:00
public async Task<DtoBaseItem[]> GetFavoriteItemsAsync(Guid userId, Guid? folderId = null)
2012-09-09 18:32:51 +00:00
{
string url = ApiUrl + "/itemlist?listtype=favorites&userId=" + userId.ToString();
if (folderId.HasValue)
{
url += "&id=" + folderId.ToString();
}
using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
{
2012-09-11 18:20:12 +00:00
return DeserializeFromStream<DtoBaseItem[]>(stream);
2012-09-09 18:32:51 +00:00
}
}
2012-09-08 17:56:51 +00:00
/// <summary>
/// Gets recently added items that are unplayed.
/// </summary>
/// <param name="userId">The user id.</param>
/// <param name="folderId">(Optional) Specify a folder Id to localize the search to a specific folder.</param>
2012-09-11 18:20:12 +00:00
public async Task<DtoBaseItem[]> GetRecentlyAddedUnplayedItemsAsync(Guid userId, Guid? folderId = null)
2012-09-08 17:56:51 +00:00
{
string url = ApiUrl + "/itemlist?listtype=recentlyaddedunplayeditems&userId=" + userId.ToString();
if (folderId.HasValue)
{
url += "&id=" + folderId.ToString();
}
using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
{
2012-09-11 18:20:12 +00:00
return DeserializeFromStream<DtoBaseItem[]>(stream);
2012-09-08 17:56:51 +00:00
}
}
/// <summary>
/// Gets all Years
/// </summary>
2012-09-11 19:37:14 +00:00
public async Task<IbnItem[]> GetAllYearsAsync(Guid userId)
2012-09-08 17:56:51 +00:00
{
string url = ApiUrl + "/years?userId=" + userId.ToString();
using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
{
2012-09-11 19:37:14 +00:00
return DeserializeFromStream<IbnItem[]>(stream);
2012-09-08 17:56:51 +00:00
}
}
/// <summary>
/// Gets all items that contain a given Year
/// </summary>
2012-09-09 18:32:51 +00:00
/// <param name="folderId">(Optional) Specify a folder Id to localize the search to a specific folder.</param>
2012-09-11 18:20:12 +00:00
public async Task<DtoBaseItem[]> GetItemsWithYearAsync(string name, Guid userId, Guid? folderId = null)
2012-09-08 17:56:51 +00:00
{
string url = ApiUrl + "/itemlist?listtype=itemswithyear&userId=" + userId.ToString() + "&name=" + name;
2012-09-09 18:32:51 +00:00
if (folderId.HasValue)
{
url += "&id=" + folderId.ToString();
}
2012-09-08 17:56:51 +00:00
using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
{
2012-09-11 18:20:12 +00:00
return DeserializeFromStream<DtoBaseItem[]>(stream);
2012-09-08 17:56:51 +00:00
}
}
/// <summary>
/// Gets all items that contain a given Genre
/// </summary>
2012-09-09 18:32:51 +00:00
/// <param name="folderId">(Optional) Specify a folder Id to localize the search to a specific folder.</param>
2012-09-11 18:20:12 +00:00
public async Task<DtoBaseItem[]> GetItemsWithGenreAsync(string name, Guid userId, Guid? folderId = null)
2012-09-08 17:56:51 +00:00
{
string url = ApiUrl + "/itemlist?listtype=itemswithgenre&userId=" + userId.ToString() + "&name=" + name;
2012-09-09 18:32:51 +00:00
if (folderId.HasValue)
{
url += "&id=" + folderId.ToString();
}
2012-09-08 17:56:51 +00:00
using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
{
2012-09-11 18:20:12 +00:00
return DeserializeFromStream<DtoBaseItem[]>(stream);
2012-09-08 17:56:51 +00:00
}
}
/// <summary>
/// Gets all items that contain a given Person
/// </summary>
2012-09-09 18:32:51 +00:00
/// <param name="folderId">(Optional) Specify a folder Id to localize the search to a specific folder.</param>
2012-09-11 18:20:12 +00:00
public async Task<DtoBaseItem[]> GetItemsWithPersonAsync(string name, Guid userId, Guid? folderId = null)
2012-09-08 17:56:51 +00:00
{
string url = ApiUrl + "/itemlist?listtype=itemswithperson&userId=" + userId.ToString() + "&name=" + name;
2012-09-09 18:32:51 +00:00
if (folderId.HasValue)
{
url += "&id=" + folderId.ToString();
}
2012-09-08 17:56:51 +00:00
using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
{
2012-09-11 18:20:12 +00:00
return DeserializeFromStream<DtoBaseItem[]>(stream);
2012-09-08 17:56:51 +00:00
}
}
/// <summary>
/// Gets all items that contain a given Person
/// </summary>
2012-09-09 18:32:51 +00:00
/// <param name="folderId">(Optional) Specify a folder Id to localize the search to a specific folder.</param>
2012-09-11 18:20:12 +00:00
public async Task<DtoBaseItem[]> GetItemsWithPersonAsync(string name, string personType, Guid userId, Guid? folderId = null)
2012-09-08 17:56:51 +00:00
{
string url = ApiUrl + "/itemlist?listtype=itemswithperson&userId=" + userId.ToString() + "&name=" + name;
url += "&persontype=" + personType;
2012-09-09 18:32:51 +00:00
if (folderId.HasValue)
{
url += "&id=" + folderId.ToString();
}
2012-09-08 17:56:51 +00:00
using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
{
2012-09-11 18:20:12 +00:00
return DeserializeFromStream<DtoBaseItem[]>(stream);
2012-09-08 17:56:51 +00:00
}
}
/// <summary>
/// Gets all studious
/// </summary>
2012-09-11 19:37:14 +00:00
public async Task<IbnItem[]> GetAllStudiosAsync(Guid userId)
2012-09-08 17:56:51 +00:00
{
string url = ApiUrl + "/studios?userId=" + userId.ToString();
using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
{
2012-09-11 19:37:14 +00:00
return DeserializeFromStream<IbnItem[]>(stream);
2012-09-08 17:56:51 +00:00
}
}
/// <summary>
/// Gets all items that contain a given Studio
/// </summary>
2012-09-09 18:32:51 +00:00
/// <param name="folderId">(Optional) Specify a folder Id to localize the search to a specific folder.</param>
2012-09-11 18:20:12 +00:00
public async Task<DtoBaseItem[]> GetItemsWithStudioAsync(string name, Guid userId, Guid? folderId = null)
2012-09-08 17:56:51 +00:00
{
string url = ApiUrl + "/itemlist?listtype=itemswithstudio&userId=" + userId.ToString() + "&name=" + name;
2012-09-09 18:32:51 +00:00
if (folderId.HasValue)
{
url += "&id=" + folderId.ToString();
}
2012-09-08 17:56:51 +00:00
using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
{
2012-09-11 18:20:12 +00:00
return DeserializeFromStream<DtoBaseItem[]>(stream);
2012-09-08 17:56:51 +00:00
}
}
/// <summary>
/// Gets a studio
/// </summary>
2012-09-11 19:37:14 +00:00
public async Task<IbnItem> GetStudioAsync(Guid userId, string name)
2012-09-08 17:56:51 +00:00
{
string url = ApiUrl + "/studio?userId=" + userId.ToString() + "&name=" + name;
using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
{
2012-09-11 19:37:14 +00:00
return DeserializeFromStream<IbnItem>(stream);
2012-09-08 17:56:51 +00:00
}
}
/// <summary>
/// Gets a genre
/// </summary>
2012-09-11 19:37:14 +00:00
public async Task<IbnItem> GetGenreAsync(Guid userId, string name)
2012-09-08 17:56:51 +00:00
{
string url = ApiUrl + "/genre?userId=" + userId.ToString() + "&name=" + name;
using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
{
2012-09-11 19:37:14 +00:00
return DeserializeFromStream<IbnItem>(stream);
2012-09-08 17:56:51 +00:00
}
}
/// <summary>
/// Gets a person
/// </summary>
2012-09-11 19:37:14 +00:00
public async Task<IbnItem> GetPersonAsync(Guid userId, string name)
2012-09-08 17:56:51 +00:00
{
string url = ApiUrl + "/person?userId=" + userId.ToString() + "&name=" + name;
using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
{
2012-09-11 19:37:14 +00:00
return DeserializeFromStream<IbnItem>(stream);
2012-09-08 17:56:51 +00:00
}
}
/// <summary>
/// Gets a year
/// </summary>
2012-09-11 19:37:14 +00:00
public async Task<IbnItem> GetYearAsync(Guid userId, int year)
2012-09-08 17:56:51 +00:00
{
string url = ApiUrl + "/year?userId=" + userId.ToString() + "&year=" + year;
using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
{
2012-09-11 19:37:14 +00:00
return DeserializeFromStream<IbnItem>(stream);
2012-09-08 17:56:51 +00:00
}
}
/// <summary>
/// Gets a list of plugins installed on the server
/// </summary>
public async Task<PluginInfo[]> GetInstalledPluginsAsync()
{
string url = ApiUrl + "/plugins";
using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
{
return DeserializeFromStream<PluginInfo[]>(stream);
}
}
/// <summary>
/// Gets a list of plugins installed on the server
/// </summary>
public Task<Stream> GetPluginAssemblyAsync(PluginInfo plugin)
{
string url = ApiUrl + "/pluginassembly?assemblyfilename=" + plugin.AssemblyFileName;
return GetStreamAsync(url);
}
/// <summary>
/// Gets the current server configuration
/// </summary>
public async Task<ServerConfiguration> GetServerConfigurationAsync()
{
string url = ApiUrl + "/ServerConfiguration";
using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
2012-09-08 17:56:51 +00:00
{
return DeserializeFromStream<ServerConfiguration>(stream);
2012-09-08 17:56:51 +00:00
}
}
/// <summary>
/// Gets weather information for the default location as set in configuration
/// </summary>
public async Task<object> GetPluginConfigurationAsync(PluginInfo plugin, Type configurationType)
{
string url = ApiUrl + "/PluginConfiguration?assemblyfilename=" + plugin.AssemblyFileName;
// At the moment this can't be retrieved in protobuf format
SerializationFormats format = DataSerializer.CanDeSerializeJsv ? SerializationFormats.Jsv : SerializationFormats.Json;
using (Stream stream = await GetSerializedStreamAsync(url, format).ConfigureAwait(false))
{
return DataSerializer.DeserializeFromStream(stream, format, configurationType);
}
}
/// <summary>
/// Gets the default user
/// </summary>
2012-09-11 18:20:12 +00:00
public async Task<DtoUser> GetDefaultUserAsync()
2012-09-08 17:56:51 +00:00
{
string url = ApiUrl + "/user";
using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
{
2012-09-11 18:20:12 +00:00
return DeserializeFromStream<DtoUser>(stream);
2012-09-08 17:56:51 +00:00
}
}
/// <summary>
/// Gets a user by id
/// </summary>
2012-09-11 18:20:12 +00:00
public async Task<DtoUser> GetUserAsync(Guid id)
2012-09-08 17:56:51 +00:00
{
string url = ApiUrl + "/user?id=" + id.ToString();
using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
{
2012-09-11 18:20:12 +00:00
return DeserializeFromStream<DtoUser>(stream);
2012-09-08 17:56:51 +00:00
}
}
/// <summary>
/// Gets weather information for the default location as set in configuration
/// </summary>
public async Task<WeatherInfo> GetWeatherInfoAsync()
{
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>
public async Task<WeatherInfo> GetWeatherInfoAsync(string zipCode)
{
string url = ApiUrl + "/weather?zipcode=" + zipCode;
using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
{
return DeserializeFromStream<WeatherInfo>(stream);
}
}
/// <summary>
/// Gets special features for a Movie
/// </summary>
2012-09-11 18:20:12 +00:00
public async Task<DtoBaseItem[]> GetMovieSpecialFeaturesAsync(Guid itemId, Guid userId)
{
string url = ApiUrl + "/MovieSpecialFeatures?id=" + itemId;
url += "&userid=" + userId;
using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
{
2012-09-11 18:20:12 +00:00
return DeserializeFromStream<DtoBaseItem[]>(stream);
}
}
/// <summary>
/// Updates played status for an item
/// </summary>
2012-09-11 18:20:12 +00:00
public async Task<DtoUserItemData> UpdatePlayedStatusAsync(Guid itemId, Guid userId, bool wasPlayed)
{
string url = ApiUrl + "/PlayedStatus?id=" + itemId;
url += "&userid=" + userId;
url += "&played=" + (wasPlayed ? "1" : "0");
using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
{
2012-09-11 18:20:12 +00:00
return DeserializeFromStream<DtoUserItemData>(stream);
}
}
2012-09-09 18:32:51 +00:00
/// <summary>
/// Updates a user's favorite status for an item and returns the updated UserItemData object.
/// </summary>
2012-09-11 18:20:12 +00:00
public async Task<DtoUserItemData> UpdateFavoriteStatusAsync(Guid itemId, Guid userId, bool isFavorite)
2012-09-09 18:32:51 +00:00
{
string url = ApiUrl + "/favoritestatus?id=" + itemId;
url += "&userid=" + userId;
url += "&isfavorite=" + (isFavorite ? "1" : "0");
using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
{
2012-09-11 18:20:12 +00:00
return DeserializeFromStream<DtoUserItemData>(stream);
2012-09-09 18:32:51 +00:00
}
}
/// <summary>
/// Clears a user's rating for an item
/// </summary>
2012-09-11 18:20:12 +00:00
public async Task<DtoUserItemData> ClearUserItemRatingAsync(Guid itemId, Guid userId)
{
string url = ApiUrl + "/UserItemRating?id=" + itemId;
url += "&userid=" + userId;
url += "&clear=1";
using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
{
2012-09-11 18:20:12 +00:00
return DeserializeFromStream<DtoUserItemData>(stream);
}
}
/// <summary>
/// Updates a user's rating for an item, based on likes or dislikes
/// </summary>
2012-09-11 18:20:12 +00:00
public async Task<DtoUserItemData> UpdateUserItemRatingAsync(Guid itemId, Guid userId, bool likes)
{
string url = ApiUrl + "/UserItemRating?id=" + itemId;
url += "&userid=" + userId;
url += "&likes=" + (likes ? "1" : "0");
using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
{
2012-09-11 18:20:12 +00:00
return DeserializeFromStream<DtoUserItemData>(stream);
}
}
2012-09-08 17:56:51 +00:00
/// <summary>
/// Authenticates a user and returns the result
/// </summary>
public async Task<AuthenticationResult> AuthenticateUserAsync(Guid userId, string password)
{
string url = ApiUrl + "/UserAuthentication?dataformat=" + SerializationFormat.ToString();
// Create the post body
string postContent = string.Format("userid={0}", userId);
if (!string.IsNullOrEmpty(password))
{
postContent += "&password=" + password;
}
#if WINDOWS_PHONE
HttpClient.Headers["Content-Type"] = "application/x-www-form-urlencoded";
var result = await HttpClient.UploadStringTaskAsync(url, "POST", postContent);
var byteArray = Encoding.UTF8.GetBytes(result);
using (MemoryStream stream = new MemoryStream(byteArray))
{
return DeserializeFromStream<AuthenticationResult>(stream);
}
#else
2012-09-08 17:56:51 +00:00
HttpContent content = new StringContent(postContent, Encoding.UTF8, "application/x-www-form-urlencoded");
HttpResponseMessage msg = await HttpClient.PostAsync(url, content).ConfigureAwait(false);
using (Stream stream = await msg.Content.ReadAsStreamAsync().ConfigureAwait(false))
{
return DeserializeFromStream<AuthenticationResult>(stream);
}
#endif
2012-09-08 17:56:51 +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)
{
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, SerializationFormats serializationFormat)
{
if (url.IndexOf('?') == -1)
{
url += "?dataformat=" + serializationFormat.ToString();
}
else
{
url += "&dataformat=" + serializationFormat.ToString();
}
return GetStreamAsync(url);
}
/// <summary>
/// This is just a helper around HttpClient
/// </summary>
private Task<Stream> GetStreamAsync(string url)
{
#if WINDOWS_PHONE
return HttpClient.OpenReadTaskAsync(url);
#else
2012-09-08 17:56:51 +00:00
return HttpClient.GetStreamAsync(url);
#endif
2012-09-08 17:56:51 +00:00
}
public override void Dispose()
{
#if !WINDOWS_PHONE
2012-09-08 17:56:51 +00:00
HttpClient.Dispose();
#endif
2012-09-08 17:56:51 +00:00
}
}
}