From 9338571e03520967bab5689812f5a2a0fe13bed8 Mon Sep 17 00:00:00 2001 From: LukePulverenti Luke Pulverenti luke pulverenti Date: Wed, 22 Aug 2012 09:05:25 -0400 Subject: [PATCH] Made api serializer a little more generic --- MediaBrowser.ApiInteraction/ApiClient.cs | 94 +++++++++++++------ ...{IJsonSerializer.cs => IDataSerializer.cs} | 2 +- .../MediaBrowser.ApiInteraction.csproj | 2 +- 3 files changed, 65 insertions(+), 33 deletions(-) rename MediaBrowser.ApiInteraction/{IJsonSerializer.cs => IDataSerializer.cs} (85%) diff --git a/MediaBrowser.ApiInteraction/ApiClient.cs b/MediaBrowser.ApiInteraction/ApiClient.cs index 04a7ad2146..4b9a712927 100644 --- a/MediaBrowser.ApiInteraction/ApiClient.cs +++ b/MediaBrowser.ApiInteraction/ApiClient.cs @@ -39,8 +39,14 @@ namespace MediaBrowser.ApiInteraction } } + /// + /// Gets or sets the format to request from the server + /// The Data Serializer will have to be able to support it. + /// + public SerializationFormat SerializationFormat { get; set; } + public HttpClient HttpClient { get; private set; } - public IJsonSerializer JsonSerializer { get; set; } + public IDataSerializer DataSerializer { get; set; } /// /// Gets an image url that can be used to download an image from the api @@ -169,9 +175,9 @@ namespace MediaBrowser.ApiInteraction url += "&id=" + id.ToString(); } - using (Stream stream = await GetStreamAsync(url).ConfigureAwait(false)) + using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false)) { - return JsonSerializer.DeserializeFromStream(stream); + return DataSerializer.DeserializeFromStream(stream); } } @@ -182,9 +188,9 @@ namespace MediaBrowser.ApiInteraction { string url = ApiUrl + "/users"; - using (Stream stream = await GetStreamAsync(url).ConfigureAwait(false)) + using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false)) { - return JsonSerializer.DeserializeFromStream>(stream); + return DataSerializer.DeserializeFromStream>(stream); } } @@ -195,9 +201,9 @@ namespace MediaBrowser.ApiInteraction { string url = ApiUrl + "/genres?userId=" + userId.ToString(); - using (Stream stream = await GetStreamAsync(url).ConfigureAwait(false)) + using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false)) { - return JsonSerializer.DeserializeFromStream>>(stream); + return DataSerializer.DeserializeFromStream>>(stream); } } @@ -208,9 +214,9 @@ namespace MediaBrowser.ApiInteraction { string url = ApiUrl + "/years?userId=" + userId.ToString(); - using (Stream stream = await GetStreamAsync(url).ConfigureAwait(false)) + using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false)) { - return JsonSerializer.DeserializeFromStream>>(stream); + return DataSerializer.DeserializeFromStream>>(stream); } } @@ -221,9 +227,9 @@ namespace MediaBrowser.ApiInteraction { string url = ApiUrl + "/itemlist?listtype=itemswithyear&userId=" + userId.ToString() + "&name=" + name; - using (Stream stream = await GetStreamAsync(url).ConfigureAwait(false)) + using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false)) { - return JsonSerializer.DeserializeFromStream>(stream); + return DataSerializer.DeserializeFromStream>(stream); } } @@ -234,9 +240,9 @@ namespace MediaBrowser.ApiInteraction { string url = ApiUrl + "/itemlist?listtype=itemswithgenre&userId=" + userId.ToString() + "&name=" + name; - using (Stream stream = await GetStreamAsync(url).ConfigureAwait(false)) + using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false)) { - return JsonSerializer.DeserializeFromStream>(stream); + return DataSerializer.DeserializeFromStream>(stream); } } @@ -247,12 +253,12 @@ namespace MediaBrowser.ApiInteraction { string url = ApiUrl + "/itemlist?listtype=itemswithperson&userId=" + userId.ToString() + "&name=" + name; - using (Stream stream = await GetStreamAsync(url).ConfigureAwait(false)) + using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false)) { - return JsonSerializer.DeserializeFromStream>(stream); + return DataSerializer.DeserializeFromStream>(stream); } } - + /// /// Gets all items that contain a given Person /// @@ -262,9 +268,9 @@ namespace MediaBrowser.ApiInteraction url += "&persontype=" + personType; - using (Stream stream = await GetStreamAsync(url).ConfigureAwait(false)) + using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false)) { - return JsonSerializer.DeserializeFromStream>(stream); + return DataSerializer.DeserializeFromStream>(stream); } } @@ -275,9 +281,9 @@ namespace MediaBrowser.ApiInteraction { string url = ApiUrl + "/studios?userId=" + userId.ToString(); - using (Stream stream = await GetStreamAsync(url).ConfigureAwait(false)) + using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false)) { - return JsonSerializer.DeserializeFromStream>>(stream); + return DataSerializer.DeserializeFromStream>>(stream); } } @@ -288,9 +294,9 @@ namespace MediaBrowser.ApiInteraction { string url = ApiUrl + "/itemlist?listtype=itemswithstudio&userId=" + userId.ToString() + "&name=" + name; - using (Stream stream = await GetStreamAsync(url).ConfigureAwait(false)) + using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false)) { - return JsonSerializer.DeserializeFromStream>(stream); + return DataSerializer.DeserializeFromStream>(stream); } } @@ -301,9 +307,9 @@ namespace MediaBrowser.ApiInteraction { string url = ApiUrl + "/studio?userId=" + userId.ToString() + "&name=" + name; - using (Stream stream = await GetStreamAsync(url).ConfigureAwait(false)) + using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false)) { - return JsonSerializer.DeserializeFromStream>(stream); + return DataSerializer.DeserializeFromStream>(stream); } } @@ -314,9 +320,9 @@ namespace MediaBrowser.ApiInteraction { string url = ApiUrl + "/genre?userId=" + userId.ToString() + "&name=" + name; - using (Stream stream = await GetStreamAsync(url).ConfigureAwait(false)) + using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false)) { - return JsonSerializer.DeserializeFromStream>(stream); + return DataSerializer.DeserializeFromStream>(stream); } } @@ -327,9 +333,9 @@ namespace MediaBrowser.ApiInteraction { string url = ApiUrl + "/person?userId=" + userId.ToString() + "&name=" + name; - using (Stream stream = await GetStreamAsync(url).ConfigureAwait(false)) + using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false)) { - return JsonSerializer.DeserializeFromStream>(stream); + return DataSerializer.DeserializeFromStream>(stream); } } @@ -340,20 +346,46 @@ namespace MediaBrowser.ApiInteraction { string url = ApiUrl + "/year?userId=" + userId.ToString() + "&year=" + year; - using (Stream stream = await GetStreamAsync(url).ConfigureAwait(false)) + using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false)) { - return JsonSerializer.DeserializeFromStream>(stream); + return DataSerializer.DeserializeFromStream>(stream); } } - private Task GetStreamAsync(string url) + /// + /// This is a helper around getting a stream from the server that contains serialized data + /// + private Task GetSerializedStreamAsync(string url) { + if (url.IndexOf('?') == -1) + { + url += "?dataformat=" + SerializationFormat.ToString().ToLower(); + } + else + { + url += "&dataformat=" + SerializationFormat.ToString().ToLower(); + } + return GetStreamAsync(url); } + /// + /// This is just a helper around HttpClient + /// + private Task GetStreamAsync(string url) + { + return HttpClient.GetStreamAsync(url); + } + public void Dispose() { HttpClient.Dispose(); } } + + public enum SerializationFormat + { + Json, + Jsv + } } diff --git a/MediaBrowser.ApiInteraction/IJsonSerializer.cs b/MediaBrowser.ApiInteraction/IDataSerializer.cs similarity index 85% rename from MediaBrowser.ApiInteraction/IJsonSerializer.cs rename to MediaBrowser.ApiInteraction/IDataSerializer.cs index 41f4049bbe..2eda7e4865 100644 --- a/MediaBrowser.ApiInteraction/IJsonSerializer.cs +++ b/MediaBrowser.ApiInteraction/IDataSerializer.cs @@ -6,7 +6,7 @@ namespace MediaBrowser.ApiInteraction /// /// Since ServiceStack Json is not portable, we need to abstract required json functions into an interface /// - public interface IJsonSerializer + public interface IDataSerializer { T DeserializeFromStream(Stream stream); } diff --git a/MediaBrowser.ApiInteraction/MediaBrowser.ApiInteraction.csproj b/MediaBrowser.ApiInteraction/MediaBrowser.ApiInteraction.csproj index 71b5d9d38e..93c6acec9e 100644 --- a/MediaBrowser.ApiInteraction/MediaBrowser.ApiInteraction.csproj +++ b/MediaBrowser.ApiInteraction/MediaBrowser.ApiInteraction.csproj @@ -40,7 +40,7 @@ - +