Skip to content

Commit 95f3b5b

Browse files
authored
Merge pull request #9 from virtualeconomy/APIKey-feature
add API Key feature
2 parents cdba1bc + 2fe6de2 commit 95f3b5b

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

src/v/systems/Blockchain.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.google.gson.*;
44
import lombok.Getter;
5+
import lombok.Setter;
56
import v.systems.contract.*;
67
import v.systems.entity.Balance;
78
import v.systems.entity.BalanceDetail;
@@ -17,14 +18,19 @@
1718

1819
import java.io.IOException;
1920
import java.util.ArrayList;
21+
import java.util.HashMap;
2022
import java.util.List;
23+
import java.util.Map;
2124

2225
public class Blockchain {
2326
public static final long V_UNITY = 100000000L;
2427
@Getter
2528
private NetworkType network;
2629
@Getter
2730
private String nodeUrl;
31+
@Getter
32+
@Setter
33+
private String apiKey;
2834
private Gson gson;
2935
private JsonParser parser;
3036

@@ -35,6 +41,11 @@ public Blockchain(NetworkType network, String nodeUrl) {
3541
parser = JsonHelper.getParserInstance();
3642
}
3743

44+
public Blockchain(NetworkType network, String nodeUrl, String apiKey) {
45+
this(network, nodeUrl);
46+
this.apiKey = apiKey;
47+
}
48+
3849
public Long getBalance(String address) throws IOException, ApiError {
3950
String url = String.format("%s/addresses/balance/%s", nodeUrl, address);
4051
Balance balance = this.callChainAPI(url, Balance.class);
@@ -48,20 +59,24 @@ public BalanceDetail getBalanceDetail(String address) throws IOException, ApiErr
4859

4960
public List<Transaction> getActiveLeaseTransactions(String address) throws IOException, ApiError {
5061
String url = String.format("%s/transactions/activeLeaseList/%s", nodeUrl, address);
51-
return this.getTransactionByUrl(url);
62+
return this.getTransactionByUrl(url, true);
5263
}
5364

5465
public List<Transaction> getTransactionHistory(String address, int num) throws IOException, ApiError {
5566
if (num <= 0) {
5667
return new ArrayList<Transaction>();
5768
}
5869
String url = String.format("%s/transactions/address/%s/limit/%d", nodeUrl, address, num);
59-
return this.getTransactionByUrl(url);
70+
return this.getTransactionByUrl(url, false);
6071
}
6172

62-
private List<Transaction> getTransactionByUrl(String url) throws IOException, ApiError {
73+
private List<Transaction> getTransactionByUrl(String url, boolean useApiKey) throws IOException, ApiError {
6374
List<Transaction> result = new ArrayList<Transaction>();
64-
String json = HttpClient.get(url);
75+
Map<String, String> header = new HashMap<String, String>();
76+
if (useApiKey && this.apiKey != null) {
77+
header.put("api_key", this.apiKey);
78+
}
79+
String json = HttpClient.get(url, header);
6580
try {
6681
JsonElement jsonElement = parser.parse(json);
6782
if (!jsonElement.isJsonArray()) {

src/v/systems/utils/HttpClient.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,16 @@
55
import java.net.HttpURLConnection;
66
import java.net.URL;
77
import java.nio.charset.StandardCharsets;
8+
import java.util.Map;
89

910

1011
public class HttpClient {
1112

1213
public static String get(String url) throws IOException {
14+
return get(url, null);
15+
}
16+
17+
public static String get(String url, Map<String, String> header) throws IOException {
1318

1419
HttpURLConnection con = null;
1520

@@ -21,6 +26,11 @@ public static String get(String url) throws IOException {
2126
con = (HttpURLConnection) webURL.openConnection();
2227
}
2328
con.setRequestMethod("GET");
29+
if (header != null) {
30+
for (Map.Entry<String, String> entry : header.entrySet()) {
31+
con.setRequestProperty(entry.getKey(), entry.getValue());
32+
}
33+
}
2434
return getResponse(con);
2535
} finally {
2636
if (con != null) {
@@ -30,6 +40,10 @@ public static String get(String url) throws IOException {
3040
}
3141

3242
public static String post(String url, String json) throws IOException {
43+
return post(url, null, json);
44+
}
45+
46+
public static String post(String url, Map<String, String> header, String json) throws IOException {
3347

3448
HttpURLConnection con = null;
3549
byte[] postData = json.getBytes(StandardCharsets.UTF_8);
@@ -44,6 +58,11 @@ public static String post(String url, String json) throws IOException {
4458
con.setDoOutput(true);
4559
con.setRequestMethod("POST");
4660
con.setRequestProperty("Content-Type", "application/json");
61+
if (header != null) {
62+
for (Map.Entry<String, String> entry : header.entrySet()) {
63+
con.setRequestProperty(entry.getKey(), entry.getValue());
64+
}
65+
}
4766
try (DataOutputStream wr = new DataOutputStream(con.getOutputStream())) {
4867
wr.write(postData);
4968
}

test/v/systems/BlockchainTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ public void getContractContent() throws SerializationError {
200200
public void getActiveLeaseTransactions() {
201201
String testAddress = "AUCJE7djDBbFrgeKRZ4UMREujAeqnD6L6Ph";
202202
List<Transaction> txList = null;
203+
defaultChain.setApiKey(TestConfig.API_KEY);
203204
try {
204205
txList = defaultChain.getActiveLeaseTransactions(testAddress);
205206
} catch (Exception e) {

0 commit comments

Comments
 (0)