Skip to content

Commit

Permalink
Merge pull request #32 from henrikfroehling/GH-30
Browse files Browse the repository at this point in the history
Resolves GH-30
  • Loading branch information
henrikfroehling committed Jul 25, 2018
2 parents e44cb3d + 74fe978 commit 2fc17cd
Show file tree
Hide file tree
Showing 40 changed files with 859 additions and 180 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
namespace TraktNet.Objects.Basic.Json.Writer
{
using Enums;
using Newtonsoft.Json;
using Objects.Json;
using System;
using System.Threading;
using System.Threading.Tasks;

internal abstract class AMetadataObjectJsonWriter<TMetadataObjectType> : AObjectJsonWriter<TMetadataObjectType> where TMetadataObjectType : ITraktMetadata
{
public override async Task WriteObjectAsync(JsonTextWriter jsonWriter, TMetadataObjectType obj, CancellationToken cancellationToken = default)
{
if (jsonWriter == null)
throw new ArgumentNullException(nameof(jsonWriter));

await jsonWriter.WriteStartObjectAsync(cancellationToken).ConfigureAwait(false);
await WriteMetadataObjectAsync(jsonWriter, obj, cancellationToken).ConfigureAwait(false);
await jsonWriter.WriteEndObjectAsync(cancellationToken).ConfigureAwait(false);
}

protected virtual async Task WriteMetadataObjectAsync(JsonTextWriter jsonWriter, TMetadataObjectType obj, CancellationToken cancellationToken = default)
{
if (obj.MediaType != null && obj.MediaType != TraktMediaType.Unspecified)
{
await jsonWriter.WritePropertyNameAsync(JsonProperties.METADATA_PROPERTY_NAME_MEDIA_TYPE, cancellationToken).ConfigureAwait(false);
await jsonWriter.WriteValueAsync(obj.MediaType.ObjectName, cancellationToken).ConfigureAwait(false);
}

if (obj.MediaResolution != null && obj.MediaResolution != TraktMediaResolution.Unspecified)
{
await jsonWriter.WritePropertyNameAsync(JsonProperties.METADATA_PROPERTY_NAME_RESOLUTION, cancellationToken).ConfigureAwait(false);
await jsonWriter.WriteValueAsync(obj.MediaResolution.ObjectName, cancellationToken).ConfigureAwait(false);
}

if (obj.Audio != null && obj.Audio != TraktMediaAudio.Unspecified)
{
await jsonWriter.WritePropertyNameAsync(JsonProperties.METADATA_PROPERTY_NAME_AUDIO, cancellationToken).ConfigureAwait(false);
await jsonWriter.WriteValueAsync(obj.Audio.ObjectName, cancellationToken).ConfigureAwait(false);
}

if (obj.AudioChannels != null && obj.AudioChannels != TraktMediaAudioChannel.Unspecified)
{
await jsonWriter.WritePropertyNameAsync(JsonProperties.METADATA_PROPERTY_NAME_AUDIO_CHANNELS, cancellationToken).ConfigureAwait(false);
await jsonWriter.WriteValueAsync(obj.AudioChannels.ObjectName, cancellationToken).ConfigureAwait(false);
}

if (obj.ThreeDimensional.HasValue)
{
await jsonWriter.WritePropertyNameAsync(JsonProperties.METADATA_PROPERTY_NAME_3D, cancellationToken).ConfigureAwait(false);
await jsonWriter.WriteValueAsync(obj.ThreeDimensional, cancellationToken).ConfigureAwait(false);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,52 +1,6 @@
namespace TraktNet.Objects.Basic.Json.Writer
{
using Enums;
using Newtonsoft.Json;
using Objects.Json;
using System;
using System.Threading;
using System.Threading.Tasks;

internal class MetadataObjectJsonWriter : AObjectJsonWriter<ITraktMetadata>
internal class MetadataObjectJsonWriter : AMetadataObjectJsonWriter<ITraktMetadata>
{
public override async Task WriteObjectAsync(JsonTextWriter jsonWriter, ITraktMetadata obj, CancellationToken cancellationToken = default)
{
if (jsonWriter == null)
throw new ArgumentNullException(nameof(jsonWriter));

await jsonWriter.WriteStartObjectAsync(cancellationToken).ConfigureAwait(false);

if (obj.MediaType != null && obj.MediaType != TraktMediaType.Unspecified)
{
await jsonWriter.WritePropertyNameAsync(JsonProperties.METADATA_PROPERTY_NAME_MEDIA_TYPE, cancellationToken).ConfigureAwait(false);
await jsonWriter.WriteValueAsync(obj.MediaType.ObjectName, cancellationToken).ConfigureAwait(false);
}

if (obj.MediaResolution != null && obj.MediaResolution != TraktMediaResolution.Unspecified)
{
await jsonWriter.WritePropertyNameAsync(JsonProperties.METADATA_PROPERTY_NAME_RESOLUTION, cancellationToken).ConfigureAwait(false);
await jsonWriter.WriteValueAsync(obj.MediaResolution.ObjectName, cancellationToken).ConfigureAwait(false);
}

if (obj.Audio != null && obj.Audio != TraktMediaAudio.Unspecified)
{
await jsonWriter.WritePropertyNameAsync(JsonProperties.METADATA_PROPERTY_NAME_AUDIO, cancellationToken).ConfigureAwait(false);
await jsonWriter.WriteValueAsync(obj.Audio.ObjectName, cancellationToken).ConfigureAwait(false);
}

if (obj.AudioChannels != null && obj.AudioChannels != TraktMediaAudioChannel.Unspecified)
{
await jsonWriter.WritePropertyNameAsync(JsonProperties.METADATA_PROPERTY_NAME_AUDIO_CHANNELS, cancellationToken).ConfigureAwait(false);
await jsonWriter.WriteValueAsync(obj.AudioChannels.ObjectName, cancellationToken).ConfigureAwait(false);
}

if (obj.ThreeDimensional.HasValue)
{
await jsonWriter.WritePropertyNameAsync(JsonProperties.METADATA_PROPERTY_NAME_3D, cancellationToken).ConfigureAwait(false);
await jsonWriter.WriteValueAsync(obj.ThreeDimensional, cancellationToken).ConfigureAwait(false);
}

await jsonWriter.WriteEndObjectAsync(cancellationToken).ConfigureAwait(false);
}
}
}
1 change: 1 addition & 0 deletions Source/Lib/Trakt.NET/Objects/Json/JsonProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,7 @@ internal static class JsonProperties
internal const string SYNC_COLLECTION_POST_SHOW_SEASON_PROPERTY_NAME_EPISODES = "episodes";

internal const string SYNC_COLLECTION_POST_SHOW_EPISODE_PROPERTY_NAME_NUMBER = "number";
internal const string SYNC_COLLECTION_POST_SHOW_EPISODE_PROPERTY_NAME_COLLECTED_AT = "collected_at";

internal const string SYNC_COLLECTION_POST_EPISODE_PROPERTY_NAME_COLLECTED_AT = "collected_at";
internal const string SYNC_COLLECTION_POST_EPISODE_PROPERTY_NAME_IDS = "ids";
Expand Down
14 changes: 14 additions & 0 deletions Source/Lib/Trakt.NET/Objects/Json/JsonReaderHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,20 @@ internal static async Task<IEnumerable<ulong>> ReadUnsignedLongArrayAsync(JsonTe
return new Pair<bool, float>(false, default);
}

internal static async Task<Pair<bool, int>> ReadIntegerValueAsync(JsonTextReader jsonReader, CancellationToken cancellationToken = default)
{
if (await jsonReader.ReadAsync(cancellationToken))
{
if (jsonReader.TokenType == JsonToken.Integer)
{
if (int.TryParse(jsonReader.Value.ToString(), out int value))
return new Pair<bool, int>(true, value);
}
}

return new Pair<bool, int>(false, default);
}

internal static async Task<Pair<bool, uint>> ReadUnsignedIntegerValueAsync(JsonTextReader jsonReader, CancellationToken cancellationToken = default)
{
if (await jsonReader.ReadAsync(cancellationToken))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@
using Get.Episodes;
using System;

public interface ITraktSyncCollectionPostEpisode
public interface ITraktSyncCollectionPostEpisode : ITraktMetadata
{
DateTime? CollectedAt { get; set; }

ITraktEpisodeIds Ids { get; set; }

ITraktMetadata Metadata { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using Get.Movies;
using System;

public interface ITraktSyncCollectionPostMovie
public interface ITraktSyncCollectionPostMovie : ITraktMetadata
{
DateTime? CollectedAt { get; set; }

Expand All @@ -13,7 +13,5 @@ public interface ITraktSyncCollectionPostMovie
int? Year { get; set; }

ITraktMovieIds Ids { get; set; }

ITraktMetadata Metadata { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using System;
using System.Collections.Generic;

public interface ITraktSyncCollectionPostShow
public interface ITraktSyncCollectionPostShow : ITraktMetadata
{
DateTime? CollectedAt { get; set; }

Expand All @@ -16,7 +16,5 @@ public interface ITraktSyncCollectionPostShow
ITraktShowIds Ids { get; set; }

IEnumerable<ITraktSyncCollectionPostShowSeason> Seasons { get; set; }

ITraktMetadata Metadata { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
namespace TraktNet.Objects.Post.Syncs.Collection
{
public interface ITraktSyncCollectionPostShowEpisode
using Basic;
using System;

public interface ITraktSyncCollectionPostShowEpisode : ITraktMetadata
{
int Number { get; set; }

DateTime? CollectedAt { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -835,8 +835,9 @@ private void EnsureEpisodesListExists()
Year = movie.Year
};

if (metadata != null)
collectionMovie.Metadata = metadata;
// TODO
//if (metadata != null)
// collectionMovie.Metadata = metadata;

if (collectedAt.HasValue)
collectionMovie.CollectedAt = collectedAt.Value.ToUniversalTime();
Expand All @@ -859,8 +860,9 @@ private void EnsureEpisodesListExists()
Year = show.Year
};

if (metadata != null)
collectionShow.Metadata = metadata;
// TODO
//if (metadata != null)
// collectionShow.Metadata = metadata;

if (collectedAt.HasValue)
collectionShow.CollectedAt = collectedAt.Value.ToUniversalTime();
Expand All @@ -881,8 +883,9 @@ private void EnsureEpisodesListExists()
Ids = episode.Ids
};

if (metadata != null)
collectionEpisode.Metadata = metadata;
// TODO
//if (metadata != null)
// collectionEpisode.Metadata = metadata;

if (collectedAt.HasValue)
collectionEpisode.CollectedAt = collectedAt.Value.ToUniversalTime();
Expand Down Expand Up @@ -910,8 +913,9 @@ private void EnsureEpisodesListExists()
Year = show.Year
};

if (metadata != null)
collectionShow.Metadata = metadata;
// TODO
//if (metadata != null)
// collectionShow.Metadata = metadata;

if (collectedAt.HasValue)
collectionShow.CollectedAt = collectedAt.Value.ToUniversalTime();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,12 @@
/// A Trakt collection post episode, containing the required episode ids,
/// optional metadata and an optional datetime, when the episode was collected.
/// </summary>
public class TraktSyncCollectionPostEpisode : ITraktSyncCollectionPostEpisode
public class TraktSyncCollectionPostEpisode : TraktMetadata, ITraktSyncCollectionPostEpisode
{
/// <summary>Gets or sets the optional UTC datetime, when the Trakt episode was collected.</summary>
public DateTime? CollectedAt { get; set; }

/// <summary>Gets or sets the required episode ids. See also <seealso cref="ITraktEpisodeIds" />.</summary>
public ITraktEpisodeIds Ids { get; set; }

/// <summary>
/// Gets or sets optional metadata about the Trakt episode. See also <seealso cref="ITraktMetadata" />.
/// <para>Nullable</para>
/// </summary>
public ITraktMetadata Metadata { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
/// A Trakt collection post movie, containing the required movie ids,
/// optional metadata and an optional datetime, when the movie was collected.
/// </summary>
public class TraktSyncCollectionPostMovie : ITraktSyncCollectionPostMovie
public class TraktSyncCollectionPostMovie : TraktMetadata, ITraktSyncCollectionPostMovie
{
/// <summary>Gets or sets the optional UTC datetime, when the Trakt movie was collected.</summary>
public DateTime? CollectedAt { get; set; }
Expand All @@ -21,11 +21,5 @@ public class TraktSyncCollectionPostMovie : ITraktSyncCollectionPostMovie

/// <summary>Gets or sets the required movie ids. See also <seealso cref="ITraktMovieIds" />.</summary>
public ITraktMovieIds Ids { get; set; }

/// <summary>
/// Gets or sets optional metadata about the Trakt movie. See also <seealso cref="ITraktMetadata" />.
/// <para>Nullable</para>
/// </summary>
public ITraktMetadata Metadata { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/// optional metadata and an optional datetime, when the show was collected.
/// <para>Can also contain optional seasons.</para>
/// </summary>
public class TraktSyncCollectionPostShow : ITraktSyncCollectionPostShow
public class TraktSyncCollectionPostShow : TraktMetadata, ITraktSyncCollectionPostShow
{
/// <summary>Gets or sets the optional UTC datetime, when the Trakt show was collected.</summary>
public DateTime? CollectedAt { get; set; }
Expand All @@ -32,11 +32,5 @@ public class TraktSyncCollectionPostShow : ITraktSyncCollectionPostShow
/// </para>
/// </summary>
public IEnumerable<ITraktSyncCollectionPostShowSeason> Seasons { get; set; }

/// <summary>
/// Gets or sets optional metadata about the Trakt show. See also <seealso cref="ITraktMetadata" />.
/// <para>Nullable</para>
/// </summary>
public ITraktMetadata Metadata { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
namespace TraktNet.Objects.Post.Syncs.Collection
{
using Basic;
using System;

/// <summary>A Trakt collection post episode, containing the required episode number.</summary>
public class TraktSyncCollectionPostShowEpisode : ITraktSyncCollectionPostShowEpisode
public class TraktSyncCollectionPostShowEpisode : TraktMetadata, ITraktSyncCollectionPostShowEpisode
{
/// <summary>Gets or sets the required season number of the Trakt episode.</summary>
public int Number { get; set; }

public DateTime? CollectedAt { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
namespace TraktNet.Objects.Post.Syncs.Collection.Json.Factories
{
using Objects.Json;
using System;
using Reader;
using Writer;

internal class SyncCollectionPostEpisodeJsonIOFactory : IJsonIOFactory<ITraktSyncCollectionPostEpisode>
{
public IObjectJsonReader<ITraktSyncCollectionPostEpisode> CreateObjectReader()
=> throw new NotSupportedException($"A object json reader for {nameof(ITraktSyncCollectionPostEpisode)} is not supported.");
public IObjectJsonReader<ITraktSyncCollectionPostEpisode> CreateObjectReader() => new SyncCollectionPostEpisodeObjectJsonReader();

public IArrayJsonReader<ITraktSyncCollectionPostEpisode> CreateArrayReader()
=> throw new NotSupportedException($"A array json reader for {nameof(ITraktSyncCollectionPostEpisode)} is not supported.");
public IArrayJsonReader<ITraktSyncCollectionPostEpisode> CreateArrayReader() => new SyncCollectionPostEpisodeArrayJsonReader();

public IObjectJsonWriter<ITraktSyncCollectionPostEpisode> CreateObjectWriter() => new SyncCollectionPostEpisodeObjectJsonWriter();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
namespace TraktNet.Objects.Post.Syncs.Collection.Json.Factories
{
using Objects.Json;
using System;
using Reader;
using Writer;

internal class SyncCollectionPostJsonIOFactory : IJsonIOFactory<ITraktSyncCollectionPost>
{
public IObjectJsonReader<ITraktSyncCollectionPost> CreateObjectReader()
=> throw new NotSupportedException($"A object json reader for {nameof(ITraktSyncCollectionPost)} is not supported.");
public IObjectJsonReader<ITraktSyncCollectionPost> CreateObjectReader() => new SyncCollectionPostObjectJsonReader();

public IArrayJsonReader<ITraktSyncCollectionPost> CreateArrayReader()
=> throw new NotSupportedException($"A array json reader for {nameof(ITraktSyncCollectionPost)} is not supported.");
public IArrayJsonReader<ITraktSyncCollectionPost> CreateArrayReader() => new SyncCollectionPostArrayJsonReader();

public IObjectJsonWriter<ITraktSyncCollectionPost> CreateObjectWriter() => new SyncCollectionPostObjectJsonWriter();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
namespace TraktNet.Objects.Post.Syncs.Collection.Json.Factories
{
using Objects.Json;
using System;
using Reader;
using Writer;

internal class SyncCollectionPostMovieJsonIOFactory : IJsonIOFactory<ITraktSyncCollectionPostMovie>
{
public IObjectJsonReader<ITraktSyncCollectionPostMovie> CreateObjectReader()
=> throw new NotSupportedException($"A object json reader for {nameof(ITraktSyncCollectionPostMovie)} is not supported.");
public IObjectJsonReader<ITraktSyncCollectionPostMovie> CreateObjectReader() => new SyncCollectionPostMovieObjectJsonReader();

public IArrayJsonReader<ITraktSyncCollectionPostMovie> CreateArrayReader()
=> throw new NotSupportedException($"A array json reader for {nameof(ITraktSyncCollectionPostMovie)} is not supported.");
public IArrayJsonReader<ITraktSyncCollectionPostMovie> CreateArrayReader() => new SyncCollectionPostMovieArrayJsonReader();

public IObjectJsonWriter<ITraktSyncCollectionPostMovie> CreateObjectWriter() => new SyncCollectionPostMovieObjectJsonWriter();
}
Expand Down
Loading

0 comments on commit 2fc17cd

Please sign in to comment.