Skip to content
This repository was archived by the owner on Apr 20, 2022. It is now read-only.

Commit a4b140b

Browse files
authored
Merge pull request #38 from blockchain/api_refactor
refactor(API refactor)
2 parents 026f6cc + 2063c0b commit a4b140b

25 files changed

+1015
-437
lines changed

src/main/java/info/blockchain/api/Access.java

Lines changed: 0 additions & 92 deletions
This file was deleted.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package info.blockchain.api;
2+
3+
import info.blockchain.wallet.util.WebUtil;
4+
import org.json.JSONObject;
5+
6+
public class AddressInfo implements BaseApi {
7+
8+
private static final String ADDRESS = "address/";
9+
public static final String PROD_ADDRESS_INFO_URL = PROTOCOL + SERVER_ADDRESS + ADDRESS;
10+
11+
private String addressInfoUrl = PROD_ADDRESS_INFO_URL;
12+
13+
public AddressInfo() {
14+
addressInfoUrl = PersistentUrls.getInstance().getAddressInfoUrl();
15+
}
16+
17+
public JSONObject getAddressInfo(String address, String parameter) {
18+
19+
JSONObject jsonObject = null;
20+
21+
try {
22+
StringBuilder url = new StringBuilder(addressInfoUrl);
23+
url.append(address);
24+
url.append("?format=json");
25+
if (parameter != null && !parameter.isEmpty())
26+
url.append(parameter);
27+
28+
String response = WebUtil.getInstance().getURL(url.toString());
29+
jsonObject = new JSONObject(response);
30+
} catch (Exception e) {
31+
jsonObject = null;
32+
e.printStackTrace();
33+
}
34+
35+
return jsonObject;
36+
}
37+
}

src/main/java/info/blockchain/api/Balance.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
import org.apache.commons.lang3.StringUtils;
55
import org.json.JSONObject;
66

7-
public class Balance {
7+
public class Balance implements BaseApi {
8+
9+
private static final String BALANCE = "balance?active=";
10+
public static final String PROD_BALANCE_URL = PROTOCOL + SERVER_ADDRESS + BALANCE;
811

912
public static final int TxFilterSent = 1;
1013
public static final int TxFilterReceived = 2;
@@ -14,20 +17,26 @@ public class Balance {
1417
public static final int TxFilterRemoveUnspendable = 6;
1518
public static final int TxFilterUnconfirmedOnly = 7;
1619

17-
public JSONObject getBalance(String[] addresses) throws Exception{
20+
private String balanceUrl = PROD_BALANCE_URL;
21+
22+
public Balance() {
23+
balanceUrl = PersistentUrls.getInstance().getBalanceUrl();
24+
}
25+
26+
public JSONObject getBalance(String[] addresses) throws Exception {
1827
return getBalanceAPICall(addresses, -1);
1928
}
2029

21-
public JSONObject getBalance(String[] addresses, int filter) throws Exception{
30+
public JSONObject getBalance(String[] addresses, int filter) throws Exception {
2231
return getBalanceAPICall(addresses, filter);
2332
}
2433

25-
private JSONObject getBalanceAPICall(String[] addresses, int filter) throws Exception{
34+
private JSONObject getBalanceAPICall(String[] addresses, int filter) throws Exception {
2635

27-
StringBuilder url = new StringBuilder(WebUtil.BALANCE_URL);
36+
StringBuilder url = new StringBuilder(balanceUrl);
2837
url.append(StringUtils.join(addresses, "|"));
29-
if(filter > 0)url.append("&filter="+filter);
30-
url.append("&api_code="+WebUtil.API_CODE);
38+
if (filter > 0) url.append("&filter=" + filter);
39+
url.append("&api_code=" + API_CODE);
3140

3241
String response = WebUtil.getInstance().getURL(url.toString());
3342

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package info.blockchain.api;
2+
3+
4+
public interface BaseApi {
5+
6+
String PROTOCOL = "https://";
7+
8+
String API_SUBDOMAIN = "api.";
9+
10+
String SERVER_ADDRESS = "blockchain.info/";
11+
12+
String API_CODE = "25a6ad13-1633-4dfb-b6ee-9b91cdf0b5c3";
13+
}

src/main/java/info/blockchain/api/DynamicFee.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,31 @@
99
import java.math.BigInteger;
1010
import java.util.ArrayList;
1111

12-
public class DynamicFee {
12+
public class DynamicFee implements BaseApi {
13+
14+
private static final String FEES = "fees";
15+
public static final String PROD_DYNAMIC_FEE = PROTOCOL + API_SUBDOMAIN + SERVER_ADDRESS + FEES;
16+
17+
private String dynamicFeeUrl = PROD_DYNAMIC_FEE;
18+
19+
public DynamicFee() {
20+
dynamicFeeUrl = PersistentUrls.getInstance().getDynamicFeeUrl();
21+
}
1322

1423
public SuggestedFee getDynamicFee() {
1524

1625
String response = null;
1726
try {
18-
response = WebUtil.getInstance().getURL(WebUtil.DYNAMIC_FEE);
27+
response = WebUtil.getInstance().getURL(dynamicFeeUrl);
1928
} catch (Exception e) {
2029
e.printStackTrace();
2130
return getDefaultFee();
2231
}
2332

24-
if(response != null) {
33+
if (response != null) {
2534

2635
JSONObject dynamicFeeJson = new JSONObject(response);
27-
if(dynamicFeeJson != null){
36+
if (dynamicFeeJson != null) {
2837

2938
SuggestedFee suggestedFee = new SuggestedFee();
3039
JSONObject defaultJson = dynamicFeeJson.getJSONObject("default");
@@ -33,7 +42,7 @@ public SuggestedFee getDynamicFee() {
3342

3443
JSONArray estimateArray = dynamicFeeJson.getJSONArray("estimate");
3544
suggestedFee.estimateList = new ArrayList<SuggestedFee.Estimates>();
36-
for(int i = 0; i < estimateArray.length(); i++){
45+
for (int i = 0; i < estimateArray.length(); i++) {
3746

3847
JSONObject estimateJson = estimateArray.getJSONObject(i);
3948

@@ -51,7 +60,7 @@ public SuggestedFee getDynamicFee() {
5160
return getDefaultFee();
5261
}
5362

54-
public SuggestedFee getDefaultFee(){
63+
public SuggestedFee getDefaultFee() {
5564
SuggestedFee defaultFee = new SuggestedFee();
5665
defaultFee.defaultFeePerKb = FeeUtil.AVERAGE_FEE_PER_KB;
5766
return defaultFee;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package info.blockchain.api;
2+
3+
import info.blockchain.wallet.util.WebUtil;
4+
5+
public class ExchangeTicker implements BaseApi {
6+
7+
public static final String PROD_EXCHANGE_URL = PROTOCOL + SERVER_ADDRESS + "ticker";
8+
9+
public String getExchangeRate() throws Exception {
10+
11+
String response = WebUtil.getInstance().getURL(PROD_EXCHANGE_URL);
12+
if (response == null) {
13+
throw new Exception("Failed to get exchange rate");
14+
}
15+
16+
return response;
17+
}
18+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package info.blockchain.api;
2+
3+
import info.blockchain.wallet.util.WebUtil;
4+
import org.spongycastle.util.encoders.Hex;
5+
6+
public class ExternalEntropy implements BaseApi {
7+
8+
private static final String RANDOM_BYTES = "v2/randombytes?bytes=32&format=hex";
9+
public static final String PROD_EXTERNAL_ENTROPY_URL = PROTOCOL + API_SUBDOMAIN + SERVER_ADDRESS + RANDOM_BYTES;
10+
11+
public byte[] getRandomBytes() throws Exception {
12+
13+
String result = WebUtil.getInstance().getURL(PROD_EXTERNAL_ENTROPY_URL);
14+
if (result == null || !result.matches("^[A-Fa-f0-9]{64}$")) {
15+
throw new Exception("Failed to get random bytes");
16+
}
17+
18+
return Hex.decode(result);
19+
}
20+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
package info.blockchain.api;
2+
3+
import info.blockchain.wallet.util.FormatsUtil;
4+
import info.blockchain.wallet.util.WebUtil;
5+
import org.apache.commons.lang3.builder.ToStringBuilder;
6+
import org.json.JSONArray;
7+
import org.json.JSONException;
8+
import org.json.JSONObject;
9+
10+
import java.util.ArrayList;
11+
12+
public class MerchantDirectory implements BaseApi {
13+
14+
public static final String PROD_MERCHANT_DIRECTORY_URL = PROTOCOL + API_SUBDOMAIN + SERVER_ADDRESS + "merchant";
15+
16+
public ArrayList<Merchant> getAllMerchants() throws Exception {
17+
18+
String result = WebUtil.getInstance().getURL(PROD_MERCHANT_DIRECTORY_URL);
19+
20+
if (result == null || !FormatsUtil.getInstance().isValidJson(result)) {
21+
throw new Exception("Merchant api call returned null or empty");
22+
} else {
23+
24+
return parse(result);
25+
}
26+
}
27+
28+
public ArrayList<Merchant> parse(String data) {
29+
30+
ArrayList<Merchant> merchantList = new ArrayList<Merchant>();
31+
32+
try {
33+
JSONArray jsonArray = new JSONArray(data);
34+
35+
if (jsonArray != null && jsonArray.length() > 0) {
36+
37+
for (int i = 0; i < jsonArray.length(); i++) {
38+
39+
Merchant merchant = new Merchant();
40+
JSONObject jsonObj = jsonArray.getJSONObject(i);
41+
42+
if (jsonObj.has("id")) {
43+
merchant.id = jsonObj.optInt("id", 0);
44+
}
45+
if (jsonObj.has("name")) {
46+
merchant.name = jsonObj.optString("name", "");
47+
}
48+
if (jsonObj.has("address")) {
49+
merchant.address = jsonObj.optString("address", "");
50+
}
51+
if (jsonObj.has("city")) {
52+
merchant.city = jsonObj.optString("city", "");
53+
}
54+
if (jsonObj.has("postal_code")) {
55+
merchant.postal_code = jsonObj.optString("postal_code", "");
56+
}
57+
if (jsonObj.has("phone")) {
58+
merchant.phone = jsonObj.optString("phone", "");
59+
}
60+
if (jsonObj.has("website")) {
61+
merchant.website = jsonObj.optString("website", "");
62+
}
63+
if (jsonObj.has("latitude")) {
64+
merchant.latitude = jsonObj.optDouble("latitude", 0.0);
65+
}
66+
if (jsonObj.has("longitude")) {
67+
merchant.longitude = jsonObj.optDouble("longitude", 0.0);
68+
}
69+
if (jsonObj.has("featured_merchant")) {
70+
merchant.featured_merchant = jsonObj.optBoolean("featured_merchant", false);
71+
}
72+
if (jsonObj.has("description")) {
73+
merchant.description = jsonObj.optString("description", "");
74+
}
75+
if (jsonObj.has("category_id")) {
76+
merchant.category_id = jsonObj.optInt("category_id", Merchant. HEADING_CAFE);
77+
}
78+
79+
merchantList.add(merchant);
80+
}
81+
}
82+
} catch (JSONException je) {
83+
je.printStackTrace();
84+
}
85+
86+
return merchantList;
87+
}
88+
89+
public static class Merchant {
90+
91+
public static final int HEADING_CAFE = 1;
92+
public static final int HEADING_BAR = 2;
93+
public static final int HEADING_RESTAURANT = 3;
94+
public static final int HEADING_SPEND = 4;
95+
public static final int HEADING_ATM = 5;
96+
97+
public int id;
98+
public String name;
99+
public String address;
100+
public String city;
101+
public String postal_code;
102+
public String phone;
103+
public String website;
104+
public double latitude;
105+
public double longitude;
106+
public boolean featured_merchant;
107+
public String description;
108+
public int category_id;
109+
110+
@Override
111+
public String toString() {
112+
return ToStringBuilder.reflectionToString(this);
113+
}
114+
}
115+
}

0 commit comments

Comments
 (0)