Skip to content

Commit

Permalink
feat: add SLF4J library (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
nyg authored Mar 10, 2024
1 parent b047f47 commit 2d7e766
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 57 deletions.
16 changes: 16 additions & 0 deletions examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,28 @@

<artifactId>kraken-api-examples</artifactId>

<properties>
<log4j.version>2.23.0</log4j.version>
</properties>

<dependencies>
<dependency>
<groupId>dev.andstuff.kraken</groupId>
<artifactId>kraken-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j2-impl</artifactId>
<version>${log4j.version}</version>
<scope>runtime</scope>
</dependency>
</dependencies>

<build>
Expand Down
24 changes: 12 additions & 12 deletions examples/src/main/java/dev/andstuff/kraken/example/Examples.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,48 +13,48 @@
import dev.andstuff.kraken.api.model.endpoint.market.response.AssetPair;
import dev.andstuff.kraken.api.model.endpoint.market.response.ServerTime;
import dev.andstuff.kraken.api.model.endpoint.market.response.SystemStatus;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class Examples {

public static void main(String[] args) {


/* Public endpoint examples */

KrakenAPI publicAPI = new KrakenAPI();

ServerTime serverTime = publicAPI.serverTime();
System.out.println(serverTime);
log.info("{}", serverTime);

SystemStatus systemStatus = publicAPI.systemStatus();
System.out.println(systemStatus);
log.info("{}", systemStatus);

Map<String, AssetInfo> assets1 = publicAPI.assetInfo(List.of("BTC", "ETH"));
System.out.println(assets1);
log.info("{}", assets1);

Map<String, AssetInfo> assets2 = publicAPI.assetInfo(List.of("DOT", "ADA"), "currency");
System.out.println(assets2);
log.info("{}", assets2);

Map<String, AssetPair> pairs1 = publicAPI.assetPairs(List.of("ETH/BTC", "ETH/USD"));
System.out.println(pairs1);
log.info("{}", pairs1);

Map<String, AssetPair> pairs2 = publicAPI.assetPairs(List.of("DOT/USD", "ADA/USD"), AssetPair.Info.MARGIN);
System.out.println(pairs2);
log.info("{}", pairs2);

JsonNode ticker = publicAPI.query(KrakenAPI.Public.TICKER, Map.of("pair", "XBTEUR"));
System.out.println(ticker);
log.info("{}", ticker);

JsonNode trades = publicAPI.queryPublic("Trades", Map.of("pair", "XBTUSD", "count", "1"));
System.out.println(trades);
log.info("{}", trades);

/* Private endpoint example */

Properties apiKeys = readFromFile("/api-keys.properties");

KrakenAPI api = new KrakenAPI(apiKeys.getProperty("key"), apiKeys.getProperty("secret"));

JsonNode balance = api.query(KrakenAPI.Private.BALANCE);
System.out.println(balance);
log.info("{}", balance);

JsonNode order = api.query(KrakenAPI.Private.ADD_ORDER, Map.of(
"ordertype", "limit",
Expand All @@ -64,6 +64,6 @@ public static void main(String[] args) {
"price", "1000",
"oflags", "post,fciq",
"validate", "true"));
System.out.println(order);
log.info("{}", order);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@
import static java.util.Arrays.asList;
import static java.util.Comparator.comparing;
import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.toList;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.math.BigDecimal;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.time.Instant;
import java.util.HashMap;
import java.util.Iterator;
Expand All @@ -28,15 +25,15 @@
*/
public class TotalRewards {

public static void main(String[] args) throws IOException, NoSuchAlgorithmException, InvalidKeyException, InterruptedException {
public static void main(String[] args) throws IOException, InterruptedException {

Properties apiKeys = readFromFile("/api-keys.properties");
KrakenAPI api = new KrakenAPI(apiKeys.getProperty("key"), apiKeys.getProperty("secret"));

Map<String, String> params = Map.of(
"type", "staking",
"without_count", "true",
"ofs", "0");
Map<String, String> params = new HashMap<>();
params.put("type", "staking");
params.put("without_count", "true");
params.put("ofs", "0");

Map<String, JsonNode> rewards = new HashMap<>();

Expand Down Expand Up @@ -76,7 +73,7 @@ public static void main(String[] args) throws IOException, NoSuchAlgorithmExcept
List<JsonNode> assetRewards = groupedRewards.get(asset).stream()
.filter(e -> !asList("migration", "spottostaking").contains(e.findValue("subtype").textValue()))
.sorted(comparing(e -> e.get("time").asInt()))
.collect(toList());
.toList();

BigDecimal assetTotalRewardAmount = assetRewards.stream()
.map(e -> new BigDecimal(e.findValue("amount").textValue())
Expand Down
18 changes: 18 additions & 0 deletions examples/src/main/resources/log4j2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8" ?>
<Configuration status="info">
<Properties>
<Property name="defaultPattern">[%d{ISO8601}] [%-20.20c{1.}] [%-7.7t] [%-5.5p] %m%n</Property>
</Properties>

<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="${defaultPattern}"/>
</Console>
</Appenders>

<Loggers>
<Root level="info">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@
import dev.andstuff.kraken.api.model.endpoint.priv.PostParams;
import dev.andstuff.kraken.api.model.endpoint.priv.PrivateEndpoint;
import dev.andstuff.kraken.api.model.endpoint.pub.PublicEndpoint;
import lombok.extern.slf4j.Slf4j;

/**
* {@link KrakenRestRequester} implementation using {@link HttpsURLConnection}.
*/
@Slf4j
public class DefaultKrakenRestRequester implements KrakenRestRequester {

private static final ObjectMapper OBJECT_MAPPER;
Expand All @@ -51,7 +53,7 @@ public DefaultKrakenRestRequester(String key, String secret) {
public <T> T execute(PublicEndpoint<T> endpoint) {
try {
HttpsURLConnection connection = createHttpsConnection(endpoint);
System.out.printf("Fetching %s%n", connection.getURL());
log.info("Fetching {}", connection.getURL());
return parseResponse(connection.getInputStream(), endpoint);
}
catch (IOException e) {
Expand All @@ -72,7 +74,7 @@ public <T> T execute(PrivateEndpoint<T> endpoint) {

try {
HttpsURLConnection connection = createHttpsConnection(endpoint);
System.out.printf("Fetching %s%n", connection.getURL());
log.info("Fetching {}", connection.getURL());
connection.addRequestProperty("API-Key", credentials.getKey());
connection.addRequestProperty("API-Sign", credentials.sign(connection.getURL(), nonce, postData));
connection.setDoOutput(true);
Expand Down
11 changes: 11 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@
<!-- Third-party dependencies -->
<jackson.version>2.16.1</jackson.version>
<lombok.version>1.18.30</lombok.version>
<slf4j.version>2.0.12</slf4j.version>
<log4j.version>2.23.0</log4j.version>

<!-- Maven plugins -->
<maven-deploy-plugin.version>3.1.1</maven-deploy-plugin.version>
Expand Down Expand Up @@ -81,6 +83,11 @@
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

Expand All @@ -102,6 +109,10 @@
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
</dependencies>

<build>
Expand Down

0 comments on commit 2d7e766

Please sign in to comment.