Added the first method to portable ApiClient

This commit is contained in:
LukePulverenti Luke Pulverenti luke pulverenti 2012-09-06 21:45:24 -04:00
parent 1523117e9b
commit 3e0c2165b6
5 changed files with 63 additions and 46 deletions

View File

@ -1,13 +1,60 @@
using System; using MediaBrowser.Model.DTO;
using System.Collections.Generic; using System;
using System.Linq;
using System.Text;
using System.Net;
using System.IO; using System.IO;
using System.Net;
namespace MediaBrowser.ApiInteraction.Portable namespace MediaBrowser.ApiInteraction.Portable
{ {
public class ApiClient : BaseApiClient public class ApiClient : BaseApiClient
{ {
private HttpWebRequest GetNewRequest(string url)
{
return HttpWebRequest.CreateHttp(url);
}
/// <summary>
/// Gets all users
/// </summary>
public void GetAllUsersAsync(Action<DTOUser[]> callback)
{
string url = ApiUrl + "/users";
GetDataAsync<DTOUser[]>(url, callback);
}
private void GetDataAsync<T>(string url, Action<T> callback)
{
GetDataAsync<T>(url, callback, SerializationFormat);
}
private void GetDataAsync<T>(string url, Action<T> callback, SerializationFormats serializationFormat)
{
if (url.IndexOf('?') == -1)
{
url += "?dataformat=" + serializationFormat.ToString();
}
else
{
url += "&dataformat=" + serializationFormat.ToString();
}
HttpWebRequest request = GetNewRequest(url);
request.BeginGetResponse(new AsyncCallback(result =>
{
T value;
using (WebResponse response = (result.AsyncState as HttpWebRequest).EndGetResponse(result))
{
using (Stream stream = response.GetResponseStream())
{
value = DeserializeFromStream<T>(stream);
}
}
callback(value);
}), request);
}
} }
} }

View File

@ -1,13 +1,12 @@
using System; using MediaBrowser.Model.Configuration;
using MediaBrowser.Model.DTO;
using MediaBrowser.Model.Weather;
using System;
using System.IO; using System.IO;
using System.Net; using System.Net;
using System.Net.Http; using System.Net.Http;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using MediaBrowser.Model.Configuration;
using MediaBrowser.Model.DTO;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Weather;
namespace MediaBrowser.ApiInteraction namespace MediaBrowser.ApiInteraction
{ {
@ -62,35 +61,6 @@ 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> /// <summary>
/// Gets all Genres /// Gets all Genres
/// </summary> /// </summary>

View File

@ -1,7 +1,7 @@
using System; using MediaBrowser.Model.DTO;
using System.IO;
using MediaBrowser.Model.DTO;
using MediaBrowser.Model.Entities; using MediaBrowser.Model.Entities;
using System;
using System.IO;
namespace MediaBrowser.ApiInteraction namespace MediaBrowser.ApiInteraction
{ {

View File

@ -1,5 +1,5 @@
using System; using ProtoBuf;
using ProtoBuf; using System;
namespace MediaBrowser.Model.DTO namespace MediaBrowser.Model.DTO
{ {

View File

@ -1,6 +1,6 @@
using System.ComponentModel.Composition; using MediaBrowser.Common.Plugins;
using MediaBrowser.Common.Plugins;
using MediaBrowser.Model.Plugins; using MediaBrowser.Model.Plugins;
using System.ComponentModel.Composition;
namespace MediaBrowser.WebDashboard namespace MediaBrowser.WebDashboard
{ {