Added an ApiInteraction project that UI's can use to talk to the server. Moved jsonserializer namespace from json to serialization, since we may be adding an xml serializer.
This commit is contained in:
parent
3058b71764
commit
defd8ed253
|
@ -1,7 +1,6 @@
|
|||
using System.IO;
|
||||
using MediaBrowser.Common.Net.Handlers;
|
||||
using MediaBrowser.Controller;
|
||||
using MediaBrowser.Common.Json;
|
||||
using MediaBrowser.Common.Serialization;
|
||||
|
||||
namespace MediaBrowser.Api.HttpHandlers
|
||||
{
|
||||
|
|
196
MediaBrowser.ApiInteraction/ApiClient.cs
Normal file
196
MediaBrowser.ApiInteraction/ApiClient.cs
Normal file
|
@ -0,0 +1,196 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.IO.Compression;
|
||||
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()
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets an image url that can be used to download an image from the api
|
||||
/// </summary>
|
||||
/// <param name="itemId">The Id of the item</param>
|
||||
/// <param name="imageType">The type of image requested</param>
|
||||
/// <param name="imageIndex">The image index, if there are multiple. Currently only applies to backdrops. Supply null or 0 for first backdrop.</param>
|
||||
/// <param name="width">Use if a fixed width is required. Aspect ratio will be preserved.</param>
|
||||
/// <param name="height">Use if a fixed height is required. Aspect ratio will be preserved.</param>
|
||||
/// <param name="maxWidth">Use if a max width is required. Aspect ratio will be preserved.</param>
|
||||
/// <param name="maxHeight">Use if a max height is required. Aspect ratio will be preserved.</param>
|
||||
/// <param name="quality">Quality level, from 0-100. Currently only applies to JPG. The default value should suffice.</param>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets an image stream based on a url
|
||||
/// </summary>
|
||||
public async Task<Stream> GetImageStream(string url)
|
||||
{
|
||||
Stream stream = await HttpClient.GetStreamAsync(url);
|
||||
|
||||
// For now this assumes the response stream is compressed. We can always improve this later.
|
||||
return new GZipStream(stream, CompressionMode.Decompress, false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a BaseItem
|
||||
/// </summary>
|
||||
public async Task<ApiBaseItemWrapper<ApiBaseItem>> 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))
|
||||
{
|
||||
using (GZipStream gzipStream = new GZipStream(stream, CompressionMode.Decompress, false))
|
||||
{
|
||||
return JsonSerializer.DeserializeFromStream<ApiBaseItemWrapper<ApiBaseItem>>(gzipStream);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets all Users
|
||||
/// </summary>
|
||||
public async Task<IEnumerable<User>> GetAllUsersAsync()
|
||||
{
|
||||
string url = ApiUrl + "/users";
|
||||
|
||||
using (Stream stream = await HttpClient.GetStreamAsync(url))
|
||||
{
|
||||
using (GZipStream gzipStream = new GZipStream(stream, CompressionMode.Decompress, false))
|
||||
{
|
||||
return JsonSerializer.DeserializeFromStream<IEnumerable<User>>(gzipStream);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets all Genres
|
||||
/// </summary>
|
||||
public async Task<IEnumerable<CategoryInfo<Genre>>> GetAllGenresAsync(Guid userId)
|
||||
{
|
||||
string url = ApiUrl + "/genres?userId=" + userId.ToString();
|
||||
|
||||
using (Stream stream = await HttpClient.GetStreamAsync(url))
|
||||
{
|
||||
using (GZipStream gzipStream = new GZipStream(stream, CompressionMode.Decompress, false))
|
||||
{
|
||||
return JsonSerializer.DeserializeFromStream<IEnumerable<CategoryInfo<Genre>>>(gzipStream);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a Genre
|
||||
/// </summary>
|
||||
public async Task<CategoryInfo<Genre>> GetGenreAsync(string name, Guid userId)
|
||||
{
|
||||
string url = ApiUrl + "/genre?userId=" + userId.ToString() + "&name=" + name;
|
||||
|
||||
using (Stream stream = await HttpClient.GetStreamAsync(url))
|
||||
{
|
||||
using (GZipStream gzipStream = new GZipStream(stream, CompressionMode.Decompress, false))
|
||||
{
|
||||
return JsonSerializer.DeserializeFromStream<CategoryInfo<Genre>>(gzipStream);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets all studious
|
||||
/// </summary>
|
||||
public async Task<IEnumerable<CategoryInfo<Studio>>> GetAllStudiosAsync(Guid userId)
|
||||
{
|
||||
string url = ApiUrl + "/studios?userId=" + userId.ToString();
|
||||
|
||||
using (Stream stream = await HttpClient.GetStreamAsync(url))
|
||||
{
|
||||
using (GZipStream gzipStream = new GZipStream(stream, CompressionMode.Decompress, false))
|
||||
{
|
||||
return JsonSerializer.DeserializeFromStream<IEnumerable<CategoryInfo<Studio>>>(gzipStream);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the current personalized configuration
|
||||
/// </summary>
|
||||
public async Task<UserConfiguration> GetUserConfigurationAsync(Guid userId)
|
||||
{
|
||||
string url = ApiUrl + "/userconfiguration?userId=" + userId.ToString();
|
||||
|
||||
using (Stream stream = await HttpClient.GetStreamAsync(url))
|
||||
{
|
||||
using (GZipStream gzipStream = new GZipStream(stream, CompressionMode.Decompress, false))
|
||||
{
|
||||
return JsonSerializer.DeserializeFromStream<UserConfiguration>(gzipStream);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a Studio
|
||||
/// </summary>
|
||||
public async Task<CategoryInfo<Studio>> GetStudioAsync(string name, Guid userId)
|
||||
{
|
||||
string url = ApiUrl + "/studio?userId=" + userId.ToString() + "&name=" + name;
|
||||
|
||||
using (Stream stream = await HttpClient.GetStreamAsync(url))
|
||||
{
|
||||
using (GZipStream gzipStream = new GZipStream(stream, CompressionMode.Decompress, false))
|
||||
{
|
||||
return JsonSerializer.DeserializeFromStream<CategoryInfo<Studio>>(gzipStream);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
25
MediaBrowser.ApiInteraction/BaseClient.cs
Normal file
25
MediaBrowser.ApiInteraction/BaseClient.cs
Normal file
|
@ -0,0 +1,25 @@
|
|||
using System;
|
||||
using System.Net.Http;
|
||||
|
||||
namespace MediaBrowser.ApiInteraction
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides a base class used by the api and image services
|
||||
/// </summary>
|
||||
public abstract class BaseClient : IDisposable
|
||||
{
|
||||
public string ApiUrl { get; set; }
|
||||
|
||||
protected HttpClient HttpClient { get; private set; }
|
||||
|
||||
public BaseClient()
|
||||
{
|
||||
HttpClient = new HttpClient();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
HttpClient.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
13
MediaBrowser.ApiInteraction/IJsonSerializer.cs
Normal file
13
MediaBrowser.ApiInteraction/IJsonSerializer.cs
Normal file
|
@ -0,0 +1,13 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace MediaBrowser.ApiInteraction
|
||||
{
|
||||
/// <summary>
|
||||
/// Since ServiceStack Json is not portable, we need to abstract required json functions into an interface
|
||||
/// </summary>
|
||||
public interface IJsonSerializer
|
||||
{
|
||||
T DeserializeFromStream<T>(Stream stream);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProjectGuid>{B1C27231-7017-4C9B-A678-DE891954FA38}</ProjectGuid>
|
||||
<OutputType>Library</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>MediaBrowser.ApiInteraction</RootNamespace>
|
||||
<AssemblyName>MediaBrowser.ApiInteraction</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
|
||||
<TargetFrameworkProfile>Profile7</TargetFrameworkProfile>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<!-- A reference to the entire .NET Framework is automatically included -->
|
||||
<ProjectReference Include="..\MediaBrowser.Model\MediaBrowser.Model.csproj">
|
||||
<Project>{7eeeb4bb-f3e8-48fc-b4c5-70f0fff8329b}</Project>
|
||||
<Name>MediaBrowser.Model</Name>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="ApiClient.cs" />
|
||||
<Compile Include="BaseClient.cs" />
|
||||
<Compile Include="IJsonSerializer.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" />
|
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
Other similar extension points exist, see Microsoft.Common.targets.
|
||||
<Target Name="BeforeBuild">
|
||||
</Target>
|
||||
<Target Name="AfterBuild">
|
||||
</Target>
|
||||
-->
|
||||
</Project>
|
30
MediaBrowser.ApiInteraction/Properties/AssemblyInfo.cs
Normal file
30
MediaBrowser.ApiInteraction/Properties/AssemblyInfo.cs
Normal file
|
@ -0,0 +1,30 @@
|
|||
using System.Resources;
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
// General Information about an assembly is controlled through the following
|
||||
// set of attributes. Change these attribute values to modify the information
|
||||
// associated with an assembly.
|
||||
[assembly: AssemblyTitle("MediaBrowser.ApiInteraction")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("")]
|
||||
[assembly: AssemblyProduct("MediaBrowser.ApiInteraction")]
|
||||
[assembly: AssemblyCopyright("Copyright © 2012")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
[assembly: NeutralResourcesLanguage("en")]
|
||||
|
||||
// Version information for an assembly consists of the following four values:
|
||||
//
|
||||
// Major Version
|
||||
// Minor Version
|
||||
// Build Number
|
||||
// Revision
|
||||
//
|
||||
// You can specify all the values or you can default the Build and Revision Numbers
|
||||
// by using the '*' as shown below:
|
||||
// [assembly: AssemblyVersion("1.0.*")]
|
||||
[assembly: AssemblyVersion("1.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("1.0.0.0")]
|
|
@ -10,8 +10,8 @@ using MediaBrowser.Common.Configuration;
|
|||
using MediaBrowser.Common.Logging;
|
||||
using MediaBrowser.Common.Net;
|
||||
using MediaBrowser.Common.Plugins;
|
||||
using MediaBrowser.Common.Serialization;
|
||||
using MediaBrowser.Model.Progress;
|
||||
using MediaBrowser.Common.Json;
|
||||
|
||||
namespace MediaBrowser.Common.Kernel
|
||||
{
|
||||
|
@ -93,8 +93,6 @@ namespace MediaBrowser.Common.Kernel
|
|||
|
||||
public virtual void Init(IProgress<TaskProgress> progress)
|
||||
{
|
||||
JsonSerializer.Configure();
|
||||
|
||||
ReloadLogger();
|
||||
|
||||
ReloadConfiguration();
|
||||
|
|
|
@ -50,7 +50,7 @@
|
|||
<ItemGroup>
|
||||
<Compile Include="Configuration\BaseApplicationConfiguration.cs" />
|
||||
<Compile Include="Events\GenericItemEventArgs.cs" />
|
||||
<Compile Include="Json\JsonSerializer.cs" />
|
||||
<Compile Include="Serialization\JsonSerializer.cs" />
|
||||
<Compile Include="Kernel\BaseKernel.cs" />
|
||||
<Compile Include="Kernel\KernelContext.cs" />
|
||||
<Compile Include="Logging\BaseLogger.cs" />
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
using MediaBrowser.Common.Kernel;
|
||||
using MediaBrowser.Common.Serialization;
|
||||
using MediaBrowser.Model.Plugins;
|
||||
using MediaBrowser.Common.Json;
|
||||
|
||||
namespace MediaBrowser.Common.Plugins
|
||||
{
|
||||
|
|
|
@ -1,17 +1,24 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace MediaBrowser.Common.Json
|
||||
namespace MediaBrowser.Common.Serialization
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides a wrapper around third party json serialization.
|
||||
/// </summary>
|
||||
public class JsonSerializer
|
||||
{
|
||||
public static void SerializeToStream<T>(T obj, Stream stream)
|
||||
{
|
||||
Configure();
|
||||
|
||||
ServiceStack.Text.JsonSerializer.SerializeToStream<T>(obj, stream);
|
||||
}
|
||||
|
||||
public static void SerializeToFile<T>(T obj, string file)
|
||||
{
|
||||
Configure();
|
||||
|
||||
using (StreamWriter streamWriter = new StreamWriter(file))
|
||||
{
|
||||
ServiceStack.Text.JsonSerializer.SerializeToWriter<T>(obj, streamWriter);
|
||||
|
@ -20,6 +27,8 @@ namespace MediaBrowser.Common.Json
|
|||
|
||||
public static object DeserializeFromFile(Type type, string file)
|
||||
{
|
||||
Configure();
|
||||
|
||||
using (Stream stream = File.OpenRead(file))
|
||||
{
|
||||
return ServiceStack.Text.JsonSerializer.DeserializeFromStream(type, stream);
|
||||
|
@ -28,6 +37,8 @@ namespace MediaBrowser.Common.Json
|
|||
|
||||
public static T DeserializeFromFile<T>(string file)
|
||||
{
|
||||
Configure();
|
||||
|
||||
using (Stream stream = File.OpenRead(file))
|
||||
{
|
||||
return ServiceStack.Text.JsonSerializer.DeserializeFromStream<T>(stream);
|
||||
|
@ -36,18 +47,28 @@ namespace MediaBrowser.Common.Json
|
|||
|
||||
public static T DeserializeFromStream<T>(Stream stream)
|
||||
{
|
||||
Configure();
|
||||
|
||||
return ServiceStack.Text.JsonSerializer.DeserializeFromStream<T>(stream);
|
||||
}
|
||||
|
||||
public static T DeserializeFromString<T>(string data)
|
||||
{
|
||||
Configure();
|
||||
|
||||
return ServiceStack.Text.JsonSerializer.DeserializeFromString<T>(data);
|
||||
}
|
||||
|
||||
public static void Configure()
|
||||
private static bool IsConfigured = false;
|
||||
private static void Configure()
|
||||
{
|
||||
ServiceStack.Text.JsConfig.ExcludeTypeInfo = true;
|
||||
ServiceStack.Text.JsConfig.IncludeNullValues = false;
|
||||
if (!IsConfigured)
|
||||
{
|
||||
ServiceStack.Text.JsConfig.ExcludeTypeInfo = true;
|
||||
ServiceStack.Text.JsConfig.IncludeNullValues = false;
|
||||
|
||||
IsConfigured = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -6,8 +6,8 @@ using System.Linq;
|
|||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using MediaBrowser.Common.Json;
|
||||
using MediaBrowser.Common.Kernel;
|
||||
using MediaBrowser.Common.Serialization;
|
||||
using MediaBrowser.Controller.Configuration;
|
||||
using MediaBrowser.Controller.Events;
|
||||
using MediaBrowser.Controller.IO;
|
||||
|
|
|
@ -21,6 +21,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MediaBrowser.Common", "Medi
|
|||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MediaBrowser.Model", "MediaBrowser.Model\MediaBrowser.Model.csproj", "{7EEEB4BB-F3E8-48FC-B4C5-70F0FFF8329B}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MediaBrowser.ApiInteraction", "MediaBrowser.ApiInteraction\MediaBrowser.ApiInteraction.csproj", "{B1C27231-7017-4C9B-A678-DE891954FA38}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
|
@ -67,6 +69,10 @@ Global
|
|||
{99B4CFE8-1441-4F0D-8C40-A70D0DD372ED}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{99B4CFE8-1441-4F0D-8C40-A70D0DD372ED}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{99B4CFE8-1441-4F0D-8C40-A70D0DD372ED}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{B1C27231-7017-4C9B-A678-DE891954FA38}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{B1C27231-7017-4C9B-A678-DE891954FA38}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{B1C27231-7017-4C9B-A678-DE891954FA38}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{B1C27231-7017-4C9B-A678-DE891954FA38}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
|
Loading…
Reference in New Issue
Block a user