-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #327 from justeat/develop
develop -> master ([RED-2026])
- Loading branch information
Showing
24 changed files
with
450 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,7 @@ A .netstandard NuGet package for use with the Zendesk v2 API. | |
|
||
#### The deprecation and replacement of Status API endpoints | ||
|
||
More detailed information on the exact changes and motivation can be found [here](https://support.zendesk.com/hc/en-us/articles/5414949730842). | ||
More detailed information on the exact changes and motivation can be found [here](https://support.zendesk.com/hc/en-us/articles/5414949730842). | ||
|
||
For the sake of this library, it means the following methods have been removed: | ||
|
||
|
@@ -50,6 +50,7 @@ Groups: | |
Help Center: | ||
- GET api/v2/help_center/articles | ||
- GET api/v2/help_center/categories | ||
- GET api/v2/help_center/sections | ||
|
||
Organization: | ||
- GET /api/v2/organization_fields | ||
|
@@ -63,11 +64,19 @@ Tickets: | |
- GET /api/v2/tickets | ||
- GET /api/v2/tickets/{ticketId}/comments | ||
- GET /api/v2/ticket_fields | ||
- GET /api/v2/ticket_audits - [Cursor Variant](https://developer.zendesk.com/api-reference/ticketing/tickets/ticket_audits/#pagination) | ||
- GET /api/v2/ticket_audits | ||
- GET | ||
|
||
Satisfaction ratings: | ||
- GET /api/v2/satisfaction_ratings | ||
|
||
Requests | ||
- GET /api/v2/requests | ||
- GET /api/v2/requests/{requestId}/comments | ||
|
||
Job Statuses | ||
- GET /api/v2/job_statuses | ||
|
||
[Further reading on Zendesk Pagination changes](https://support.zendesk.com/hc/en-us/articles/4402610093338-Introducing-Pagination-Changes-Zendesk-API) | ||
|
||
## 3.x.x | ||
|
@@ -126,6 +135,43 @@ await client.Search.SearchAsync<Ticket>(q => | |
); | ||
``` | ||
|
||
## Using Cursor Based Pagination | ||
You can use the `CursorPaginatedIterator` to loop through multiple pages as shown below: | ||
```c# | ||
|
||
var services = new ServiceCollection(); | ||
services.AddZendeskClientWithHttpClientFactory("https://yoursubomain.zendesk.com", "[email protected]", "your_token_"); | ||
var serviceProvider = services.BuildServiceProvider(); | ||
var client = serviceProvider.GetRequiredService<IZendeskClient>(); | ||
|
||
var ticketCursorResponse = await client.Tickets.GetAllAsync(new CursorPager { Size = 5 }); // low page number to force pagination | ||
var iteratorFactory = serviceProvider.GetRequiredService<ICursorPaginatedIteratorFactory>(); | ||
// creates the iterator with the response object of the first request | ||
var iterator = iteratorFactory.Create<Ticket>(ticketCursorResponse); | ||
|
||
foreach (var ticket in iterator) | ||
{ | ||
Console.WriteLine("the id of this ticket is:" + ticket.Id); | ||
} // this loop will stop at the first page | ||
while (iterator.HasMore()) // to loop through all pages | ||
{ | ||
await iterator.NextPage(); | ||
foreach (var ticket in iterator) | ||
{ | ||
Console.WriteLine("the id of this ticket is:" + ticket.Id); | ||
} | ||
} | ||
|
||
// alternatively you can use .All() from the iterator | ||
await foreach (var ticket in iterator.All()) | ||
{ | ||
Console.WriteLine("the id of this ticket is:" + ticket.Id); | ||
} | ||
|
||
``` | ||
|
||
## The Zendesk API | ||
|
||
The zendesk api documentation is available at http://developer.zendesk.com/documentation/rest_api/introduction.html | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,4 +29,4 @@ public interface IZendeskClient | |
ILocaleResource Locales { get; } | ||
ITagsResource Tags { get; } | ||
} | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
src/ZendeskApi.Client/Pagination/CursorPaginatedIterator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using Newtonsoft.Json; | ||
using ZendeskApi.Client.Responses; | ||
|
||
namespace ZendeskApi.Client.Pagination; | ||
public class CursorPaginatedIterator<T> : IEnumerable<T> | ||
{ | ||
|
||
public ICursorPagination<T> Response { get; set; } | ||
|
||
private IZendeskApiClient client; | ||
|
||
|
||
private string ResponseType { get; } | ||
|
||
public CursorPaginatedIterator(ICursorPagination<T> response, IZendeskApiClient client) | ||
{ | ||
Response = response; | ||
this.client = client; | ||
ResponseType = response.GetType().FullName; | ||
} | ||
|
||
public bool HasMore() => Response.Meta.HasMore; | ||
|
||
public IEnumerator<T> GetEnumerator() | ||
{ | ||
return Response.GetEnumerator(); | ||
} | ||
|
||
IEnumerator IEnumerable.GetEnumerator() | ||
{ | ||
return Response.GetEnumerator(); | ||
} | ||
|
||
|
||
public async Task NextPage() | ||
{ | ||
await ExecuteRequest(Response.Links.Next); | ||
} | ||
|
||
public async Task PrevPage() | ||
{ | ||
await ExecuteRequest(Response.Links.Prev); | ||
} | ||
|
||
public async IAsyncEnumerable<T> All() | ||
{ | ||
foreach (var item in Response) | ||
{ | ||
yield return item; | ||
} | ||
while (HasMore()) | ||
{ | ||
await NextPage(); | ||
foreach (var item in Response) | ||
{ | ||
yield return item; | ||
} | ||
} | ||
yield break; | ||
} | ||
|
||
private async Task ExecuteRequest(string requestUrl) | ||
{ | ||
var httpResponseMessage = await client.CreateClient().GetAsync(requestUrl); | ||
var responseBody = await httpResponseMessage.Content.ReadAsStringAsync(); | ||
Response = (ICursorPagination<T>)JsonConvert.DeserializeObject(responseBody, Type.GetType(ResponseType)); | ||
} | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
src/ZendeskApi.Client/Pagination/CursorPaginatedIteratorFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using ZendeskApi.Client.Responses; | ||
|
||
namespace ZendeskApi.Client.Pagination | ||
{ | ||
public interface ICursorPaginatedIteratorFactory | ||
{ | ||
CursorPaginatedIterator<T> Create<T>(ICursorPagination<T> response); | ||
} | ||
|
||
public class CursorPaginatedIteratorFactory : ICursorPaginatedIteratorFactory | ||
{ | ||
private readonly IZendeskApiClient zendeskApiClient; | ||
|
||
public CursorPaginatedIteratorFactory(IZendeskApiClient _zendeskApiClient) | ||
{ | ||
zendeskApiClient = _zendeskApiClient; | ||
} | ||
|
||
public CursorPaginatedIterator<T> Create<T>(ICursorPagination<T> response) | ||
{ | ||
return new CursorPaginatedIterator<T>(response, zendeskApiClient); | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using Newtonsoft.Json; | ||
|
15 changes: 15 additions & 0 deletions
15
src/ZendeskApi.Client/Responses/JobStatus/JobStatusListCursorResponse.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 JobStatusListCursorResponse : CursorPaginationResponse<JobStatusResponse> | ||
{ | ||
[JsonProperty("job_statuses")] | ||
public IEnumerable<JobStatusResponse> JobStatuses { get; set; } | ||
|
||
protected override IEnumerable<JobStatusResponse> Enumerable => JobStatuses; | ||
} | ||
} |
Oops, something went wrong.