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
39 changes: 39 additions & 0 deletions src/main/java/com/siftscience/CreateMerchantRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.siftscience;

import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.Credentials;

import java.io.IOException;


public class CreateMerchantRequest extends SiftMerchantRequest<CreateMerchantResponse> {

CreateMerchantRequest(HttpUrl baseUrl, String accountId, OkHttpClient okClient, FieldSet fields) {
super(baseUrl, accountId, okClient, fields);
}

@Override
protected HttpUrl path(HttpUrl baseUrl) {
HttpUrl.Builder path = baseUrl.newBuilder("/v3/accounts")
.addPathSegment(getAccountId())
.addPathSegment("psp_management")
.addPathSegment("merchants");
return path.build();
}

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

@Override
protected void modifyRequestBuilder(Request.Builder builder) {
super.modifyRequestBuilder(builder);
builder.header("Authorization", Credentials.basic(fieldSet.getApiKey(), ""));
}

}
21 changes: 21 additions & 0 deletions src/main/java/com/siftscience/CreateMerchantResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.siftscience;

import com.siftscience.model.CreateMerchantResponseBody;
import okhttp3.Response;

import java.io.IOException;

import static com.siftscience.FieldSet.gson;

public class CreateMerchantResponse extends SiftMerchantResponse<CreateMerchantResponseBody> {


public CreateMerchantResponse(Response okResponse, FieldSet requestBody) throws IOException{
super(okResponse, requestBody);
}

@Override
void populateBodyFromJson(String jsonBody) {
body = gson.fromJson(jsonBody, CreateMerchantResponseBody.class);
}
}
37 changes: 37 additions & 0 deletions src/main/java/com/siftscience/GetMerchantRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.siftscience;

import com.siftscience.model.GetMerchantFieldSet;
import okhttp3.*;

import java.io.IOException;


public class GetMerchantRequest extends SiftMerchantRequest<GetMerchantResponse> {

GetMerchantRequest(HttpUrl baseUrl, String accountId, OkHttpClient okClient, FieldSet fields) {
super(baseUrl, accountId, okClient, fields);
}

@Override
protected HttpUrl path(HttpUrl baseUrl) {
GetMerchantFieldSet fieldSet = (GetMerchantFieldSet) this.fieldSet;
HttpUrl.Builder path = baseUrl.newBuilder("/v3/accounts")
.addPathSegment(getAccountId())
.addPathSegment("psp_management")
.addPathSegment("merchants")
.addPathSegment(fieldSet.getMerchantId());
return path.build();
}

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

@Override
protected void modifyRequestBuilder(Request.Builder builder) {
super.modifyRequestBuilder(builder);
builder.header("Authorization", Credentials.basic(fieldSet.getApiKey(), "")).get();
}
}
20 changes: 20 additions & 0 deletions src/main/java/com/siftscience/GetMerchantResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.siftscience;

import com.siftscience.model.CreateMerchantResponseBody;
import okhttp3.Response;

import java.io.IOException;

import static com.siftscience.FieldSet.gson;

public class GetMerchantResponse extends SiftMerchantResponse<CreateMerchantResponseBody> {

public GetMerchantResponse(Response okResponse, FieldSet requestBody) throws IOException {
super(okResponse, requestBody);
}

@Override
void populateBodyFromJson(String jsonBody) {
body = gson.fromJson(jsonBody, CreateMerchantResponseBody.class);
}
}
65 changes: 65 additions & 0 deletions src/main/java/com/siftscience/GetMerchantsRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.siftscience;

import com.siftscience.model.GetMerchantsFieldSet;
import okhttp3.*;

import java.io.IOException;


public class GetMerchantsRequest extends SiftMerchantRequest<GetMerchantsResponse> {

GetMerchantsRequest(HttpUrl baseUrl, String accountId, OkHttpClient okClient, FieldSet fields) {
super(baseUrl, accountId, okClient, fields);
}

private static String DEFAULT_BATCH_SIZE = "1000";

public enum Query {
BATCH_TOKEN("batch_token"),
BATCH_SIZE("batch_size");

private final String value;

Query(String value) {
this.value = value;
}

@Override
public String toString() {
return value;
}
}

@Override
protected HttpUrl path(HttpUrl baseUrl) {
GetMerchantsFieldSet fieldSet = (GetMerchantsFieldSet) this.fieldSet;
HttpUrl.Builder path = baseUrl.newBuilder("/v3/accounts")
.addPathSegment(getAccountId())
.addPathSegment("psp_management")
.addPathSegment("merchants");

if (fieldSet.getBatchSize() != null) {
path.addQueryParameter(Query.BATCH_SIZE.toString(), fieldSet.getBatchSize());
} else {
path.addQueryParameter(Query.BATCH_SIZE.toString(), DEFAULT_BATCH_SIZE);
}

if (fieldSet.getBatchToken() != null) {
path.addQueryParameter(Query.BATCH_TOKEN.toString(), String.valueOf(fieldSet.getBatchToken()));
}

return path.build();
}

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

@Override
protected void modifyRequestBuilder(Request.Builder builder) {
super.modifyRequestBuilder(builder);
builder.header("Authorization", Credentials.basic(fieldSet.getApiKey(), "")).get();
}
}
20 changes: 20 additions & 0 deletions src/main/java/com/siftscience/GetMerchantsResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.siftscience;

import com.siftscience.model.GetMerchantsResponseBody;
import okhttp3.Response;

import java.io.IOException;

import static com.siftscience.FieldSet.gson;

public class GetMerchantsResponse extends SiftMerchantResponse<GetMerchantsResponseBody> {

public GetMerchantsResponse(Response okResponse, FieldSet requestBody) throws IOException {
super(okResponse, requestBody);
}

@Override
void populateBodyFromJson(String jsonBody) {
body = gson.fromJson(jsonBody, GetMerchantsResponseBody.class);
}
}
24 changes: 24 additions & 0 deletions src/main/java/com/siftscience/SiftClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
import com.siftscience.model.UnlabelFieldSet;
import com.siftscience.model.UserScoreFieldSet;
import com.siftscience.model.WorkflowStatusFieldSet;
import com.siftscience.model.GetMerchantFieldSet;
import com.siftscience.model.CreateMerchantFieldSet;
import com.siftscience.model.UpdateMerchantFieldSet;
import com.siftscience.model.GetMerchantsFieldSet;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;

Expand Down Expand Up @@ -118,6 +122,26 @@ public WorkflowStatusRequest buildRequest(WorkflowStatusFieldSet fields) {
return new WorkflowStatusRequest(baseUrl, getAccountId(), okClient, fields);
}

public GetMerchantsRequest buildRequest(GetMerchantsFieldSet fields) {
setupApiKey(fields);
return new GetMerchantsRequest(baseUrl, getAccountId(), okClient, fields);
}

public GetMerchantRequest buildRequest(GetMerchantFieldSet fields) {
setupApiKey(fields);
return new GetMerchantRequest(baseUrl, getAccountId(), okClient, fields);
}

public CreateMerchantRequest buildRequest(CreateMerchantFieldSet fields) {
setupApiKey(fields);
return new CreateMerchantRequest(baseUrl, getAccountId(), okClient, fields);
}

public UpdateMerchantRequest buildRequest(UpdateMerchantFieldSet fields, String merchantId) {
setupApiKey(fields);
return new UpdateMerchantRequest(baseUrl, getAccountId(), okClient, fields, merchantId);
}

private void setupApiKey(FieldSet fields) {
fields.setApiKey(getApiKey());
}
Expand Down
65 changes: 65 additions & 0 deletions src/main/java/com/siftscience/SiftMerchantRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.siftscience;

import com.siftscience.exception.MerchantAPIException;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.Credentials;
import okhttp3.RequestBody;
import okhttp3.MediaType;

import java.io.IOException;

public abstract class SiftMerchantRequest<T extends SiftMerchantResponse> {
private final String accountId;
FieldSet fieldSet;
private OkHttpClient okClient;
private HttpUrl baseUrl;

protected abstract HttpUrl path(HttpUrl baseUrl);

public HttpUrl url() {
return path(baseUrl);
}

SiftMerchantRequest(HttpUrl baseUrl, String accountId, OkHttpClient okClient, FieldSet fields) {
this.baseUrl = baseUrl;
this.accountId = accountId;
this.okClient = okClient;
this.fieldSet = fields;
}

/**
* By default, the request is a JSON encoded POST.
*/
protected void modifyRequestBuilder(Request.Builder builder) {
builder.header("Authorization", Credentials.basic(fieldSet.getApiKey(), "")).get();
builder.post(RequestBody.create(MediaType.parse("application/json"), fieldSet.toJson()));
}

abstract T buildResponse(Response response, FieldSet requestFields) throws IOException;

public T send() throws IOException {
fieldSet.validate();

Request.Builder okRequestBuilder = new Request.Builder().url(this.url());
modifyRequestBuilder(okRequestBuilder);
Request request = okRequestBuilder.build();
T response = buildResponse(okClient.newCall(request).execute(), fieldSet);

if (!response.isOk()) {
throw new MerchantAPIException(response.getApiErrorMessage());
}

return response;
}

public FieldSet getFieldSet() {
return fieldSet;
}

protected String getAccountId() {
return accountId;
}
}
76 changes: 76 additions & 0 deletions src/main/java/com/siftscience/SiftMerchantResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.siftscience;

import com.google.gson.JsonSyntaxException;
import com.siftscience.FieldSet;
import com.siftscience.model.MerchantBaseResponseBody;
import okhttp3.Response;
import okhttp3.ResponseBody;

import java.io.IOException;

/**
* It is a simple wrapper around the actual OkHttp response.
*/
public abstract class SiftMerchantResponse<T extends MerchantBaseResponseBody<T>> {
private Response okResponse;
private int time;
private FieldSet requestBody;
T body;

SiftMerchantResponse(Response okResponse, FieldSet requestBody) throws IOException {
this.okResponse = okResponse;
this.requestBody = requestBody;

ResponseBody rspBody = okResponse.body();
if (rspBody != null) {
String bodyString = rspBody.string();
if (!bodyString.isEmpty()) {
try {
populateBodyFromJson(bodyString);
} catch (JsonSyntaxException e) {
// If parsing the response body as JSON failed for a 5xx, ignore the parse
// exception.
int code = okResponse.code();
if (code < 500 || code >= 600) {
throw e;
}
}
}
}
}

abstract void populateBodyFromJson(String jsonBody);

public int getHttpStatusCode() {
return okResponse.code();
}

public T getBody() {
return body;
}

public int getTime() {
return time;
}

SiftMerchantResponse setTime(int time) {
this.time = time;
return this;
}

public FieldSet getRequestBody() {
return requestBody;
}

public boolean isOk() {
return okResponse != null && okResponse.isSuccessful();
}

public String getApiErrorMessage() {
if (body != null) {
return body.getError();
}
return null;
}

}
Loading