Skip to content
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
178 changes: 176 additions & 2 deletions src/main/java/org/zendesk/client/v2/Zendesk.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,13 @@
import org.zendesk.client.v2.model.Organization;
import org.zendesk.client.v2.model.OrganizationField;
import org.zendesk.client.v2.model.OrganizationMembership;
import org.zendesk.client.v2.model.Page;
import org.zendesk.client.v2.model.SatisfactionRating;
import org.zendesk.client.v2.model.SearchResultEntity;
import org.zendesk.client.v2.model.SortOrder;
import org.zendesk.client.v2.model.Sorting;
import org.zendesk.client.v2.model.Status;
import org.zendesk.client.v2.model.SupportCenterLocale;
import org.zendesk.client.v2.model.SuspendedTicket;
import org.zendesk.client.v2.model.Ticket;
import org.zendesk.client.v2.model.TicketForm;
Expand All @@ -63,6 +66,8 @@
import org.zendesk.client.v2.model.hc.Translation;
import org.zendesk.client.v2.model.hc.PermissionGroup;
import org.zendesk.client.v2.model.hc.UserSegment;
import org.zendesk.client.v2.model.oauth.OAuthRequest;
import org.zendesk.client.v2.model.oauth.OAuthToken;
import org.zendesk.client.v2.model.schedules.Holiday;
import org.zendesk.client.v2.model.schedules.Schedule;
import org.zendesk.client.v2.model.targets.BasecampTarget;
Expand All @@ -86,6 +91,7 @@
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Objects;
Expand Down Expand Up @@ -118,7 +124,7 @@ private static Map<String, Class<? extends SearchResultEntity>> searchResultType
result.put("group", Group.class);
result.put("organization", Organization.class);
result.put("topic", Topic.class);
result.put("article", Article.class);
result.put("article", Article.class);
return Collections.unmodifiableMap(result);
}

Expand Down Expand Up @@ -202,6 +208,13 @@ public void close() {
// Action methods
//////////////////////////////////////////////////////////////////////

public String getOAuthToken(String subdomain, String code, String redirectUri, String clientId, String clientSecret) {
return complete(submit(reqUnauthorized("POST", new TemplateUri("https://{subdomain}.zendesk.com/oauth/tokens").set("subdomain", subdomain),
JSON, json(new OAuthRequest(code, redirectUri, clientId, clientSecret))
),
handle(OAuthToken.class))).getAccessToken();
}

public <T> JobStatus<T> getJobStatus(JobStatus<T> status) {
return complete(getJobStatusAsync(status));
}
Expand Down Expand Up @@ -411,6 +424,31 @@ public Iterable<Article> getArticleFromSearch(String searchTerm, Long sectionId)
.set("query", searchTerm).set("section", sectionId), handleList(Article.class, "results"));
}

public Iterable<Article> getArticlesByQueryAndSection(String locale, String searchTerm, Section section, Category category, Page page) {

TemplateUri tmpl = tmpl("/help_center/articles/search.json{?locale, query, category, section, page, per_page")
.set("locale", locale)
.set("query", searchTerm);

if(category != null)
{
tmpl.set("category", category.getId());
}

if(section != null)
{
tmpl.set("section", section.getId());
}

if(page != null)
{
tmpl.set("page", page.getPageNo());
tmpl.set("per_page", page.getPerPage());
}

return new PagedIterable<>(tmpl, handleList(Article.class, "results"));
}

public Iterable<Article> getArticlesFromAnyLabels(List<String> labels) {
return new PagedIterable<>(tmpl("/help_center/articles/search.json{?label_names}").set("label_names", labels),
handleList(Article.class, "results"));
Expand All @@ -426,6 +464,13 @@ public List<ArticleAttachments> getAttachmentsFromArticle(Long articleID) {
handleArticleAttachmentsList("article_attachments")));
}

public List<ArticleAttachments> getAttachmentsFromArticle(String locale, Long articleId) {
return complete(submit(req("GET", tmpl("/help_center/{locale}/articles/{articleId}/attachments.json")
.set("locale", locale)
.set("articleId", articleId)),
handleArticleAttachmentsList("article_attachments")));
}

public List<Ticket> getTickets(long id, long... ids) {
return complete(submit(req("GET", tmpl("/tickets/show_many.json{?ids}").set("ids", idArray(id, ids))),
handleList(Ticket.class, "tickets")));
Expand Down Expand Up @@ -1678,6 +1723,15 @@ public Iterable<DynamicContentItem> getDynamicContentItems() {
return new PagedIterable<>(cnst("/dynamic_content/items.json"), handleList(DynamicContentItem.class, "items"));
}

public Iterable<DynamicContentItem> getDynamicContentItems(Page page, Sorting sorting) {
return new PagedIterable<>(tmpl("/dynamic_content/items.json?page={page}&per_page={per_page}&sort_by={sort_by}&sort_order={sort_order}")
.set("page", page.getPageNo())
.set("per_page", page.getPerPage())
.set("sort_by", sorting.getSortBy())
.set("sort_order", sorting.getSortOrder().getQueryParameter()),
handleList(DynamicContentItem.class, "items"));
}

public DynamicContentItem getDynamicContentItem(long id) {
return complete(submit(req("GET", tmpl("/dynamic_content/items/{id}.json").set("id", id)), handle(DynamicContentItem.class, "item")));
}
Expand All @@ -1703,8 +1757,13 @@ public void deleteDynamicContentItem(DynamicContentItem item) {

public Iterable<DynamicContentItemVariant> getDynamicContentItemVariants(DynamicContentItem item) {
checkHasId(item);
return getDynamicContentItemVariants(item.getId());
}

public Iterable<DynamicContentItemVariant> getDynamicContentItemVariants(Long itemId) {
checkHasItemId(itemId);
return new PagedIterable<>(
tmpl("/dynamic_content/items/{id}/variants.json").set("id", item.getId()),
tmpl("/dynamic_content/items/{id}/variants.json").set("id", itemId),
handleList(DynamicContentItemVariant.class, "variants"));
}

Expand Down Expand Up @@ -1734,6 +1793,25 @@ public void deleteDynamicContentItemVariant(Long itemId, DynamicContentItemVaria

// TODO search with query building API

//////////////////////////////////////////////////////////////////////
// Support Center locales
//////////////////////////////////////////////////////////////////////
/**
* Get enabled locales for Support Center
* @return
*/
public List<SupportCenterLocale> getEnabledSupportCenterLocales() {
return (List)complete(this.submit(this.req("GET", this.cnst("/locales.json")), this.handleList(Locale.class, "locales")));
}

/**
* Get all available locales for Support Center
* @return
*/
public List<SupportCenterLocale> getAvailableSupportCenterLocales() {
return (List)complete(this.submit(this.req("GET", this.cnst("/locales/public.json")), this.handleList(Locale.class, "locales")));
}

//////////////////////////////////////////////////////////////////////
// Action methods for Help Center
//////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -1941,17 +2019,44 @@ public List<Article> getArticlesFromPage(int page) {
handleList(Article.class, "articles")));
}

public Iterable<Article> getArticles(String locale, Page page, Sorting sorting) {
return complete(submit(
req("GET",
tmpl("/help_center/{locale}/articles.json{?page, per_page, sort_by, sort_order}")
.set("locale", locale)
.set("page", page.getPageNo())
.set("per_page", page.getPerPage())
.set("sort_by", sorting.getSortBy())
.set("sort_order", sorting.getSortOrder())
),
handleList(Article.class, "articles")));
}

public Article getArticle(long id) {
return complete(submit(req("GET", tmpl("/help_center/articles/{id}.json").set("id", id)),
handle(Article.class, "article")));
}

public Article getArticle(String locale, long id) {
return complete(submit(req("GET", tmpl("/help_center/{locale}/articles/{id}.json")
.set("id", id).set("locale", locale)),
handle(Article.class, "article")));
}

public Iterable<Translation> getArticleTranslations(Long articleId) {
return new PagedIterable<>(
tmpl("/help_center/articles/{articleId}/translations.json").set("articleId", articleId),
handleList(Translation.class, "translations"));
}

public Translation getArticleTranslation(Long articleId, String locale) {
return complete(submit(req("GET", tmpl("/help_center/articles/{articleId}/translations/{locale}.json")
.set("articleId", articleId)
.set("locale", locale)),
handle(Translation.class, "translation")
));
}

public Article createArticle(Article article) {
checkHasSectionId(article);
return complete(submit(req("POST", tmpl("/help_center/sections/{id}/articles.json").set("id", article.getSectionId()),
Expand Down Expand Up @@ -2033,16 +2138,42 @@ public Iterable<Category> getCategories() {
handleList(Category.class, "categories"));
}

public Iterable<Category> getCategories(String locale, Page page, Sorting sorting) {
return new PagedIterable<>(
tmpl("/help_center/{locale}/categories.json?page={page}&per_page={per_page}&sort_by={sort_by}&sort_order={sort_order}")
.set("locale", locale)
.set("page", page.getPageNo())
.set("per_page", page.getPerPage())
.set("sort_by", sorting.getSortBy())
.set("sort_order", sorting.getSortOrder().getQueryParameter()),
handleList(Category.class, "categories"));
}

public Category getCategory(long id) {
return complete(submit(req("GET", tmpl("/help_center/categories/{id}.json").set("id", id)),
handle(Category.class, "category")));
}

public Category getCategory(String locale, long id) {
return complete(submit(req("GET", tmpl("/help_center/{locale}/categories/{id}.json")
.set("id", id).set("locale", locale)),
handle(Category.class, "category")));
}

public Iterable<Translation> getCategoryTranslations(Long categoryId) {
return new PagedIterable<>(
tmpl("/help_center/categories/{categoryId}/translations.json").set("categoryId", categoryId),
handleList(Translation.class, "translations"));
}

public Translation getCategoryTranslation(Long categoryId, String locale) {
return complete(submit(req("GET", tmpl("/help_center/categories/{categoryId}/translations/{locale}.json")
.set("categoryId", categoryId)
.set("locale", locale)),
handle(Translation.class, "translation")
));
}

public Category createCategory(Category category) {
return complete(submit(req("POST", cnst("/help_center/categories.json"),
JSON, json(Collections.singletonMap("category", category))), handle(Category.class, "category")));
Expand Down Expand Up @@ -2084,16 +2215,51 @@ public Iterable<Section> getSections(Category category) {
handleList(Section.class, "sections"));
}

public Iterable<Section> getSections(String locale, Page page, Sorting sorting) {
return new PagedIterable<>(
tmpl("/help_center/{locale}/sections.json?page={page}&per_page={per_page}&sort_by={sort_by}&sort_order={sort_order}")
.set("locale", locale)
.set("page", page.getPageNo())
.set("per_page", page.getPerPage())
.set("sort_by", sorting.getSortBy())
.set("sort_order", sorting.getSortOrder().getQueryParameter()),
handleList(Section.class, "sections"));
}

public Iterable<Section> getSections(String locale, Category category, Page page, Sorting sorting) {
checkHasId(category);
return new PagedIterable<>(
tmpl("/help_center/{locale}/categories/{categoryId}/sections.json?page={page}&per_page={per_page}&sort_by={sort_by}&sort_order={sort_order}")
.set("locale", locale)
.set("categoryId", category.getId())
.set("page", page.getPageNo())
.set("per_page", page.getPerPage())
.set("sort_by", sorting.getSortBy())
.set("sort_order", sorting.getSortOrder().getQueryParameter()),
handleList(Section.class, "sections"));
}

public Section getSection(long id) {
return complete(submit(req("GET", tmpl("/help_center/sections/{id}.json").set("id", id)),
handle(Section.class, "section")));
}

public Section getSection(String locale, long id) {
return complete(submit(req("GET", tmpl("/help_center/{locale}/sections/{sectionId}.json").set("id", id).set("locale", locale)),
handle(Section.class, "section")));
}

public Iterable<Translation> getSectionTranslations(Long sectionId) {
return new PagedIterable<>(
tmpl("/help_center/sections/{sectionId}/translations.json").set("sectionId", sectionId),
handleList(Translation.class, "translations"));
}

public Translation getSectionTranslation(String locale, long sectionId) {
return complete(submit(req("GET", tmpl("/help_center/sections/{sectionId}/translations/{locale}.json").set("sectionId", sectionId).set("locale", locale)),
handle(Translation.class, "translation")));
}

public Section createSection(Section section) {
checkHasCategoryId(section);
return complete(submit(req("POST", tmpl("/help_center/categories/{id}/sections.json").set("id", section.getCategoryId()),
Expand Down Expand Up @@ -2252,6 +2418,14 @@ private RequestBuilder reqBuilder(String method, String url) {
return builder.setUrl(RESTRICTED_PATTERN.matcher(url).replaceAll("+")); // replace out %2B with + due to API restriction
}

private Request reqUnauthorized(String method, Uri template, String contentType, byte[] body) {
RequestBuilder builder = new RequestBuilder(method);
builder.setUrl(template.toString());
builder.addHeader("Content-type", contentType);
builder.setBody(body);
return builder.build();
}

protected ZendeskAsyncCompletionHandler<Void> handleStatus() {
return new ZendeskAsyncCompletionHandler<Void>() {
@Override
Expand Down
66 changes: 66 additions & 0 deletions src/main/java/org/zendesk/client/v2/model/Page.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package org.zendesk.client.v2.model;

import java.util.Objects;

public class Page
{
int pageNo;
int perPage;

public Page()
{
}

public Page(int pageNo, int perPage)
{
this.pageNo = pageNo;
this.perPage = perPage;
}

public int getPageNo()
{
return pageNo;
}

public void setPageNo(int pageNo)
{
this.pageNo = pageNo;
}

public int getPerPage()
{
return perPage;
}

public void setPerPage(int perPage)
{
this.perPage = perPage;
}

@Override
public String toString()
{
return "Page{" +
"page=" + pageNo +
", perPage=" + perPage +
'}';
}

@Override
public boolean equals(Object o)
{
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Page page1 = (Page) o;
return pageNo == page1.pageNo &&
perPage == page1.perPage;
}

@Override
public int hashCode()
{
return Objects.hash(pageNo, perPage);
}
}
Loading