mirror of
https://github.com/gosticks/iota.lib.java.git
synced 2025-10-16 11:45:37 +00:00
Merge pull request #29 from jpkrohling/JPK-FixBuilder
Fixed IOTA API Builder
This commit is contained in:
commit
75c433e1e8
10
README.md
10
README.md
@ -35,15 +35,15 @@ Other dependencies:
|
||||
|
||||
Connect to your local node with the default settings is quite straightforward: it requires only 2 lines of code. For example, in order to fetch the Node Info:
|
||||
|
||||
IotaApi api = new IotaApi.Builder.build();
|
||||
GetNodeInfoResponse response = api.getNodeInfo();
|
||||
IotaAPI api = new IotaAPI.Builder().build();
|
||||
GetNodeInfoResponse response = api.getNodeInfo();
|
||||
|
||||
of if you need to connect to a remote node:
|
||||
|
||||
IotaApi api = new IotaApi.Builder
|
||||
IotaAPI api = new IotaAPI.Builder()
|
||||
.protocol("http")
|
||||
.nodeAddress("somewhere_over_the_rainbow")
|
||||
.port(14265)
|
||||
.host("somewhere_over_the_rainbow")
|
||||
.port("14265")
|
||||
.build();
|
||||
|
||||
GetNodeInfoResponse response = api.getNodeInfo();
|
||||
|
||||
@ -13,6 +13,7 @@ import retrofit2.Retrofit;
|
||||
import retrofit2.converter.gson.GsonConverterFactory;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
@ -206,65 +207,77 @@ public class IotaAPICore {
|
||||
return wrapCheckedException(res).body();
|
||||
}
|
||||
|
||||
public String getProtocol() {
|
||||
return protocol;
|
||||
}
|
||||
|
||||
public String getHost() {
|
||||
return host;
|
||||
}
|
||||
|
||||
public String getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static class Builder<T extends Builder<T>> {
|
||||
private FileReader fileReader = null;
|
||||
private BufferedReader bufferedReader = null;
|
||||
private Properties nodeConfig = null;
|
||||
|
||||
String protocol, host, port;
|
||||
|
||||
public IotaAPICore build() {
|
||||
// resolution order: builder value, configuration file, default value
|
||||
|
||||
if (protocol == null || host == null || port == null) {
|
||||
if (null == protocol) {
|
||||
protocol = getFromConfigurationOrEnvironment("iota.node.protocol", "IOTA_NODE_PROTOCOL", "http");
|
||||
}
|
||||
|
||||
// check properties files.
|
||||
if (!checkPropertiesFiles()) {
|
||||
if (null == host) {
|
||||
host = getFromConfigurationOrEnvironment("iota.node.host", "IOTA_NODE_HOST", "localhost");
|
||||
}
|
||||
|
||||
// last resort: best effort on enviroment variable,
|
||||
// before assigning default values.
|
||||
checkEnviromentVariables();
|
||||
}
|
||||
if (null == port) {
|
||||
port = getFromConfigurationOrEnvironment("iota.node.port", "IOTA_NODE_PORT", "14265");
|
||||
}
|
||||
|
||||
return new IotaAPICore(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return
|
||||
*/
|
||||
private boolean checkPropertiesFiles() {
|
||||
|
||||
try {
|
||||
|
||||
FileReader fileReader = new FileReader("node_config.properties");
|
||||
BufferedReader bufferedReader = new BufferedReader(fileReader);
|
||||
|
||||
final Properties nodeConfig = new Properties();
|
||||
nodeConfig.load(bufferedReader);
|
||||
|
||||
if (nodeConfig.getProperty("iota.node.protocol") != null) {
|
||||
protocol = nodeConfig.getProperty("iota.node.protocol");
|
||||
}
|
||||
|
||||
if (nodeConfig.getProperty("iota.node.host") != null) {
|
||||
host = nodeConfig.getProperty("iota.node.host");
|
||||
}
|
||||
|
||||
if (nodeConfig.getProperty("iota.node.port") != null) {
|
||||
port = nodeConfig.getProperty("iota.node.port");
|
||||
}
|
||||
|
||||
} catch (IOException e1) {
|
||||
log.debug("node_config.properties not found. Rolling back for another solution...");
|
||||
private String getFromConfigurationOrEnvironment(String propertyKey, String envName, String defaultValue) {
|
||||
if (getNodeConfig().getProperty(propertyKey) != null) {
|
||||
return nodeConfig.getProperty(propertyKey);
|
||||
} else {
|
||||
return env(envName, defaultValue);
|
||||
}
|
||||
return (port != null && protocol != null && host != null);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private void checkEnviromentVariables() {
|
||||
protocol = env("IOTA_NODE_PROTOCOL", "http");
|
||||
host = env("IOTA_NODE_HOST", "localhost");
|
||||
port = env("IOTA_NODE_PORT", "14265");
|
||||
private Properties getNodeConfig() {
|
||||
if (null != nodeConfig) {
|
||||
return nodeConfig;
|
||||
}
|
||||
|
||||
nodeConfig = new Properties();
|
||||
if (null == fileReader) {
|
||||
try {
|
||||
fileReader = new FileReader("node_config.properties");
|
||||
|
||||
if (null == bufferedReader) {
|
||||
bufferedReader = new BufferedReader(fileReader);
|
||||
}
|
||||
nodeConfig.load(bufferedReader);
|
||||
} catch (IOException e) {
|
||||
log.debug("node_config.properties not found. Rolling back for another solution...");
|
||||
}
|
||||
}
|
||||
|
||||
return nodeConfig;
|
||||
}
|
||||
|
||||
public T config(Properties properties) {
|
||||
nodeConfig = properties;
|
||||
return (T) this;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -17,6 +17,7 @@ import org.junit.Test;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertThat;
|
||||
@ -76,8 +77,40 @@ public class IotaAPITest {
|
||||
public void shouldCreateIotaApiProxyInstanceWithDefaultValues() {
|
||||
IotaAPI proxy = new IotaAPI.Builder().build();
|
||||
assertThat(proxy, IsNull.notNullValue());
|
||||
assertThat(proxy.getHost(), Is.is("localhost"));
|
||||
assertThat(proxy.getPort(), Is.is("14265"));
|
||||
assertThat(proxy.getProtocol(), Is.is("http"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldRetainValuesFromBuilder() {
|
||||
IotaAPI proxy = new IotaAPI.Builder().host("somewhere_over_the_rainbow").build();
|
||||
assertThat(proxy.getHost(), Is.is("somewhere_over_the_rainbow"));
|
||||
|
||||
proxy = new IotaAPI.Builder().port("15515").build();
|
||||
assertThat(proxy.getPort(), Is.is("15515"));
|
||||
|
||||
proxy = new IotaAPI.Builder().protocol("https").build();
|
||||
assertThat(proxy.getProtocol(), Is.is("https"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldGetValuesFromProperties() {
|
||||
Properties properties = new Properties();
|
||||
properties.put("iota.node.host", "somewhere_over_the_rainbow");
|
||||
IotaAPI proxy = new IotaAPI.Builder().config(properties).build();
|
||||
assertThat(proxy.getHost(), Is.is("somewhere_over_the_rainbow"));
|
||||
|
||||
properties = new Properties();
|
||||
properties.put("iota.node.port", "15515");
|
||||
proxy = new IotaAPI.Builder().config(properties).build();
|
||||
assertThat(proxy.getPort(), Is.is("15515"));
|
||||
|
||||
properties = new Properties();
|
||||
properties.put("iota.node.protocol", "https");
|
||||
proxy = new IotaAPI.Builder().config(properties).build();
|
||||
assertThat(proxy.getProtocol(), Is.is("https"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldGetInputs() throws InvalidSecurityLevelException, InvalidAddressException {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user