Skip to content

Commit d9a43f4

Browse files
[MSUE-123] - Added support for merchant management api (#87)
added support for merchant management api
1 parent 89fd81d commit d9a43f4

29 files changed

+1683
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.siftscience;
2+
3+
import okhttp3.HttpUrl;
4+
import okhttp3.OkHttpClient;
5+
import okhttp3.Request;
6+
import okhttp3.Response;
7+
import okhttp3.Credentials;
8+
9+
import java.io.IOException;
10+
11+
12+
public class CreateMerchantRequest extends SiftMerchantRequest<CreateMerchantResponse> {
13+
14+
CreateMerchantRequest(HttpUrl baseUrl, String accountId, OkHttpClient okClient, FieldSet fields) {
15+
super(baseUrl, accountId, okClient, fields);
16+
}
17+
18+
@Override
19+
protected HttpUrl path(HttpUrl baseUrl) {
20+
HttpUrl.Builder path = baseUrl.newBuilder("/v3/accounts")
21+
.addPathSegment(getAccountId())
22+
.addPathSegment("psp_management")
23+
.addPathSegment("merchants");
24+
return path.build();
25+
}
26+
27+
@Override
28+
CreateMerchantResponse buildResponse(Response response, FieldSet requestFields)
29+
throws IOException {
30+
return new CreateMerchantResponse(response, requestFields);
31+
}
32+
33+
@Override
34+
protected void modifyRequestBuilder(Request.Builder builder) {
35+
super.modifyRequestBuilder(builder);
36+
builder.header("Authorization", Credentials.basic(fieldSet.getApiKey(), ""));
37+
}
38+
39+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.siftscience;
2+
3+
import com.siftscience.model.CreateMerchantResponseBody;
4+
import okhttp3.Response;
5+
6+
import java.io.IOException;
7+
8+
import static com.siftscience.FieldSet.gson;
9+
10+
public class CreateMerchantResponse extends SiftMerchantResponse<CreateMerchantResponseBody> {
11+
12+
13+
public CreateMerchantResponse(Response okResponse, FieldSet requestBody) throws IOException{
14+
super(okResponse, requestBody);
15+
}
16+
17+
@Override
18+
void populateBodyFromJson(String jsonBody) {
19+
body = gson.fromJson(jsonBody, CreateMerchantResponseBody.class);
20+
}
21+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.siftscience;
2+
3+
import com.siftscience.model.GetMerchantFieldSet;
4+
import okhttp3.*;
5+
6+
import java.io.IOException;
7+
8+
9+
public class GetMerchantRequest extends SiftMerchantRequest<GetMerchantResponse> {
10+
11+
GetMerchantRequest(HttpUrl baseUrl, String accountId, OkHttpClient okClient, FieldSet fields) {
12+
super(baseUrl, accountId, okClient, fields);
13+
}
14+
15+
@Override
16+
protected HttpUrl path(HttpUrl baseUrl) {
17+
GetMerchantFieldSet fieldSet = (GetMerchantFieldSet) this.fieldSet;
18+
HttpUrl.Builder path = baseUrl.newBuilder("/v3/accounts")
19+
.addPathSegment(getAccountId())
20+
.addPathSegment("psp_management")
21+
.addPathSegment("merchants")
22+
.addPathSegment(fieldSet.getMerchantId());
23+
return path.build();
24+
}
25+
26+
@Override
27+
GetMerchantResponse buildResponse(Response response, FieldSet requestFields)
28+
throws IOException {
29+
return new GetMerchantResponse(response, requestFields);
30+
}
31+
32+
@Override
33+
protected void modifyRequestBuilder(Request.Builder builder) {
34+
super.modifyRequestBuilder(builder);
35+
builder.header("Authorization", Credentials.basic(fieldSet.getApiKey(), "")).get();
36+
}
37+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.siftscience;
2+
3+
import com.siftscience.model.CreateMerchantResponseBody;
4+
import okhttp3.Response;
5+
6+
import java.io.IOException;
7+
8+
import static com.siftscience.FieldSet.gson;
9+
10+
public class GetMerchantResponse extends SiftMerchantResponse<CreateMerchantResponseBody> {
11+
12+
public GetMerchantResponse(Response okResponse, FieldSet requestBody) throws IOException {
13+
super(okResponse, requestBody);
14+
}
15+
16+
@Override
17+
void populateBodyFromJson(String jsonBody) {
18+
body = gson.fromJson(jsonBody, CreateMerchantResponseBody.class);
19+
}
20+
}
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.GetMerchantsFieldSet;
4+
import okhttp3.*;
5+
6+
import java.io.IOException;
7+
8+
9+
public class GetMerchantsRequest extends SiftMerchantRequest<GetMerchantsResponse> {
10+
11+
GetMerchantsRequest(HttpUrl baseUrl, String accountId, OkHttpClient okClient, FieldSet fields) {
12+
super(baseUrl, accountId, okClient, fields);
13+
}
14+
15+
private static String DEFAULT_BATCH_SIZE = "1000";
16+
17+
public enum Query {
18+
BATCH_TOKEN("batch_token"),
19+
BATCH_SIZE("batch_size");
20+
21+
private final String value;
22+
23+
Query(String value) {
24+
this.value = value;
25+
}
26+
27+
@Override
28+
public String toString() {
29+
return value;
30+
}
31+
}
32+
33+
@Override
34+
protected HttpUrl path(HttpUrl baseUrl) {
35+
GetMerchantsFieldSet fieldSet = (GetMerchantsFieldSet) this.fieldSet;
36+
HttpUrl.Builder path = baseUrl.newBuilder("/v3/accounts")
37+
.addPathSegment(getAccountId())
38+
.addPathSegment("psp_management")
39+
.addPathSegment("merchants");
40+
41+
if (fieldSet.getBatchSize() != null) {
42+
path.addQueryParameter(Query.BATCH_SIZE.toString(), fieldSet.getBatchSize());
43+
} else {
44+
path.addQueryParameter(Query.BATCH_SIZE.toString(), DEFAULT_BATCH_SIZE);
45+
}
46+
47+
if (fieldSet.getBatchToken() != null) {
48+
path.addQueryParameter(Query.BATCH_TOKEN.toString(), String.valueOf(fieldSet.getBatchToken()));
49+
}
50+
51+
return path.build();
52+
}
53+
54+
@Override
55+
GetMerchantsResponse buildResponse(Response response, FieldSet requestFields)
56+
throws IOException {
57+
return new GetMerchantsResponse(response, requestFields);
58+
}
59+
60+
@Override
61+
protected void modifyRequestBuilder(Request.Builder builder) {
62+
super.modifyRequestBuilder(builder);
63+
builder.header("Authorization", Credentials.basic(fieldSet.getApiKey(), "")).get();
64+
}
65+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.siftscience;
2+
3+
import com.siftscience.model.GetMerchantsResponseBody;
4+
import okhttp3.Response;
5+
6+
import java.io.IOException;
7+
8+
import static com.siftscience.FieldSet.gson;
9+
10+
public class GetMerchantsResponse extends SiftMerchantResponse<GetMerchantsResponseBody> {
11+
12+
public GetMerchantsResponse(Response okResponse, FieldSet requestBody) throws IOException {
13+
super(okResponse, requestBody);
14+
}
15+
16+
@Override
17+
void populateBodyFromJson(String jsonBody) {
18+
body = gson.fromJson(jsonBody, GetMerchantsResponseBody.class);
19+
}
20+
}

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
import com.siftscience.model.UnlabelFieldSet;
99
import com.siftscience.model.UserScoreFieldSet;
1010
import com.siftscience.model.WorkflowStatusFieldSet;
11+
import com.siftscience.model.GetMerchantFieldSet;
12+
import com.siftscience.model.CreateMerchantFieldSet;
13+
import com.siftscience.model.UpdateMerchantFieldSet;
14+
import com.siftscience.model.GetMerchantsFieldSet;
1115
import okhttp3.HttpUrl;
1216
import okhttp3.OkHttpClient;
1317

@@ -118,6 +122,26 @@ public WorkflowStatusRequest buildRequest(WorkflowStatusFieldSet fields) {
118122
return new WorkflowStatusRequest(baseUrl, getAccountId(), okClient, fields);
119123
}
120124

125+
public GetMerchantsRequest buildRequest(GetMerchantsFieldSet fields) {
126+
setupApiKey(fields);
127+
return new GetMerchantsRequest(baseUrl, getAccountId(), okClient, fields);
128+
}
129+
130+
public GetMerchantRequest buildRequest(GetMerchantFieldSet fields) {
131+
setupApiKey(fields);
132+
return new GetMerchantRequest(baseUrl, getAccountId(), okClient, fields);
133+
}
134+
135+
public CreateMerchantRequest buildRequest(CreateMerchantFieldSet fields) {
136+
setupApiKey(fields);
137+
return new CreateMerchantRequest(baseUrl, getAccountId(), okClient, fields);
138+
}
139+
140+
public UpdateMerchantRequest buildRequest(UpdateMerchantFieldSet fields, String merchantId) {
141+
setupApiKey(fields);
142+
return new UpdateMerchantRequest(baseUrl, getAccountId(), okClient, fields, merchantId);
143+
}
144+
121145
private void setupApiKey(FieldSet fields) {
122146
fields.setApiKey(getApiKey());
123147
}
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.exception.MerchantAPIException;
4+
import okhttp3.HttpUrl;
5+
import okhttp3.OkHttpClient;
6+
import okhttp3.Request;
7+
import okhttp3.Response;
8+
import okhttp3.Credentials;
9+
import okhttp3.RequestBody;
10+
import okhttp3.MediaType;
11+
12+
import java.io.IOException;
13+
14+
public abstract class SiftMerchantRequest<T extends SiftMerchantResponse> {
15+
private final String accountId;
16+
FieldSet fieldSet;
17+
private OkHttpClient okClient;
18+
private HttpUrl baseUrl;
19+
20+
protected abstract HttpUrl path(HttpUrl baseUrl);
21+
22+
public HttpUrl url() {
23+
return path(baseUrl);
24+
}
25+
26+
SiftMerchantRequest(HttpUrl baseUrl, String accountId, OkHttpClient okClient, FieldSet fields) {
27+
this.baseUrl = baseUrl;
28+
this.accountId = accountId;
29+
this.okClient = okClient;
30+
this.fieldSet = fields;
31+
}
32+
33+
/**
34+
* By default, the request is a JSON encoded POST.
35+
*/
36+
protected void modifyRequestBuilder(Request.Builder builder) {
37+
builder.header("Authorization", Credentials.basic(fieldSet.getApiKey(), "")).get();
38+
builder.post(RequestBody.create(MediaType.parse("application/json"), fieldSet.toJson()));
39+
}
40+
41+
abstract T buildResponse(Response response, FieldSet requestFields) throws IOException;
42+
43+
public T send() throws IOException {
44+
fieldSet.validate();
45+
46+
Request.Builder okRequestBuilder = new Request.Builder().url(this.url());
47+
modifyRequestBuilder(okRequestBuilder);
48+
Request request = okRequestBuilder.build();
49+
T response = buildResponse(okClient.newCall(request).execute(), fieldSet);
50+
51+
if (!response.isOk()) {
52+
throw new MerchantAPIException(response.getApiErrorMessage());
53+
}
54+
55+
return response;
56+
}
57+
58+
public FieldSet getFieldSet() {
59+
return fieldSet;
60+
}
61+
62+
protected String getAccountId() {
63+
return accountId;
64+
}
65+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.siftscience;
2+
3+
import com.google.gson.JsonSyntaxException;
4+
import com.siftscience.FieldSet;
5+
import com.siftscience.model.MerchantBaseResponseBody;
6+
import okhttp3.Response;
7+
import okhttp3.ResponseBody;
8+
9+
import java.io.IOException;
10+
11+
/**
12+
* It is a simple wrapper around the actual OkHttp response.
13+
*/
14+
public abstract class SiftMerchantResponse<T extends MerchantBaseResponseBody<T>> {
15+
private Response okResponse;
16+
private int time;
17+
private FieldSet requestBody;
18+
T body;
19+
20+
SiftMerchantResponse(Response okResponse, FieldSet requestBody) throws IOException {
21+
this.okResponse = okResponse;
22+
this.requestBody = requestBody;
23+
24+
ResponseBody rspBody = okResponse.body();
25+
if (rspBody != null) {
26+
String bodyString = rspBody.string();
27+
if (!bodyString.isEmpty()) {
28+
try {
29+
populateBodyFromJson(bodyString);
30+
} catch (JsonSyntaxException e) {
31+
// If parsing the response body as JSON failed for a 5xx, ignore the parse
32+
// exception.
33+
int code = okResponse.code();
34+
if (code < 500 || code >= 600) {
35+
throw e;
36+
}
37+
}
38+
}
39+
}
40+
}
41+
42+
abstract void populateBodyFromJson(String jsonBody);
43+
44+
public int getHttpStatusCode() {
45+
return okResponse.code();
46+
}
47+
48+
public T getBody() {
49+
return body;
50+
}
51+
52+
public int getTime() {
53+
return time;
54+
}
55+
56+
SiftMerchantResponse setTime(int time) {
57+
this.time = time;
58+
return this;
59+
}
60+
61+
public FieldSet getRequestBody() {
62+
return requestBody;
63+
}
64+
65+
public boolean isOk() {
66+
return okResponse != null && okResponse.isSuccessful();
67+
}
68+
69+
public String getApiErrorMessage() {
70+
if (body != null) {
71+
return body.getError();
72+
}
73+
return null;
74+
}
75+
76+
}

0 commit comments

Comments
 (0)