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,59 @@
|
|||
package com.example;
|
||||
|
||||
import rife.bld.BaseProject;
|
||||
import rife.bld.BuildCommand;
|
||||
import rife.bld.extension.CompileKotlinOperation;
|
||||
import rife.bld.extension.CompileKotlinOptions;
|
||||
import rife.bld.operations.RunOperation;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static rife.bld.dependencies.Repository.*;
|
||||
import static rife.bld.dependencies.Scope.compile;
|
||||
|
||||
public class CryptoPriceExampleBuild extends BaseProject {
|
||||
public CryptoPriceExampleBuild() {
|
||||
pkg = "com.example";
|
||||
name = "Example";
|
||||
version = version(0, 1, 0);
|
||||
|
||||
mainClass = "com.example.CryptoPriceExampleKt";
|
||||
|
||||
javaRelease = 11;
|
||||
downloadSources = true;
|
||||
autoDownloadPurge = true;
|
||||
repositories = List.of(MAVEN_LOCAL, MAVEN_CENTRAL, SONATYPE_SNAPSHOTS_LEGACY);
|
||||
|
||||
scope(compile)
|
||||
.include(dependency("org.jetbrains.kotlin", "kotlin-stdlib", version(1, 9, 20)))
|
||||
.include(dependency("net.thauvin.erik", "cryptoprice", version(1, 0, 1)))
|
||||
.include(dependency("org.json", "json", "20231013"));
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
new CryptoPriceExampleBuild().start(args);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void compile() throws Exception {
|
||||
new CompileKotlinOperation()
|
||||
.fromProject(this)
|
||||
.compileOptions(
|
||||
new CompileKotlinOptions()
|
||||
.jdkRelease(javaRelease)
|
||||
.verbose(true)
|
||||
)
|
||||
.execute();
|
||||
|
||||
// Also compile the Java source code
|
||||
super.compile();
|
||||
}
|
||||
|
||||
@BuildCommand(value = "run-java", summary = "Runs the Java example")
|
||||
public void runJava() throws Exception {
|
||||
new RunOperation()
|
||||
.fromProject(this)
|
||||
.mainClass("com.example.CryptoPriceSample")
|
||||
.execute();
|
||||
}
|
||||
}
|
|
@ -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