-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathLedgerClient.java
More file actions
111 lines (94 loc) · 3.21 KB
/
LedgerClient.java
File metadata and controls
111 lines (94 loc) · 3.21 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
* Copyright (c) 2020-2021 - for information on the respective copyright owner
* see the NOTICE file and/or the repository at
* https://github.com/hyperledger-labs/acapy-java-client
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.hyperledger.aries;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import lombok.Builder;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import org.apache.commons.lang3.StringUtils;
import org.hyperledger.aries.util.ledger.LedgerDIDCreate;
import org.hyperledger.aries.util.ledger.LedgerDIDResponse;
import javax.annotation.Nullable;
import java.io.IOException;
import java.lang.reflect.Type;
import java.time.Duration;
import java.time.Instant;
import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;
/**
* ACA-PY client
*/
@Slf4j
@SuppressWarnings("unused")
public class LedgerClient extends BaseClient {
public static final String LEDGER_GENESIS_TXN_URL = "/genesis";
public static final String LEDGER_DID_REGISTRATION_URL = "/register";
public static final String LEDGER_DID_ENDORSER_ROLE = "ENDORSER";
private final String url;
/**
* @param url The ledger URL without a path e.g. protocol://host:[port]
* @param client {@link OkHttpClient} if null a default client is created
*/
@Builder
public LedgerClient(@NonNull String url,
@Nullable OkHttpClient client) {
super(client);
this.url = StringUtils.trim(url);
}
// ----------------------------------------------------
// Register a new public DID on the ledger
// ----------------------------------------------------
/**
* Create a public DID
* @param didCreate {@link DIDCreate}
* @return {@link DID}
* @throws IOException if the request could not be executed due to cancellation, a connectivity problem or timeout.
*/
public Optional<LedgerDIDResponse> ledgerDidCreate(@NonNull LedgerDIDCreate didCreate) throws IOException {
Request req = buildPost(url + LEDGER_DID_REGISTRATION_URL, didCreate);
return call(req, LedgerDIDResponse.class);
}
// ----------------------------------------------------
// Internal
// ----------------------------------------------------
private Request buildPost(String u, Object body) {
return request(u)
.post(jsonBody(gson.toJson(body)))
.build();
}
private Request buildPut(String u, Object body) {
return request(u)
.put(jsonBody(gson.toJson(body)))
.build();
}
private Request buildPatch(String u, Object body) {
return request(u)
.patch(jsonBody(gson.toJson(body)))
.build();
}
private Request buildGet(String u) {
return request(u)
.get()
.build();
}
private Request buildDelete(String u) {
return request(u)
.delete()
.build();
}
private Request.Builder request(String u) {
Request.Builder b = new Request.Builder()
.url(u);
return b;
}
}