Skip to content

Commit

Permalink
Merge pull request #316 from zendesk/fvilela/add_tags_cbp
Browse files Browse the repository at this point in the history
[RED-1965] - Add support to a new resource - /api/v2/tags
  • Loading branch information
mikerogers123 authored Nov 2, 2023
2 parents 6bb6c88 + 54eeb90 commit e92590e
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/ZendeskApi.Build/ZendeskApi.Commons.props
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<PropertyGroup>
<Major>7</Major>
<Minor>0</Minor>
<Revision>1</Revision>
<Revision>4</Revision>
<PackageVersion>$(Major).$(Minor).$(Revision)</PackageVersion>
</PropertyGroup>
<ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions src/ZendeskApi.Client/IZendeskClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ public interface IZendeskClient
IServiceStatusResource ServiceStatus { get; }
IHelpCenterResource HelpCenter { get; }
ILocaleResource Locales { get; }
ITagsResource Tags { get; }
}
}
12 changes: 12 additions & 0 deletions src/ZendeskApi.Client/Models/Tag.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Newtonsoft.Json;
namespace ZendeskApi.Client.Models
{
public class Tag
{
[JsonProperty("name")]
public string Name { get; set; }

[JsonProperty("count")]
public int Count { get; set; }
}
}
21 changes: 21 additions & 0 deletions src/ZendeskApi.Client/Resources/Interfaces/ITagsResource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Threading;
using System.Threading.Tasks;
using ZendeskApi.Client.Models;
using ZendeskApi.Client.Responses;

namespace ZendeskApi.Client.Resources.Interfaces
{
public interface ITagsResource
{
#region List
/// <summary>
/// Lists all tags. This request is paginated.
/// </summary>
Task<ICursorPagination<Tag>> GetAllAsync(
CursorPager cursor,
CancellationToken cancellationToken = default(CancellationToken));

#endregion
}
}

28 changes: 28 additions & 0 deletions src/ZendeskApi.Client/Resources/TagsResource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using ZendeskApi.Client.Models;
using ZendeskApi.Client.Resources.Interfaces;
using ZendeskApi.Client.Responses;

namespace ZendeskApi.Client.Resources
{
public class TagsResource : AbstractBaseResource<TagsResource>, ITagsResource
{
private static string ResourceUri = "/api/v2/tags";
public TagsResource(IZendeskApiClient apiClient, ILogger logger) : base(apiClient, logger, "tags")
{
}

public async Task<ICursorPagination<Tag>> GetAllAsync(CursorPager cursor, CancellationToken cancellationToken = default)
{
return await GetAsync<TagResponse>(
ResourceUri,
"list-tags",
"ListAsync",
cursor,
cancellationToken: cancellationToken);
}
}
}

15 changes: 15 additions & 0 deletions src/ZendeskApi.Client/Responses/Tag/TagResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Collections.Generic;
using Newtonsoft.Json;
using ZendeskApi.Client.Models;

namespace ZendeskApi.Client.Responses
{
[JsonObject]
public class TagResponse : CursorPaginationResponse<Tag>
{
[JsonProperty("tags")]
public IEnumerable<Tag> Tags { get; set; }

protected override IEnumerable<Tag> Enumerable => Tags;
}
}
3 changes: 3 additions & 0 deletions src/ZendeskApi.Client/ZendeskClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,8 @@ public ZendeskClient(IZendeskApiClient apiClient, ILogger logger = null)
private Lazy<ILocaleResource> LocalesLazy =>
new Lazy<ILocaleResource>(() => new LocaleResource(_apiClient, _logger));
public ILocaleResource Locales => LocalesLazy.Value;

private Lazy<ITagsResource> TagsLazy => new Lazy<ITagsResource>(() => new TagsResource(_apiClient, _logger));
public ITagsResource Tags => TagsLazy.Value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System.Linq;
using System.Threading.Tasks;
using Xunit;
using ZendeskApi.Client.IntegrationTests.Factories;
using ZendeskApi.Client.Models;

namespace ZendeskApi.Client.IntegrationTests.Resources
{
public class TagsResourceTest : IClassFixture<ZendeskClientFactory>
{
private readonly ZendeskClientFactory _clientFactory;

public TagsResourceTest(
ZendeskClientFactory clientFactory)
{
_clientFactory = clientFactory;
}


[Fact]
public async Task GetAllAsync_WhenCalledWithCursorPagination_ShouldBePaginatable()
{
var client = _clientFactory.GetClient();

var cursorPager = new CursorPager { Size = 5 };
var tagsPageOne = await client
.Tags.GetAllAsync(cursorPager);

Assert.NotNull(tagsPageOne);
Assert.Equal(5, tagsPageOne.Count());
Assert.True(tagsPageOne.Meta.HasMore);

cursorPager.AfterCursor = tagsPageOne.Meta.AfterCursor;

var tagsPageTwo = await client.Tags.GetAllAsync(cursorPager);
Assert.NotNull(tagsPageTwo);
Assert.Equal(5, tagsPageTwo.Count());

var tagIdsPageOne = tagsPageOne.Select(tag => tag.Name).ToList();
var tagIdsPageTwo = tagsPageTwo.Select(tag => tag.Name).ToList();
Assert.NotEqual(tagIdsPageOne, tagIdsPageTwo);
}
}
}

0 comments on commit e92590e

Please sign in to comment.