Skip to content

Commit 60a1411

Browse files
author
Timothy Lim
committed
Add support for Scroll API
1 parent 16badb0 commit 60a1411

File tree

4 files changed

+37
-2
lines changed

4 files changed

+37
-2
lines changed

README.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,16 @@ user = User.find(params);
134134
user.addCustomAttribute(CustomAttribute.newStringAttribute("role", "captain"));
135135
User.update(user);
136136

137-
// Iterate over all users
137+
// Iterate over all users (up to 10k records, to read all use Scroll API)
138138
UserCollection users = User.list();
139139
while(users.hasNext()) {
140140
System.out.println(users.next().getUserId());
141141
}
142142

143+
// Retrieve users via Scroll API
144+
UserCollection users = User.scroll(null);
145+
users = User.scroll(users.getScrollParam());
146+
143147
// Bulk submit users
144148
final List<JobItem<User>> items = Lists.newArrayList();
145149
items.add(new JobItem<User>("post", user1));
@@ -187,12 +191,16 @@ while(contacts.hasNext()) {
187191
System.out.println(contacts.next());
188192
}
189193

190-
// Iterate over all contacts
194+
// Iterate over all contacts (up to 10k records, to read all use Scroll API)
191195
ContactCollection allContacts = Contact.list();
192196
while(allContacts.hasNext()) {
193197
System.out.println(allContacts.next());
194198
}
195199

200+
// Retrieve contacts via Scroll API
201+
ContactCollection contacts = Contact.scroll(null);
202+
contacts = Contact.scroll(contacts.getScrollParam());
203+
196204
// Remove a contact
197205
Contact.delete(contact);
198206

@@ -586,6 +594,8 @@ These return a Collection object (eg `UserCollection`) which can be iterated in
586594

587595
- Java's inbuilt iterator methods `next()` and `hasNext()` - these are useful when you want to fetch data without manually handling pagination.
588596

597+
- User and Contact listing only works up to 10k records. To retrieve all records use the Scroll API via `scroll()`
598+
589599
### Error handling
590600

591601
You do not need to deal with the HTTP response from an API call directly.

intercom-java/src/main/java/io/intercom/api/Contact.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@ public static ContactCollection list()
4949
return DataResource.list(SENTINEL, "contacts", ContactCollection.class);
5050
}
5151

52+
public static ContactCollection scroll(String scrollParam)
53+
throws AuthorizationException, ClientException, ServerException, InvalidException, RateLimitException {
54+
Map<String, String> params = Maps.newHashMap();
55+
if(scrollParam != null && scrollParam.trim() != ""){
56+
params.put("scroll_param", scrollParam);
57+
}
58+
return DataResource.list(params, "contacts/scroll", ContactCollection.class);
59+
}
60+
5261
public static Contact create(Contact c)
5362
throws AuthorizationException, ClientException, ServerException, InvalidException, RateLimitException {
5463
return DataResource.create(ContactUpdate.buildFrom(c), "contacts", Contact.class);

intercom-java/src/main/java/io/intercom/api/TypedDataCollection.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ public abstract class TypedDataCollection<T extends TypedData> extends TypedData
2727
@JsonProperty("type")
2828
protected String type;
2929

30+
@JsonProperty("scroll_param")
31+
protected String scrollParam;
32+
3033
public abstract TypedDataCollection<T> nextPage();
3134

3235
public boolean hasNextPage() {
@@ -55,6 +58,10 @@ public String getType() {
5558
return type;
5659
}
5760

61+
public String getScrollParam() {
62+
return scrollParam;
63+
}
64+
5865
@Override
5966
public boolean equals(Object o) {
6067
if (this == o) return true;

intercom-java/src/main/java/io/intercom/api/User.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,15 @@ public static UserCollection list()
7070
return DataResource.list(SENTINEL, "users", UserCollection.class);
7171
}
7272

73+
public static UserCollection scroll(String scrollParam)
74+
throws AuthorizationException, ClientException, ServerException, InvalidException, RateLimitException {
75+
Map<String, String> params = Maps.newHashMap();
76+
if(scrollParam != null && scrollParam.trim() != ""){
77+
params.put("scroll_param", scrollParam);
78+
}
79+
return DataResource.list(params, "users/scroll", UserCollection.class);
80+
}
81+
7382
public static Job submit(final List<JobItem<User>> items)
7483
throws AuthorizationException, ClientException, ServerException, InvalidException, RateLimitException {
7584
return submit(items, null);

0 commit comments

Comments
 (0)