aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLukePulverenti Luke Pulverenti luke pulverenti <LukePulverenti Luke Pulverenti luke.pulverenti@gmail.com>2012-07-31 12:29:07 -0400
committerLukePulverenti Luke Pulverenti luke pulverenti <LukePulverenti Luke Pulverenti luke.pulverenti@gmail.com>2012-07-31 12:29:07 -0400
commitdefd8ed253a1e47d0951d6e0abf850129f776327 (patch)
treed2ee50baeb07d38bc5d8dafa0a725cd377cf99c1
parent3058b71764bd2775fedbb7f71342b497134f6c86 (diff)
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.
-rw-r--r--MediaBrowser.Api/HttpHandlers/JsonHandler.cs3
-rw-r--r--MediaBrowser.ApiInteraction/ApiClient.cs196
-rw-r--r--MediaBrowser.ApiInteraction/BaseClient.cs25
-rw-r--r--MediaBrowser.ApiInteraction/IJsonSerializer.cs13
-rw-r--r--MediaBrowser.ApiInteraction/MediaBrowser.ApiInteraction.csproj55
-rw-r--r--MediaBrowser.ApiInteraction/Properties/AssemblyInfo.cs30
-rw-r--r--MediaBrowser.Common/Kernel/BaseKernel.cs4
-rw-r--r--MediaBrowser.Common/MediaBrowser.Common.csproj2
-rw-r--r--MediaBrowser.Common/Plugins/BasePlugin.cs2
-rw-r--r--MediaBrowser.Common/Serialization/JsonSerializer.cs (renamed from MediaBrowser.Common/Json/JsonSerializer.cs)29
-rw-r--r--MediaBrowser.Controller/Kernel.cs2
-rw-r--r--MediaBrowser.sln6
12 files changed, 355 insertions, 12 deletions
diff --git a/MediaBrowser.Api/HttpHandlers/JsonHandler.cs b/MediaBrowser.Api/HttpHandlers/JsonHandler.cs
index 90502a0c5..06ce4c2d4 100644
--- a/MediaBrowser.Api/HttpHandlers/JsonHandler.cs
+++ b/MediaBrowser.Api/HttpHandlers/JsonHandler.cs
@@ -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
{
diff --git a/MediaBrowser.ApiInteraction/ApiClient.cs b/MediaBrowser.ApiInteraction/ApiClient.cs
new file mode 100644
index 000000000..f9234441c
--- /dev/null
+++ b/MediaBrowser.ApiInteraction/ApiClient.cs
@@ -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);
+ }
+ }
+ }
+ }
+}
diff --git a/MediaBrowser.ApiInteraction/BaseClient.cs b/MediaBrowser.ApiInteraction/BaseClient.cs
new file mode 100644
index 000000000..ab76b9e48
--- /dev/null
+++ b/MediaBrowser.ApiInteraction/BaseClient.cs
@@ -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();
+ }
+ }
+}
diff --git a/MediaBrowser.ApiInteraction/IJsonSerializer.cs b/MediaBrowser.ApiInteraction/IJsonSerializer.cs
new file mode 100644
index 000000000..41f4049bb
--- /dev/null
+++ b/MediaBrowser.ApiInteraction/IJsonSerializer.cs
@@ -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);
+ }
+}
diff --git a/MediaBrowser.ApiInteraction/MediaBrowser.ApiInteraction.csproj b/MediaBrowser.ApiInteraction/MediaBrowser.ApiInteraction.csproj
new file mode 100644
index 000000000..682d974da
--- /dev/null
+++ b/MediaBrowser.ApiInteraction/MediaBrowser.ApiInteraction.csproj
@@ -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> \ No newline at end of file
diff --git a/MediaBrowser.ApiInteraction/Properties/AssemblyInfo.cs b/MediaBrowser.ApiInteraction/Properties/AssemblyInfo.cs
new file mode 100644
index 000000000..a788c5f71
--- /dev/null
+++ b/MediaBrowser.ApiInteraction/Properties/AssemblyInfo.cs
@@ -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")]
diff --git a/MediaBrowser.Common/Kernel/BaseKernel.cs b/MediaBrowser.Common/Kernel/BaseKernel.cs
index 0fdc1f813..cc3cb76f0 100644
--- a/MediaBrowser.Common/Kernel/BaseKernel.cs
+++ b/MediaBrowser.Common/Kernel/BaseKernel.cs
@@ -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();
diff --git a/MediaBrowser.Common/MediaBrowser.Common.csproj b/MediaBrowser.Common/MediaBrowser.Common.csproj
index 8c8146cdc..7f0605f09 100644
--- a/MediaBrowser.Common/MediaBrowser.Common.csproj
+++ b/MediaBrowser.Common/MediaBrowser.Common.csproj
@@ -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" />
diff --git a/MediaBrowser.Common/Plugins/BasePlugin.cs b/MediaBrowser.Common/Plugins/BasePlugin.cs
index 98b6120f7..e6ebfa551 100644
--- a/MediaBrowser.Common/Plugins/BasePlugin.cs
+++ b/MediaBrowser.Common/Plugins/BasePlugin.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
{
diff --git a/MediaBrowser.Common/Json/JsonSerializer.cs b/MediaBrowser.Common/Serialization/JsonSerializer.cs
index 8e132c152..57b285cee 100644
--- a/MediaBrowser.Common/Json/JsonSerializer.cs
+++ b/MediaBrowser.Common/Serialization/JsonSerializer.cs
@@ -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;
+ }
}
}
}
diff --git a/MediaBrowser.Controller/Kernel.cs b/MediaBrowser.Controller/Kernel.cs
index 5cc51d6a1..ca7fd382a 100644
--- a/MediaBrowser.Controller/Kernel.cs
+++ b/MediaBrowser.Controller/Kernel.cs
@@ -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;
diff --git a/MediaBrowser.sln b/MediaBrowser.sln
index 6f94baf31..078c998c0 100644
--- a/MediaBrowser.sln
+++ b/MediaBrowser.sln
@@ -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