Skip to content

Commit

Permalink
keep backward compatible for API Chat updated
Browse files Browse the repository at this point in the history
  • Loading branch information
hieudion committed Oct 23, 2024
1 parent 6137b70 commit 2e6302e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 6 deletions.
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,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();
}
}
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 + "/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
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 @@ -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 =
Expand Down

0 comments on commit 2e6302e

Please sign in to comment.