Skip to content

Commit

Permalink
Support incremental organization export (Speedygeek#356)
Browse files Browse the repository at this point in the history
* Support incremental org exports

* Add tests for incremental organization export
  • Loading branch information
bcemmett authored and mozts2005 committed Jul 23, 2018
1 parent daf5ee1 commit b751efd
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace ZendeskApi_v2.Models.Organizations
{
public class GroupOrganizationExportResponse
{
[JsonProperty("count")]
public long Count { get; set; }

[JsonProperty("end_time")]
[JsonConverter(typeof(UnixDateTimeConverter))]
public DateTimeOffset EndTime { get; set; }

[JsonProperty("next_page")]
public string NextPage { get; set; }

[JsonProperty("organizations")]
public IList<Organization> Organizations { get; set; }
}
}
32 changes: 32 additions & 0 deletions src/ZendeskApi_v2/Requests/Organizations.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#if ASYNC
using System.Threading.Tasks;
#endif
using System;
using System.Collections.Generic;
using System.Linq;
using ZendeskApi_v2.Extensions;
Expand Down Expand Up @@ -40,6 +41,9 @@ public interface IOrganizations : ICore
bool DeleteOrganizationMembership(long id, long userId);
JobStatusResponse DestroyManyOrganizationMemberships(IEnumerable<long> ids);
GroupOrganizationMembershipResponse SetOrganizationMembershipAsDefault(long id, long userId);

GroupOrganizationExportResponse GetIncrementalOrganizationExport(DateTimeOffset startTime);
GroupOrganizationExportResponse GetIncrementalOrganizationExportNextPage(string nextPage);
#endif

#if ASYNC
Expand Down Expand Up @@ -72,11 +76,16 @@ public interface IOrganizations : ICore
Task<bool> DeleteOrganizationMembershipAsync(long id, long userId);
Task<JobStatusResponse> DestroyManyOrganizationMembershipsAsync(IEnumerable<long> ids);
Task<GroupOrganizationMembershipResponse> SetOrganizationMembershipAsDefaultAsync(long id, long userId);

Task<GroupOrganizationExportResponse> GetIncrementalOrganizationExportAsync(DateTimeOffset startTime);
Task<GroupOrganizationExportResponse> GetIncrementalOrganizationExportNextPageAsync(string nextPage);
#endif
}

public class Organizations : Core, IOrganizations
{
private const string _incremental_export = "incremental/organizations.json?start_time=";

public Organizations(string yourZendeskUrl, string user, string password, string apiToken, string p_OAuthToken)
: base(yourZendeskUrl, user, password, apiToken, p_OAuthToken)
{
Expand Down Expand Up @@ -192,6 +201,17 @@ public GroupOrganizationMembershipResponse SetOrganizationMembershipAsDefault(lo
return GenericPut<GroupOrganizationMembershipResponse>($"users/{userId}/organization_memberships/{id}/make_default.json");
}

public GroupOrganizationExportResponse GetIncrementalOrganizationExport(DateTimeOffset startTime)
{
var resource = _incremental_export + startTime.UtcDateTime.GetEpoch();
return GenericGet<GroupOrganizationExportResponse>(resource);
}

public GroupOrganizationExportResponse GetIncrementalOrganizationExportNextPage(string nextPage)
{
var resource = nextPage.Replace(ZendeskUrl, string.Empty);
return GenericGet<GroupOrganizationExportResponse>(resource);
}
#endif

#if ASYNC
Expand Down Expand Up @@ -303,6 +323,18 @@ public async Task<GroupOrganizationMembershipResponse> SetOrganizationMembership
{
return await GenericPutAsync<GroupOrganizationMembershipResponse>($"users/{userId}/organization_memberships/{id}/make_default.json");
}

public async Task<GroupOrganizationExportResponse> GetIncrementalOrganizationExportAsync(DateTimeOffset startTime)
{
var resource = _incremental_export + startTime.UtcDateTime.GetEpoch();
return await GenericGetAsync<GroupOrganizationExportResponse>(resource);
}

public async Task<GroupOrganizationExportResponse> GetIncrementalOrganizationExportNextPageAsync(string nextPage)
{
var resource = nextPage.Replace(ZendeskUrl, string.Empty);
return await GenericGetAsync<GroupOrganizationExportResponse>(resource);
}
#endif
}
}
21 changes: 21 additions & 0 deletions test/ZendeskApi_v2.Test/OrganizationTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
Expand Down Expand Up @@ -199,5 +200,25 @@ public async Task CanSearchForOrganizationsAsync()
var search = await api.Organizations.SearchForOrganizationsAsync(Settings.DefaultExternalId);
Assert.Greater(search.Count, 0);
}

[Test]
public void CanGetIncrementalOrganizationExport()
{
var incrementalOrganizationExport = api.Organizations.GetIncrementalOrganizationExport(DateTimeOffset.MinValue);
Assert.That(incrementalOrganizationExport.Organizations.Count, Is.GreaterThan(0));

var incrementalOrganizationExportNextPage = api.Organizations.GetIncrementalOrganizationExportNextPage(incrementalOrganizationExport.NextPage);
Assert.That(incrementalOrganizationExportNextPage.Organizations.Count, Is.GreaterThan(0));
}

[Test]
public async Task CanGetIncrementalOrganizationExportAsync()
{
var incrementalOrganizationExport = await api.Organizations.GetIncrementalOrganizationExportAsync(DateTimeOffset.MinValue);
Assert.That(incrementalOrganizationExport.Organizations.Count, Is.GreaterThan(0));

var incrementalOrganizationExportNextPage = await api.Organizations.GetIncrementalOrganizationExportNextPageAsync(incrementalOrganizationExport.NextPage);
Assert.That(incrementalOrganizationExportNextPage.Organizations.Count, Is.GreaterThan(0));
}
}
}

0 comments on commit b751efd

Please sign in to comment.