diff --git a/MediaBrowser.ApiInteraction.Metro/ApiClient.cs b/MediaBrowser.ApiInteraction.Metro/ApiClient.cs new file mode 100644 index 0000000000..bf49a896ee --- /dev/null +++ b/MediaBrowser.ApiInteraction.Metro/ApiClient.cs @@ -0,0 +1,12 @@ +using System.Net.Http; + +namespace MediaBrowser.ApiInteraction +{ + public class ApiClient : BaseHttpApiClient + { + public ApiClient(HttpClientHandler handler) + : base(handler) + { + } + } +} diff --git a/MediaBrowser.ApiInteraction.Metro/MediaBrowser.ApiInteraction.Metro.csproj b/MediaBrowser.ApiInteraction.Metro/MediaBrowser.ApiInteraction.Metro.csproj index 8b59cfbfe2..00e0992b52 100644 --- a/MediaBrowser.ApiInteraction.Metro/MediaBrowser.ApiInteraction.Metro.csproj +++ b/MediaBrowser.ApiInteraction.Metro/MediaBrowser.ApiInteraction.Metro.csproj @@ -39,15 +39,16 @@ - - ApiClient.cs - BaseApiClient.cs + + BaseHttpApiClient.cs + SerializationFormats.cs + diff --git a/MediaBrowser.ApiInteraction/ApiClient.cs b/MediaBrowser.ApiInteraction/ApiClient.cs index 62a534e326..14326ba40c 100644 --- a/MediaBrowser.ApiInteraction/ApiClient.cs +++ b/MediaBrowser.ApiInteraction/ApiClient.cs @@ -1,453 +1,18 @@ -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.Net.Cache; using System.Net.Http; -using System.Text; -using System.Threading.Tasks; namespace MediaBrowser.ApiInteraction { - public class ApiClient : BaseApiClient + public class ApiClient : BaseHttpApiClient { public ApiClient(HttpClientHandler handler) - : base() + : base(handler) { - handler.AutomaticDecompression = DecompressionMethods.Deflate; - - HttpClient = new HttpClient(handler); - } - - private HttpClient HttpClient { get; set; } - - /// - /// Gets an image stream based on a url - /// - public Task GetImageStreamAsync(string url) - { - return 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 GetSerializedStreamAsync(url).ConfigureAwait(false)) - { - return DeserializeFromStream(stream); - } - } - - /// - /// Gets all Users - /// - public async Task GetAllUsersAsync() - { - string url = ApiUrl + "/users"; - - using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false)) - { - return DeserializeFromStream(stream); - } - } - - /// - /// Gets all Genres - /// - public async Task GetAllGenresAsync(Guid userId) - { - string url = ApiUrl + "/genres?userId=" + userId.ToString(); - - using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false)) - { - return DeserializeFromStream(stream); - } - } - - /// - /// Gets in-progress items - /// - /// The user id. - /// (Optional) Specify a folder Id to localize the search to a specific folder. - public async Task GetInProgressItemsItemsAsync(Guid userId, Guid? folderId = null) - { - string url = ApiUrl + "/itemlist?listtype=inprogressitems&userId=" + userId.ToString(); - - if (folderId.HasValue) - { - url += "&id=" + folderId.ToString(); - } - - using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false)) - { - return DeserializeFromStream(stream); - } - } - - /// - /// Gets recently added items - /// - /// The user id. - /// (Optional) Specify a folder Id to localize the search to a specific folder. - public async Task GetRecentlyAddedItemsAsync(Guid userId, Guid? folderId = null) - { - string url = ApiUrl + "/itemlist?listtype=recentlyaddeditems&userId=" + userId.ToString(); - - if (folderId.HasValue) - { - url += "&id=" + folderId.ToString(); - } - - using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false)) - { - return DeserializeFromStream(stream); - } - } - - /// - /// Gets recently added items that are unplayed. - /// - /// The user id. - /// (Optional) Specify a folder Id to localize the search to a specific folder. - public async Task 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(stream); - } - } - - /// - /// Gets all Years - /// - public async Task GetAllYearsAsync(Guid userId) - { - string url = ApiUrl + "/years?userId=" + userId.ToString(); - - using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false)) - { - return 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 GetSerializedStreamAsync(url).ConfigureAwait(false)) - { - return 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 GetSerializedStreamAsync(url).ConfigureAwait(false)) - { - return 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 GetSerializedStreamAsync(url).ConfigureAwait(false)) - { - return 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 GetSerializedStreamAsync(url).ConfigureAwait(false)) - { - return DeserializeFromStream(stream); - } - } - - /// - /// Gets all studious - /// - public async Task GetAllStudiosAsync(Guid userId) - { - string url = ApiUrl + "/studios?userId=" + userId.ToString(); - - using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false)) - { - return 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 GetSerializedStreamAsync(url).ConfigureAwait(false)) - { - return 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 GetSerializedStreamAsync(url).ConfigureAwait(false)) - { - return 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 GetSerializedStreamAsync(url).ConfigureAwait(false)) - { - return 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 GetSerializedStreamAsync(url).ConfigureAwait(false)) - { - return 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 GetSerializedStreamAsync(url).ConfigureAwait(false)) - { - return DeserializeFromStream(stream); - } - } - - /// - /// Gets a list of plugins installed on the server - /// - public async Task GetInstalledPluginsAsync() - { - string url = ApiUrl + "/plugins"; - - using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false)) - { - return DeserializeFromStream(stream); - } - } - - /// - /// Gets a list of plugins installed on the server - /// - public Task GetPluginAssemblyAsync(PluginInfo plugin) - { - string url = ApiUrl + "/pluginassembly?assemblyfilename=" + plugin.AssemblyFileName; - - return GetStreamAsync(url); - } - - /// - /// Gets the current server configuration - /// - public async Task GetServerConfigurationAsync() - { - string url = ApiUrl + "/ServerConfiguration"; - - // 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); - } - } - - /// - /// Gets weather information for the default location as set in configuration - /// - public async Task 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); - } - } - - /// - /// Gets the default user - /// - public async Task GetDefaultUserAsync() - { - string url = ApiUrl + "/user"; - - using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false)) - { - return DeserializeFromStream(stream); - } - } - - /// - /// Gets a user by id - /// - public async Task GetUserAsync(Guid id) - { - string url = ApiUrl + "/user?id=" + id.ToString(); - - using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false)) - { - return DeserializeFromStream(stream); - } - } - - /// - /// Gets weather information for the default location as set in configuration - /// - public async Task GetWeatherInfoAsync() - { - string url = ApiUrl + "/weather"; - - using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false)) - { - return DeserializeFromStream(stream); - } - } - - /// - /// Gets weather information for a specific zip code - /// - public async Task GetWeatherInfoAsync(string zipCode) - { - string url = ApiUrl + "/weather?zipcode=" + zipCode; - - using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false)) - { - return DeserializeFromStream(stream); - } - } - - /// - /// Authenticates a user and returns the result - /// - public async Task 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; - } - - 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(stream); - } - } - - /// - /// This is a helper around getting a stream from the server that contains serialized data - /// - private Task GetSerializedStreamAsync(string url) - { - return GetSerializedStreamAsync(url, SerializationFormat); - } - - /// - /// This is a helper around getting a stream from the server that contains serialized data - /// - private Task GetSerializedStreamAsync(string url, SerializationFormats serializationFormat) - { - if (url.IndexOf('?') == -1) - { - url += "?dataformat=" + serializationFormat.ToString(); - } - else - { - url += "&dataformat=" + serializationFormat.ToString(); - } - - return GetStreamAsync(url); - } - - /// - /// This is just a helper around HttpClient - /// - private Task GetStreamAsync(string url) - { - return HttpClient.GetStreamAsync(url); } - public override void Dispose() + public ApiClient() + : this(new WebRequestHandler() { CachePolicy = new RequestCachePolicy(RequestCacheLevel.Revalidate) }) { - HttpClient.Dispose(); } } } diff --git a/MediaBrowser.ApiInteraction/BaseApiClient.cs b/MediaBrowser.ApiInteraction/BaseApiClient.cs index c218db0ae8..59fb72911d 100644 --- a/MediaBrowser.ApiInteraction/BaseApiClient.cs +++ b/MediaBrowser.ApiInteraction/BaseApiClient.cs @@ -5,6 +5,9 @@ using System.IO; namespace MediaBrowser.ApiInteraction { + /// + /// Provides api methods that are usable on all platforms + /// public abstract class BaseApiClient : IDisposable { public BaseApiClient() diff --git a/MediaBrowser.ApiInteraction/BaseHttpApiClient.cs b/MediaBrowser.ApiInteraction/BaseHttpApiClient.cs new file mode 100644 index 0000000000..f298e1f7ec --- /dev/null +++ b/MediaBrowser.ApiInteraction/BaseHttpApiClient.cs @@ -0,0 +1,456 @@ +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.Net.Http; +using System.Text; +using System.Threading.Tasks; + +namespace MediaBrowser.ApiInteraction +{ + /// + /// Provides api methods centered around an HttpClient + /// + public abstract class BaseHttpApiClient : BaseApiClient + { + public BaseHttpApiClient(HttpClientHandler handler) + : base() + { + handler.AutomaticDecompression = DecompressionMethods.Deflate; + + HttpClient = new HttpClient(handler); + } + + private HttpClient HttpClient { get; set; } + + /// + /// Gets an image stream based on a url + /// + public Task GetImageStreamAsync(string url) + { + return 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 GetSerializedStreamAsync(url).ConfigureAwait(false)) + { + return DeserializeFromStream(stream); + } + } + + /// + /// Gets all Users + /// + public async Task GetAllUsersAsync() + { + string url = ApiUrl + "/users"; + + using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false)) + { + return DeserializeFromStream(stream); + } + } + + /// + /// Gets all Genres + /// + public async Task GetAllGenresAsync(Guid userId) + { + string url = ApiUrl + "/genres?userId=" + userId.ToString(); + + using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false)) + { + return DeserializeFromStream(stream); + } + } + + /// + /// Gets in-progress items + /// + /// The user id. + /// (Optional) Specify a folder Id to localize the search to a specific folder. + public async Task GetInProgressItemsItemsAsync(Guid userId, Guid? folderId = null) + { + string url = ApiUrl + "/itemlist?listtype=inprogressitems&userId=" + userId.ToString(); + + if (folderId.HasValue) + { + url += "&id=" + folderId.ToString(); + } + + using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false)) + { + return DeserializeFromStream(stream); + } + } + + /// + /// Gets recently added items + /// + /// The user id. + /// (Optional) Specify a folder Id to localize the search to a specific folder. + public async Task GetRecentlyAddedItemsAsync(Guid userId, Guid? folderId = null) + { + string url = ApiUrl + "/itemlist?listtype=recentlyaddeditems&userId=" + userId.ToString(); + + if (folderId.HasValue) + { + url += "&id=" + folderId.ToString(); + } + + using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false)) + { + return DeserializeFromStream(stream); + } + } + + /// + /// Gets recently added items that are unplayed. + /// + /// The user id. + /// (Optional) Specify a folder Id to localize the search to a specific folder. + public async Task 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(stream); + } + } + + /// + /// Gets all Years + /// + public async Task GetAllYearsAsync(Guid userId) + { + string url = ApiUrl + "/years?userId=" + userId.ToString(); + + using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false)) + { + return 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 GetSerializedStreamAsync(url).ConfigureAwait(false)) + { + return 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 GetSerializedStreamAsync(url).ConfigureAwait(false)) + { + return 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 GetSerializedStreamAsync(url).ConfigureAwait(false)) + { + return 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 GetSerializedStreamAsync(url).ConfigureAwait(false)) + { + return DeserializeFromStream(stream); + } + } + + /// + /// Gets all studious + /// + public async Task GetAllStudiosAsync(Guid userId) + { + string url = ApiUrl + "/studios?userId=" + userId.ToString(); + + using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false)) + { + return 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 GetSerializedStreamAsync(url).ConfigureAwait(false)) + { + return 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 GetSerializedStreamAsync(url).ConfigureAwait(false)) + { + return 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 GetSerializedStreamAsync(url).ConfigureAwait(false)) + { + return 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 GetSerializedStreamAsync(url).ConfigureAwait(false)) + { + return 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 GetSerializedStreamAsync(url).ConfigureAwait(false)) + { + return DeserializeFromStream(stream); + } + } + + /// + /// Gets a list of plugins installed on the server + /// + public async Task GetInstalledPluginsAsync() + { + string url = ApiUrl + "/plugins"; + + using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false)) + { + return DeserializeFromStream(stream); + } + } + + /// + /// Gets a list of plugins installed on the server + /// + public Task GetPluginAssemblyAsync(PluginInfo plugin) + { + string url = ApiUrl + "/pluginassembly?assemblyfilename=" + plugin.AssemblyFileName; + + return GetStreamAsync(url); + } + + /// + /// Gets the current server configuration + /// + public async Task GetServerConfigurationAsync() + { + string url = ApiUrl + "/ServerConfiguration"; + + // 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); + } + } + + /// + /// Gets weather information for the default location as set in configuration + /// + public async Task 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); + } + } + + /// + /// Gets the default user + /// + public async Task GetDefaultUserAsync() + { + string url = ApiUrl + "/user"; + + using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false)) + { + return DeserializeFromStream(stream); + } + } + + /// + /// Gets a user by id + /// + public async Task GetUserAsync(Guid id) + { + string url = ApiUrl + "/user?id=" + id.ToString(); + + using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false)) + { + return DeserializeFromStream(stream); + } + } + + /// + /// Gets weather information for the default location as set in configuration + /// + public async Task GetWeatherInfoAsync() + { + string url = ApiUrl + "/weather"; + + using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false)) + { + return DeserializeFromStream(stream); + } + } + + /// + /// Gets weather information for a specific zip code + /// + public async Task GetWeatherInfoAsync(string zipCode) + { + string url = ApiUrl + "/weather?zipcode=" + zipCode; + + using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false)) + { + return DeserializeFromStream(stream); + } + } + + /// + /// Authenticates a user and returns the result + /// + public async Task 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; + } + + 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(stream); + } + } + + /// + /// This is a helper around getting a stream from the server that contains serialized data + /// + private Task GetSerializedStreamAsync(string url) + { + return GetSerializedStreamAsync(url, SerializationFormat); + } + + /// + /// This is a helper around getting a stream from the server that contains serialized data + /// + private Task GetSerializedStreamAsync(string url, SerializationFormats serializationFormat) + { + if (url.IndexOf('?') == -1) + { + url += "?dataformat=" + serializationFormat.ToString(); + } + else + { + url += "&dataformat=" + serializationFormat.ToString(); + } + + return GetStreamAsync(url); + } + + /// + /// This is just a helper around HttpClient + /// + private Task GetStreamAsync(string url) + { + return HttpClient.GetStreamAsync(url); + } + + public override void Dispose() + { + HttpClient.Dispose(); + } + } +} diff --git a/MediaBrowser.ApiInteraction/MediaBrowser.ApiInteraction.csproj b/MediaBrowser.ApiInteraction/MediaBrowser.ApiInteraction.csproj index 0d1f25b05d..aba95a93ad 100644 --- a/MediaBrowser.ApiInteraction/MediaBrowser.ApiInteraction.csproj +++ b/MediaBrowser.ApiInteraction/MediaBrowser.ApiInteraction.csproj @@ -39,6 +39,7 @@ + @@ -48,6 +49,7 @@ +