-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #71 from Mulavar/feat-limit
feat(*): add block、tx query services with limit
- Loading branch information
Showing
10 changed files
with
451 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package cn.hyperchain.sdk.response; | ||
|
||
import cn.hyperchain.sdk.response.block.BlockResponse; | ||
import cn.hyperchain.sdk.response.tx.TxResponse; | ||
import com.google.gson.Gson; | ||
import com.google.gson.GsonBuilder; | ||
import com.google.gson.JsonArray; | ||
import com.google.gson.JsonElement; | ||
import com.google.gson.annotations.Expose; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class PageResult<T> { | ||
@Expose | ||
private String hasmore; | ||
@Expose | ||
private JsonElement data; | ||
Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create(); | ||
|
||
/** | ||
* parse result to real result(blocks or transactions). | ||
* | ||
* @param clz can only be TxResponse.Transaction.class or BlockResponse.Block.class | ||
* @return parse result | ||
*/ | ||
public List<T> parseResult(Class clz) { | ||
if (clz != BlockResponse.Block.class && clz != TxResponse.Transaction.class) { | ||
throw new RuntimeException("method argument `class` must be BlockResponse.Block.class or TxResponse.Transaction.class!"); | ||
} | ||
ArrayList<T> results = new ArrayList<>(); | ||
|
||
if (data.isJsonArray()) { | ||
JsonArray jsonArray = data.getAsJsonArray(); | ||
for (JsonElement jsonElement : jsonArray) { | ||
results.add((T) gson.fromJson(jsonElement, clz)); | ||
} | ||
} else { | ||
T block = (T) gson.fromJson(data, clz); | ||
results.add(block); | ||
} | ||
return results; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "PageResult{" + | ||
"hasmore='" + hasmore + '\'' + | ||
", data=" + data + | ||
'}'; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/cn/hyperchain/sdk/response/block/BlockLimitResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package cn.hyperchain.sdk.response.block; | ||
|
||
import cn.hyperchain.sdk.response.PageResult; | ||
import cn.hyperchain.sdk.response.Response; | ||
import com.google.gson.annotations.Expose; | ||
|
||
import java.util.List; | ||
|
||
public class BlockLimitResponse extends Response { | ||
@Expose | ||
private PageResult<BlockResponse.Block> result; | ||
|
||
public List<BlockResponse.Block> getResult() { | ||
return result.parseResult(BlockResponse.Block.class); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "BlockLimitResponse{" + | ||
"result=" + result + | ||
", jsonrpc='" + jsonrpc + '\'' + | ||
", id='" + id + '\'' + | ||
", code=" + code + | ||
", message='" + message + '\'' + | ||
'}'; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/cn/hyperchain/sdk/response/tx/TxLimitResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package cn.hyperchain.sdk.response.tx; | ||
|
||
import cn.hyperchain.sdk.response.PageResult; | ||
import cn.hyperchain.sdk.response.Response; | ||
import com.google.gson.annotations.Expose; | ||
|
||
import java.util.List; | ||
|
||
public class TxLimitResponse extends Response { | ||
@Expose | ||
private PageResult<TxResponse.Transaction> result; | ||
|
||
public List<TxResponse.Transaction> getResult() { | ||
return result.parseResult(TxResponse.Transaction.class); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "TxLimitResponse{" + | ||
"result=" + result + | ||
", jsonrpc='" + jsonrpc + '\'' + | ||
", id='" + id + '\'' + | ||
", code=" + code + | ||
", message='" + message + '\'' + | ||
'}'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.