22
33import com .google .gson .*;
44import lombok .Getter ;
5+ import lombok .Setter ;
56import v .systems .contract .*;
67import v .systems .entity .Balance ;
78import v .systems .entity .BalanceDetail ;
1718
1819import java .io .IOException ;
1920import java .util .ArrayList ;
21+ import java .util .HashMap ;
2022import java .util .List ;
23+ import java .util .Map ;
2124
2225public 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 ()) {
0 commit comments