Skip to content

Commit

Permalink
Added support for players and experiences
Browse files Browse the repository at this point in the history
  • Loading branch information
Richard Pizzo authored and Richard Pizzo committed May 16, 2022
1 parent 91aa852 commit d450e29
Show file tree
Hide file tree
Showing 26 changed files with 1,086 additions and 15 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -385,4 +385,5 @@ FodyWeavers.xsd

# JetBrains Rider
.idea/
*.sln.iml
*.sln.iml
/package.zip
2 changes: 2 additions & 0 deletions Brightcove.Core/Brightcove.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
<Compile Include="Models\IngestMaster.cs" />
<Compile Include="Models\IngestTextTrack.cs" />
<Compile Include="Models\IngestVideo.cs" />
<Compile Include="Models\Experience.cs" />
<Compile Include="Models\Player.cs" />
<Compile Include="Models\TemporaryIngestUrls.cs" />
<Compile Include="Models\Converters\BrightcovePlayListSearchFieldConverter.cs" />
<Compile Include="Models\Count.cs" />
Expand Down
30 changes: 30 additions & 0 deletions Brightcove.Core/Models/Experience.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;

namespace Brightcove.Core.Models
{
/// <summary>
/// Represents a experience object from the Brightcove API
/// For more information, see https://apis.support.brightcove.com/ipx/references/reference.html#operation/GetExperiences
/// </summary>
public class Experience : Asset
{
[JsonProperty("created_at", NullValueHandling = NullValueHandling.Ignore)]
public DateTime CreationDate { get; set; }

[JsonProperty("publishedUrl", NullValueHandling = NullValueHandling.Ignore)]
public string Url { get; set; }

public Experience ShallowCopy()
{
return (Experience)this.MemberwiseClone();
}
}

public class ExperienceList
{
[JsonProperty("items", NullValueHandling = NullValueHandling.Ignore)]
public IEnumerable<Experience> Items { get; set; }
}
}
47 changes: 47 additions & 0 deletions Brightcove.Core/Models/Player.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;

namespace Brightcove.Core.Models
{
/// <summary>
/// Represents a players object from the Brightcove API
/// For more information, see https://apis.support.brightcove.com/player-management/references/reference.html#operation/GetPlayer
/// </summary>
public class Player : Asset
{
[JsonProperty("created_at", NullValueHandling = NullValueHandling.Ignore)]
public DateTime CreattionDate { get; set; }

[JsonProperty("url", NullValueHandling = NullValueHandling.Ignore)]
public string Url { get; set; }

[JsonProperty("branches", NullValueHandling = NullValueHandling.Ignore)]
public Branches Branches { get; set; }

public Player ShallowCopy()
{
return (Player)this.MemberwiseClone();
}
}

public class Branches
{
[JsonProperty("master", NullValueHandling = NullValueHandling.Ignore)]
public Master Master { get; set; }
}

public class Master
{
[JsonProperty("updated_at", NullValueHandling = NullValueHandling.Ignore)]
public DateTime UpdatedAt { get; set; }
}

public class PlayerList
{
[JsonProperty("items", NullValueHandling = NullValueHandling.Ignore)]
public IEnumerable<Player> Items { get; set; }
}


}
118 changes: 113 additions & 5 deletions Brightcove.Core/Services/BrightcoveService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ public class BrightcoveService

readonly string cmsBaseUrl = "https://cms.api.brightcove.com/v1/accounts";
readonly string ingestBaseUrl = "https://ingest.api.brightcove.com/v1/accounts";
readonly string playersBaseUrl = "https://players.api.brightcove.com/v1/accounts";
readonly string experienceBaseUrl = "https://experiences.api.brightcove.com/v1/accounts";
readonly string accountId;
readonly BrightcoveAuthenticationService authenticationService;

public BrightcoveService(string accountId, string clientId, string clientSecret)
{
if(string.IsNullOrWhiteSpace(accountId))
if (string.IsNullOrWhiteSpace(accountId))
{
throw new ArgumentException("argument must not be null or empty", nameof(accountId));
}
Expand Down Expand Up @@ -162,6 +164,46 @@ public Video UpdateVideo(Video video)
return JsonConvert.DeserializeObject<Video>(response.Content.ReadAsString());
}

public Player CreatePlayer(string name, string description)
{
var playerRequest = new { name = name, description = description };

HttpRequestMessage request = new HttpRequestMessage();
request.Content = new StringContent(JsonConvert.SerializeObject(playerRequest), Encoding.UTF8, "application/json");
request.Method = HttpMethod.Post;
request.RequestUri = new Uri($"{playersBaseUrl}/{accountId}/players");

HttpResponseMessage response = SendRequest(request);
return JsonConvert.DeserializeObject<Player>(response.Content.ReadAsString());
}

public Player UpdatePlayer(Player player)
{
HttpRequestMessage request = new HttpRequestMessage();
request.Method = new HttpMethod("PATCH");
request.RequestUri = new Uri($"{playersBaseUrl}/{accountId}/players/{player.Id}");

var newPlayer = new { name = player.Name, description = player.ShortDescription };

request.Content = new StringContent(JsonConvert.SerializeObject(newPlayer), Encoding.UTF8, "application/json");
HttpResponseMessage response = SendRequest(request);
return JsonConvert.DeserializeObject<Player>(response.Content.ReadAsString());
}

public void DeletePlayer(string playerId)
{
if (string.IsNullOrWhiteSpace(playerId))
return;

if (playerId.Contains(","))
throw new ArgumentException("the player ID must not contain any commas", nameof(playerId));

HttpRequestMessage request = new HttpRequestMessage();
request.Method = HttpMethod.Delete;
request.RequestUri = new Uri($"{playersBaseUrl}/{accountId}/players/{playerId}");
SendRequest(request);
}

public PlayList UpdatePlaylist(PlayList playlist)
{
HttpRequestMessage request = new HttpRequestMessage();
Expand Down Expand Up @@ -209,15 +251,81 @@ public IEnumerable<Video> GetVideos(int offset = 0, int limit = 20, string sort
return JsonConvert.DeserializeObject<List<Video>>(response.Content.ReadAsString());
}

public PlayerList GetPlayers()
{
HttpRequestMessage request = new HttpRequestMessage();

request.Method = HttpMethod.Get;
request.RequestUri = new Uri($"{playersBaseUrl}/{accountId}/players");

HttpResponseMessage response = SendRequest(request);

PlayerList players = JsonConvert.DeserializeObject<PlayerList>(response.Content.ReadAsString());
foreach (var p in players.Items)
p.LastSyncTime = DateTime.UtcNow;

return players;
}

public ExperienceList GetExperiences()
{
HttpRequestMessage request = new HttpRequestMessage();

request.Method = HttpMethod.Get;
request.RequestUri = new Uri($"{experienceBaseUrl}/{accountId}/experiences");

HttpResponseMessage response = SendRequest(request);

return JsonConvert.DeserializeObject<ExperienceList>(response.Content.ReadAsString());
}

public bool TryGetPlayer(string playerId, out Player player)
{
if (string.IsNullOrWhiteSpace(playerId))
{
player = null;
return false;
}

if (playerId.Contains(","))
{
throw new ArgumentException("the video ID must not contain any commas", nameof(playerId));
}

HttpRequestMessage request = new HttpRequestMessage();
HttpResponseMessage response;

request.Method = HttpMethod.Get;
request.RequestUri = new Uri($"{playersBaseUrl}/{accountId}/players/{playerId}");

try
{
response = SendRequest(request);
}
catch (HttpStatusException ex)
{
if (ex.Response.StatusCode == HttpStatusCode.NotFound)
{
player = null;
return false;
}

throw ex;
}

player = JsonConvert.DeserializeObject<Player>(response.Content.ReadAsString());
return true;
}

public bool TryGetVideo(string videoId, out Video video)
{
if(string.IsNullOrWhiteSpace(videoId))
if (string.IsNullOrWhiteSpace(videoId))
{
video = null;
return false;
}

if(videoId.Contains(","))
if (videoId.Contains(","))
{
throw new ArgumentException("the video ID must not contain any commas", nameof(videoId));
}
Expand All @@ -232,9 +340,9 @@ public bool TryGetVideo(string videoId, out Video video)
{
response = SendRequest(request);
}
catch(HttpStatusException ex)
catch (HttpStatusException ex)
{
if(ex.Response.StatusCode == HttpStatusCode.NotFound)
if (ex.Response.StatusCode == HttpStatusCode.NotFound)
{
video = null;
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -450,12 +450,16 @@
<Compile Include="Converters\ResolveOrUpdateAssetModelPipelineStepConverter.cs" />
<Compile Include="Processors\BasePipelineStepWithWebApiEndpointProcessor.cs" />
<Compile Include="Processors\BasePipelineStepWithEndpointFromProcessor.cs" />
<Compile Include="Processors\GetExperiencesPipelineStepProcessor.cs" />
<Compile Include="Processors\GetPlayersPipelineStepProcessor.cs" />
<Compile Include="Processors\ReadAssetItemsPipelineStepProcessor.cs" />
<Compile Include="Processors\ResolveAssetItemPipelineStepProcessor.cs" />
<Compile Include="Processors\GetPlayListsPipelineStepProcessor.cs" />
<Compile Include="Processors\GetVideosPipelineStepProcessor.cs" />
<Compile Include="Processors\ResolvePlayerModelPipelineStepProcessor.cs" />
<Compile Include="Processors\ResolvePlayListModelPipelineStepProcessor.cs" />
<Compile Include="Processors\UpdatePlaylistModelPipelineStepProcessor.cs" />
<Compile Include="Processors\UpdatePlayerModelPipelineStepProcessor.cs" />
<Compile Include="Processors\UpdateVideoModelPipelineStepProcessor.cs" />
<Compile Include="Processors\ResolveVideoModelPipelineStepProcessor.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class BasePipelineStepWithEndpointFromProcessor : BasePipelineStepWithEnd
{
protected Endpoint EndpointFrom { get; set; }


protected override void ProcessPipelineStep(PipelineStep pipelineStep = null, PipelineContext pipelineContext = null, ILogger logger = null)
{
if (pipelineStep == null)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Brightcove.Core.Models;
using Brightcove.Core.Services;
using Brightcove.DataExchangeFramework.Settings;
using Sitecore.Data.Fields;
using Sitecore.Data.Items;
using Sitecore.DataExchange.Attributes;
using Sitecore.DataExchange.Contexts;
using Sitecore.DataExchange.Converters.PipelineSteps;
Expand All @@ -9,6 +11,7 @@
using Sitecore.DataExchange.Plugins;
using Sitecore.DataExchange.Processors.PipelineSteps;
using Sitecore.DataExchange.Repositories;
using Sitecore.SecurityModel;
using Sitecore.Services.Core.Diagnostics;
using System;
using System.Collections.Generic;
Expand All @@ -27,7 +30,7 @@ protected override void ProcessPipelineStep(PipelineStep pipelineStep = null, Pi
{
base.ProcessPipelineStep(pipelineStep, pipelineContext, logger);

if(pipelineContext.CriticalError)
if (pipelineContext.CriticalError)
{
return;
}
Expand Down Expand Up @@ -78,5 +81,41 @@ protected override void ProcessPipelineStep(PipelineStep pipelineStep = null, Pi
return;
}
}

public void SetFolderSettings(string folderName)
{
Sitecore.Data.Database masterDB = Sitecore.Configuration.Factory.GetDatabase("master");
Sitecore.Data.Items.Item node = masterDB.GetItem("/sitecore/media library/BrightCove/BrightCove Account/"+ folderName);
if (node == null)
{
Sitecore.Data.Items.Item parentNode = masterDB.GetItem("/sitecore/media library/BrightCove/BrightCove Account");
Sitecore.Data.Items.Item folder = masterDB.GetItem("/sitecore/templates/Common/Folder");
parentNode.Add(folderName, new TemplateItem(folder));

node = masterDB.GetItem("/sitecore/media library/BrightCove/BrightCove Account/"+ folderName);
using (new Sitecore.Data.Items.EditContext(node, SecurityCheck.Disable))
{
IsBucketItemCheckBox(node).Checked = true;
}
}
else
{
using (new Sitecore.Data.Items.EditContext(node, SecurityCheck.Disable))
{
if (!IsBucketItemCheck(node))
IsBucketItemCheckBox(node).Checked = true;
}
}
}

public bool IsBucketItemCheck(Item item)
{
return (((item != null) && (item.Fields[Sitecore.Buckets.Util.Constants.IsBucket] != null)) && item.Fields[Sitecore.Buckets.Util.Constants.IsBucket].Value.Equals("1"));
}

public CheckboxField IsBucketItemCheckBox(Item item)
{
return item.Fields[Sitecore.Buckets.Util.Constants.IsBucket];
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Brightcove.Core.Services;
using Brightcove.DataExchangeFramework.Settings;
using Sitecore.Data.Fields;
using Sitecore.Data.Items;
using Sitecore.DataExchange.Contexts;
using Sitecore.DataExchange.Models;
using Sitecore.DataExchange.Plugins;
using Sitecore.SecurityModel;
using Sitecore.Services.Core.Diagnostics;

namespace Brightcove.DataExchangeFramework.Processors
{
class GetExperiencesPipelineStepProcessor : BasePipelineStepWithWebApiEndpointProcessor
{
BrightcoveService service;

protected override void ProcessPipelineStep(PipelineStep pipelineStep = null, PipelineContext pipelineContext = null, ILogger logger = null)
{
base.ProcessPipelineStep(pipelineStep, pipelineContext, logger);

service = new BrightcoveService(WebApiSettings.AccountId, WebApiSettings.ClientId, WebApiSettings.ClientSecret);

var data = service.GetExperiences().Items;
var dataSettings = new IterableDataSettings(data);

pipelineContext.AddPlugin(dataSettings);

SetFolderSettings("Experiences");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ protected override void ProcessPipelineStep(PipelineStep pipelineStep = null, Pi
var dataSettings = new IterableDataSettings(data);

pipelineContext.AddPlugin(dataSettings);

SetFolderSettings("Playlists");
}

protected virtual IEnumerable<PlayList> GetIterableData(WebApiSettings settings, PipelineStep pipelineStep)
Expand Down
Loading

0 comments on commit d450e29

Please sign in to comment.