Skip to content

Commit 8a63c4e

Browse files
author
Christopher Eteka
committed
Restructure to return execution spec
1 parent 5be4fb7 commit 8a63c4e

34 files changed

Lines changed: 859 additions & 334 deletions

‎README.md‎

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,38 +24,43 @@ class Example {
2424
.buildPaystackClientFrom("<Your secret key here>");
2525

2626
//Synchronous
27-
RichResponse<PlanResponse.Single> syncRes = client.synchronous()
27+
RichResponse<PlanResponse.Single> syncRes = client
2828
.plan()
29-
.create(new CreatePlanRequest("Sample Plan 9", Interval.DAILY,
30-
Amount.actualValue(BigDecimal.valueOf(10_000)).ofCurrency(Currency.NGN)));
29+
.createPlan(new CreatePlanRequest("Sample Plan 9", Interval.DAILY,
30+
Amount.actualValue(BigDecimal.valueOf(10_000)).ofCurrency(Currency.NGN)))
31+
.execute(); //sync call is made at this point
3132

3233
//fetch plans with query param
33-
RichResponse<PlanResponse.Multiple> res = client.synchronous()
34+
RichResponse<PlanResponse.Multiple> res = client
3435
.plan()
35-
.fetchMultiple(new PlanListQueryParam(BigInteger.TEN, BigInteger.ONE)
36+
.listPlans(new PlanListQueryParam(BigInteger.TEN, BigInteger.ONE)
3637
.amount(Amount.actualValue(BigDecimal.valueOf(100_000)).ofCurrency(Currency.NGN))
3738
.interval(Interval.BIANNUALLY)
38-
.status("approved"));
39+
.status("approved"))
40+
.execute();
3941

4042
//fetch plans without query param
41-
RichResponse<PlanResponse.Multiple> res = client.synchronous()
43+
RichResponse<PlanResponse.Multiple> res = client
4244
.plan()
43-
.fetchMultiple(null);
45+
.listPlans(null)
46+
.execute();
4447

4548
//fetch single plan
46-
RichResponse<PlanResponse.Single> res = client.synchronous()
49+
RichResponse<PlanResponse.Single> res = client
4750
.plan()
48-
.fetchByIdOrCode("id001");
51+
.fetchPlan("id001")
52+
.execute();
4953

5054
String json = res.raw();
5155
PlanResponse.Multiple result = res.result();
5256
Map<String, Object> objectMap = res.rawJsonAsMap();
5357

5458
//Asynchronous
55-
CompletableFuture<RichResponse<PlanResponse.Single>> asyncRes = client.asynchronous()
59+
CompletableFuture<RichResponse<PlanResponse.Single>> asyncRes = client
5660
.plan()
57-
.create(new CreatePlanRequest("Sample Plan 9", Interval.ANNUALLY,
58-
Amount.actualValue(BigDecimal.valueOf(1_000_000)).ofCurrency(Currency.NGN)));
61+
.createPlan(new CreatePlanRequest("Sample Plan 9", Interval.ANNUALLY,
62+
Amount.actualValue(BigDecimal.valueOf(1_000_000)).ofCurrency(Currency.NGN)))
63+
.executeAsync();
5964
}
6065
}
6166
```
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.chrisworks.paystackclient;
2+
3+
import com.chrisworks.paystackclient.domain.PaystackException;
4+
import com.chrisworks.paystackclient.domain.response.RichResponse;
5+
import okhttp3.*;
6+
import org.jetbrains.annotations.NotNull;
7+
8+
import java.io.IOException;
9+
import java.util.concurrent.CompletableFuture;
10+
11+
import static com.chrisworks.paystackclient.domain.SerialisationUtil.objectMapper;
12+
13+
public final class ExecutionSpec<T> {
14+
15+
private final Request req;
16+
private final Class<T> retType;
17+
private final OkHttpClient httpClient;
18+
19+
ExecutionSpec(Request req, Class<T> retType, OkHttpClient httpClient) {
20+
this.req = req;
21+
this.retType = retType;
22+
this.httpClient = httpClient;
23+
}
24+
25+
public RichResponse<T> execute() {
26+
try(final Response response = httpClient.newCall(req).execute()) {
27+
final String responseBody = response.body().string();
28+
if (response.isSuccessful()) {
29+
return new RichResponse.RichResponseImpl<>(objectMapper.readValue(responseBody, retType), responseBody);
30+
}
31+
throw new PaystackException(
32+
objectMapper.readValue(responseBody, PaystackException.Model.class), response.code());
33+
} catch (IOException e) {
34+
throw new PaystackException(e);
35+
}
36+
}
37+
38+
public CompletableFuture<RichResponse<T>> executeAsync() {
39+
final CompletableFuture<RichResponse<T>> future = new CompletableFuture<>();
40+
httpClient.newCall(req).enqueue(new Callback() {
41+
@Override
42+
public void onFailure(@NotNull Call call, @NotNull IOException e) {
43+
future.completeExceptionally(e);
44+
}
45+
@Override
46+
public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
47+
final String responseBody = response.body().string();
48+
if (response.isSuccessful()) {
49+
future.complete(new RichResponse.RichResponseImpl<>(objectMapper.readValue(responseBody, retType), responseBody));
50+
}
51+
else {
52+
future.completeExceptionally(new PaystackException(
53+
objectMapper.readValue(responseBody, PaystackException.Model.class), response.code()));
54+
}
55+
response.close();
56+
}
57+
});
58+
59+
httpClient.dispatcher().executorService().shutdown();
60+
61+
return future;
62+
}
63+
}
Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,34 @@
11
package com.chrisworks.paystackclient;
22

3-
import com.chrisworks.paystackclient.asynchronous.Asynchronous;
4-
import com.chrisworks.paystackclient.synchronous.Synchronous;
3+
import com.chrisworks.paystackclient.definitions.ApplePayClient;
4+
import com.chrisworks.paystackclient.definitions.PlanClient;
5+
import com.chrisworks.paystackclient.definitions.TransactionClient;
56
import okhttp3.OkHttpClient;
67

78
public interface PaystackClient {
89

9-
Synchronous synchronous();
10-
Asynchronous asynchronous();
10+
ApplePayClient applePay();
11+
PlanClient plan();
12+
TransactionClient transaction();
1113

1214
final class Impl extends WithConfiguredHttpClient implements PaystackClient {
1315
Impl(OkHttpClient httpClient) {
1416
super(httpClient);
1517
}
1618

1719
@Override
18-
public Synchronous synchronous() {
19-
return new Synchronous.Impl(httpClient);
20+
public ApplePayClient applePay() {
21+
return new ApplePayClient.Impl(httpClient);
2022
}
2123

2224
@Override
23-
public Asynchronous asynchronous() {
24-
return new Asynchronous.Impl(httpClient);
25+
public PlanClient plan() {
26+
return new PlanClient.Impl(httpClient);
27+
}
28+
29+
@Override
30+
public TransactionClient transaction() {
31+
return new TransactionClient.Impl(httpClient);
2532
}
2633
}
2734
}

‎paystack-clients/src/main/java/com/chrisworks/paystackclient/PaystackClientConfiguration.java‎

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
package com.chrisworks.paystackclient;
22

3-
import com.chrisworks.paystackclient.domain.Amount;
4-
import com.chrisworks.paystackclient.domain.Currency;
5-
import com.chrisworks.paystackclient.domain.Interval;
3+
64
import com.chrisworks.paystackclient.domain.PaystackException;
7-
import com.chrisworks.paystackclient.domain.plan.CreatePlanRequest;
8-
import com.chrisworks.paystackclient.domain.plan.PlanListQueryParam;
9-
import com.chrisworks.paystackclient.domain.plan.PlanResponse;
105
import com.chrisworks.paystackclient.domain.response.RichResponse;
6+
import com.chrisworks.paystackclient.domain.transaction.TransactionResponse;
7+
import com.chrisworks.paystackclient.domain.transaction.TransactionTotalResponse;
118
import okhttp3.Interceptor;
129
import okhttp3.OkHttpClient;
1310
import okhttp3.Response;
@@ -17,13 +14,11 @@
1714
import javax.net.ssl.TrustManager;
1815
import javax.net.ssl.X509TrustManager;
1916
import java.io.IOException;
20-
import java.math.BigDecimal;
2117
import java.math.BigInteger;
2218
import java.security.KeyManagementException;
2319
import java.security.NoSuchAlgorithmException;
2420
import java.security.cert.X509Certificate;
2521
import java.time.Duration;
26-
import java.util.Map;
2722
import java.util.function.Function;
2823

2924
public class PaystackClientConfiguration {
@@ -94,17 +89,35 @@ public X509Certificate[] getAcceptedIssuers() {
9489

9590
public static void main(String[] args) {
9691
//Usage sample
97-
final PaystackClient client = PaystackClientConfiguration.buildPaystackClientFrom("sk_test_c33ddd838c1dfd9cf30d983a1238224172e702a4");
98-
client.synchronous();
99-
// RichResponse<PlanResponse.Single> res1 = client.synchronous()
92+
final PaystackClient client = PaystackClientConfiguration.buildPaystackClientFrom("<Your secret key here>");
93+
// RichResponse<PlanResponse.Single> res1 = client
10094
// .plan()
101-
// .create(new CreatePlanRequest("Sample Plan 9", Interval.DAILY,
102-
// Amount.actualValue(BigDecimal.valueOf(10_000)).ofCurrency(Currency.NGN)));
95+
// .createPlan(new CreatePlanRequest("Sample Plan 9", Interval.DAILY,
96+
// Amount.actualValue(BigDecimal.valueOf(10_000)).ofCurrency(Currency.NGN)))
97+
// .execute();
10398
// System.out.println(res1);
10499

105-
RichResponse<PlanResponse.Single> res = client.synchronous()
106-
.plan()
107-
.fetchByIdOrCode("PLN_16x08vyy3bu2h7x");
100+
// Void res = client
101+
// .plan()
102+
// .fetchPlan("PLN_16x08vyy3bu2h7x")
103+
// .executeAsync()
104+
// .thenAccept(re -> System.out.println(re.result()))
105+
// .join();
106+
107+
RichResponse<TransactionResponse.Single> transactions = client
108+
.transaction()
109+
.fetchTransaction(BigInteger.valueOf(3200117924L))
110+
.execute();
111+
112+
RichResponse<TransactionTotalResponse.Single> total = client.transaction().transactionTotals(null).execute();
113+
114+
115+
// RichResponse<PlanResponse.Single> res2 = client
116+
// .plan()
117+
// .fetchPlan("PLN_16x08vyy3bu2h7x")
118+
// .execute();
119+
System.out.println(transactions);
120+
System.out.println(total);
108121

109122
// String json = res.raw();
110123
// PlanResponse.Multiple result = res.result();
Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,10 @@
11
package com.chrisworks.paystackclient;
22

3-
import com.chrisworks.paystackclient.domain.PaystackException;
43
import com.chrisworks.paystackclient.domain.request.QueryParamBuilder;
5-
import com.chrisworks.paystackclient.domain.response.RichResponse;
6-
import okhttp3.MediaType;
7-
import okhttp3.OkHttpClient;
8-
import okhttp3.Request;
9-
import okhttp3.Response;
4+
import okhttp3.*;
105

11-
import java.io.IOException;
126
import java.util.Optional;
137

14-
import static com.chrisworks.paystackclient.domain.SerialisationUtil.objectMapper;
15-
168
public abstract class WithConfiguredHttpClient {
179

1810
protected final MediaType applicationJson = MediaType.parse("application/json");
@@ -27,16 +19,7 @@ protected <T extends QueryParamBuilder> String safeExtractQueryParams(T queryPar
2719
.orElse("");
2820
}
2921

30-
protected <T> RichResponse<T> execute(Request request, Class<T> responseType) {
31-
try(final Response response = httpClient.newCall(request).execute()) {
32-
final String responseBody = response.body().string();
33-
if (response.isSuccessful()) {
34-
return new RichResponse.Impl<>(objectMapper.readValue(responseBody, responseType), responseBody);
35-
}
36-
throw new PaystackException(
37-
objectMapper.readValue(responseBody, PaystackException.Model.class), response.code());
38-
} catch (IOException e) {
39-
throw new PaystackException(e);
40-
}
22+
protected <T> ExecutionSpec<T> execSpec(Request request, Class<T> returnType) {
23+
return new ExecutionSpec<>(request, returnType, httpClient);
4124
}
4225
}

‎paystack-clients/src/main/java/com/chrisworks/paystackclient/asynchronous/Asynchronous.java‎

Lines changed: 0 additions & 28 deletions
This file was deleted.

‎paystack-clients/src/main/java/com/chrisworks/paystackclient/asynchronous/definitions/ApplePlayClient.java‎

Lines changed: 0 additions & 44 deletions
This file was deleted.

‎paystack-clients/src/main/java/com/chrisworks/paystackclient/asynchronous/definitions/Operations.java‎

Lines changed: 0 additions & 43 deletions
This file was deleted.

0 commit comments

Comments
 (0)