2012-09-07 01:45:24 +00:00
|
|
|
|
using MediaBrowser.Model.Configuration;
|
|
|
|
|
using MediaBrowser.Model.DTO;
|
2012-09-07 16:17:39 +00:00
|
|
|
|
using MediaBrowser.Model.Entities;
|
2012-09-07 01:45:24 +00:00
|
|
|
|
using MediaBrowser.Model.Weather;
|
|
|
|
|
using System;
|
2012-07-31 16:29:07 +00:00
|
|
|
|
using System.IO;
|
2012-08-18 08:22:54 +00:00
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Net.Http;
|
2012-09-06 20:29:33 +00:00
|
|
|
|
using System.Text;
|
2012-07-31 16:29:07 +00:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.ApiInteraction
|
|
|
|
|
{
|
2012-09-07 01:25:09 +00:00
|
|
|
|
public class ApiClient : BaseApiClient
|
2012-07-31 16:29:07 +00:00
|
|
|
|
{
|
2012-08-18 08:22:54 +00:00
|
|
|
|
public ApiClient(HttpClientHandler handler)
|
2012-09-07 01:25:09 +00:00
|
|
|
|
: base()
|
2012-08-18 08:22:54 +00:00
|
|
|
|
{
|
|
|
|
|
handler.AutomaticDecompression = DecompressionMethods.Deflate;
|
|
|
|
|
|
|
|
|
|
HttpClient = new HttpClient(handler);
|
2012-08-25 20:02:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-09-07 01:25:09 +00:00
|
|
|
|
private HttpClient HttpClient { get; set; }
|
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-09-06 00:17:54 +00:00
|
|
|
|
public async Task<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-09-06 00:17:54 +00:00
|
|
|
|
public async Task<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-09-07 07:07:29 +00:00
|
|
|
|
/// <summary>
|
2012-09-07 20:59:59 +00:00
|
|
|
|
/// Gets in-progress items
|
2012-09-07 07:07:29 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="userId">The user id.</param>
|
2012-09-07 20:59:59 +00:00
|
|
|
|
/// <param name="folderId">(Optional) Specify a folder Id to localize the search to a specific folder.</param>
|
|
|
|
|
public async Task<DTOBaseItem[]> GetInProgressItemsItemsAsync(Guid userId, Guid? folderId = null)
|
2012-09-07 07:07:29 +00:00
|
|
|
|
{
|
2012-09-07 20:59:59 +00:00
|
|
|
|
string url = ApiUrl + "/itemlist?listtype=inprogressitems&userId=" + userId.ToString();
|
|
|
|
|
|
|
|
|
|
if (folderId.HasValue)
|
|
|
|
|
{
|
|
|
|
|
url += "&id=" + folderId.ToString();
|
|
|
|
|
}
|
2012-09-07 16:17:39 +00:00
|
|
|
|
|
|
|
|
|
using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
|
2012-09-07 07:07:29 +00:00
|
|
|
|
{
|
|
|
|
|
return DeserializeFromStream<DTOBaseItem[]>(stream);
|
|
|
|
|
}
|
2012-09-07 16:17:39 +00:00
|
|
|
|
}
|
2012-09-07 07:07:29 +00:00
|
|
|
|
|
2012-09-07 16:17:39 +00:00
|
|
|
|
/// <summary>
|
2012-09-07 20:59:59 +00:00
|
|
|
|
/// Gets recently added items
|
2012-09-07 16:17:39 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="userId">The user id.</param>
|
2012-09-07 20:59:59 +00:00
|
|
|
|
/// <param name="folderId">(Optional) Specify a folder Id to localize the search to a specific folder.</param>
|
|
|
|
|
public async Task<DTOBaseItem[]> GetRecentlyAddedItemsAsync(Guid userId, Guid? folderId = null)
|
2012-09-07 16:17:39 +00:00
|
|
|
|
{
|
|
|
|
|
string url = ApiUrl + "/itemlist?listtype=recentlyaddeditems&userId=" + userId.ToString();
|
|
|
|
|
|
2012-09-07 20:59:59 +00:00
|
|
|
|
if (folderId.HasValue)
|
|
|
|
|
{
|
|
|
|
|
url += "&id=" + folderId.ToString();
|
|
|
|
|
}
|
2012-09-07 16:17:39 +00:00
|
|
|
|
|
|
|
|
|
using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
|
|
|
|
|
{
|
|
|
|
|
return DeserializeFromStream<DTOBaseItem[]>(stream);
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-09-07 20:59:59 +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>
|
|
|
|
|
public async Task<DTOBaseItem[]> GetRecentlyAddedUnplayedItemsAsync(Guid userId, Guid? folderId = null)
|
|
|
|
|
{
|
|
|
|
|
string url = ApiUrl + "/itemlist?listtype=recentlyaddedunplayeditems&userId=" + userId.ToString();
|
|
|
|
|
|
|
|
|
|
if (folderId.HasValue)
|
|
|
|
|
{
|
|
|
|
|
url += "&id=" + folderId.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
|
|
|
|
|
{
|
|
|
|
|
return DeserializeFromStream<DTOBaseItem[]>(stream);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-14 16:06:46 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets all Years
|
|
|
|
|
/// </summary>
|
2012-09-06 00:17:54 +00:00
|
|
|
|
public async Task<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-09-06 00:17:54 +00:00
|
|
|
|
public async Task<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-09-06 00:17:54 +00:00
|
|
|
|
public async Task<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-09-06 00:17:54 +00:00
|
|
|
|
public async Task<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>
|
2012-09-06 00:17:54 +00:00
|
|
|
|
public async Task<DTOBaseItem[]> GetItemsWithPersonAsync(string name, string personType, Guid userId)
|
2012-08-18 08:22:54 +00:00
|
|
|
|
{
|
|
|
|
|
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-09-06 00:17:54 +00:00
|
|
|
|
public async Task<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-09-06 00:17:54 +00:00
|
|
|
|
public async Task<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>
|
2012-09-03 19:12:02 +00:00
|
|
|
|
public async Task<PluginInfo[]> GetInstalledPluginsAsync()
|
2012-09-03 18:15:07 +00:00
|
|
|
|
{
|
|
|
|
|
string url = ApiUrl + "/plugins";
|
|
|
|
|
|
|
|
|
|
using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
|
|
|
|
|
{
|
|
|
|
|
return DeserializeFromStream<PluginInfo[]>(stream);
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-09-03 19:12:02 +00:00
|
|
|
|
|
|
|
|
|
/// <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);
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-02 05:30:25 +00:00
|
|
|
|
/// <summary>
|
2012-09-03 21:56:30 +00:00
|
|
|
|
/// Gets the current server configuration
|
2012-09-02 05:30:25 +00:00
|
|
|
|
/// </summary>
|
2012-09-02 13:45:02 +00:00
|
|
|
|
public async Task<ServerConfiguration> GetServerConfigurationAsync()
|
|
|
|
|
{
|
|
|
|
|
string url = ApiUrl + "/ServerConfiguration";
|
|
|
|
|
|
2012-09-03 21:56:30 +00:00
|
|
|
|
// At the moment this can't be retrieved in protobuf format
|
2012-09-06 14:00:44 +00:00
|
|
|
|
SerializationFormats format = DataSerializer.CanDeSerializeJsv ? SerializationFormats.Jsv : SerializationFormats.Json;
|
2012-09-03 21:56:30 +00:00
|
|
|
|
|
|
|
|
|
using (Stream stream = await GetSerializedStreamAsync(url, format).ConfigureAwait(false))
|
|
|
|
|
{
|
2012-09-06 14:00:44 +00:00
|
|
|
|
return DataSerializer.DeserializeFromStream<ServerConfiguration>(stream, format);
|
2012-09-03 21:56:30 +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
|
2012-09-06 14:00:44 +00:00
|
|
|
|
SerializationFormats format = DataSerializer.CanDeSerializeJsv ? SerializationFormats.Jsv : SerializationFormats.Json;
|
2012-09-03 21:56:30 +00:00
|
|
|
|
|
|
|
|
|
using (Stream stream = await GetSerializedStreamAsync(url, format).ConfigureAwait(false))
|
2012-09-02 13:45:02 +00:00
|
|
|
|
{
|
2012-09-06 14:00:44 +00:00
|
|
|
|
return DataSerializer.DeserializeFromStream(stream, format, configurationType);
|
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-09-06 18:38:29 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Authenticates a user and returns the result
|
|
|
|
|
/// </summary>
|
2012-09-06 19:20:35 +00:00
|
|
|
|
public async Task<AuthenticationResult> AuthenticateUserAsync(Guid userId, string password)
|
2012-09-06 18:38:29 +00:00
|
|
|
|
{
|
2012-09-06 18:50:16 +00:00
|
|
|
|
string url = ApiUrl + "/UserAuthentication?dataformat=" + SerializationFormat.ToString();
|
2012-09-06 18:38:29 +00:00
|
|
|
|
|
2012-09-06 18:50:16 +00:00
|
|
|
|
// Create the post body
|
2012-09-07 16:24:42 +00:00
|
|
|
|
string postContent = string.Format("userid={0}", userId);
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(password))
|
|
|
|
|
{
|
|
|
|
|
postContent += "&password=" + password;
|
|
|
|
|
}
|
2012-09-06 18:42:21 +00:00
|
|
|
|
|
|
|
|
|
HttpContent content = new StringContent(postContent, Encoding.UTF8, "application/x-www-form-urlencoded");
|
2012-09-06 18:38:29 +00:00
|
|
|
|
|
|
|
|
|
HttpResponseMessage msg = await HttpClient.PostAsync(url, content).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
using (Stream stream = await msg.Content.ReadAsStreamAsync().ConfigureAwait(false))
|
|
|
|
|
{
|
|
|
|
|
return DeserializeFromStream<AuthenticationResult>(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>
|
2012-09-06 14:00:44 +00:00
|
|
|
|
private Task<Stream> GetSerializedStreamAsync(string url, SerializationFormats serializationFormat)
|
2012-08-22 12:56:44 +00:00
|
|
|
|
{
|
2012-08-22 13:05:25 +00:00
|
|
|
|
if (url.IndexOf('?') == -1)
|
|
|
|
|
{
|
2012-09-06 18:38:29 +00:00
|
|
|
|
url += "?dataformat=" + serializationFormat.ToString();
|
2012-08-22 13:05:25 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2012-09-06 18:38:29 +00:00
|
|
|
|
url += "&dataformat=" + serializationFormat.ToString();
|
2012-08-22 13:05:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-22 12:56:44 +00:00
|
|
|
|
return GetStreamAsync(url);
|
|
|
|
|
}
|
2012-09-03 19:12:02 +00:00
|
|
|
|
|
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-09-07 01:25:09 +00:00
|
|
|
|
public override void Dispose()
|
2012-08-15 13:20:29 +00:00
|
|
|
|
{
|
|
|
|
|
HttpClient.Dispose();
|
|
|
|
|
}
|
2012-07-31 16:29:07 +00:00
|
|
|
|
}
|
|
|
|
|
}
|