From 621047bc1912dc9bd699b917b9860abf96b48346 Mon Sep 17 00:00:00 2001 From: Anders Bjerner Date: Tue, 5 Oct 2021 14:49:31 +0200 Subject: [PATCH] Introduced new concept with provider credentials details Whenever we save the details about a video, it may be useful to also save a reference to the credentials that were used for retrieving the video - eg. if we later need to fetch additional information about the video. Implements #6 --- .../IVideoProviderCredentialsDetails.cs | 25 +++++++++ .../VideoProviderCredentialsDetails.cs | 53 +++++++++++++++++++ .../Models/VideoPickerValue.cs | 14 +++++ .../Providers/Vimeo/VimeoVideoProvider.cs | 8 ++- .../Providers/YouTube/YouTubeVideoProvider.cs | 4 +- 5 files changed, 101 insertions(+), 3 deletions(-) create mode 100644 src/Skybrud.VideoPicker/Models/Providers/IVideoProviderCredentialsDetails.cs create mode 100644 src/Skybrud.VideoPicker/Models/Providers/VideoProviderCredentialsDetails.cs diff --git a/src/Skybrud.VideoPicker/Models/Providers/IVideoProviderCredentialsDetails.cs b/src/Skybrud.VideoPicker/Models/Providers/IVideoProviderCredentialsDetails.cs new file mode 100644 index 0000000..ddbd591 --- /dev/null +++ b/src/Skybrud.VideoPicker/Models/Providers/IVideoProviderCredentialsDetails.cs @@ -0,0 +1,25 @@ +using System; +using Newtonsoft.Json; +using Skybrud.VideoPicker.Models.Config; + +namespace Skybrud.VideoPicker.Models.Providers { + + /// + /// Interface describing a trimmed down version of . + /// + /// The general idea with this interface is that while classes implementing may + /// expose sensitive information (eg. API keys or access tokens for the provider), classes implementing + /// should instead be seen as a reference to the crendentials - and + /// without any sensitive information. + /// + public interface IVideoProviderCredentialsDetails { + + /// + /// Gets the unique ID of the credentials. + /// + [JsonProperty("id")] + Guid Id { get; } + + } + +} \ No newline at end of file diff --git a/src/Skybrud.VideoPicker/Models/Providers/VideoProviderCredentialsDetails.cs b/src/Skybrud.VideoPicker/Models/Providers/VideoProviderCredentialsDetails.cs new file mode 100644 index 0000000..7fa27bb --- /dev/null +++ b/src/Skybrud.VideoPicker/Models/Providers/VideoProviderCredentialsDetails.cs @@ -0,0 +1,53 @@ +using System; +using Newtonsoft.Json.Linq; +using Skybrud.Essentials.Json.Extensions; +using Skybrud.VideoPicker.Models.Config; + +namespace Skybrud.VideoPicker.Models.Providers { + + /// + /// Default implementation of the interface. + /// + public class VideoProviderCredentialsDetails : IVideoProviderCredentialsDetails { + + #region Properties + + /// + /// Gets the unique ID of the credentials. + /// + public Guid Id { get; } + + #endregion + + #region Constructors + + private VideoProviderCredentialsDetails(JObject json) { + Id = json.GetGuid("id"); + } + + /// + /// Initializes a new instance from the specified . + /// + /// The full crendentials model this instance should be based on. + public VideoProviderCredentialsDetails(IProviderCredentials credentials) { + Id = credentials.Id; + } + + #endregion + + #region Static methods + + /// + /// Parses the specified into an instance of . + /// + /// The instance of to be parsed. + /// An instance of . + public static VideoProviderCredentialsDetails Parse(JObject json) { + return json == null ? null : new VideoProviderCredentialsDetails(json); + } + + #endregion + + } + +} \ No newline at end of file diff --git a/src/Skybrud.VideoPicker/Models/VideoPickerValue.cs b/src/Skybrud.VideoPicker/Models/VideoPickerValue.cs index bed1ed3..30abc31 100644 --- a/src/Skybrud.VideoPicker/Models/VideoPickerValue.cs +++ b/src/Skybrud.VideoPicker/Models/VideoPickerValue.cs @@ -1,6 +1,7 @@ using System.Web; using Newtonsoft.Json; using Skybrud.Essentials.Json.Converters; +using Skybrud.VideoPicker.Models.Config; using Skybrud.VideoPicker.Models.Providers; using Skybrud.VideoPicker.Models.Videos; @@ -13,6 +14,12 @@ public class VideoPickerValue { [JsonProperty("provider")] public IVideoProviderDetails Provider { get; } + /// + /// Gets a reference to the credentials used when this video was inserted. + /// + [JsonProperty("credentials")] + public IVideoProviderCredentialsDetails Credentials { get; } + [JsonProperty("details")] public IVideoDetails Details { get; } @@ -33,6 +40,13 @@ public VideoPickerValue(IVideoProviderDetails provider, IVideoDetails details, I Embed = embed; } + public VideoPickerValue(IVideoProviderDetails provider, IVideoProviderCredentialsDetails credentials, IVideoDetails details, IVideoEmbedOptions embed) { + Provider = provider; + Credentials = credentials; + Details = details; + Embed = embed; + } + #endregion } diff --git a/src/Skybrud.VideoPicker/Providers/Vimeo/VimeoVideoProvider.cs b/src/Skybrud.VideoPicker/Providers/Vimeo/VimeoVideoProvider.cs index e37b899..862a22d 100644 --- a/src/Skybrud.VideoPicker/Providers/Vimeo/VimeoVideoProvider.cs +++ b/src/Skybrud.VideoPicker/Providers/Vimeo/VimeoVideoProvider.cs @@ -89,11 +89,13 @@ public VideoPickerValue GetVideo(VideoPickerService service, IVideoOptions optio VideoProviderDetails provider = new VideoProviderDetails(Alias, Name); + VideoProviderCredentialsDetails credentailsDetails = new VideoProviderCredentialsDetails(credentials); + VimeoVideoDetails details = new VimeoVideoDetails(video); VimeoVideoEmbedOptions embed = new VimeoVideoEmbedOptions(details); - return new VideoPickerValue(provider, details, embed); + return new VideoPickerValue(provider, credentailsDetails, details, embed); } @@ -101,11 +103,13 @@ public VideoPickerValue ParseValue(JObject obj, IProviderDataTypeConfig config) VideoProviderDetails provider = new VideoProviderDetails(Alias, Name); + VideoProviderCredentialsDetails credentials = obj.GetObject("credentials", VideoProviderCredentialsDetails.Parse); + VimeoVideoDetails details = obj.GetObject("details", VimeoVideoDetails.Parse); VimeoVideoEmbedOptions embed = new VimeoVideoEmbedOptions(details, config as VimeoDataTypeConfig); - return new VideoPickerValue(provider, details, embed); + return new VideoPickerValue(provider, credentials, details, embed); } diff --git a/src/Skybrud.VideoPicker/Providers/YouTube/YouTubeVideoProvider.cs b/src/Skybrud.VideoPicker/Providers/YouTube/YouTubeVideoProvider.cs index 0f9d563..e5ea67a 100644 --- a/src/Skybrud.VideoPicker/Providers/YouTube/YouTubeVideoProvider.cs +++ b/src/Skybrud.VideoPicker/Providers/YouTube/YouTubeVideoProvider.cs @@ -103,11 +103,13 @@ public VideoPickerValue ParseValue(JObject obj, IProviderDataTypeConfig config) VideoProviderDetails provider = new VideoProviderDetails(Alias, Name); + VideoProviderCredentialsDetails credentials = obj.GetObject("credentials", VideoProviderCredentialsDetails.Parse); + YouTubeVideoDetails details = obj.GetObject("details", YouTubeVideoDetails.Parse); YouTubeVideoEmbedOptions embed = new YouTubeVideoEmbedOptions(details, config as YouTubeDataTypeConfig); - return new VideoPickerValue(provider, details, embed); + return new VideoPickerValue(provider, credentials, details, embed); }