diff --git a/src/main/java/org/embulk/input/zendesk/services/ZendeskChatService.java b/src/main/java/org/embulk/input/zendesk/services/ZendeskChatService.java index bf9dbdd..5d2c975 100644 --- a/src/main/java/org/embulk/input/zendesk/services/ZendeskChatService.java +++ b/src/main/java/org/embulk/input/zendesk/services/ZendeskChatService.java @@ -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; @@ -164,9 +165,9 @@ private List 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) @@ -186,7 +187,7 @@ private String buildSearchParam(final String startTime, final String endTime) private String buildSearchRequest(final List ids) { return ZendeskUtils.getURIBuilder(task.getLoginUrl()) - .setPath(ZendeskConstants.Url.API_CHAT) + .setPath(resolveEndpointPatternByDomain(ZendeskConstants.Url.ENDPOINT_CHAT)) .setParameter("ids", buildIdsParam(ids)) .toString(); } @@ -274,4 +275,21 @@ private String getEndTime() ? task.getEndTime().get() : OffsetDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ISO_INSTANT); } + + 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(); + } } diff --git a/src/main/java/org/embulk/input/zendesk/utils/ZendeskConstants.java b/src/main/java/org/embulk/input/zendesk/utils/ZendeskConstants.java index a6f7f49..7177a69 100644 --- a/src/main/java/org/embulk/input/zendesk/utils/ZendeskConstants.java +++ b/src/main/java/org/embulk/input/zendesk/utils/ZendeskConstants.java @@ -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 + "/chat/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 @@ -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 diff --git a/src/test/java/org/embulk/input/zendesk/services/TestZendeskChatService.java b/src/test/java/org/embulk/input/zendesk/services/TestZendeskChatService.java index cc15185..62819d5 100644 --- a/src/test/java/org/embulk/input/zendesk/services/TestZendeskChatService.java +++ b/src/test/java/org/embulk/input/zendesk/services/TestZendeskChatService.java @@ -67,6 +67,25 @@ public void testFetchData() 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); + + verify(recordImporter, times(2)).addRecord(any()); + } + private void setup() { ZendeskInputPlugin.PluginTask task =