Skip to content

Commit

Permalink
WIP - Implement search for Content Tags
Browse files Browse the repository at this point in the history
  • Loading branch information
andy-may-at committed Mar 15, 2023
1 parent 977bb06 commit a60e12f
Showing 1 changed file with 63 additions and 1 deletion.
64 changes: 63 additions & 1 deletion src/main/java/org/zendesk/client/v2/Zendesk.java
Original file line number Diff line number Diff line change
Expand Up @@ -2532,7 +2532,7 @@ public ContentTag getContentTag(String contentTagId) {

public ContentTag createContentTag(ContentTag contentTag) {
checkHasName(contentTag);
return complete(submit(req("POST", tmpl("/guide/content_tags"),
return complete(submit(req("POST", cnst("/guide/content_tags"),
JSON, json(Collections.singletonMap("content_tag", contentTag))),
handle(ContentTag.class, "content_tag")));
}
Expand All @@ -2551,6 +2551,33 @@ public void deleteContentTag(ContentTag contentTag) {
handleStatus()));
}

public Iterable<ContentTag> getContentTags() {
int defaultPageSize = 10;
return getContentTags(defaultPageSize);
}

public Iterable<ContentTag> getContentTags(int pageSize) {
TemplateUri templateUri = tmpl("/guide/content_tags/{id}" +
"?page[size]={pageSize}]" +
"&page[after]={afterCursor}")
.set("pageSize", pageSize);

return complete(submit(req("GET", templateUri),
handleListWithAfterCursorButNoLinks(ContentTag.class, templateUri,"records")));
}

public Iterable<ContentTag> getContentTags(int pageSize, String namePrefix) {
TemplateUri templateUri = tmpl("/guide/content_tags/{id}" +
"?page[size]={pageSize}]" +
"&page[after]={afterCursor}" +
"&filter[name_prefix]={namePrefix}")
.set("pageSize", pageSize)
.set("namePrefix", namePrefix);

return complete(submit(req("GET", templateUri),
handleListWithAfterCursorButNoLinks(ContentTag.class, templateUri,"records")));
}

//////////////////////////////////////////////////////////////////////
// Helper methods
//////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -2704,6 +2731,7 @@ public JobStatus onCompleted(Response response) throws Exception {
private static final String COUNT = "count";
private static final int INCREMENTAL_EXPORT_MAX_COUNT_BY_REQUEST = 1000;


private abstract class PagedAsyncCompletionHandler<T> extends ZendeskAsyncCompletionHandler<T> {
private String nextPage;

Expand Down Expand Up @@ -2888,6 +2916,40 @@ public List<ArticleAttachments> onCompleted(Response response) throws Exception
};
}

/** For a resource (e.g. ContentTag) which supports cursor based pagination for multiple results,
* but where the response does not have a `links.next` node (which would hold the URL of the next page)
* So we need to build the next page URL from the meta.after_cursor node value
* @param <T> The class of the resource
* @param templateUri a template which has an 'afterCursor' variable in it, which can be used to build the next page link
* @param name the name of the Json node that contains the resources entities (e.g. 'records' for ContentTag)
*/
private <T> PagedAsyncCompletionHandler<List<T>> handleListWithAfterCursorButNoLinks(
Class<T> clazz, TemplateUri templateUri, String name) {

return new PagedAsyncListCompletionHandler<T>(clazz, name) {
@Override
public void setPagedProperties(JsonNode responseNode, Class<?> clazz) {
JsonNode metaNode = responseNode.get("meta");
String nextPage = null;
if (metaNode == null) {
if (logger.isDebugEnabled()) {
logger.debug("meta" + " property not found, pagination not supported" +
(clazz != null ? " for " + clazz.getName() : ""));
}
} else {
JsonNode afterCursorNode = metaNode.get("after_cursor");
if (afterCursorNode != null) {
JsonNode hasMoreNode = metaNode.get("has_more");
if (hasMoreNode != null && hasMoreNode.asBoolean()) {
nextPage = templateUri.set("afterCursor", afterCursorNode.asText()).toString();
}
}
}
setNextPage(nextPage);
}
};
}

private TemplateUri tmpl(String template) {
return new TemplateUri(url + template);
}
Expand Down

0 comments on commit a60e12f

Please sign in to comment.