Skip to content

Commit 86780de

Browse files
committed
factory method for class creation
1 parent a90b307 commit 86780de

9 files changed

+285
-77
lines changed

.idea/inspectionProfiles/Project_Default.xml

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/jpa-buddy.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/uiDesigner.xml

+124
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
plugins {
22
id 'java'
3+
id 'maven-publish'
34
}
45

56
group = 'ru.chrshnv'
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,20 @@
11
package ru.chrshnv.enotiosdk.client;
22

3-
import com.google.gson.Gson;
4-
import com.google.gson.reflect.TypeToken;
53
import ru.chrshnv.enotiosdk.invoice.InvoiceInfoResponse;
6-
import ru.chrshnv.enotiosdk.invoice.InvoiceRequest;
74
import ru.chrshnv.enotiosdk.invoice.InvoiceResponse;
5+
import ru.chrshnv.enotiosdk.other.Currency;
86
import ru.chrshnv.enotiosdk.other.Response;
97
import ru.chrshnv.enotiosdk.tariffs.Tariffs;
108

119
import java.io.IOException;
12-
import java.lang.reflect.Type;
1310
import java.net.URI;
1411
import java.net.URISyntaxException;
15-
import java.net.http.HttpClient;
16-
import java.net.http.HttpRequest;
17-
import java.net.http.HttpResponse;
12+
import java.util.List;
1813
import java.util.UUID;
1914

20-
public class EnotIoSDKClient {
21-
private String apiKey;
22-
private final String enotUrl = "https://api.enot.io/";
23-
private final HttpClient httpClient = HttpClient.newHttpClient();
24-
private final Gson gson;
25-
26-
public EnotIoSDKClient(String apiKey, Gson gson) {
27-
this.apiKey = apiKey;
28-
this.gson = gson;
29-
}
30-
31-
public Response<Tariffs> getTariffs(UUID shopId) throws URISyntaxException, IOException, InterruptedException {
32-
URI url = new URI(enotUrl + "/shops/" + shopId + "/payment-tariffs");
33-
HttpRequest req = HttpRequest
34-
.newBuilder()
35-
.uri(url)
36-
.header("Content-Type", "application/json")
37-
.header("x-api-key", apiKey)
38-
.GET()
39-
.build();
40-
41-
HttpResponse<String> stringHttpResponse = httpClient.send(req, HttpResponse.BodyHandlers.ofString());
42-
Type responseType = TypeToken.getParameterized(Response.class, Tariffs.class).getType();
43-
44-
return gson.fromJson(stringHttpResponse.body(), responseType);
45-
}
46-
47-
public Response<InvoiceResponse> createInvoice(InvoiceRequest request) throws IOException, InterruptedException, URISyntaxException {
48-
String body = gson.toJson(request);
49-
50-
URI url = new URI(enotUrl + "/invoice/create");
51-
HttpRequest req = HttpRequest
52-
.newBuilder()
53-
.uri(url)
54-
.header("Content-Type", "application/json")
55-
.header("x-api-key", apiKey)
56-
.POST(HttpRequest.BodyPublishers.ofString(body))
57-
.build();
58-
59-
HttpResponse<String> stringHttpResponse = httpClient.send(req, HttpResponse.BodyHandlers.ofString());
60-
Type responseType = TypeToken.getParameterized(Response.class, InvoiceResponse.class).getType();
61-
62-
return gson.fromJson(stringHttpResponse.body(), responseType);
63-
}
64-
65-
public Response<InvoiceInfoResponse> invoiceInfo(UUID shopId, UUID invoiceId, String orderId) throws URISyntaxException, IOException, InterruptedException {
66-
URI url = new URI(enotUrl + "/invoice/info?invoice_id=" + invoiceId + "&shop_id=" + shopId + "&order_id=" + orderId);
67-
68-
HttpRequest req = HttpRequest
69-
.newBuilder()
70-
.uri(url)
71-
.header("Content-Type", "application/json")
72-
.header("x-api-key", apiKey)
73-
.GET()
74-
.build();
75-
76-
HttpResponse<String> stringHttpResponse = httpClient.send(req, HttpResponse.BodyHandlers.ofString());
77-
Type responseType = TypeToken.getParameterized(Response.class, InvoiceInfoResponse.class).getType();
78-
79-
return gson.fromJson(stringHttpResponse.body(), responseType);
80-
}
15+
public interface EnotIoSDKClient {
16+
Response<Tariffs> getTariffs() throws URISyntaxException, IOException, InterruptedException;
17+
Response<InvoiceResponse> createInvoice(Double amount, String orderId) throws URISyntaxException, IOException, InterruptedException;
18+
Response<InvoiceResponse> createInvoice(Double amount, String orderId, Currency currency, String hookUrl, String customFields, String comment, URI failUrl, URI successUrl, Integer expire, List<String> includeService, List<String> excludeService) throws URISyntaxException, IOException, InterruptedException;
19+
Response<InvoiceInfoResponse> invoiceInfo(UUID shopId, UUID invoiceId, String orderId) throws URISyntaxException, IOException, InterruptedException;
8120
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package ru.chrshnv.enotiosdk.client;
2+
3+
import java.util.UUID;
4+
5+
public interface EnotIoSDKClientFactory {
6+
EnotIoSDKClient create(String apiKey, UUID shopId);
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package ru.chrshnv.enotiosdk.client.impl;
2+
3+
import com.google.gson.Gson;
4+
import com.google.gson.GsonBuilder;
5+
import com.google.gson.reflect.TypeToken;
6+
import ru.chrshnv.enotiosdk.client.EnotIoSDKClient;
7+
import ru.chrshnv.enotiosdk.invoice.InvoiceInfoResponse;
8+
import ru.chrshnv.enotiosdk.invoice.InvoiceRequest;
9+
import ru.chrshnv.enotiosdk.invoice.InvoiceResponse;
10+
import ru.chrshnv.enotiosdk.other.Currency;
11+
import ru.chrshnv.enotiosdk.other.Response;
12+
import ru.chrshnv.enotiosdk.tariffs.Tariffs;
13+
14+
import java.io.IOException;
15+
import java.lang.reflect.Type;
16+
import java.net.URI;
17+
import java.net.URISyntaxException;
18+
import java.net.http.HttpClient;
19+
import java.net.http.HttpRequest;
20+
import java.net.http.HttpResponse;
21+
import java.util.List;
22+
import java.util.UUID;
23+
24+
public class GsonEnotIoSDKClient implements EnotIoSDKClient {
25+
private final String apiKey;
26+
private final UUID shopId;
27+
private final String enotUrl = "https://api.enot.io/";
28+
private final HttpClient httpClient = HttpClient.newHttpClient();
29+
private final Gson gson = new GsonBuilder().create();
30+
31+
public GsonEnotIoSDKClient(String apiKey, UUID shopId) {
32+
this.apiKey = apiKey;
33+
this.shopId = shopId;
34+
}
35+
36+
@Override
37+
public Response<Tariffs> getTariffs() throws URISyntaxException, IOException, InterruptedException {
38+
URI url = new URI(enotUrl + "/shops/" + shopId + "/payment-tariffs");
39+
HttpRequest req = HttpRequest
40+
.newBuilder()
41+
.uri(url)
42+
.header("Content-Type", "application/json")
43+
.header("x-api-key", apiKey)
44+
.GET()
45+
.build();
46+
47+
HttpResponse<String> stringHttpResponse = httpClient.send(req, HttpResponse.BodyHandlers.ofString());
48+
Type responseType = TypeToken.getParameterized(Response.class, Tariffs.class).getType();
49+
50+
return gson.fromJson(stringHttpResponse.body(), responseType);
51+
}
52+
53+
@Override
54+
public Response<InvoiceResponse> createInvoice(Double amount, String orderId) throws URISyntaxException, IOException, InterruptedException {
55+
InvoiceRequest build = InvoiceRequest.builder()
56+
.setAmount(amount)
57+
.setOrderId(orderId)
58+
.setShopId(shopId)
59+
.build();
60+
61+
return getInvoiceResponseResponse(build);
62+
}
63+
64+
@Override
65+
public Response<InvoiceResponse> createInvoice(Double amount, String orderId, Currency currency, String hookUrl, String customFields, String comment, URI failUrl, URI successUrl, Integer expire, List<String> includeService, List<String> excludeService) throws URISyntaxException, IOException, InterruptedException {
66+
InvoiceRequest request = new InvoiceRequest(amount, orderId, currency, shopId, hookUrl, customFields, comment, failUrl, successUrl, expire, includeService, excludeService);
67+
68+
return getInvoiceResponseResponse(request);
69+
}
70+
71+
private Response<InvoiceResponse> getInvoiceResponseResponse(InvoiceRequest request) throws URISyntaxException, IOException, InterruptedException {
72+
String body = gson.toJson(request);
73+
74+
URI url = new URI(enotUrl + "/invoice/create");
75+
HttpRequest req = HttpRequest
76+
.newBuilder()
77+
.uri(url)
78+
.header("Content-Type", "application/json")
79+
.header("x-api-key", apiKey)
80+
.POST(HttpRequest.BodyPublishers.ofString(body))
81+
.build();
82+
83+
HttpResponse<String> stringHttpResponse = httpClient.send(req, HttpResponse.BodyHandlers.ofString());
84+
Type responseType = TypeToken.getParameterized(Response.class, InvoiceResponse.class).getType();
85+
86+
return gson.fromJson(stringHttpResponse.body(), responseType);
87+
}
88+
89+
@Override
90+
public Response<InvoiceInfoResponse> invoiceInfo(UUID shopId, UUID invoiceId, String orderId) throws URISyntaxException, IOException, InterruptedException {
91+
URI url = new URI(enotUrl + "/invoice/info?invoice_id=" + invoiceId + "&shop_id=" + shopId + "&order_id=" + orderId);
92+
93+
HttpRequest req = HttpRequest
94+
.newBuilder()
95+
.uri(url)
96+
.header("Content-Type", "application/json")
97+
.header("x-api-key", apiKey)
98+
.GET()
99+
.build();
100+
101+
HttpResponse<String> stringHttpResponse = httpClient.send(req, HttpResponse.BodyHandlers.ofString());
102+
Type responseType = TypeToken.getParameterized(Response.class, InvoiceInfoResponse.class).getType();
103+
104+
return gson.fromJson(stringHttpResponse.body(), responseType);
105+
}
106+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package ru.chrshnv.enotiosdk.client.impl;
2+
3+
import ru.chrshnv.enotiosdk.client.EnotIoSDKClient;
4+
import ru.chrshnv.enotiosdk.client.EnotIoSDKClientFactory;
5+
6+
import java.util.UUID;
7+
8+
public class GsonEnotIoSDKClientFactory implements EnotIoSDKClientFactory {
9+
@Override
10+
public EnotIoSDKClient create(String apiKey, UUID shopId) {
11+
return new GsonEnotIoSDKClient(apiKey, shopId);
12+
}
13+
}

0 commit comments

Comments
 (0)