diff --git a/MediaBrowser.ApiInteraction/ApiClient.cs b/MediaBrowser.ApiInteraction/ApiClient.cs index 04a7ad214..4b9a71292 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,15 +346,35 @@ 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); } } + /// + /// 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 GetStreamAsync(url); + return HttpClient.GetStreamAsync(url); } public void Dispose() @@ -356,4 +382,10 @@ namespace MediaBrowser.ApiInteraction 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 41f4049bb..2eda7e486 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 71b5d9d38..93c6acec9 100644 --- a/MediaBrowser.ApiInteraction/MediaBrowser.ApiInteraction.csproj +++ b/MediaBrowser.ApiInteraction/MediaBrowser.ApiInteraction.csproj @@ -40,7 +40,7 @@ - +