Moved from Gradle to bld
This commit is contained in:
parent
5ec9020a9d
commit
da5aea6b26
68 changed files with 1520 additions and 792 deletions
|
@ -0,0 +1,58 @@
|
|||
package com.example;
|
||||
|
||||
import net.thauvin.erik.crypto.CryptoException;
|
||||
import net.thauvin.erik.crypto.CryptoPrice;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collections;
|
||||
import java.util.Currency;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
public class CryptoPriceSample {
|
||||
public static void main(final String[] args) {
|
||||
try {
|
||||
if (args.length > 0) {
|
||||
final String currency;
|
||||
if (args.length == 2) {
|
||||
currency = args[1];
|
||||
} else {
|
||||
currency = Currency.getInstance(Locale.getDefault()).getCurrencyCode();
|
||||
}
|
||||
final var price = CryptoPrice.spotPrice(args[0], currency);
|
||||
System.out.println("The current " + price.getBase() + " price is " + price.toCurrency());
|
||||
} else {
|
||||
// Get current Bitcoin spot price.
|
||||
final var price = CryptoPrice.spotPrice("BTC");
|
||||
System.out.println("The current Bitcoin price is " + price.toCurrency());
|
||||
|
||||
System.out.println();
|
||||
|
||||
// Get current Ethereum buy price in Pound sterling.
|
||||
final var gbpPrice = CryptoPrice.buyPrice("ETH", "GBP");
|
||||
System.out.println("The current Ethereum buy price is " + gbpPrice.toCurrency());
|
||||
|
||||
// Get current Litecoin sell price in Euros.
|
||||
final var euroPrice = CryptoPrice.sellPrice("LTC", "EUR");
|
||||
System.out.println("The current Litecoin sell price is " + euroPrice.toCurrency());
|
||||
|
||||
System.out.println();
|
||||
|
||||
// Get exchange rate using API.
|
||||
// See: https://docs.cloud.coinbase.com/sign-in-with-coinbase/docs/api-exchange-rates
|
||||
final var response = CryptoPrice.apiCall(List.of("exchange-rates"),
|
||||
Collections.singletonMap("currency", "USD"));
|
||||
final var rates = new JSONObject(response).getJSONObject("data").getJSONObject("rates");
|
||||
System.out.println("The USD-EUR exchange rate is: " + rates.getString("EUR"));
|
||||
}
|
||||
} catch (CryptoException e) {
|
||||
System.err.println("HTTP Status Code: " + e.getStatusCode());
|
||||
System.err.println(e.getMessage() + " (" + e.getId() + ')');
|
||||
} catch (IllegalArgumentException e) {
|
||||
System.err.println("Could not display the specified currency: " + args[1]);
|
||||
} catch (IOException e) {
|
||||
System.err.println(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,49 @@
|
|||
package com.example
|
||||
|
||||
import net.thauvin.erik.crypto.CryptoException
|
||||
import net.thauvin.erik.crypto.CryptoPrice.Companion.apiCall
|
||||
import net.thauvin.erik.crypto.CryptoPrice.Companion.buyPrice
|
||||
import net.thauvin.erik.crypto.CryptoPrice.Companion.sellPrice
|
||||
import net.thauvin.erik.crypto.CryptoPrice.Companion.spotPrice
|
||||
import org.json.JSONObject
|
||||
import java.io.IOException
|
||||
import java.util.*
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
try {
|
||||
if (args.isNotEmpty()) {
|
||||
val currency = if (args.size == 2) args[1] else Currency.getInstance(Locale.getDefault()).currencyCode
|
||||
val price = spotPrice(args[0], currency)
|
||||
println("The current ${price.base} price is ${price.toCurrency()}")
|
||||
} else {
|
||||
// Get current Bitcoin spot price.
|
||||
val price = spotPrice("BTC")
|
||||
println("The current Bitcoin price is ${price.toCurrency()}")
|
||||
|
||||
println()
|
||||
|
||||
// Get current Ethereum sell price in Pound sterling.
|
||||
val gbpPrice = sellPrice("ETH", "GBP")
|
||||
println("The current Ethereum sell price is ${gbpPrice.toCurrency()}")
|
||||
|
||||
// Get current Litecoin buy price in Euro.
|
||||
val euroPrice = buyPrice("LTC", "EUR")
|
||||
println("The current Litecoin buy price is ${euroPrice.toCurrency()}")
|
||||
|
||||
println()
|
||||
|
||||
// Get exchange rate using API.
|
||||
// See: https://docs.cloud.coinbase.com/sign-in-with-coinbase/docs/api-exchange-rates
|
||||
val response = apiCall(listOf("exchange-rates"), mapOf("currency" to "usd"))
|
||||
val rates = JSONObject(response).getJSONObject("data").getJSONObject("rates")
|
||||
println("The USD-EUR exchange rate is: ${rates.getString("EUR")}")
|
||||
}
|
||||
} catch (e: CryptoException) {
|
||||
System.err.println("HTTP Status Code: ${e.statusCode}")
|
||||
System.err.println("${e.message} (${e.id})")
|
||||
} catch (e: IllegalArgumentException) {
|
||||
System.err.println("Could not display the specified currency: ${args[1]}")
|
||||
} catch (e: IOException) {
|
||||
System.err.println(e.message)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue