Skip to content

Commit dc2db88

Browse files
author
stephen powis
committed
finish prospect query, add tests
1 parent da4b63d commit dc2db88

File tree

13 files changed

+686
-28
lines changed

13 files changed

+686
-28
lines changed

src/main/java/com/darksci/pardot/api/PardotClient.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.darksci.pardot.api.parser.email.EmailReadResponseParser;
99
import com.darksci.pardot.api.parser.email.EmailStatsResponseParser;
1010
import com.darksci.pardot.api.parser.login.LoginResponseParser;
11+
import com.darksci.pardot.api.parser.prospect.ProspectQueryResponseParser;
1112
import com.darksci.pardot.api.parser.prospect.ProspectReadResponseParser;
1213
import com.darksci.pardot.api.parser.user.UserAbilitiesParser;
1314
import com.darksci.pardot.api.parser.user.UserQueryResponseParser;
@@ -23,6 +24,7 @@
2324
import com.darksci.pardot.api.request.email.EmailSendOneToOneRequest;
2425
import com.darksci.pardot.api.request.email.EmailStatsRequest;
2526
import com.darksci.pardot.api.request.login.LoginRequest;
27+
import com.darksci.pardot.api.request.prospect.ProspectQueryRequest;
2628
import com.darksci.pardot.api.request.prospect.ProspectReadRequest;
2729
import com.darksci.pardot.api.request.user.UserAbilitiesRequest;
2830
import com.darksci.pardot.api.request.user.UserQueryRequest;
@@ -35,6 +37,7 @@
3537
import com.darksci.pardot.api.response.email.EmailStatsResponse;
3638
import com.darksci.pardot.api.response.login.LoginResponse;
3739
import com.darksci.pardot.api.response.prospect.Prospect;
40+
import com.darksci.pardot.api.response.prospect.ProspectQueryResponse;
3841
import com.darksci.pardot.api.response.user.User;
3942
import com.darksci.pardot.api.response.user.UserAbilitiesResponse;
4043
import com.darksci.pardot.api.response.user.UserQueryResponse;
@@ -174,6 +177,7 @@ private void checkLogin() {
174177

175178
/**
176179
* Make login request
180+
* @param request Login request definition.
177181
* @return LoginResponse returned from server.
178182
*/
179183
public LoginResponse login(LoginRequest request) {
@@ -297,6 +301,15 @@ public Prospect prospectRead(final ProspectReadRequest request) {
297301
return submitRequest(request, new ProspectReadResponseParser());
298302
}
299303

304+
/**
305+
* Make API request to query prospects.
306+
* @param request Request definition.
307+
* @return Parsed api response.
308+
*/
309+
public ProspectQueryResponse.Result prospectQuery(final ProspectQueryRequest request) {
310+
return submitRequest(request, new ProspectQueryResponseParser());
311+
}
312+
300313
/**
301314
* Clean up instance, releasing any resources held internally.
302315
*/
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.darksci.pardot.api.parser.prospect;
2+
3+
import com.darksci.pardot.api.parser.JacksonFactory;
4+
import com.darksci.pardot.api.parser.ResponseParser;
5+
import com.darksci.pardot.api.response.campaign.CampaignQueryResponse;
6+
import com.darksci.pardot.api.response.prospect.ProspectQueryResponse;
7+
8+
import java.io.IOException;
9+
10+
/**
11+
* Handles parsing ProspectQuery API responses into POJOs.
12+
*/
13+
public class ProspectQueryResponseParser implements ResponseParser<ProspectQueryResponse.Result> {
14+
15+
@Override
16+
public ProspectQueryResponse.Result parseResponse(final String responseStr) throws IOException {
17+
return JacksonFactory.newInstance().readValue(responseStr, ProspectQueryResponse.class).getResult();
18+
}
19+
}

src/main/java/com/darksci/pardot/api/request/BaseRequest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package com.darksci.pardot.api.request;
22

3+
import com.darksci.pardot.api.request.email.EmailSendOneToOneRequest;
4+
35
import java.util.Collection;
46
import java.util.HashMap;
7+
import java.util.HashSet;
58
import java.util.Map;
69

710
/**
@@ -39,6 +42,23 @@ protected T setBooleanParam(final String parameterName, final boolean booleanVal
3942
return setParam(parameterName, booleanValue);
4043
}
4144

45+
protected T withCollectionParam(final String name, final Object value) {
46+
Collection<Object> values = getParam(name);
47+
if (values == null) {
48+
values = new HashSet<>();
49+
}
50+
values.add(value);
51+
52+
return setParam(name, values);
53+
}
54+
55+
protected T withCollectionParams(final String name, Collection<?> values) {
56+
for (final Object value: values) {
57+
withCollectionParam(name, value);
58+
}
59+
return (T) this;
60+
}
61+
4262
/**
4363
* Returns all set RequestParameters for the Request.
4464
* @return All set Request Parameters generated from Request builder.

src/main/java/com/darksci/pardot/api/request/email/EmailSendOneToOneRequest.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,7 @@ public EmailSendOneToOneRequest withReplyToEmail(final String replyToEmail) {
112112
* @return RequestBuilder
113113
*/
114114
public EmailSendOneToOneRequest withTag(final String tag) {
115-
Collection<String> tags = getParam("tags");
116-
if (tags == null) {
117-
tags = new HashSet<>();
118-
}
119-
tags.add(tag);
120-
121-
return setParam("tags", tags);
115+
return withCollectionParam("tags", tag);
122116
}
123117

124118
/**
@@ -127,10 +121,7 @@ public EmailSendOneToOneRequest withTag(final String tag) {
127121
* @return RequestBuilder
128122
*/
129123
public EmailSendOneToOneRequest withTags(Collection<String> tags) {
130-
for (final String tag: tags) {
131-
withTag(tag);
132-
}
133-
return this;
124+
return withCollectionParams("tags", tags);
134125
}
135126

136127
/**
Lines changed: 268 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,268 @@
1+
package com.darksci.pardot.api.request.prospect;
2+
3+
import com.darksci.pardot.api.request.BaseQueryRequest;
4+
import com.darksci.pardot.api.request.DateParameter;
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
8+
import java.util.Collection;
9+
import java.util.stream.Collectors;
10+
11+
/**
12+
* Defines a Prospect Query Request.
13+
*/
14+
public class ProspectQueryRequest extends BaseQueryRequest<ProspectQueryRequest> {
15+
16+
@Override
17+
public String getApiEndpoint() {
18+
return "prospect/do/query";
19+
}
20+
21+
/**
22+
* Specifies the fields to be returned. Note: If this parameter isn't present, all default fields and custom fields
23+
* for which the prospect has a value will be returned;
24+
* id field will always be returned.
25+
*
26+
* Each call will append the field argument to the list of previously passed fields.
27+
*
28+
* @param fields Collection of fields to be selected by the request.
29+
* @return RequestBuilder
30+
*/
31+
public ProspectQueryRequest withFields(Collection<String> fields) {
32+
final String fieldsStr = fields.stream().collect(Collectors.joining( "," ));
33+
final String currentValue = getParam("fields");
34+
if (currentValue == null) {
35+
// set
36+
return setParam("fields", fieldsStr);
37+
} else {
38+
// Append
39+
return setParam("fields", currentValue + "," + fieldsStr);
40+
}
41+
}
42+
43+
/**
44+
* Specifies the fields to be returned. Note: If this parameter isn't present, all default fields and custom fields
45+
* for which the prospect has a value will be returned;
46+
* id field will always be returned.
47+
*
48+
* Each call will append the field argument to the list of previously passed fields.
49+
*
50+
* @param field Field to be selected by request.
51+
* @return RequestBuilder
52+
*/
53+
public ProspectQueryRequest withField(final String field) {
54+
final String currentValue = getParam("fields");
55+
if (currentValue == null) {
56+
// set
57+
return setParam("fields", field);
58+
} else {
59+
// Append
60+
return setParam("fields", currentValue + "," + field);
61+
}
62+
}
63+
64+
/**
65+
* Only select prospects who are assigned.
66+
* @return RequestBuilder
67+
*/
68+
public ProspectQueryRequest withAssignedOnly() {
69+
return setBooleanParam("assigned", true);
70+
}
71+
72+
/**
73+
* Only select prospects who are NOT assigned.
74+
* @return RequestBuilder
75+
*/
76+
public ProspectQueryRequest withUnassignedOnly() {
77+
return setBooleanParam("assigned", false);
78+
}
79+
80+
/**
81+
* Only select prospects who are assigned to the specified user, by Id.
82+
* @param userId The userId to filter by.
83+
* @return RequestBuilder
84+
*/
85+
public ProspectQueryRequest withAssignedUser(final Long userId) {
86+
return setParam("assigned_to_user", userId);
87+
}
88+
89+
/**
90+
* Only select prospects who are assigned to the specified user, by email address.
91+
* @param userEmail The user to filter by, as defined by their Email address.
92+
* @return RequestBuilder
93+
*/
94+
public ProspectQueryRequest withAssignUser(final String userEmail) {
95+
return setParam("assigned_to_user", userEmail);
96+
}
97+
98+
/**
99+
* Only select prospects who are archived.
100+
* @return RequestBuilder
101+
*/
102+
public ProspectQueryRequest withArchivedOnly() {
103+
return super.withArchivedOnly(true);
104+
}
105+
106+
/**
107+
* Only select prospects who are NOT archived. (Default value)
108+
* @return RequestBuilder
109+
*/
110+
public ProspectQueryRequest withNotArchivedOnly() {
111+
return super.withArchivedOnly(false);
112+
}
113+
114+
/**
115+
* Only select prospects who have a grade equal to the specified grade.
116+
* @param grade Grade in format of "A", "A+", "B", "C-"
117+
* @return RequestBuilder
118+
*/
119+
public ProspectQueryRequest withGradeEqualTo(final String grade) {
120+
return setParam("grade_equal_to", grade);
121+
}
122+
123+
/**
124+
* Only select prospects who have a grade greater than to the specified grade.
125+
* @param grade Grade in format of "A", "A+", "B", "C-"
126+
* @return RequestBuilder
127+
*/
128+
public ProspectQueryRequest withGradeGreaterThan(final String grade) {
129+
return setParam("grade_greater_than", grade);
130+
}
131+
132+
/**
133+
* Only select prospects who have a grade less than the specified grade.
134+
* @param grade Grade in format of "A", "A+", "B", "C-"
135+
* @return RequestBuilder
136+
*/
137+
public ProspectQueryRequest withGradeLessThan(final String grade) {
138+
return setParam("grade_less_than", grade);
139+
}
140+
141+
/**
142+
* Only select prospects who have been starred.
143+
* @return RequestBuilder
144+
*/
145+
public ProspectQueryRequest withStarredOnly() {
146+
return setBooleanParam("is_starred", true);
147+
}
148+
149+
/**
150+
* Only select prospects who have NOT been starred.
151+
* @return RequestBuilder
152+
*/
153+
public ProspectQueryRequest withNotStarredOnly() {
154+
return setBooleanParam("is_starred", false);
155+
}
156+
157+
/**
158+
* Only select prospects who have a last activity date after a specified value.
159+
* @param lastActivityAfter The date to filter.
160+
* @return RequestBuilder
161+
*/
162+
public ProspectQueryRequest withLastActivityAfter(DateParameter lastActivityAfter) {
163+
return setParam("last_activity_after", lastActivityAfter);
164+
}
165+
166+
/**
167+
* Only select prospects who have no activity.
168+
* @return RequestBuilder
169+
*/
170+
public ProspectQueryRequest withNonActiveOnly() {
171+
return setBooleanParam("last_activity_never", true);
172+
}
173+
174+
/**
175+
* Only select prospects who are member's of the specified list Id.
176+
* @param listId Id of List to filter prospects by.
177+
* @return RequestBuilder
178+
*/
179+
public ProspectQueryRequest withListId(final Long listId) {
180+
return setParam("list_id", listId);
181+
}
182+
183+
/**
184+
* Only select prospects who are considered 'new'.
185+
* @return RequestBuilder
186+
*/
187+
public ProspectQueryRequest withNewOnly() {
188+
return setBooleanParam("new", true);
189+
}
190+
191+
/**
192+
* Only select prospects who are NOT considered 'new'.
193+
* @return RequestBuilder
194+
*/
195+
public ProspectQueryRequest withNotNewOnly() {
196+
return setBooleanParam("new", false);
197+
}
198+
199+
/**
200+
* Only select prospects who have have a score equal to the specified value.
201+
* @param score Score value to filter by.
202+
* @return RequestBuilder
203+
*/
204+
public ProspectQueryRequest withScoreEqualTo(final Integer score) {
205+
return setParam("score_equal_to", score);
206+
}
207+
208+
/**
209+
* Only select prospects who have have a score greater than the specified value.
210+
* @param score Score value to filter by.
211+
* @return RequestBuilder
212+
*/
213+
public ProspectQueryRequest withScoreGreaterThan(final Integer score) {
214+
return setParam("score_greater_than", score);
215+
}
216+
217+
/**
218+
* Only select prospects who have have a score less than the specified value.
219+
* @param score Score value to filter by.
220+
* @return RequestBuilder
221+
*/
222+
public ProspectQueryRequest withScoreLessThan(final Integer score) {
223+
return setParam("score_less_than", score);
224+
}
225+
226+
/**
227+
* Sort by CreatedAt.
228+
* @return BaseQueryRequest
229+
*/
230+
public ProspectQueryRequest withSortByCreatedAt() {
231+
return super.withSortByCreatedAt();
232+
}
233+
234+
/**
235+
* Sort results by UpdatedAt.
236+
* @return BaseQueryRequest
237+
*/
238+
public ProspectQueryRequest withSortByUpdatedAt() {
239+
return super.withSortByUpdatedAt();
240+
}
241+
242+
/**
243+
* Sort results by Probability.
244+
* @return BaseQueryRequest
245+
*/
246+
public ProspectQueryRequest withSortByProbability() {
247+
return withSortBy("probability");
248+
}
249+
250+
251+
/**
252+
* Sort results by Probability.
253+
* @return BaseQueryRequest
254+
*/
255+
public ProspectQueryRequest withSortByLastActivityAt() {
256+
return withSortBy("last_activity_at");
257+
}
258+
259+
260+
/**
261+
* Sort results by Id.
262+
* @return BaseQueryRequest
263+
*/
264+
public ProspectQueryRequest withSortById() {
265+
return super.withSortById();
266+
}
267+
268+
}

0 commit comments

Comments
 (0)