Skip to content

Commit

Permalink
Merge pull request #88 from treasure-data/fix-chat-api-endpoint-format
Browse files Browse the repository at this point in the history
Update Chat API endpoint follow new pattern
  • Loading branch information
hieudion authored Oct 23, 2024
2 parents 1fb24b3 + e96fa05 commit dcdc0a5
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 10 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
## 0.4.5 - 2024-10-29
## 0.4.6 - 2024-10-24
* [enhancement] Update the API endpoint pattern for Chat endpoint [#88](https://github.com/treasure-data/embulk-input-zendesk/pull/88)

## 0.4.5 - 2024-10-24
* [enhancement] Remove checking for Chat API due to domain update [#87](https://github.com/treasure-data/embulk-input-zendesk/pull/87)

## 0.4.4 - 2023-07-21
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ repositories {
def embulkVersion = '0.10.31'

group = "com.treasuredata.embulk.plugins"
version = "0.4.5-SNAPSHOT"
version = "0.4.6-SNAPSHOT"
description = "Loads records From Zendesk"

sourceCompatibility = 1.8
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.IntStream;

import static org.embulk.input.zendesk.ZendeskInputPlugin.CONFIG_MAPPER_FACTORY;
Expand Down Expand Up @@ -164,9 +165,9 @@ private List<String> getListIDS(final String startTime, final String endTime, fi
private String buildSearchRequest(final String startTime, final String endTime, final int page)
{
return ZendeskUtils.getURIBuilder(task.getLoginUrl())
.setPath(ZendeskConstants.Url.API_CHAT_SEARCH)
.setParameter("q", buildSearchParam(startTime, endTime))
.setParameter("page", String.valueOf(page)).toString();
.setPath(resolveEndpointPatternByDomain(ZendeskConstants.Url.ENDPOINT_CHAT_SEARCH))
.setParameter("q", buildSearchParam(startTime, endTime))
.setParameter("page", String.valueOf(page)).toString();
}

private String buildSearchParam(final String startTime, final String endTime)
Expand All @@ -186,7 +187,7 @@ private String buildSearchParam(final String startTime, final String endTime)
private String buildSearchRequest(final List<String> ids)
{
return ZendeskUtils.getURIBuilder(task.getLoginUrl())
.setPath(ZendeskConstants.Url.API_CHAT)
.setPath(resolveEndpointPatternByDomain(ZendeskConstants.Url.ENDPOINT_CHAT))
.setParameter("ids", buildIdsParam(ids))
.toString();
}
Expand Down Expand Up @@ -274,4 +275,24 @@ private String getEndTime()
? task.getEndTime().get()
: OffsetDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ISO_INSTANT);
}

// Zopim domain is the domain for old Chat API
// But it will be removed soon and replaced by new Chat API follow new pattern {subdomain}.zendesk.com/api/v2/chat
// The expected released day is before October 29, 2024. So we need to handle this case
private String resolveEndpointPatternByDomain(String endpoint)
{
if (isZopimDomain()) {
return ZendeskConstants.Url.API + endpoint;
}
else {
return ZendeskConstants.Url.NEW_API_CHAT + endpoint;
}
}

private boolean isZopimDomain()
{
return Pattern.compile(ZendeskConstants.Regex.ZOPIM_LOGIN_URL)
.matcher(task.getLoginUrl())
.matches();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ public static class Url
public static final String API_OBJECT_RECORD = "api/sunshine/objects/records";
public static final String API_RELATIONSHIP_RECORD = "api/sunshine/relationships/records";
public static final String API_USER_EVENT = "api/v2/users/%s/events";
public static final String API_CHAT = API + "/chats";
public static final String API_CHAT_SEARCH = API_CHAT + "/search";
public static final String NEW_API_CHAT = API + "/chat";
public static final String ENDPOINT_CHAT = "/chats";
public static final String ENDPOINT_CHAT_SEARCH = "/chats/search";
}

public static class Misc
Expand All @@ -68,6 +69,8 @@ public static class Regex
{
public static final String ID = "_id$";
public static final String LOGIN_URL = "^https?://+[a-z0-9_\\\\-]+(.zendesk.com/?)$";
public static final String ZOPIM_LOGIN_URL = "^https://www.zopim.com/?$";

}

public static class HttpStatus
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,28 @@ public void testFetchData()
JsonNode dataSearchJson = ZendeskTestHelper.getJsonFromFile("data/chat_search.json");
JsonNode dataJson = ZendeskTestHelper.getJsonFromFile("data/chat.json");

when(zendeskRestClient.doGet(eq("https://www.chat.zendesk.com/api/v2/chats/search?q=timestamp%3A%5B2018-09-15T05%3A00%3A00Z+TO+2019-09-29T05%3A00%3A00Z%5D&page=1"), any(), anyBoolean())).thenReturn(dataSearchJson.toString());
when(zendeskRestClient.doGet(eq("https://www.chat.zendesk.com/api/v2/chat/chats/search?q=timestamp%3A%5B2018-09-15T05%3A00%3A00Z+TO+2019-09-29T05%3A00%3A00Z%5D&page=1"), any(), anyBoolean())).thenReturn(dataSearchJson.toString());

when(zendeskRestClient.doGet(eq("https://www.chat.zendesk.com/api/v2/chats?ids=id_1%2Cid_2"), any(), anyBoolean())).thenReturn(dataJson.toString());
when(zendeskRestClient.doGet(eq("https://www.chat.zendesk.com/api/v2/chat/chats?ids=id_1%2Cid_2"), any(), anyBoolean())).thenReturn(dataJson.toString());

zendeskChatService.fetchData("2018-09-15T05:00:00Z", "2019-09-29T05:00:00Z", 1, recordImporter);

verify(recordImporter, times(2)).addRecord(any());
}

@Test
public void testFetchDataWithZopimDomain()
{
ZendeskInputPlugin.PluginTask task =
CONFIG_MAPPER.map(ZendeskTestHelper.getConfigSource("chat.yml").set("login_url", "https://www.zopim.com"), ZendeskInputPlugin.PluginTask.class);
setupZendeskChatService(task);

JsonNode dataSearchJson = ZendeskTestHelper.getJsonFromFile("data/chat_search.json");
JsonNode dataJson = ZendeskTestHelper.getJsonFromFile("data/chat.json");

when(zendeskRestClient.doGet(eq("https://www.zopim.com/api/v2/chats/search?q=timestamp%3A%5B2018-09-15T05%3A00%3A00Z+TO+2019-09-29T05%3A00%3A00Z%5D&page=1"), any(), anyBoolean())).thenReturn(dataSearchJson.toString());

when(zendeskRestClient.doGet(eq("https://www.zopim.com/api/v2/chats?ids=id_1%2Cid_2"), any(), anyBoolean())).thenReturn(dataJson.toString());

zendeskChatService.fetchData("2018-09-15T05:00:00Z", "2019-09-29T05:00:00Z", 1, recordImporter);

Expand Down

0 comments on commit dcdc0a5

Please sign in to comment.