From 53067530f86ff0febedcb487e2fddb7b132e8491 Mon Sep 17 00:00:00 2001 From: davassi Date: Fri, 7 Oct 2016 15:24:27 +0200 Subject: [PATCH] implemented getTrytes command request --- README.md | 6 ++-- src/main/java/jota/IotaAPIProxy.java | 22 ++++++++++---- src/main/java/jota/IotaAPIService.java | 30 +++++++++++++------ .../request/IotaFindTransactionsRequest.java | 8 ++--- .../dto/request/IotaGetTransferRequest.java | 7 +++-- .../dto/request/IotaGetTrytesRequest.java | 17 +++++++++++ .../dto/response/GetTransfersResponse.java | 8 +++++ .../jota/dto/response/GetTrytesResponse.java | 10 +++++++ src/test/java/jota/IotaAPIProxyTest.java | 29 ++++++++++++++++-- 9 files changed, 109 insertions(+), 28 deletions(-) create mode 100644 src/main/java/jota/dto/request/IotaGetTrytesRequest.java create mode 100644 src/main/java/jota/dto/response/GetTrytesResponse.java diff --git a/README.md b/README.md index 9d331b9..f082bc9 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ It allows to connect easily using java directly to a local or a remote [[IOTA]]( * **Compatibility:** IOTA IRI v1.0.6 * **API coverage:** 20 of 20 commands fully implemented * **License:** Apache License 2.0 -* **Readme updated:** 2016-02-17 10:16:43 (UTC) +* **Readme updated:** 2016-10-07 14:23:43 (UTC) A list of all *IOTA* JSON-REST API commands currently supported by jota wrapper can be found in the `Commands` enum (see [here](IotaAPICommands.java) for more details). @@ -21,9 +21,7 @@ The JOTA library has been designed to be used exclusively with Java 8+. Core dependencies: * Retrofit Client 2.1.0 [[link]](https://square.github.io/retrofit/) -* Gson JSON Processor : - * Annotations 2.5.0 (`jackson-annotations`) [[link]](https://github.com/FasterXML/jackson-annotations) - * Databind 2.5.0 (`jackson-databind`) [[link]](https://github.com/FasterXML/jackson-databind) +* Gson JSON Processor : [[link]](https://github.com/google/gson) * Lombok 1.16.2 [[link]](https://github.com/rzwitserloot/lombok) Other dependencies: diff --git a/src/main/java/jota/IotaAPIProxy.java b/src/main/java/jota/IotaAPIProxy.java index 421abee..9d0fc66 100644 --- a/src/main/java/jota/IotaAPIProxy.java +++ b/src/main/java/jota/IotaAPIProxy.java @@ -4,6 +4,7 @@ import java.io.BufferedReader; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; +import java.util.Arrays; import java.util.Collection; import java.util.Optional; import java.util.Properties; @@ -81,7 +82,8 @@ public class IotaAPIProxy { } public GetTransfersResponse getTransfers(String seed, Integer securityLevel) { - final Call res = service.getTransfers(IotaGetTransferRequest.createGetTransferRequest(seed, securityLevel)); + final IotaGetTransferRequest tr = IotaGetTransferRequest.createGetTransferRequest(seed, securityLevel); + final Call res = service.getTransfers(tr); return wrapCheckedException(res).body(); } @@ -98,19 +100,19 @@ public class IotaAPIProxy { return wrapCheckedException(res).body(); } - public FindTransactionResponse findTransactionsByAddresses(String [] addresses) { + public FindTransactionResponse findTransactionsByAddresses(final String ... addresses) { return findTransactions(addresses, null, null, null); } - public FindTransactionResponse findTransactionsByBundles(String [] bundles) { + public FindTransactionResponse findTransactionsByBundles(final String ... bundles) { return findTransactions(null, null, null, bundles); } - public FindTransactionResponse findTransactionsByApprovees(String [] approvees) { + public FindTransactionResponse findTransactionsByApprovees(final String ... approvees) { return findTransactions(null, null, approvees, null); } - public FindTransactionResponse findTransactionsByDigests(String [] digests) { + public FindTransactionResponse findTransactionsByDigests(final String ... digests) { return findTransactions(null, digests, null, null); } @@ -126,6 +128,16 @@ public class IotaAPIProxy { return wrapCheckedException(res).body(); } + public GetBundleResponse getBundle(String transaction) { + final Call res = service.getBundle(IotaGetBundleRequest.createIotaGetBundleRequest(transaction)); + return wrapCheckedException(res).body(); + } + + public GetTrytesResponse getTrytes(String ... hashes) { + final Call res = service.getTrytes(IotaGetTrytesRequest.createGetTrytesRequest(hashes)); + return wrapCheckedException(res).body(); + } + protected static Response wrapCheckedException(final Call call) { try { final Response res = call.execute(); diff --git a/src/main/java/jota/IotaAPIService.java b/src/main/java/jota/IotaAPIService.java index 0e4c20a..d38a1ce 100644 --- a/src/main/java/jota/IotaAPIService.java +++ b/src/main/java/jota/IotaAPIService.java @@ -53,19 +53,19 @@ public interface IotaAPIService { /** * Get the list of latest tips (unconfirmed transactions). - * + * * curl http://localhost:14265 \ -X POST \ -H 'Content-Type: * application/json' \ -d '{"command": "getTips"}' */ @Headers({ CONTENT_TYPE_HEADER, USER_AGENT_HEADER }) @POST("./") Call getTips(@Body IotaCommandRequest request); - + /** - * Get the list of latest tips (unconfirmed transactions). - * - * curl http://localhost:14265 \ -X POST \ -H 'Content-Type: - * application/json' \ -d '{"command": "getTips"}' + * Get the list of transfers from a specified seed (account). + * + * curl http://localhost:14265 -X POST -H 'Content-Type: application/json' + * -d '{"command": "getTransfers", "seed": "AAA999999999999999999999999999999999999999999999999999999999999999999999999999999", "securityLevel": 1}' */ @Headers({ CONTENT_TYPE_HEADER, USER_AGENT_HEADER }) @POST("./") @@ -88,22 +88,34 @@ public interface IotaAPIService { * * curl http://localhost:14265 -X POST -H 'Content-Type: application/json' * -d '{"command": "getInclusionStates", "transactions"Q9HZWYKFWYWZRE9JQKG9REPKIASHUUECPSQO9JT9XNMVKWYGVAZETAIRPTM"], "tips" : []}' - * - * */ @Headers({ CONTENT_TYPE_HEADER, USER_AGENT_HEADER }) @POST("./") Call getInclusionStates(@Body IotaGetInclusionStateRequest request); + /** + * Get the list of transactions which were bundled with the specified tail transaction. This call returns the full value of all individual transactions, not just the hashes. + * + * curl http://localhost:14265 -X POST -H 'Content-Type: application/json' + * -d '{"command": "getBundle", "transaction": "ZJVYUGTDRPDYFGFXMKOTV9ZWSGFK9CFPXTITQLQNLPPG9YNAARMKNKYQO9GSCSBIOTGMLJUFLZWSY9999"}' + * + */ @Headers({ CONTENT_TYPE_HEADER, USER_AGENT_HEADER }) @POST("./") Call getBundle(@Body IotaGetBundleRequest request); - /* + /** + * Returns the raw trytes data of a transaction. + * + * curl http://localhost:14265 -X POST -H 'Content-Type: application/json' + * -d '{"command": "getTrytes", "hashes": ["OAATQS9VQLSXCLDJVJJVYUGONXAXOFMJOZNSYWRZSWECMXAQQURHQBJNLD9IOFEPGZEPEMPXCIVRX9999"]}' + * + */ @Headers({ CONTENT_TYPE_HEADER, USER_AGENT_HEADER }) @POST("./") Call getTrytes(@Body IotaGetTrytesRequest request); + /* @Headers({ CONTENT_TYPE_HEADER, USER_AGENT_HEADER }) @POST("./") Call analyzeTransactions(@Body IotaAnalyzeTransactionRequest request); diff --git a/src/main/java/jota/dto/request/IotaFindTransactionsRequest.java b/src/main/java/jota/dto/request/IotaFindTransactionsRequest.java index 537fb4f..4bd838b 100644 --- a/src/main/java/jota/dto/request/IotaFindTransactionsRequest.java +++ b/src/main/java/jota/dto/request/IotaFindTransactionsRequest.java @@ -17,22 +17,22 @@ public class IotaFindTransactionsRequest extends IotaCommandRequest { return new IotaFindTransactionsRequest(); } - public IotaFindTransactionsRequest byBundles(String [] bundles) { + public IotaFindTransactionsRequest byBundles(String ... bundles) { this.bundles = bundles; return this; } - public IotaFindTransactionsRequest byAddresses(String [] addresses) { + public IotaFindTransactionsRequest byAddresses(String ... addresses) { this.addresses = addresses; return this; } - public IotaFindTransactionsRequest byDigests(String [] digests) { + public IotaFindTransactionsRequest byDigests(String ... digests) { this.digests = digests; return this; } - public IotaFindTransactionsRequest byApprovees(String [] approvees) { + public IotaFindTransactionsRequest byApprovees(String ... approvees) { this.approvees = approvees; return this; } diff --git a/src/main/java/jota/dto/request/IotaGetTransferRequest.java b/src/main/java/jota/dto/request/IotaGetTransferRequest.java index 15ec5da..82512dd 100644 --- a/src/main/java/jota/dto/request/IotaGetTransferRequest.java +++ b/src/main/java/jota/dto/request/IotaGetTransferRequest.java @@ -1,19 +1,20 @@ package jota.dto.request; +import com.google.gson.Gson; import jota.IotaAPICommands; public class IotaGetTransferRequest extends IotaCommandRequest { private String seed; - private String securityLevel; + private Integer securityLevel; - private IotaGetTransferRequest(final String seed, final String securityLevel) { + private IotaGetTransferRequest(final String seed, final Integer securityLevel) { super(IotaAPICommands.GET_TRANSFER); this.seed = seed; this.securityLevel = securityLevel; } public static IotaGetTransferRequest createGetTransferRequest(String seed, Integer securityLevel) { - return new IotaGetTransferRequest(seed, String.valueOf(securityLevel)); + return new IotaGetTransferRequest(seed, securityLevel); } } diff --git a/src/main/java/jota/dto/request/IotaGetTrytesRequest.java b/src/main/java/jota/dto/request/IotaGetTrytesRequest.java new file mode 100644 index 0000000..efed7f7 --- /dev/null +++ b/src/main/java/jota/dto/request/IotaGetTrytesRequest.java @@ -0,0 +1,17 @@ +package jota.dto.request; + +import jota.IotaAPICommands; + +public class IotaGetTrytesRequest extends IotaCommandRequest { + + private String [] hashes; + + private IotaGetTrytesRequest(final String ... hashes) { + super(IotaAPICommands.GET_TRYTES); + this.hashes = hashes; + } + + public static IotaGetTrytesRequest createGetTrytesRequest(String ... hashes) { + return new IotaGetTrytesRequest(hashes); + } +} diff --git a/src/main/java/jota/dto/response/GetTransfersResponse.java b/src/main/java/jota/dto/response/GetTransfersResponse.java index 9d9960e..1a94863 100644 --- a/src/main/java/jota/dto/response/GetTransfersResponse.java +++ b/src/main/java/jota/dto/response/GetTransfersResponse.java @@ -1,5 +1,8 @@ package jota.dto.response; +import org.apache.commons.lang3.builder.ToStringBuilder; +import org.apache.commons.lang3.builder.ToStringStyle; + public class GetTransfersResponse extends AbstractResponse { private Transfers[] transfers; @@ -26,6 +29,11 @@ public class GetTransfersResponse extends AbstractResponse { public String getValue() { return value; } + + @Override + public String toString() { + return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE); + } } public Transfers[] getTransfers() { diff --git a/src/main/java/jota/dto/response/GetTrytesResponse.java b/src/main/java/jota/dto/response/GetTrytesResponse.java new file mode 100644 index 0000000..cac912d --- /dev/null +++ b/src/main/java/jota/dto/response/GetTrytesResponse.java @@ -0,0 +1,10 @@ +package jota.dto.response; + +public class GetTrytesResponse extends AbstractResponse { + + private String [] trites; + + public String[] getTrites() { + return trites; + } +} diff --git a/src/test/java/jota/IotaAPIProxyTest.java b/src/test/java/jota/IotaAPIProxyTest.java index f329993..fdb2702 100644 --- a/src/test/java/jota/IotaAPIProxyTest.java +++ b/src/test/java/jota/IotaAPIProxyTest.java @@ -14,6 +14,11 @@ import org.junit.Test; */ public class IotaAPIProxyTest { + private static final String TEST_SEED = "AAA999999999999999999999999999999999999999999999999999999999999999999999999999999"; + private static final String TEST_TRANSACTION = "ZJVYUGTDRPDYFGFXMKOTV9ZWSGFK9CFPXTITQLQNLPPG9YNAARMKNKYQO9GSCSBIOTGMLJUFLZWSY9999"; + private static final String TEST_HASH = "OAATQS9VQLSXCLDJVJJVYUGONXAXOFMJOZNSYWRZSWECMXAQQURHQBJNLD9IOFEPGZEPEMPXCIVRX9999"; + private static final Integer TEST_MILESTONE_INDEX = 8059; + private IotaAPIProxy proxy; @Before @@ -40,7 +45,7 @@ public class IotaAPIProxyTest { @Test public void shouldGetMilestone() { - GetMilestoneResponse milestone = proxy.getMilestone(1000); + GetMilestoneResponse milestone = proxy.getMilestone(TEST_MILESTONE_INDEX); assertThat(milestone, IsNull.notNullValue()); } @@ -58,7 +63,7 @@ public class IotaAPIProxyTest { @Test public void shouldFindTransactionsByAddresses() { - FindTransactionResponse trans = proxy.findTransactionsByAddresses(new String[]{"123ABC"}); + FindTransactionResponse trans = proxy.findTransactionsByAddresses(TEST_TRANSACTION); assertThat(trans, IsNull.notNullValue()); } @@ -80,10 +85,28 @@ public class IotaAPIProxyTest { assertThat(trans, IsNull.notNullValue()); } + @Test + public void shouldGetTransactions() { + GetTransfersResponse res = proxy.getTransfers(TEST_SEED, 1); + assertThat(res, IsNull.notNullValue()); + } + @Test public void shouldGetInclusionStates() { - GetInclusionStateResponse res = proxy.getInclusionStates(new String[]{"123ABC"}, + GetInclusionStateResponse res = proxy.getInclusionStates(new String[]{TEST_TRANSACTION}, new String[]{"123"}); assertThat(res, IsNull.notNullValue()); } + + @Test + public void shouldGetBundle() { + GetBundleResponse res = proxy.getBundle(TEST_TRANSACTION); + assertThat(res, IsNull.notNullValue()); + } + + @Test + public void shouldGetTrytes() { + GetTrytesResponse res = proxy.getTrytes(TEST_HASH); + assertThat(res, IsNull.notNullValue()); + } }