Skip to content

Commit 3903013

Browse files
committed
Add support for Rescore User and Get User Score APIs
1 parent bc327fc commit 3903013

File tree

12 files changed

+526
-4
lines changed

12 files changed

+526
-4
lines changed

CHANGES.MD

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
2.3.0 (2018-08-06)
2+
=================
3+
4+
- Add support for Rescore User and Get User Score APIs
5+
16
2.2.0 (2018-07-05)
27
=================
38

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.siftscience;
2+
3+
import com.siftscience.model.AbuseScoreV205;
4+
import com.siftscience.model.EntityScoreResponseBody;
5+
import okhttp3.Response;
6+
7+
import java.io.IOException;
8+
9+
public class EntityScoreResponse extends SiftResponse<EntityScoreResponseBody> {
10+
EntityScoreResponse(Response okResponse, FieldSet requestBody) throws IOException {
11+
super(okResponse, requestBody);
12+
}
13+
14+
@Override
15+
void populateBodyFromJson(String jsonBody) {
16+
body = EntityScoreResponseBody.fromJson(jsonBody);
17+
}
18+
19+
public AbuseScoreV205 getScoreResponse(String abuseType) {
20+
if (this.getBody() != null && this.getBody().getScores() != null) {
21+
return this.getBody().getScores().get(abuseType);
22+
}
23+
return null;
24+
}
25+
26+
public Double getScore(String abuseType) {
27+
AbuseScoreV205 abuseScore = getScoreResponse(abuseType);
28+
if (abuseScore != null) {
29+
return abuseScore.getScore();
30+
}
31+
return null;
32+
}
33+
}

src/main/java/com/siftscience/SiftClient.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ public ScoreRequest buildRequest(ScoreFieldSet fields) {
8282
return new ScoreRequest(baseUrl, okClient, fields);
8383
}
8484

85+
public UserScoreRequest buildRequest(UserScoreFieldSet fields) {
86+
setupApiKey(fields);
87+
return new UserScoreRequest(baseUrl, okClient, fields);
88+
}
89+
8590
public WorkflowStatusRequest buildRequest(WorkflowStatusFieldSet fields) {
8691
setupApiKey(fields);
8792
return new WorkflowStatusRequest(baseApi3Url, okClient, fields);
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.siftscience;
2+
3+
import com.siftscience.model.UserScoreFieldSet;
4+
import okhttp3.HttpUrl;
5+
import okhttp3.OkHttpClient;
6+
import okhttp3.Request;
7+
import okhttp3.RequestBody;
8+
import okhttp3.Response;
9+
10+
import java.io.IOException;
11+
12+
/**
13+
* UserScoreRequest is the request type of Sift User Score API requests.
14+
*
15+
* This includes:
16+
* 1. The get user score API, which returns the latest score(s) computed for a user.
17+
* See details here: https://siftscience.com/developers/docs/java/score-api/get-score
18+
* 2. The rescore user API, which recomputes scores for a user.
19+
* See details here: https://siftscience.com/developers/docs/java/score-api/rescore
20+
*/
21+
public class UserScoreRequest extends SiftRequest<EntityScoreResponse> {
22+
UserScoreRequest(HttpUrl baseUrl, OkHttpClient okClient, UserScoreFieldSet fields) {
23+
super(baseUrl, okClient, fields);
24+
}
25+
26+
/**
27+
* Use a POST request if this is a rescore user request, otherwise use a GET request.
28+
*/
29+
@Override
30+
protected void modifyRequestBuilder(Request.Builder builder) {
31+
if (((UserScoreFieldSet)fieldSet).getRescoreUser()) {
32+
builder.post(RequestBody.create(null, new byte[0]));
33+
} else {
34+
builder.get();
35+
}
36+
}
37+
38+
@Override
39+
EntityScoreResponse buildResponse(Response response, FieldSet requestFields)
40+
throws IOException {
41+
return new EntityScoreResponse(response, requestFields);
42+
}
43+
44+
/**
45+
* For user score requests, the api key and abuse types are encoded into the URL as query params
46+
*/
47+
@Override
48+
protected HttpUrl path(HttpUrl baseUrl) {
49+
UserScoreFieldSet userSoreFieldSet = (UserScoreFieldSet)fieldSet;
50+
HttpUrl.Builder builder = baseUrl.newBuilder().addPathSegment("v205");
51+
builder.addPathSegment("users")
52+
.addPathSegment(userSoreFieldSet.getUserId())
53+
.addPathSegment("score")
54+
.addQueryParameter("api_key", userSoreFieldSet.getApiKey());
55+
if (userSoreFieldSet.getAbuseTypes() != null && userSoreFieldSet.getAbuseTypes().size() > 0) {
56+
String queryParamVal = "";
57+
for (String abuseType : userSoreFieldSet.getAbuseTypes()) {
58+
queryParamVal += (abuseType + ",");
59+
}
60+
builder.addQueryParameter("abuse_types",
61+
queryParamVal.substring(0, queryParamVal.length() - 1));
62+
}
63+
return builder.build();
64+
}
65+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.siftscience.model;
2+
3+
import com.google.gson.annotations.Expose;
4+
import com.google.gson.annotations.SerializedName;
5+
6+
import java.util.List;
7+
8+
public class AbuseScoreV205 {
9+
@Expose @SerializedName("score") private Double score;
10+
@Expose @SerializedName("reasons") private List<Reason> reasons;
11+
@Expose @SerializedName("status") private int status;
12+
@Expose @SerializedName("error_message") private String error_message;
13+
@Expose @SerializedName("time") private long time;
14+
15+
public Double getScore() {
16+
return score;
17+
}
18+
19+
public AbuseScoreV205 setScore(Double score) {
20+
this.score = score;
21+
return this;
22+
}
23+
24+
public List<Reason> getReasons() {
25+
return reasons;
26+
}
27+
28+
public AbuseScoreV205 setReasons(List<Reason> reasons) {
29+
this.reasons = reasons;
30+
return this;
31+
}
32+
33+
public int getStatus() {
34+
return status;
35+
}
36+
37+
public AbuseScoreV205 setStatus(int status) {
38+
this.status = status;
39+
return this;
40+
}
41+
42+
public String getError_message() {
43+
return error_message;
44+
}
45+
46+
public AbuseScoreV205 setError_message(String error_message) {
47+
this.error_message = error_message;
48+
return this;
49+
}
50+
51+
public long getTime() {
52+
return time;
53+
}
54+
55+
public AbuseScoreV205 setTime(long time) {
56+
this.time = time;
57+
return this;
58+
}
59+
60+
}

src/main/java/com/siftscience/model/Decision.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55

66
public class Decision {
77
@Expose @SerializedName("id") private String id;
8+
@Expose @SerializedName("category") private String category;
9+
@Expose @SerializedName("time") private long time;
10+
@Expose @SerializedName("source") private String source;
11+
@Expose @SerializedName("description") private String description;
812

913
public String getId() {
1014
return id;
@@ -14,4 +18,54 @@ public Decision setId(String id) {
1418
this.id = id;
1519
return this;
1620
}
21+
22+
public String getCategory() {
23+
return category;
24+
}
25+
26+
public Decision setCategory(String category) {
27+
this.category = category;
28+
return this;
29+
}
30+
31+
public long getTime() {
32+
return time;
33+
}
34+
35+
public Decision setTime(long time) {
36+
this.time = time;
37+
return this;
38+
}
39+
40+
public String getSource() {
41+
return source;
42+
}
43+
44+
public Decision setSource(String source) {
45+
this.source = source;
46+
return this;
47+
}
48+
49+
public String getDescription() {
50+
return description;
51+
}
52+
53+
public Decision setDescription(String description) {
54+
this.description = description;
55+
return this;
56+
}
57+
58+
@Override
59+
public boolean equals(Object other) {
60+
if (!(other instanceof Decision)) {
61+
return false;
62+
}
63+
64+
Decision o = (Decision) other;
65+
return getId().equals(o.getId())
66+
&& getCategory().equals(o.getCategory())
67+
&& getTime() == o.getTime()
68+
&& getSource().equals(o.getSource())
69+
&& getDescription().equals(o.getDescription());
70+
}
1771
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.siftscience.model;
2+
3+
import com.google.gson.annotations.Expose;
4+
import com.google.gson.annotations.SerializedName;
5+
6+
import java.util.List;
7+
import java.util.Map;
8+
9+
public class EntityScoreResponseBody extends BaseResponseBody<EntityScoreResponseBody> {
10+
public static EntityScoreResponseBody fromJson(String json) {
11+
return gson.fromJson(json, EntityScoreResponseBody.class);
12+
}
13+
14+
@Expose @SerializedName("entity_type") private String entityType;
15+
@Expose @SerializedName("entity_id") private String entityId;
16+
@Expose @SerializedName("scores") private Map<String, AbuseScoreV205> scores;
17+
@Expose @SerializedName("latest_labels") private Map<String, Label> latestLabels;
18+
@Expose @SerializedName("latest_decisions") private Map<String, Decision> latestDecisions;
19+
@Expose @SerializedName("workflow_statuses") private List<WorkflowStatus> workflowStatuses;
20+
21+
public String getEntityType() {
22+
return entityType;
23+
}
24+
25+
public EntityScoreResponseBody setEntityType(String entityType) {
26+
this.entityType = entityType;
27+
return this;
28+
}
29+
30+
public String getEntityId() {
31+
return entityId;
32+
}
33+
34+
public EntityScoreResponseBody setEntityId(String entityId) {
35+
this.entityId = entityId;
36+
return this;
37+
}
38+
39+
public Map<String, AbuseScoreV205> getScores() {
40+
return scores;
41+
}
42+
43+
public EntityScoreResponseBody setScores(Map<String, AbuseScoreV205> scores) {
44+
this.scores = scores;
45+
return this;
46+
}
47+
48+
public Map<String, Label> getLatestLabels() {
49+
return latestLabels;
50+
}
51+
52+
public EntityScoreResponseBody setLatestLabels(Map<String, Label> latestLabels) {
53+
this.latestLabels = latestLabels;
54+
return this;
55+
}
56+
57+
public Map<String, Decision> getLatestDecisions() {
58+
return latestDecisions;
59+
}
60+
61+
public EntityScoreResponseBody setLatestDecisions(Map<String, Decision> latestDecisions) {
62+
this.latestDecisions = latestDecisions;
63+
return this;
64+
}
65+
66+
public List<WorkflowStatus> getWorkflowStatuses() {
67+
return workflowStatuses;
68+
}
69+
70+
public EntityScoreResponseBody setWorkflowStatuses(List<WorkflowStatus> workflowStatuses) {
71+
this.workflowStatuses = workflowStatuses;
72+
return this;
73+
}
74+
75+
public WorkflowStatus getWorkflowStatus(int i) {
76+
return workflowStatuses.get(i);
77+
}
78+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.siftscience.model;
2+
3+
import com.google.gson.annotations.Expose;
4+
import com.google.gson.annotations.SerializedName;
5+
import com.siftscience.FieldSet;
6+
7+
import java.util.List;
8+
9+
public class UserScoreFieldSet extends FieldSet<UserScoreFieldSet> {
10+
@Expose @SerializedName(USER_ID) private String userId;
11+
private List<String> abuseTypes;
12+
private boolean rescoreUser = false;
13+
14+
public String getUserId() {
15+
return userId;
16+
}
17+
18+
public UserScoreFieldSet setUserId(String userId) {
19+
this.userId = userId;
20+
return this;
21+
}
22+
23+
public List<String> getAbuseTypes() {
24+
return abuseTypes;
25+
}
26+
27+
public UserScoreFieldSet setAbuseTypes(List<String> abuseTypes) {
28+
this.abuseTypes = abuseTypes;
29+
return this;
30+
}
31+
32+
public boolean getRescoreUser() {
33+
return rescoreUser;
34+
}
35+
36+
public UserScoreFieldSet setRescoreUser(boolean rescoreUser) {
37+
this.rescoreUser = rescoreUser;
38+
return this;
39+
}
40+
41+
}

src/test/java/com/siftscience/ContentDecisionStatusTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,6 @@ public void testContentDecisionStatus() throws Exception {
6161
// Verify the response was parsed correctly.
6262
Assert.assertEquals(HTTP_OK, siftResponse.getHttpStatusCode());
6363
JSONAssert.assertEquals(response.getBody().readUtf8(),
64-
siftResponse.getBody().toJson(), true);
64+
siftResponse.getBody().toJson(), false);
6565
}
6666
}

src/test/java/com/siftscience/DecisionStatusTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public void testDecisionStatus() throws Exception {
8686
// Verify the response was parsed correctly.
8787
Assert.assertEquals(HTTP_OK, siftResponse.getHttpStatusCode());
8888
JSONAssert.assertEquals(response.getBody().readUtf8(),
89-
siftResponse.getBody().toJson(), true);
89+
siftResponse.getBody().toJson(), false);
9090
}
9191

9292
@Test
@@ -136,6 +136,6 @@ public void testContentDecisionStatus() throws Exception {
136136
// Verify the response was parsed correctly.
137137
Assert.assertEquals(HTTP_OK, siftResponse.getHttpStatusCode());
138138
JSONAssert.assertEquals(response.getBody().readUtf8(),
139-
siftResponse.getBody().toJson(), true);
139+
siftResponse.getBody().toJson(), false);
140140
}
141141
}

0 commit comments

Comments
 (0)