Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGES.MD
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
2.3.0 (2018-08-08)
=================

- Add support for Rescore User and Get User Score APIs

2.2.1 (2018-08-06)
=================

- Remove Guava dependency
- Replace use of internal Sun API class

Expand Down
33 changes: 33 additions & 0 deletions src/main/java/com/siftscience/EntityScoreResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.siftscience;

import com.siftscience.model.AbuseScore;
import com.siftscience.model.EntityScoreResponseBody;
import okhttp3.Response;

import java.io.IOException;

public class EntityScoreResponse extends SiftResponse<EntityScoreResponseBody> {
EntityScoreResponse(Response okResponse, FieldSet requestBody) throws IOException {
super(okResponse, requestBody);
}

@Override
void populateBodyFromJson(String jsonBody) {
body = EntityScoreResponseBody.fromJson(jsonBody);
}

public AbuseScore getScoreResponse(String abuseType) {
if (this.getBody() != null && this.getBody().getScores() != null) {
return this.getBody().getScores().get(abuseType);
}
return null;
}

public Double getScore(String abuseType) {
AbuseScore abuseScore = getScoreResponse(abuseType);
if (abuseScore != null) {
return abuseScore.getScore();
}
return null;
}
}
5 changes: 5 additions & 0 deletions src/main/java/com/siftscience/SiftClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ public ScoreRequest buildRequest(ScoreFieldSet fields) {
return new ScoreRequest(baseUrl, okClient, fields);
}

public UserScoreRequest buildRequest(UserScoreFieldSet fields) {
setupApiKey(fields);
return new UserScoreRequest(baseUrl, okClient, fields);
}

public WorkflowStatusRequest buildRequest(WorkflowStatusFieldSet fields) {
setupApiKey(fields);
return new WorkflowStatusRequest(baseApi3Url, okClient, fields);
Expand Down
62 changes: 62 additions & 0 deletions src/main/java/com/siftscience/UserScoreRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.siftscience;

import com.siftscience.model.UserScoreFieldSet;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

import java.io.IOException;

/**
* UserScoreRequest is the request type of Sift User Score API requests.
*
* This includes:
* 1. The get user score API, which returns the latest score(s) computed for a user.
* See details here: https://siftscience.com/developers/docs/java/score-api/get-score
* 2. The rescore user API, which recomputes scores for a user.
* See details here: https://siftscience.com/developers/docs/java/score-api/rescore
*/
public class UserScoreRequest extends SiftRequest<EntityScoreResponse> {
UserScoreRequest(HttpUrl baseUrl, OkHttpClient okClient, UserScoreFieldSet fields) {
super(baseUrl, okClient, fields);
}

/**
* Use a POST request if this is a rescore user request, otherwise use a GET request.
*/
@Override
protected void modifyRequestBuilder(Request.Builder builder) {
if (((UserScoreFieldSet)fieldSet).getRescoreUser()) {
builder.post(RequestBody.create(null, new byte[0]));
} else {
builder.get();
}
}

@Override
EntityScoreResponse buildResponse(Response response, FieldSet requestFields)
throws IOException {
return new EntityScoreResponse(response, requestFields);
}

/**
* For user score requests, the api key and abuse types are encoded into the URL as query params
*/
@Override
protected HttpUrl path(HttpUrl baseUrl) {
UserScoreFieldSet userScoreFieldSet = (UserScoreFieldSet)fieldSet;
HttpUrl.Builder builder = baseUrl.newBuilder().addPathSegment("v205");
builder.addPathSegment("users")
.addPathSegment(userScoreFieldSet.getUserId())
.addPathSegment("score")
.addQueryParameter("api_key", userScoreFieldSet.getApiKey());
if (userScoreFieldSet.getAbuseTypes() != null
&& !userScoreFieldSet.getAbuseTypes().isEmpty()) {
String queryParamVal = StringUtils.joinWithComma(userScoreFieldSet.getAbuseTypes());
builder.addQueryParameter("abuse_types", queryParamVal);
}
return builder.build();
}
}
30 changes: 30 additions & 0 deletions src/main/java/com/siftscience/model/AbuseScore.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
public class AbuseScore {
@Expose @SerializedName("score") private Double score;
@Expose @SerializedName("reasons") private List<Reason> reasons;
@Expose @SerializedName("status") private int status;
@Expose @SerializedName("error_message") private String errorMessage;
@Expose @SerializedName("time") private long time;

public Double getScore() {
return score;
Expand All @@ -26,4 +29,31 @@ public AbuseScore setReasons(List<Reason> reasons) {
this.reasons = reasons;
return this;
}

public int getStatus() {
return status;
}

public AbuseScore setStatus(int status) {
this.status = status;
return this;
}

public String getErrorMessage() {
return errorMessage;
}

public AbuseScore setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
return this;
}

public long getTime() {
return time;
}

public AbuseScore setTime(long time) {
this.time = time;
return this;
}
}
56 changes: 56 additions & 0 deletions src/main/java/com/siftscience/model/Decision.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

import java.util.Objects;

public class Decision {
@Expose @SerializedName("id") private String id;
@Expose @SerializedName("category") private String category;
@Expose @SerializedName("time") private long time;
@Expose @SerializedName("source") private String source;
@Expose @SerializedName("description") private String description;

public String getId() {
return id;
Expand All @@ -14,4 +20,54 @@ public Decision setId(String id) {
this.id = id;
return this;
}

public String getCategory() {
return category;
}

public Decision setCategory(String category) {
this.category = category;
return this;
}

public long getTime() {
return time;
}

public Decision setTime(long time) {
this.time = time;
return this;
}

public String getSource() {
return source;
}

public Decision setSource(String source) {
this.source = source;
return this;
}

public String getDescription() {
return description;
}

public Decision setDescription(String description) {
this.description = description;
return this;
}

@Override
public boolean equals(Object other) {
if (!(other instanceof Decision)) {
return false;
}

Decision o = (Decision) other;
return Objects.equals(getId(), o.getId())
&& Objects.equals(getCategory(), o.getCategory())
&& getTime() == o.getTime()
&& Objects.equals(getSource(), o.getSource())
&& Objects.equals(getDescription(), o.getDescription());
}
}
78 changes: 78 additions & 0 deletions src/main/java/com/siftscience/model/EntityScoreResponseBody.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.siftscience.model;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

import java.util.List;
import java.util.Map;

public class EntityScoreResponseBody extends BaseResponseBody<EntityScoreResponseBody> {
public static EntityScoreResponseBody fromJson(String json) {
return gson.fromJson(json, EntityScoreResponseBody.class);
}

@Expose @SerializedName("entity_type") private String entityType;
@Expose @SerializedName("entity_id") private String entityId;
@Expose @SerializedName("scores") private Map<String, AbuseScore> scores;
@Expose @SerializedName("latest_labels") private Map<String, Label> latestLabels;
@Expose @SerializedName("latest_decisions") private Map<String, Decision> latestDecisions;
@Expose @SerializedName("workflow_statuses") private List<WorkflowStatus> workflowStatuses;

public String getEntityType() {
return entityType;
}

public EntityScoreResponseBody setEntityType(String entityType) {
this.entityType = entityType;
return this;
}

public String getEntityId() {
return entityId;
}

public EntityScoreResponseBody setEntityId(String entityId) {
this.entityId = entityId;
return this;
}

public Map<String, AbuseScore> getScores() {
return scores;
}

public EntityScoreResponseBody setScores(Map<String, AbuseScore> scores) {
this.scores = scores;
return this;
}

public Map<String, Label> getLatestLabels() {
return latestLabels;
}

public EntityScoreResponseBody setLatestLabels(Map<String, Label> latestLabels) {
this.latestLabels = latestLabels;
return this;
}

public Map<String, Decision> getLatestDecisions() {
return latestDecisions;
}

public EntityScoreResponseBody setLatestDecisions(Map<String, Decision> latestDecisions) {
this.latestDecisions = latestDecisions;
return this;
}

public List<WorkflowStatus> getWorkflowStatuses() {
return workflowStatuses;
}

public EntityScoreResponseBody setWorkflowStatuses(List<WorkflowStatus> workflowStatuses) {
this.workflowStatuses = workflowStatuses;
return this;
}

public WorkflowStatus getWorkflowStatus(int i) {
return workflowStatuses.get(i);
}
}
41 changes: 41 additions & 0 deletions src/main/java/com/siftscience/model/UserScoreFieldSet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.siftscience.model;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import com.siftscience.FieldSet;

import java.util.List;

public class UserScoreFieldSet extends FieldSet<UserScoreFieldSet> {
@Expose @SerializedName(USER_ID) private String userId;
private List<String> abuseTypes;
private boolean rescoreUser = false;

public String getUserId() {
return userId;
}

public UserScoreFieldSet setUserId(String userId) {
this.userId = userId;
return this;
}

public List<String> getAbuseTypes() {
return abuseTypes;
}

public UserScoreFieldSet setAbuseTypes(List<String> abuseTypes) {
this.abuseTypes = abuseTypes;
return this;
}

public boolean getRescoreUser() {
return rescoreUser;
}

public UserScoreFieldSet setRescoreUser(boolean rescoreUser) {
this.rescoreUser = rescoreUser;
return this;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@ public void testContentDecisionStatus() throws Exception {
// Verify the response was parsed correctly.
Assert.assertEquals(HTTP_OK, siftResponse.getHttpStatusCode());
JSONAssert.assertEquals(response.getBody().readUtf8(),
siftResponse.getBody().toJson(), true);
siftResponse.getBody().toJson(), false);
}
}
4 changes: 2 additions & 2 deletions src/test/java/com/siftscience/DecisionStatusTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public void testDecisionStatus() throws Exception {
// Verify the response was parsed correctly.
Assert.assertEquals(HTTP_OK, siftResponse.getHttpStatusCode());
JSONAssert.assertEquals(response.getBody().readUtf8(),
siftResponse.getBody().toJson(), true);
siftResponse.getBody().toJson(), false);
}

@Test
Expand Down Expand Up @@ -136,6 +136,6 @@ public void testContentDecisionStatus() throws Exception {
// Verify the response was parsed correctly.
Assert.assertEquals(HTTP_OK, siftResponse.getHttpStatusCode());
JSONAssert.assertEquals(response.getBody().readUtf8(),
siftResponse.getBody().toJson(), true);
siftResponse.getBody().toJson(), false);
}
}
Loading