Skip to content

Commit

Permalink
add BackendApi
Browse files Browse the repository at this point in the history
  • Loading branch information
zeroleak committed May 20, 2019
1 parent f0d4cf1 commit bbdc5d0
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
18 changes: 7 additions & 11 deletions java/com/samourai/wallet/api/backend/BackendApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@
public class BackendApi {
private Logger log = LoggerFactory.getLogger(BackendApi.class);

// samourai backend urls
private static final String URL_BACKEND_TESTNET = "https://api.samouraiwallet.com/test";
private static final String URL_BACKEND_MAINNET = "https://api.samouraiwallet.com";

private static final String URL_UNSPENT = "/v2/unspent?active=";
private static final String URL_MULTIADDR = "/v2/multiaddr?active=";
private static final String URL_INIT_BIP84 = "/v2/xpub";
Expand All @@ -24,8 +20,8 @@ public class BackendApi {
private IBackendClient httpClient;
private String urlBackend;

public BackendApi(IBackendClient httpClient, boolean testnet) {
this(httpClient, testnet ? URL_BACKEND_TESTNET : URL_BACKEND_MAINNET);
public BackendApi(IBackendClient httpClient, BackendServer backendServer) {
this(httpClient, backendServer.getBackendUrl());
}

public BackendApi(IBackendClient httpClient, String urlBackend) {
Expand All @@ -38,7 +34,7 @@ public List<UnspentResponse.UnspentOutput> fetchUtxos(String zpub) throws Except
if (log.isDebugEnabled()) {
log.debug("fetchUtxos: " + url);
}
UnspentResponse unspentResponse = httpClient.parseJson(url, UnspentResponse.class);
UnspentResponse unspentResponse = httpClient.getJson(url, UnspentResponse.class);
List<UnspentResponse.UnspentOutput> unspentOutputs =
new ArrayList<UnspentResponse.UnspentOutput>();
if (unspentResponse.unspent_outputs != null) {
Expand All @@ -52,7 +48,7 @@ public List<MultiAddrResponse.Address> fetchAddresses(String zpub) throws Except
if (log.isDebugEnabled()) {
log.debug("fetchAddress: " + url);
}
MultiAddrResponse multiAddrResponse = httpClient.parseJson(url, MultiAddrResponse.class);
MultiAddrResponse multiAddrResponse = httpClient.getJson(url, MultiAddrResponse.class);
List<MultiAddrResponse.Address> addresses = new ArrayList<MultiAddrResponse.Address>();
if (multiAddrResponse.addresses != null) {
addresses = Arrays.asList(multiAddrResponse.addresses);
Expand Down Expand Up @@ -88,12 +84,12 @@ public void initBip84(String zpub) throws Exception {
postBody.put("xpub", zpub);
postBody.put("type", "new");
postBody.put("segwit", "bip84");
httpClient.postUrlEncoded(url, postBody);
httpClient.postUrlEncoded(url, Void.class, postBody);
}

public SamouraiFee fetchFees() throws Exception {
String url = urlBackend + URL_FEES;
Map<String, Integer> feeResponse = httpClient.parseJson(url, Map.class);
Map<String, Integer> feeResponse = httpClient.getJson(url, Map.class);
if (feeResponse == null) {
throw new Exception("Invalid fee response from server");
}
Expand All @@ -110,7 +106,7 @@ public void pushTx(String txHex) throws Exception {
Map<String, String> postBody = new HashMap<String, String>();
postBody.put("tx", txHex);
try {
httpClient.postUrlEncoded(url, postBody);
httpClient.postUrlEncoded(url, Void.class, postBody);
} catch (HttpException e) {
if (log.isDebugEnabled()) {
log.error("pushTx failed", e);
Expand Down
16 changes: 16 additions & 0 deletions java/com/samourai/wallet/api/backend/BackendServer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.samourai.wallet.api.backend;

public enum BackendServer {
MAINNET("https://api.samouraiwallet.com"),
TESTNET("https://api.samouraiwallet.com/test");

private String backendUrl;

BackendServer(String backendUrl) {
this.backendUrl = backendUrl;
}

public String getBackendUrl() {
return backendUrl;
}
}
4 changes: 2 additions & 2 deletions java/com/samourai/wallet/api/backend/IBackendClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import java.util.Map;

public interface IBackendClient {
<T> T parseJson(String url, Class<T> entityClass) throws HttpException;
<T> T getJson(String url, Class<T> responseType) throws HttpException;

void postUrlEncoded(String url, Map<String, String> body) throws HttpException;
<T> T postUrlEncoded(String url, Class<T> responseType, Map<String, String> body) throws HttpException;
}

0 comments on commit bbdc5d0

Please sign in to comment.