Skip to content

Commit

Permalink
Refactor into separate Maven modules
Browse files Browse the repository at this point in the history
  • Loading branch information
nyg committed Jan 20, 2024
1 parent 3d79434 commit f6d8884
Show file tree
Hide file tree
Showing 11 changed files with 158 additions and 75 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -2,3 +2,4 @@ target
.DS_Store
*.iml
.idea
api-keys.properties
12 changes: 10 additions & 2 deletions README.md
@@ -1,6 +1,14 @@
# A Kraken API Client in Java
Query the Kraken API in Java. No external libraries used.

Execute `mvn clean package` and the JAR will be in the `target` folder. After that, `Examples.java` can also be executed using `java -cp target/classes edu/self/kraken/Examples`.
Query the Kraken API in Java.

Execute `mvn clean package` and the JAR will be in the `target` folder. After that, `Examples.java` can also be executed
using `java -cp target/classes edu/self/kraken/Examples`.

https://www.kraken.com/en-us/help/api

TODO:

* return json objects instead of string
* return objects for method calls
* SLF4J
23 changes: 23 additions & 0 deletions examples/pom.xml
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>edu.self.nyg</groupId>
<artifactId>kraken-api-parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>

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

<dependencies>
<dependency>
<groupId>edu.self.nyg</groupId>
<artifactId>kraken-api</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>

</project>
64 changes: 64 additions & 0 deletions examples/src/main/java/edu/self/nyg/kraken/example/Examples.java
@@ -0,0 +1,64 @@
package edu.self.nyg.kraken.example;

import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import edu.self.nyg.kraken.api.KrakenApi;

public class Examples {

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

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

KrakenApi api = new KrakenApi();
api.setKey(apiKeys.getProperty("key"));
api.setSecret(apiKeys.getProperty("secret"));

String response;
Map<String, String> input = new HashMap<>();

input.put("pair", "XBTEUR");
response = api.queryPublic(KrakenApi.Method.TICKER, input);
System.out.println(response);

input.clear();
input.put("pair", "XBTUSD,XLTCZUSD");
response = api.queryPublic(KrakenApi.Method.ASSET_PAIRS, input);
System.out.println(response);

input.clear();
input.put("asset", "ZEUR");
response = api.queryPrivate(KrakenApi.Method.BALANCE, input);
System.out.println(response);

input.clear();
input.put("ordertype", "limit");
input.put("type", "sell");
input.put("volume", "1");
input.put("pair", "XLTCZUSD");
input.put("price", "1000");
input.put("oflags", "post,fciq");
input.put("validate", "true");
response = api.queryPrivate(KrakenApi.Method.ADD_ORDER, input);
System.out.println(response);

}

private static Properties readPropertiesFromFile(String path) {
try {
InputStream stream = Examples.class.getResourceAsStream(path);
Properties properties = new Properties();
properties.load(stream);
return properties;
}
catch (IOException e) {
throw new RuntimeException(String.format("Could not read properties file: %s", path));
}
}
}
2 changes: 2 additions & 0 deletions examples/src/main/resources/api-keys.properties.example
@@ -0,0 +1,2 @@
key=YOUR-API-KEY
secret=YOUR-API-SECRET
22 changes: 22 additions & 0 deletions library/pom.xml
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>edu.self.nyg</groupId>
<artifactId>kraken-api-parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>

<artifactId>kraken-api</artifactId>

<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>

</project>
@@ -1,4 +1,4 @@
package edu.self.kraken.api;
package edu.self.nyg.kraken.api;

import java.io.BufferedReader;
import java.io.IOException;
Expand All @@ -12,7 +12,7 @@

import javax.net.ssl.HttpsURLConnection;

import edu.self.kraken.api.KrakenApi.Method;
import edu.self.nyg.kraken.api.KrakenApi.Method;

/**
* Represents an HTTPS request for querying the Kraken API.
Expand Down
@@ -1,30 +1,32 @@
package edu.self.kraken.api;
package edu.self.nyg.kraken.api;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.RequiredArgsConstructor;
import lombok.Setter;

/**
* A KrakenApi instance allows querying the Kraken API.
*
* @author nyg
*/
@Getter
@Setter
@NoArgsConstructor
@RequiredArgsConstructor
public class KrakenApi {

private static final String OTP = "otp";
private static final String NONCE = "nonce";
private static final String MICRO_SECONDS = "000";

/**
* The API key.
*/
private String key;

/**
* The API secret.
*/
private String secret;

/**
Expand Down Expand Up @@ -132,24 +134,6 @@ public String queryPrivate(Method method, Map<String, String> parameters) throws
return queryPrivate(method, null, parameters);
}

/**
* Sets the API key.
*
* @param key the API key
*/
public void setKey(String key) {
this.key = key;
}

/**
* Sets the API secret.
*
* @param secret the API secret
*/
public void setSecret(String secret) {
this.secret = secret;
}

/**
* Represents an API method.
*
Expand Down
@@ -1,4 +1,4 @@
package edu.self.kraken.api;
package edu.self.nyg.kraken.api;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
Expand All @@ -21,8 +21,6 @@ final class KrakenUtils {
private static final String ERROR_NULL_INPUT = "Input can't be null.";
private static final String ERROR_NULL_ARRAYS = "Given arrays can't be null.";

private static final String UTF8 = "UTF-8";

private static final String SHA256 = "SHA-256";
private static final String HMAC_SHA512 = "HmacSHA512";

Expand Down Expand Up @@ -69,7 +67,7 @@ public static byte[] stringToBytes(String input) {
}

public static String urlEncode(String input) throws UnsupportedEncodingException {
return URLEncoder.encode(input, UTF8);
return URLEncoder.encode(input, StandardCharsets.UTF_8);
}

private KrakenUtils() {
Expand Down
26 changes: 22 additions & 4 deletions pom.xml
Expand Up @@ -5,14 +5,32 @@
<modelVersion>4.0.0</modelVersion>

<groupId>edu.self.nyg</groupId>
<artifactId>kraken-api-java</artifactId>
<version>1.0-SNAPSHOT</version>
<artifactId>kraken-api-parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.release>21</maven.compiler.release>

<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<!-- Third-party dependencies -->
<lombok.version>1.18.28</lombok.version>
</properties>

<modules>
<module>library</module>
<module>examples</module>
</modules>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
</dependencyManagement>

</project>
37 changes: 0 additions & 37 deletions src/main/java/edu/self/kraken/Examples.java

This file was deleted.

0 comments on commit f6d8884

Please sign in to comment.