Browse Source

First version of a method to retrieve full season details as a part of a tvshow call

Issue-36
Naliath 8 years ago
parent
commit
e09e126f6f
  1. 82
      TMDbLib/Client/TMDbClientTvShows.cs
  2. 9
      TMDbLib/Objects/TvShows/TvShowWithSeasonDetails.cs
  3. 19
      TMDbLibTests/ClientTvShowTests.cs
  4. 4
      TMDbLibTests/TMDbLibTests.csproj

82
TMDbLib/Client/TMDbClientTvShows.cs

@ -1,7 +1,11 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using TMDbLib.Objects.Authentication;
using TMDbLib.Objects.Changes;
using TMDbLib.Objects.General;
@ -99,6 +103,84 @@ namespace TMDbLib.Client
return item;
}
/// <summary>
/// Retrieve a tv Show by id.
/// </summary>
/// <param name="id">TMDb id of the tv show to retrieve.</param>
/// <param name="extraMethods">Enum flags indicating any additional data that should be fetched in the same request.</param>
/// <param name="language">If specified the api will attempt to return a localized result. ex: en,it,es </param>
/// <returns>The requested Tv Show</returns>
public async Task<TvShow> GetTvShowWithFullSeasonInfoAsync(int id, TvShowMethods extraMethods = TvShowMethods.Undefined, string language = null)
{
if (extraMethods.HasFlag(TvShowMethods.AccountStates))
RequireSessionId(SessionType.UserSession);
RestRequest req = _client.Create("tv/{id}");
req.AddUrlSegment("id", id.ToString(CultureInfo.InvariantCulture));
if (extraMethods.HasFlag(TvShowMethods.AccountStates))
AddSessionId(req, SessionType.UserSession);
language = language ?? DefaultLanguage;
if (!string.IsNullOrWhiteSpace(language))
req.AddParameter("language", language);
// Add all method flags
var appendValues = Enum.GetValues(typeof(TvShowMethods))
.OfType<TvShowMethods>()
.Except(new[] { TvShowMethods.Undefined })
.Where(s => extraMethods.HasFlag(s))
.Select(s => s.GetDescription())
.ToList();
int maxNumberOfAppends = 20;
for (int i = 0; i < maxNumberOfAppends - appendValues.Count; i++)
{
appendValues.Add($"season/{i}");
}
req.AddParameter("append_to_response", string.Join(",", appendValues));
RestResponse response = await req.ExecuteGet().ConfigureAwait(false);
Stream content = await response.GetContent().ConfigureAwait(false);
TvShowWithSeasonDetails item;
using (StreamReader sr = new StreamReader(content, _client.Encoding))
using (JsonTextReader tr = new JsonTextReader(sr))
{
var dynamicResult = _client.Serializer.Deserialize<JObject>(tr);
// Get the base show information
item = dynamicResult.ToObject<TvShowWithSeasonDetails>(_client.Serializer);
// No data to patch up so return
if (item == null)
return null;
// Extract the full season details
item.SeasonsWithDetails =
((IEnumerable<KeyValuePair<string, JToken>>)dynamicResult)
.Where(x => x.Key.StartsWith("season/"))
.Select(x => x.Value.ToObject<TvSeason>(_client.Serializer))
.ToList();
// TODO retrieve missing seasons with additional call and add them to the list
// Patch the season id's, at time of writing a _id is returned to does not match the normal season id's
foreach (var season in item.Seasons)
item.SeasonsWithDetails.First(s=>s.SeasonNumber == season.SeasonNumber).Id = season.Id;
}
// Patch up data, so that the end user won't notice that we share objects between request-types.
if (item.Translations != null)
item.Translations.Id = id;
if (item.AccountStates != null)
item.AccountStates.Id = id;
return item;
}
public async Task<ChangesContainer> GetTvShowChangesAsync(int id)
{
return await GetTvShowMethod<ChangesContainer>(id, TvShowMethods.Changes).ConfigureAwait(false);

9
TMDbLib/Objects/TvShows/TvShowWithSeasonDetails.cs

@ -0,0 +1,9 @@
using System.Collections.Generic;
namespace TMDbLib.Objects.TvShows
{
public class TvShowWithSeasonDetails : TvShow
{
public List<TvSeason> SeasonsWithDetails { get; set; }
}
}

19
TMDbLibTests/ClientTvShowTests.cs

@ -48,6 +48,25 @@ namespace TMDbLibTests
Assert.Null(selector(tvShow));
}
[Fact]
public void TestTvShowWithSeasonsExtrasNone()
{
// We will intentionally ignore errors reg. missing JSON as we do not request it
IgnoreMissingJson(" / known_for", " / account_states", " / alternative_titles", " / changes", " / content_ratings", " / credits", " / external_ids", " / images", " / keywords", " / similar", " / translations", " / videos", " / genre_ids",
" / SeasonsWithDetails", "season/0 / account_states", "season/0 / credits", "season/0 / external_ids", "season/0 / id", "season/0 / images", "season/0 / videos", "season/1 / account_states", "season/1 / credits", "season/1 / external_ids",
"season/1 / id", "season/1 / images", "season/1 / videos", "season/2 / account_states", "season/2 / credits", "season/2 / external_ids", "season/2 / id", "season/2 / images", "season/2 / videos", "season/3 / account_states", "season/3 / credits",
"season/3 / external_ids", "season/3 / id", "season/3 / images", "season/3 / videos", "season/4 / account_states", "season/4 / credits", "season/4 / external_ids", "season/4 / id", "season/4 / images", "season/4 / videos", "season/5 / account_states",
"season/5 / credits", "season/5 / external_ids", "season/5 / id", "season/5 / images", "season/5 / videos");
IgnoreMissingCSharp("season/0 / season/0", "season/0._id / _id", "season/1 / season/1", "season/1._id / _id", "season/2 / season/2", "season/2._id / _id", "season/3 / season/3", "season/3._id / _id", "season/4 / season/4", "season/4._id / _id", "season/5 / season/5", "season/5._id / _id");
TvShow tvShow = Config.Client.GetTvShowWithFullSeasonInfoAsync(IdHelper.BreakingBad).Result;
TestBreakingBadBaseProperties(tvShow);
// Test all extras, ensure none of them are populated
foreach (Func<TvShow, object> selector in _methods.Values)
Assert.Null(selector(tvShow));
}
[Fact]
public void TestTvShowExtrasAll()
{

4
TMDbLibTests/TMDbLibTests.csproj

@ -21,4 +21,8 @@
<PackageReference Include="xunit" Version="2.2.0" />
</ItemGroup>
<ItemGroup>
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup>
</Project>
Loading…
Cancel
Save