using MediaBrowser.Model.Authentication;
using MediaBrowser.Model.Configuration;
using MediaBrowser.Model.DTO;
using MediaBrowser.Model.Weather;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
namespace MediaBrowser.ApiInteraction.Portable
{
public class ApiClient : BaseApiClient
{
private HttpWebRequest GetNewRequest(string url)
{
return HttpWebRequest.CreateHttp(url);
}
///
/// Gets an image stream based on a url
///
public void GetImageStreamAsync(string url, Action callback)
{
GetStreamAsync(url, callback);
}
///
/// Gets an image stream based on a url
///
private void GetStreamAsync(string url, Action callback)
{
HttpWebRequest request = GetNewRequest(url);
request.BeginGetResponse(new AsyncCallback(result =>
{
using (WebResponse response = (result.AsyncState as HttpWebRequest).EndGetResponse(result))
{
Stream stream = response.GetResponseStream();
callback(stream);
}
}), request);
}
///
/// Gets a BaseItem
///
public void GetItemAsync(Guid id, Guid userId, Action callback)
{
string url = ApiUrl + "/item?userId=" + userId.ToString();
if (id != Guid.Empty)
{
url += "&id=" + id.ToString();
}
GetDataAsync(url, callback);
}
///
/// Gets all users
///
public void GetAllUsersAsync(Action callback)
{
string url = ApiUrl + "/users";
GetDataAsync(url, callback);
}
///
/// Gets all Genres
///
public void GetAllGenresAsync(Guid userId, Action callback)
{
string url = ApiUrl + "/genres?userId=" + userId.ToString();
GetDataAsync(url, callback);
}
///
/// Gets in-progress items
///
/// The user id.
/// (Optional) Specify a folder Id to localize the search to a specific folder.
public void GetInProgressItemsItemsAsync(Guid userId, Action callback, Guid? folderId = null)
{
string url = ApiUrl + "/itemlist?listtype=inprogressitems&userId=" + userId.ToString();
if (folderId.HasValue)
{
url += "&id=" + folderId.ToString();
}
GetDataAsync(url, callback);
}
///
/// Gets recently added items
///
/// The user id.
/// (Optional) Specify a folder Id to localize the search to a specific folder.
public void GetRecentlyAddedItemsAsync(Guid userId, Action callback, Guid? folderId = null)
{
string url = ApiUrl + "/itemlist?listtype=recentlyaddeditems&userId=" + userId.ToString();
if (folderId.HasValue)
{
url += "&id=" + folderId.ToString();
}
GetDataAsync(url, callback);
}
///
/// Gets recently added items that are unplayed.
///
/// The user id.
/// (Optional) Specify a folder Id to localize the search to a specific folder.
public void GetRecentlyAddedUnplayedItemsAsync(Guid userId, Action callback, Guid? folderId = null)
{
string url = ApiUrl + "/itemlist?listtype=recentlyaddedunplayeditems&userId=" + userId.ToString();
if (folderId.HasValue)
{
url += "&id=" + folderId.ToString();
}
GetDataAsync(url, callback);
}
///
/// Gets all Years
///
public void GetAllYearsAsync(Guid userId, Action callback)
{
string url = ApiUrl + "/years?userId=" + userId.ToString();
GetDataAsync(url, callback);
}
///
/// Gets all items that contain a given Year
///
public void GetItemsWithYearAsync(string name, Guid userId, Action callback)
{
string url = ApiUrl + "/itemlist?listtype=itemswithyear&userId=" + userId.ToString() + "&name=" + name;
GetDataAsync(url, callback);
}
///
/// Gets all items that contain a given Genre
///
public void GetItemsWithGenreAsync(string name, Guid userId, Action callback)
{
string url = ApiUrl + "/itemlist?listtype=itemswithgenre&userId=" + userId.ToString() + "&name=" + name;
GetDataAsync(url, callback);
}
///
/// Gets all items that contain a given Person
///
public void GetItemsWithPersonAsync(string name, Guid userId, Action callback)
{
string url = ApiUrl + "/itemlist?listtype=itemswithperson&userId=" + userId.ToString() + "&name=" + name;
GetDataAsync(url, callback);
}
///
/// Gets all items that contain a given Person
///
public void GetItemsWithPersonAsync(string name, string personType, Guid userId, Action callback)
{
string url = ApiUrl + "/itemlist?listtype=itemswithperson&userId=" + userId.ToString() + "&name=" + name;
url += "&persontype=" + personType;
GetDataAsync(url, callback);
}
///
/// Gets all studious
///
public void GetAllStudiosAsync(Guid userId, Action callback)
{
string url = ApiUrl + "/studios?userId=" + userId.ToString();
GetDataAsync(url, callback);
}
///
/// Gets all items that contain a given Studio
///
public void GetItemsWithStudioAsync(string name, Guid userId, Action callback)
{
string url = ApiUrl + "/itemlist?listtype=itemswithstudio&userId=" + userId.ToString() + "&name=" + name;
GetDataAsync(url, callback);
}
///
/// Gets a studio
///
public void GetStudioAsync(Guid userId, string name, Action callback)
{
string url = ApiUrl + "/studio?userId=" + userId.ToString() + "&name=" + name;
GetDataAsync(url, callback);
}
///
/// Gets a genre
///
public void GetGenreAsync(Guid userId, string name, Action callback)
{
string url = ApiUrl + "/genre?userId=" + userId.ToString() + "&name=" + name;
GetDataAsync(url, callback);
}
///
/// Gets a person
///
public void GetPersonAsync(Guid userId, string name, Action callback)
{
string url = ApiUrl + "/person?userId=" + userId.ToString() + "&name=" + name;
GetDataAsync(url, callback);
}
///
/// Gets a year
///
public void GetYearAsync(Guid userId, int year, Action callback)
{
string url = ApiUrl + "/year?userId=" + userId.ToString() + "&year=" + year;
GetDataAsync(url, callback);
}
///
/// Gets a list of plugins installed on the server
///
public void GetInstalledPluginsAsync(Action callback)
{
string url = ApiUrl + "/plugins";
GetDataAsync(url, callback);
}
///
/// Gets a list of plugins installed on the server
///
public void GetPluginAssemblyAsync(PluginInfo plugin, Action callback)
{
string url = ApiUrl + "/pluginassembly?assemblyfilename=" + plugin.AssemblyFileName;
GetStreamAsync(url, callback);
}
///
/// Gets the current server configuration
///
public void GetServerConfigurationAsync(Action callback)
{
string url = ApiUrl + "/ServerConfiguration";
GetDataAsync(url, callback);
}
///
/// Gets weather information for the default location as set in configuration
///
public void GetPluginConfigurationAsync(PluginInfo plugin, Type configurationType, Action