Skip to content

Commit b084c3a

Browse files
authored
Merge pull request #142 from intercom/MM/scroll
add scroll API support for User and Contact
2 parents 854e1db + cd94971 commit b084c3a

File tree

7 files changed

+109
-2
lines changed

7 files changed

+109
-2
lines changed

README.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,17 @@ 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+
ScrollableUserCollection usersScroll = User.scroll();
145+
List<User> users = usersScroll.getPage();
146+
usersScroll = usersScroll.scroll();
147+
143148
// Bulk submit users
144149
final List<JobItem<User>> items = Lists.newArrayList();
145150
items.add(new JobItem<User>("post", user1));
@@ -187,12 +192,18 @@ while(contacts.hasNext()) {
187192
System.out.println(contacts.next());
188193
}
189194

190-
// Iterate over all contacts
195+
// Iterate over all contacts (up to 10k records, to read all use Scroll API)
191196
ContactCollection allContacts = Contact.list();
192197
while(allContacts.hasNext()) {
193198
System.out.println(allContacts.next());
194199
}
195200

201+
// Retrieve contacts via Scroll API
202+
ScrollableContactCollection contactsScroll = Contact.scroll();
203+
List<Contact> contacts = contactsScroll.getPage();
204+
contactsScroll = contactsScroll.scroll();
205+
206+
196207
// Remove a contact
197208
Contact.delete(contact);
198209

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

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

600+
- User and Contact listing only works up to 10k records. To retrieve all records use the Scroll API via `scroll()`
601+
589602
### Error handling
590603

591604
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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ public static ContactCollection list()
4949
return DataResource.list(SENTINEL, "contacts", ContactCollection.class);
5050
}
5151

52+
public static ScrollableContactCollection scroll()
53+
throws AuthorizationException, ClientException, ServerException, InvalidException, RateLimitException {
54+
return DataResource.scroll(null, "contacts", ScrollableContactCollection.class);
55+
}
56+
5257
public static Contact create(Contact c)
5358
throws AuthorizationException, ClientException, ServerException, InvalidException, RateLimitException {
5459
return DataResource.create(ContactUpdate.buildFrom(c), "contacts", Contact.class);

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package io.intercom.api;
22

3+
import com.google.common.base.Strings;
4+
import com.google.common.collect.Maps;
5+
36
import java.net.URI;
47
import java.util.List;
58
import java.util.Map;
@@ -58,4 +61,13 @@ public static <C> C list(Map<String, String> params, String collectionPath, Clas
5861
return resource.get(c);
5962
}
6063

64+
public static <C> C scroll(String scrollParam, String collectionPath, Class<C> c) {
65+
Map<String, String> params = Maps.newHashMap();
66+
if (!Strings.isNullOrEmpty(scrollParam)) {
67+
params.put("scroll_param", scrollParam);
68+
}
69+
final HttpClient resource = new HttpClient(UriBuilder.newBuilder().path(collectionPath + "/scroll").query(params).build());
70+
return resource.get(c);
71+
}
72+
6173
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package io.intercom.api;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
5+
import java.util.List;
6+
7+
public class ScrollableContactCollection extends ScrollableTypedDataCollection<Contact> {
8+
9+
@Override
10+
public ScrollableContactCollection scroll() {
11+
return DataResource.scroll(getScrollParam(), "contacts", ScrollableContactCollection.class);
12+
}
13+
14+
@JsonProperty("contacts")
15+
@Override
16+
public List<Contact> getPage() {
17+
return super.getPage();
18+
}
19+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package io.intercom.api;
2+
3+
import com.fasterxml.jackson.annotation.JsonInclude;
4+
import com.fasterxml.jackson.annotation.JsonProperty;
5+
import com.google.common.collect.Lists;
6+
7+
import java.util.List;
8+
9+
@JsonInclude(JsonInclude.Include.NON_EMPTY)
10+
public abstract class ScrollableTypedDataCollection<T extends TypedData> extends TypedData {
11+
12+
protected List<T> page = Lists.newArrayList();
13+
14+
@JsonProperty("scroll_param")
15+
protected String scrollParam;
16+
17+
@JsonProperty("type")
18+
protected String type;
19+
20+
public String getScrollParam() {
21+
return scrollParam;
22+
}
23+
24+
public String getType() {
25+
return type;
26+
}
27+
28+
public abstract ScrollableTypedDataCollection<T> scroll();
29+
30+
public List<T> getPage() {
31+
return page;
32+
}
33+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package io.intercom.api;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
5+
import java.util.List;
6+
7+
public class ScrollableUserCollection extends ScrollableTypedDataCollection<User> {
8+
9+
@Override
10+
public ScrollableUserCollection scroll() {
11+
return DataResource.scroll(getScrollParam(), "users", ScrollableUserCollection.class);
12+
}
13+
14+
@JsonProperty("users")
15+
@Override
16+
public List<User> getPage() {
17+
return super.getPage();
18+
}
19+
20+
}

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

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

73+
public static ScrollableUserCollection scroll()
74+
throws AuthorizationException, ClientException, ServerException, InvalidException, RateLimitException {
75+
return DataResource.scroll(null, "users", ScrollableUserCollection.class);
76+
}
77+
7378
public static Job submit(final List<JobItem<User>> items)
7479
throws AuthorizationException, ClientException, ServerException, InvalidException, RateLimitException {
7580
return submit(items, null);

0 commit comments

Comments
 (0)