Преглед на файлове

Added a fully portable ApiClient

LukePulverenti Luke Pulverenti luke pulverenti преди 12 години
родител
ревизия
a48dd192ae

+ 3 - 0
MediaBrowser.ApiInteraction.Metro/MediaBrowser.ApiInteraction.Metro.csproj

@@ -42,6 +42,9 @@
     <Compile Include="..\MediaBrowser.ApiInteraction\ApiClient.cs">
       <Link>ApiClient.cs</Link>
     </Compile>
+    <Compile Include="..\MediaBrowser.ApiInteraction\BaseApiClient.cs">
+      <Link>BaseApiClient.cs</Link>
+    </Compile>
     <Compile Include="..\MediaBrowser.ApiInteraction\SerializationFormats.cs">
       <Link>SerializationFormats.cs</Link>
     </Compile>

+ 17 - 0
MediaBrowser.ApiInteraction.Portable/ApiClient.cs

@@ -0,0 +1,17 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Net;
+using System.IO;
+
+namespace MediaBrowser.ApiInteraction.Portable
+{
+    public class ApiClient : BaseApiClient
+    {
+        private HttpWebRequest GetNewRequest(string url)
+        {
+            HttpWebRequest request = HttpWebRequest.CreateHttp(url);
+        }
+    }
+}

+ 69 - 0
MediaBrowser.ApiInteraction.Portable/MediaBrowser.ApiInteraction.Portable.csproj

@@ -0,0 +1,69 @@
+<?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>{756B93D2-5FFA-4B8D-BDCA-127476F1E6FB}</ProjectGuid>
+    <OutputType>Library</OutputType>
+    <AppDesignerFolder>Properties</AppDesignerFolder>
+    <RootNamespace>MediaBrowser.ApiInteraction.Portable</RootNamespace>
+    <AssemblyName>MediaBrowser.ApiInteraction.Portable</AssemblyName>
+    <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
+    <TargetFrameworkProfile>Profile4</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>
+    <Compile Include="..\MediaBrowser.ApiInteraction\BaseApiClient.cs">
+      <Link>BaseApiClient.cs</Link>
+    </Compile>
+    <Compile Include="..\MediaBrowser.ApiInteraction.Metro\DataSerializer.cs">
+      <Link>DataSerializer.cs</Link>
+    </Compile>
+    <Compile Include="..\MediaBrowser.ApiInteraction\SerializationFormats.cs">
+      <Link>SerializationFormats.cs</Link>
+    </Compile>
+    <Compile Include="ApiClient.cs" />
+    <Compile Include="Properties\AssemblyInfo.cs" />
+  </ItemGroup>
+  <ItemGroup>
+    <Reference Include="Newtonsoft.Json">
+      <HintPath>..\Json.Net\Portable\Newtonsoft.Json.dll</HintPath>
+    </Reference>
+    <Reference Include="protobuf-net">
+      <HintPath>..\protobuf-net\Full\portable\protobuf-net.dll</HintPath>
+    </Reference>
+  </ItemGroup>
+  <ItemGroup>
+    <ProjectReference Include="..\MediaBrowser.Model\MediaBrowser.Model.csproj">
+      <Project>{7eeeb4bb-f3e8-48fc-b4c5-70f0fff8329b}</Project>
+      <Name>MediaBrowser.Model</Name>
+    </ProjectReference>
+  </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 - 0
MediaBrowser.ApiInteraction.Portable/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.Portable")]
+[assembly: AssemblyDescription("")]
+[assembly: AssemblyConfiguration("")]
+[assembly: AssemblyCompany("")]
+[assembly: AssemblyProduct("MediaBrowser.ApiInteraction.Portable")]
+[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")]

+ 6 - 0
MediaBrowser.ApiInteraction.sln

@@ -12,12 +12,18 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{F0E0E6
 EndProject
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MediaBrowser.ApiInteraction.Metro", "MediaBrowser.ApiInteraction.Metro\MediaBrowser.ApiInteraction.Metro.csproj", "{94CEA07A-307C-4663-AA43-7BD852808574}"
 EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MediaBrowser.ApiInteraction.Portable", "MediaBrowser.ApiInteraction.Portable\MediaBrowser.ApiInteraction.Portable.csproj", "{756B93D2-5FFA-4B8D-BDCA-127476F1E6FB}"
+EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 		Debug|Any CPU = Debug|Any CPU
 		Release|Any CPU = Release|Any CPU
 	EndGlobalSection
 	GlobalSection(ProjectConfigurationPlatforms) = postSolution
+		{756B93D2-5FFA-4B8D-BDCA-127476F1E6FB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{756B93D2-5FFA-4B8D-BDCA-127476F1E6FB}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{756B93D2-5FFA-4B8D-BDCA-127476F1E6FB}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{756B93D2-5FFA-4B8D-BDCA-127476F1E6FB}.Release|Any CPU.Build.0 = Release|Any CPU
 		{7EEEB4BB-F3E8-48FC-B4C5-70F0FFF8329B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
 		{7EEEB4BB-F3E8-48FC-B4C5-70F0FFF8329B}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{7EEEB4BB-F3E8-48FC-B4C5-70F0FFF8329B}.Release|Any CPU.ActiveCfg = Release|Any CPU

+ 33 - 344
MediaBrowser.ApiInteraction/ApiClient.cs

@@ -11,352 +11,17 @@ using MediaBrowser.Model.Weather;
 
 namespace MediaBrowser.ApiInteraction
 {
-    public class ApiClient : IDisposable
+    public class ApiClient : BaseApiClient
     {
         public ApiClient(HttpClientHandler handler)
+            : base()
         {
             handler.AutomaticDecompression = DecompressionMethods.Deflate;
 
             HttpClient = new HttpClient(handler);
-
-            DataSerializer.Configure();
-        }
-
-        /// <summary>
-        /// Gets or sets the server host name (myserver or 192.168.x.x)
-        /// </summary>
-        public string ServerHostName { get; set; }
-
-        /// <summary>
-        /// Gets or sets the port number used by the API
-        /// </summary>
-        public int ServerApiPort { get; set; }
-
-        /// <summary>
-        /// Gets the current api url based on hostname and port.
-        /// </summary>
-        protected string ApiUrl
-        {
-            get
-            {
-                return string.Format("http://{0}:{1}/mediabrowser/api", ServerHostName, ServerApiPort);
-            }
-        }
-
-        /// <summary>
-        /// Gets the data format to request from the server
-        /// </summary>
-        private SerializationFormats SerializationFormat
-        {
-            get
-            {
-                return ApiInteraction.SerializationFormats.Protobuf;
-            }
-        }
-
-        public HttpClient HttpClient { get; private set; }
-
-        /// <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 = null, int? width = null, int? height = null, int? maxWidth = null, int? maxHeight = null, int? quality = null)
-        {
-            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 url that can be used to download an image from the api
-        /// </summary>
-        /// <param name="userId">The Id of the user</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 GetUserImageUrl(Guid userId, int? width = null, int? height = null, int? maxWidth = null, int? maxHeight = null, int? quality = null)
-        {
-            string url = ApiUrl + "/image";
-
-            url += "?userId=" + userId.ToString();
-
-            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 url that can be used to download an image from the api
-        /// </summary>
-        /// <param name="name">The name of the person</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 GetPersonImageUrl(string name, int? width = null, int? height = null, int? maxWidth = null, int? maxHeight = null, int? quality = null)
-        {
-            string url = ApiUrl + "/image";
-
-            url += "?personname=" + name;
-
-            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 url that can be used to download an image from the api
-        /// </summary>
-        /// <param name="year">The year</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 GetYearImageUrl(int year, int? width = null, int? height = null, int? maxWidth = null, int? maxHeight = null, int? quality = null)
-        {
-            string url = ApiUrl + "/image";
-
-            url += "?year=" + year;
-
-            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 url that can be used to download an image from the api
-        /// </summary>
-        /// <param name="name">The name of the genre</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 GetGenreImageUrl(string name, int? width = null, int? height = null, int? maxWidth = null, int? maxHeight = null, int? quality = null)
-        {
-            string url = ApiUrl + "/image";
-
-            url += "?genre=" + name;
-
-            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 url that can be used to download an image from the api
-        /// </summary>
-        /// <param name="name">The name of the studio</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 GetStudioImageUrl(string name, int? width = null, int? height = null, int? maxWidth = null, int? maxHeight = null, int? quality = null)
-        {
-            string url = ApiUrl + "/image";
-
-            url += "?studio=" + name;
-
-            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>
-        /// This is a helper to get a list of backdrop url's from a given ApiBaseItemWrapper. If the actual item does not have any backdrops it will return backdrops from the first parent that does.
-        /// </summary>
-        /// <param name="item">A given item.</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[] GetBackdropImageUrls(DTOBaseItem item, int? width = null, int? height = null, int? maxWidth = null, int? maxHeight = null, int? quality = null)
-        {
-            Guid? backdropItemId = null;
-            int backdropCount = 0;
-
-            if (item.BackdropCount == 0)
-            {
-                backdropItemId = item.ParentBackdropItemId;
-                backdropCount = item.ParentBackdropCount ?? 0;
-            }
-            else
-            {
-                backdropItemId = item.Id;
-                backdropCount = item.BackdropCount;
-            }
-
-            if (backdropItemId == null)
-            {
-                return new string[] { };
-            }
-
-            string[] files = new string[backdropCount];
-
-            for (int i = 0; i < backdropCount; i++)
-            {
-                files[i] = GetImageUrl(backdropItemId.Value, ImageType.Backdrop, i, width, height, maxWidth, maxHeight, quality);
-            }
-
-            return files;
-        }
-
-        /// <summary>
-        /// This is a helper to get the logo image url from a given ApiBaseItemWrapper. If the actual item does not have a logo, it will return the logo from the first parent that does, or null.
-        /// </summary>
-        /// <param name="item">A given item.</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 GetLogoImageUrl(DTOBaseItem item, int? width = null, int? height = null, int? maxWidth = null, int? maxHeight = null, int? quality = null)
-        {
-            Guid? logoItemId = item.HasLogo ? item.Id : item.ParentLogoItemId;
-
-            if (logoItemId.HasValue)
-            {
-                return GetImageUrl(logoItemId.Value, ImageType.Logo, null, width, height, maxWidth, maxHeight, quality);
-            }
-
-            return null;
-        }
+        private HttpClient HttpClient { get; set; }
 
         /// <summary>
         /// Gets an image stream based on a url
@@ -397,6 +62,35 @@ namespace MediaBrowser.ApiInteraction
             }
         }
 
+        public void GetAllUsersAsync(Action<DTOUser[]> callback)
+        {
+            string url = ApiUrl + "/users";
+
+            HttpWebRequest request = HttpWebRequest.CreateHttp(url);
+
+            request.BeginGetResponse(new AsyncCallback(result =>
+            {
+                HttpWebResponse response = (result.AsyncState as HttpWebRequest).EndGetResponse(result) as HttpWebResponse;
+
+                Stream stream = response.GetResponseStream();
+
+            }), request);
+        }
+
+        public void GetData<T>(string url, SerializationFormats serializationFormat, Action<T> callback)
+        {
+            HttpWebRequest request = HttpWebRequest.CreateHttp(url);
+            //request.AutomaticDecompression = DecompressionMethods.Deflate;
+
+            request.BeginGetResponse(new AsyncCallback(result =>
+            {
+                HttpWebResponse response = (result.AsyncState as HttpWebRequest).EndGetResponse(result) as HttpWebResponse;
+
+                Stream stream = response.GetResponseStream();
+
+            }), request);
+        }
+
         /// <summary>
         /// Gets all Genres
         /// </summary>
@@ -694,11 +388,6 @@ namespace MediaBrowser.ApiInteraction
             return GetStreamAsync(url);
         }
 
-        private T DeserializeFromStream<T>(Stream stream)
-        {
-            return DataSerializer.DeserializeFromStream<T>(stream, SerializationFormat);
-        }
-
         /// <summary>
         /// This is just a helper around HttpClient
         /// </summary>
@@ -707,7 +396,7 @@ namespace MediaBrowser.ApiInteraction
             return HttpClient.GetStreamAsync(url);
         }
 
-        public void Dispose()
+        public override void Dispose()
         {
             HttpClient.Dispose();
         }

+ 358 - 0
MediaBrowser.ApiInteraction/BaseApiClient.cs

@@ -0,0 +1,358 @@
+using System;
+using System.IO;
+using MediaBrowser.Model.DTO;
+using MediaBrowser.Model.Entities;
+
+namespace MediaBrowser.ApiInteraction
+{
+    public abstract class BaseApiClient : IDisposable
+    {
+        public BaseApiClient()
+        {
+            DataSerializer.Configure();
+        }
+
+        /// <summary>
+        /// Gets or sets the server host name (myserver or 192.168.x.x)
+        /// </summary>
+        public string ServerHostName { get; set; }
+
+        /// <summary>
+        /// Gets or sets the port number used by the API
+        /// </summary>
+        public int ServerApiPort { get; set; }
+
+        /// <summary>
+        /// Gets the current api url based on hostname and port.
+        /// </summary>
+        protected string ApiUrl
+        {
+            get
+            {
+                return string.Format("http://{0}:{1}/mediabrowser/api", ServerHostName, ServerApiPort);
+            }
+        }
+
+        /// <summary>
+        /// Gets the default data format to request from the server
+        /// </summary>
+        protected SerializationFormats SerializationFormat
+        {
+            get
+            {
+                return ApiInteraction.SerializationFormats.Protobuf;
+            }
+        }
+
+        /// <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 = null, int? width = null, int? height = null, int? maxWidth = null, int? maxHeight = null, int? quality = null)
+        {
+            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 url that can be used to download an image from the api
+        /// </summary>
+        /// <param name="userId">The Id of the user</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 GetUserImageUrl(Guid userId, int? width = null, int? height = null, int? maxWidth = null, int? maxHeight = null, int? quality = null)
+        {
+            string url = ApiUrl + "/image";
+
+            url += "?userId=" + userId.ToString();
+
+            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 url that can be used to download an image from the api
+        /// </summary>
+        /// <param name="name">The name of the person</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 GetPersonImageUrl(string name, int? width = null, int? height = null, int? maxWidth = null, int? maxHeight = null, int? quality = null)
+        {
+            string url = ApiUrl + "/image";
+
+            url += "?personname=" + name;
+
+            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 url that can be used to download an image from the api
+        /// </summary>
+        /// <param name="year">The year</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 GetYearImageUrl(int year, int? width = null, int? height = null, int? maxWidth = null, int? maxHeight = null, int? quality = null)
+        {
+            string url = ApiUrl + "/image";
+
+            url += "?year=" + year;
+
+            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 url that can be used to download an image from the api
+        /// </summary>
+        /// <param name="name">The name of the genre</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 GetGenreImageUrl(string name, int? width = null, int? height = null, int? maxWidth = null, int? maxHeight = null, int? quality = null)
+        {
+            string url = ApiUrl + "/image";
+
+            url += "?genre=" + name;
+
+            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 url that can be used to download an image from the api
+        /// </summary>
+        /// <param name="name">The name of the studio</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 GetStudioImageUrl(string name, int? width = null, int? height = null, int? maxWidth = null, int? maxHeight = null, int? quality = null)
+        {
+            string url = ApiUrl + "/image";
+
+            url += "?studio=" + name;
+
+            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>
+        /// This is a helper to get a list of backdrop url's from a given ApiBaseItemWrapper. If the actual item does not have any backdrops it will return backdrops from the first parent that does.
+        /// </summary>
+        /// <param name="item">A given item.</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[] GetBackdropImageUrls(DTOBaseItem item, int? width = null, int? height = null, int? maxWidth = null, int? maxHeight = null, int? quality = null)
+        {
+            Guid? backdropItemId = null;
+            int backdropCount = 0;
+
+            if (item.BackdropCount == 0)
+            {
+                backdropItemId = item.ParentBackdropItemId;
+                backdropCount = item.ParentBackdropCount ?? 0;
+            }
+            else
+            {
+                backdropItemId = item.Id;
+                backdropCount = item.BackdropCount;
+            }
+
+            if (backdropItemId == null)
+            {
+                return new string[] { };
+            }
+
+            string[] files = new string[backdropCount];
+
+            for (int i = 0; i < backdropCount; i++)
+            {
+                files[i] = GetImageUrl(backdropItemId.Value, ImageType.Backdrop, i, width, height, maxWidth, maxHeight, quality);
+            }
+
+            return files;
+        }
+
+        /// <summary>
+        /// This is a helper to get the logo image url from a given ApiBaseItemWrapper. If the actual item does not have a logo, it will return the logo from the first parent that does, or null.
+        /// </summary>
+        /// <param name="item">A given item.</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 GetLogoImageUrl(DTOBaseItem item, int? width = null, int? height = null, int? maxWidth = null, int? maxHeight = null, int? quality = null)
+        {
+            Guid? logoItemId = item.HasLogo ? item.Id : item.ParentLogoItemId;
+
+            if (logoItemId.HasValue)
+            {
+                return GetImageUrl(logoItemId.Value, ImageType.Logo, null, width, height, maxWidth, maxHeight, quality);
+            }
+
+            return null;
+        }
+
+        protected T DeserializeFromStream<T>(Stream stream)
+        {
+            return DataSerializer.DeserializeFromStream<T>(stream, SerializationFormat);
+        }
+
+        public virtual void Dispose()
+        {
+        }
+    }
+}

+ 1 - 0
MediaBrowser.ApiInteraction/MediaBrowser.ApiInteraction.csproj

@@ -47,6 +47,7 @@
   </ItemGroup>
   <ItemGroup>
     <Compile Include="ApiClient.cs" />
+    <Compile Include="BaseApiClient.cs" />
     <Compile Include="DataSerializer.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="SerializationFormats.cs" />