-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathTransactionsConsulClient.java
More file actions
59 lines (52 loc) · 1.82 KB
/
TransactionsConsulClient.java
File metadata and controls
59 lines (52 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package com.ecwid.consul.v1.transactions;
import com.ecwid.consul.SingleUrlParameters;
import com.ecwid.consul.UrlParameters;
import com.ecwid.consul.json.GsonFactory;
import com.ecwid.consul.transport.HttpResponse;
import com.ecwid.consul.v1.ConsulRawClient;
import com.ecwid.consul.v1.OperationException;
import com.ecwid.consul.v1.Response;
import com.ecwid.consul.v1.transactions.model.TxnResult;
/**
* Transactions Client for consul
*
* @author Trisia Cliven (quanguanyu@qq.com)
*/
public class TransactionsConsulClient implements TransactionsClient {
private final ConsulRawClient rawClient;
public TransactionsConsulClient(ConsulRawClient rawClient) {
this.rawClient = rawClient;
}
public TransactionsConsulClient() {
this(new ConsulRawClient());
}
/**
* Submit transaction
*
* @param token token
* @param builder parameters builder
* @return process result report
*/
@Override
public Response<TxnResult> commit(String token, ParamBuilder builder) {
UrlParameters tokenParams = token != null ? new SingleUrlParameters("token", token) : null;
String json = GsonFactory.getGson().toJson(builder.build());
HttpResponse httpResponse = rawClient.makePutRequest("/v1/txn", json, tokenParams);
if (httpResponse.getStatusCode() == 200) {
TxnResult value = GsonFactory.getGson().fromJson(httpResponse.getContent(), TxnResult.class);
return new Response<>(value, httpResponse);
} else {
throw new OperationException(httpResponse);
}
}
/**
* Submit transaction
*
* @param builder parameters builder
* @return process result report
*/
@Override
public Response<TxnResult> commit(ParamBuilder builder) {
return this.commit(null, builder);
}
}