|
|
@ -1,7 +1,9 @@ |
|
|
|
package com.sparrowwallet.sparrow.io; |
|
|
|
package com.sparrowwallet.sparrow.net; |
|
|
|
|
|
|
|
import com.google.common.net.HostAndPort; |
|
|
|
import com.google.gson.Gson; |
|
|
|
import com.sparrowwallet.sparrow.event.ExchangeRatesUpdatedEvent; |
|
|
|
import com.sparrowwallet.sparrow.io.Config; |
|
|
|
import javafx.concurrent.ScheduledService; |
|
|
|
import javafx.concurrent.Service; |
|
|
|
import javafx.concurrent.Task; |
|
|
@ -11,6 +13,8 @@ import org.slf4j.LoggerFactory; |
|
|
|
import java.io.InputStream; |
|
|
|
import java.io.InputStreamReader; |
|
|
|
import java.io.Reader; |
|
|
|
import java.net.InetSocketAddress; |
|
|
|
import java.net.Proxy; |
|
|
|
import java.net.URL; |
|
|
|
import java.nio.charset.StandardCharsets; |
|
|
|
import java.util.*; |
|
|
@ -48,8 +52,9 @@ public enum ExchangeSource { |
|
|
|
|
|
|
|
private CoinbaseRates getRates() { |
|
|
|
String url = "https://api.coinbase.com/v2/exchange-rates?currency=BTC"; |
|
|
|
Proxy proxy = getProxy(); |
|
|
|
|
|
|
|
try(InputStream is = new URL(url).openStream(); Reader reader = new InputStreamReader(is, StandardCharsets.UTF_8)) { |
|
|
|
try(InputStream is = (proxy == null ? new URL(url).openStream() : new URL(url).openConnection(proxy).getInputStream()); Reader reader = new InputStreamReader(is, StandardCharsets.UTF_8)) { |
|
|
|
Gson gson = new Gson(); |
|
|
|
return gson.fromJson(reader, CoinbaseRates.class); |
|
|
|
} catch (Exception e) { |
|
|
@ -78,8 +83,9 @@ public enum ExchangeSource { |
|
|
|
|
|
|
|
private CoinGeckoRates getRates() { |
|
|
|
String url = "https://api.coingecko.com/api/v3/exchange_rates"; |
|
|
|
Proxy proxy = getProxy(); |
|
|
|
|
|
|
|
try(InputStream is = new URL(url).openStream(); Reader reader = new InputStreamReader(is, StandardCharsets.UTF_8)) { |
|
|
|
try(InputStream is = (proxy == null ? new URL(url).openStream() : new URL(url).openConnection(proxy).getInputStream()); Reader reader = new InputStreamReader(is, StandardCharsets.UTF_8)) { |
|
|
|
Gson gson = new Gson(); |
|
|
|
return gson.fromJson(reader, CoinGeckoRates.class); |
|
|
|
} catch (Exception e) { |
|
|
@ -110,6 +116,17 @@ public enum ExchangeSource { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private static Proxy getProxy() { |
|
|
|
Config config = Config.get(); |
|
|
|
if(config.isUseProxy()) { |
|
|
|
HostAndPort proxy = HostAndPort.fromString(config.getProxyServer()); |
|
|
|
InetSocketAddress proxyAddress = new InetSocketAddress(proxy.getHost(), proxy.getPortOrDefault(ProxyTcpOverTlsTransport.DEFAULT_PROXY_PORT)); |
|
|
|
return new Proxy(Proxy.Type.SOCKS, proxyAddress); |
|
|
|
} |
|
|
|
|
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public String toString() { |
|
|
|
return name; |