Skip to content

Commit

Permalink
Merge pull request #114 from mfilippov/tag-support
Browse files Browse the repository at this point in the history
Add support for tags #112
  • Loading branch information
mfilippov authored Jan 8, 2018
2 parents 0f12744 + 886ef4a commit e9741bf
Show file tree
Hide file tree
Showing 9 changed files with 485 additions and 3 deletions.
75 changes: 75 additions & 0 deletions src/VimeoDotNet.Tests/TagTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using System.Threading.Tasks;
using Shouldly;
using Xunit;

namespace VimeoDotNet.Tests
{
public class TagTests : BaseTest
{
private static async Task CleanupTags(IVimeoClient client, long clipId)
{
var tags = await client.GetVideoTags(clipId);
foreach (var tag in tags.Data)
{
await client.DeleteVideoTagAsync(clipId, tag.Id);
}
}

[Fact]
public async Task ShouldCorrectlyAddTagVideoTag()
{
var client = CreateAuthenticatedClient();
await CleanupTags(client, VimeoSettings.VideoId);
var video = await client.GetVideoAsync(VimeoSettings.VideoId);
video.Tags.Count.ShouldBe(0);
var tag = await client.AddVideoTagAsync(VimeoSettings.VideoId, "test-tag1");
tag.ShouldNotBeNull();
tag.Id.ShouldBe("test-tag1");
tag.Name.ShouldBe("test-tag1");
tag.Canonical.ShouldBe("test-tag1");
tag.Uri.ShouldNotBeEmpty();
tag.Metadata.ShouldNotBeNull();
tag.Metadata.Connections.ShouldNotBeNull();
tag.Metadata.Connections.Videos.Uri.ShouldNotBeEmpty();
tag.Metadata.Connections.Videos.Options.ShouldNotBeEmpty();
tag.Metadata.Connections.Videos.Total.ShouldBeGreaterThan(0);
video = await client.GetVideoAsync(VimeoSettings.VideoId);
video.Tags.Count.ShouldBe(1);

await CleanupTags(client, VimeoSettings.VideoId);
video = await client.GetVideoAsync(VimeoSettings.VideoId);
video.Tags.Count.ShouldBe(0);
}

[Fact]
public async Task ShouldCorrectlyGetVideoTag()
{
var client = CreateAuthenticatedClient();
await CleanupTags(client, VimeoSettings.VideoId);
var video = await client.GetVideoAsync(VimeoSettings.VideoId);
video.Tags.Count.ShouldBe(0);

await client.AddVideoTagAsync(VimeoSettings.VideoId, "test-tag1");

var result = await client.GetVideoTagAsync("test-tag1");
result.Id.ShouldBe("test-tag1");

await CleanupTags(client, VimeoSettings.VideoId);
video = await client.GetVideoAsync(VimeoSettings.VideoId);
video.Tags.Count.ShouldBe(0);
}

[Fact]
public async Task ShouldCorrectlyGetVideobyTag()
{
var client = CreateAuthenticatedClient();
var result = await client.GetVideoByTag("test", 1, 10, GetVideoByTagSort.Name,
GetVideoByTagDirection.Asc, new[] {"uri", "name"});
result.Page.ShouldBe(1);
result.PerPage.ShouldBe(10);
result.Data.Count.ShouldBeGreaterThan(0);
result.Data[0].Name.ShouldNotBeEmpty();
result.Data[0].Uri.ShouldNotBeEmpty();
}
}
}
5 changes: 4 additions & 1 deletion src/VimeoDotNet/Constants/Endpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ internal static class Endpoints
public const string VideoLikes = "/videos/{clipId}/likes";
public const string VideoPreset = "/videos/{clipId}/presets/{presetId}";
public const string VideoTags = "/videos/{clipId}/tags";
public const string VideoTag = "/videos/{clipId}/tags/{tag}";
public const string VideoTag = "/videos/{clipId}/tags/{tagId}";
public const string VideosByTag = "/tags/{tagId}/videos";
public const string VideoAllowedUsers = "/videos/{clipId}/privacy/users";
public const string VideoAllowedUser = "/videos/{clipId}/privacy/users/{userId}";
public const string VideoAllowedDomains = "/videos/{clipId}/privacy/domains";
Expand All @@ -88,6 +89,8 @@ internal static class Endpoints
public const string Picture = "/videos/{clipId}/pictures/{pictureId}";
public const string Pictures = "/videos/{clipId}/pictures";

public const string Tag = "/tags/{tagId}";

public static string GetCurrentUserEndpoint(string endpoint)
{
return endpoint.Replace("users/{userId}", "me");
Expand Down
46 changes: 46 additions & 0 deletions src/VimeoDotNet/GetVideoByTagDirection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;
using JetBrains.Annotations;

namespace VimeoDotNet
{
/// <summary>
/// Get video by tag direction type
/// </summary>
[PublicAPI]
public enum GetVideoByTagDirection
{
/// <summary>
/// Ascending
/// </summary>
Asc,
/// <summary>
/// Descending
/// </summary>
Desc
}

/// <summary>
/// Get video by tag direction type
/// </summary>
internal static class GetVideoByTagDirectionEx
{
/// <summary>
/// Return string representation for enum value
/// </summary>
/// <param name="direction">Value</param>
/// <returns>String representation</returns>
/// <exception cref="ArgumentOutOfRangeException">Throw if value not handled.</exception>
public static string GetStringValue(this GetVideoByTagDirection direction)
{
switch (direction)
{
case GetVideoByTagDirection.Asc:
return "asc";
case GetVideoByTagDirection.Desc:
return "desc";
default:
throw new ArgumentOutOfRangeException(nameof(direction), direction, null);
}
}
}
}
51 changes: 51 additions & 0 deletions src/VimeoDotNet/GetVideoByTagSort.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using JetBrains.Annotations;

namespace VimeoDotNet
{
/// <summary>
/// Get video by tag sort type
/// </summary>
[PublicAPI]
public enum GetVideoByTagSort
{
/// <summary>
/// By created time
/// </summary>
CreatedTime,

/// <summary>
/// By duration
/// </summary>
Duration,

/// <summary>
/// By name
/// </summary>
Name
}

internal static class GetVideoByTagSortEx
{
/// <summary>
/// Return string representation for enum value
/// </summary>
/// <param name="sort">Value</param>
/// <returns>String representation</returns>
/// <exception cref="ArgumentOutOfRangeException">Throw if value not handled.</exception>
public static string GetStringValue(this GetVideoByTagSort sort)
{
switch (sort)
{
case GetVideoByTagSort.CreatedTime:
return "created_time";
case GetVideoByTagSort.Duration:
return "duration";
case GetVideoByTagSort.Name:
return "name";
default:
throw new ArgumentOutOfRangeException(nameof(sort), sort, null);
}
}
}
}
51 changes: 49 additions & 2 deletions src/VimeoDotNet/IVimeoClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public interface IVimeoClient
/// Get video by ClipId asynchronously
/// </summary>
/// <param name="clipId">ClipId</param>
/// <param name="fields"></param>
/// <param name="fields">JSON filter, as per https://developer.vimeo.com/api/common-formats#json-filter</param>
/// <returns>Video</returns>
Task<Video> GetVideoAsync(long clipId, string[] fields = null);

Expand All @@ -61,7 +61,7 @@ public interface IVimeoClient
/// <param name="userId">UserId</param>
/// <param name="perPage">Number of items to show on each page. Max 50</param>
/// <param name="query">Search query</param>
/// <param name="fields"></param>
/// <param name="fields">JSON filter, as per https://developer.vimeo.com/api/common-formats#json-filter</param>
/// <param name="page">The page number to show</param>
/// <returns>Paginated videos</returns>
Task<Paginated<Video>> GetVideosAsync(UserId userId, int? page = null, int? perPage = null,
Expand Down Expand Up @@ -307,5 +307,52 @@ Task<IUploadRequest> UploadEntireFileAsync(IBinaryContent fileContent,
DateTime RateLimitReset { get; }

#endregion

#region Tags

/// <summary>
/// Add a tag to a video
/// </summary>
/// <param name="clipId">Clip Id</param>
/// <param name="tag">Tag</param>
/// <returns>Tag</returns>
Task<Tag> AddVideoTagAsync(long clipId, string tag);

/// <summary>
/// Delete a tag from a video
/// </summary>
Task DeleteVideoTagAsync(long clipId, string tag);

/// <summary>
/// List a videos' tags
/// </summary>
/// <param name="clipId">Clip Id</param>
/// <param name="page">Page number</param>
/// <param name="perPage">Tags per page</param>
/// <returns></returns>
/// <returns>Paginated tags</returns>
Task<Paginated<Tag>> GetVideoTags(long clipId, int? page = null, int? perPage = null);

/// <summary>
/// Get a tag
/// </summary>
/// <param name="tag">Tag word</param>
/// <returns>Tag</returns>
Task<Tag> GetVideoTagAsync(string tag);

/// <summary>
/// Get all videos tagged with a specific word
/// </summary>
/// <param name="tag">Tag id</param>
/// <param name="page">Page number</param>
/// <param name="perPage">Video per page</param>
/// <param name="sort">Technique used to sort the results.</param>
/// <param name="direction">The direction that the results are sorted.</param>
/// <param name="fields">JSON filter, as per https://developer.vimeo.com/api/common-formats#json-filter</param>
/// <returns>Paginated videos</returns>
Task<Paginated<Video>> GetVideoByTag(string tag, int? page = null,
int? perPage = null, GetVideoByTagSort? sort = null, GetVideoByTagDirection? direction = null, string[] fields = null);

#endregion
}
}
22 changes: 22 additions & 0 deletions src/VimeoDotNet/Models/Tag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,21 @@ namespace VimeoDotNet.Models
/// </summary>
public class Tag
{
/// <summary>
/// Id
/// </summary>
[PublicAPI]
[JsonProperty(PropertyName = "tag")]
public string Id { get; set; }


/// <summary>
/// URI
/// </summary>
[PublicAPI]
[JsonProperty(PropertyName = "uri")]
public string Uri { get; set; }

/// <summary>
/// Name
/// </summary>
Expand All @@ -21,5 +36,12 @@ public class Tag
[PublicAPI]
[JsonProperty(PropertyName = "canonical")]
public string Canonical { get; set; }

/// <summary>
/// Metadata
/// </summary>
[PublicAPI]
[JsonProperty(PropertyName = "metadata")]
public TagMetadata Metadata { get; set; }
}
}
15 changes: 15 additions & 0 deletions src/VimeoDotNet/Models/TagMetadata.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using JetBrains.Annotations;
using Newtonsoft.Json;

namespace VimeoDotNet.Models
{
public class TagMetadata
{
/// <summary>
/// Tag connections
/// </summary>
[PublicAPI]
[JsonProperty(PropertyName = "connections")]
public TagMetadataConnections Connections { get; set; }
}
}
15 changes: 15 additions & 0 deletions src/VimeoDotNet/Models/TagMetadataConnections.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using JetBrains.Annotations;
using Newtonsoft.Json;

namespace VimeoDotNet.Models
{
public class TagMetadataConnections
{
/// <summary>
/// Video connections
/// </summary>
[PublicAPI]
[JsonProperty(PropertyName = "videos")]
public VideoConnectionsEntry Videos { get; set; }
}
}
Loading

0 comments on commit e9741bf

Please sign in to comment.