Adding support for the 'lookup_accounts' API call
This commit is contained in:
parent
67eb4953d0
commit
e6c53b074e
5 changed files with 171 additions and 3 deletions
|
@ -57,7 +57,7 @@ public class Main {
|
||||||
// test.testGetAccountByName();
|
// test.testGetAccountByName();
|
||||||
// test.testGetRequiredFees();
|
// test.testGetRequiredFees();
|
||||||
// test.testRandomNumberGeneration();
|
// test.testRandomNumberGeneration();
|
||||||
test.testBrainKeyOperations(false);
|
// test.testBrainKeyOperations(false);
|
||||||
// test.testBip39Opertion();
|
// test.testBip39Opertion();
|
||||||
// test.testAccountNamebyAddress();
|
// test.testAccountNamebyAddress();
|
||||||
// test.testAccountNameById();
|
// test.testAccountNameById();
|
||||||
|
@ -68,5 +68,6 @@ public class Main {
|
||||||
// test.testAccountUpdateOperationBroadcast();
|
// test.testAccountUpdateOperationBroadcast();
|
||||||
// test.testCreateBinFile();
|
// test.testCreateBinFile();
|
||||||
// test.testImportBinFile();
|
// test.testImportBinFile();
|
||||||
|
test.testLookupAccounts();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,4 +15,5 @@ public class RPC {
|
||||||
public static final String CALL_GET_ACCOUNTS = "get_accounts";
|
public static final String CALL_GET_ACCOUNTS = "get_accounts";
|
||||||
public static final String CALL_GET_KEY_REFERENCES = "get_key_references";
|
public static final String CALL_GET_KEY_REFERENCES = "get_key_references";
|
||||||
public static final String CALL_GET_RELATIVE_ACCOUNT_HISTORY = "get_relative_account_history";
|
public static final String CALL_GET_RELATIVE_ACCOUNT_HISTORY = "get_relative_account_history";
|
||||||
|
public static final String LOOKUP_ACCOUNTS = "lookup_accounts";
|
||||||
}
|
}
|
||||||
|
|
|
@ -650,4 +650,39 @@ public class Test {
|
||||||
System.out.println("WebSocketException. Msg: "+e.getMessage());
|
System.out.println("WebSocketException. Msg: "+e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testLookupAccounts(){
|
||||||
|
WitnessResponseListener listener = new WitnessResponseListener() {
|
||||||
|
@Override
|
||||||
|
public void onSuccess(WitnessResponse response) {
|
||||||
|
System.out.println("onSuccess");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onError(BaseResponse.Error error) {
|
||||||
|
System.out.println("onError");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
SSLContext context = null;
|
||||||
|
try {
|
||||||
|
context = NaiveSSLContext.getInstance("TLS");
|
||||||
|
WebSocketFactory factory = new WebSocketFactory();
|
||||||
|
|
||||||
|
// Set the custom SSL context.
|
||||||
|
factory.setSSLContext(context);
|
||||||
|
|
||||||
|
WebSocket mWebSocket = factory.createSocket(OPENLEDGER_WITNESS_URL);
|
||||||
|
|
||||||
|
mWebSocket.addListener(new LookupAccounts("bilthon", listener));
|
||||||
|
mWebSocket.connect();
|
||||||
|
|
||||||
|
} catch (NoSuchAlgorithmException e) {
|
||||||
|
System.out.println("NoSuchAlgorithmException. Msg: "+e.getMessage());
|
||||||
|
} catch (WebSocketException e) {
|
||||||
|
System.out.println("WebSocketException. Msg: "+e.getMessage());
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.out.println("IOException. Msg: "+e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
package com.luminiasoft.bitshares;
|
package com.luminiasoft.bitshares;
|
||||||
|
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.*;
|
||||||
import com.luminiasoft.bitshares.interfaces.ByteSerializable;
|
import com.luminiasoft.bitshares.interfaces.ByteSerializable;
|
||||||
import com.luminiasoft.bitshares.interfaces.JsonSerializable;
|
import com.luminiasoft.bitshares.interfaces.JsonSerializable;
|
||||||
|
|
||||||
|
@ -8,6 +8,7 @@ import java.io.ByteArrayOutputStream;
|
||||||
import java.io.DataOutput;
|
import java.io.DataOutput;
|
||||||
import java.io.DataOutputStream;
|
import java.io.DataOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.lang.reflect.Type;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class tha represents a graphene user account.
|
* Class tha represents a graphene user account.
|
||||||
|
@ -17,15 +18,35 @@ public class UserAccount extends GrapheneObject implements ByteSerializable, Jso
|
||||||
|
|
||||||
public static final String PROXY_TO_SELF = "1.2.5";
|
public static final String PROXY_TO_SELF = "1.2.5";
|
||||||
|
|
||||||
|
private String accountName;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor that expects a user account in the string representation.
|
* Constructor that expects a user account in the string representation.
|
||||||
* That is in the 1.2.x format.
|
* That is in the 1.2.x format.
|
||||||
* @param id: The string representing the account apiId.
|
* @param id: The string representing the user account.
|
||||||
*/
|
*/
|
||||||
public UserAccount(String id) {
|
public UserAccount(String id) {
|
||||||
super(id);
|
super(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor that expects a user account withe the proper graphene object id and an account name.
|
||||||
|
* @param id: The string representing the user account.
|
||||||
|
* @param name: The name of this user account.
|
||||||
|
*/
|
||||||
|
public UserAccount(String id, String name){
|
||||||
|
super(id);
|
||||||
|
this.accountName = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Getter for the account name field.
|
||||||
|
* @return: The name of this account.
|
||||||
|
*/
|
||||||
|
public String getAccountName() {
|
||||||
|
return accountName;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public byte[] toBytes() {
|
public byte[] toBytes() {
|
||||||
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
||||||
|
@ -52,4 +73,24 @@ public class UserAccount extends GrapheneObject implements ByteSerializable, Jso
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return this.toJsonString();
|
return this.toJsonString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Custom deserializer used to deserialize user accounts provided as response from the 'lookup_accounts' api call.
|
||||||
|
* This response contains serialized user accounts in the form [[{id1},{name1}][{id1},{name1}]].
|
||||||
|
*
|
||||||
|
* For instance:
|
||||||
|
* [["bilthon-1","1.2.139205"],["bilthon-2","1.2.139207"],["bilthon-2016","1.2.139262"]]
|
||||||
|
*
|
||||||
|
* So this class will pick up this data and turn it into a UserAccount object.
|
||||||
|
*/
|
||||||
|
public static class UserAccountDeserializer implements JsonDeserializer<UserAccount> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public UserAccount deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
|
||||||
|
JsonArray array = json.getAsJsonArray();
|
||||||
|
String name = array.get(0).getAsString();
|
||||||
|
String id = array.get(1).getAsString();
|
||||||
|
return new UserAccount(id, name);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,90 @@
|
||||||
|
package com.luminiasoft.bitshares.ws;
|
||||||
|
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
import com.google.gson.GsonBuilder;
|
||||||
|
import com.google.gson.JsonArray;
|
||||||
|
import com.google.gson.reflect.TypeToken;
|
||||||
|
import com.luminiasoft.bitshares.RPC;
|
||||||
|
import com.luminiasoft.bitshares.UserAccount;
|
||||||
|
import com.luminiasoft.bitshares.interfaces.WitnessResponseListener;
|
||||||
|
import com.luminiasoft.bitshares.models.ApiCall;
|
||||||
|
import com.luminiasoft.bitshares.models.BaseResponse;
|
||||||
|
import com.luminiasoft.bitshares.models.WitnessResponse;
|
||||||
|
import com.neovisionaries.ws.client.WebSocket;
|
||||||
|
import com.neovisionaries.ws.client.WebSocketAdapter;
|
||||||
|
import com.neovisionaries.ws.client.WebSocketException;
|
||||||
|
import com.neovisionaries.ws.client.WebSocketFrame;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.lang.reflect.Type;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by henry on 07/12/16.
|
||||||
|
*/
|
||||||
|
public class LookupAccounts extends WebSocketAdapter {
|
||||||
|
|
||||||
|
public static final int DEFAULT_MAX = 1000;
|
||||||
|
private final String accountName;
|
||||||
|
private int maxAccounts = DEFAULT_MAX;
|
||||||
|
private final WitnessResponseListener mListener;
|
||||||
|
|
||||||
|
public LookupAccounts(String accountName, WitnessResponseListener listener){
|
||||||
|
this.accountName = accountName;
|
||||||
|
this.maxAccounts = DEFAULT_MAX;
|
||||||
|
this.mListener = listener;
|
||||||
|
}
|
||||||
|
|
||||||
|
public LookupAccounts(String accountName, int maxAccounts, WitnessResponseListener listener){
|
||||||
|
this.accountName = accountName;
|
||||||
|
this.maxAccounts = maxAccounts;
|
||||||
|
this.mListener = listener;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onConnected(WebSocket websocket, Map<String, List<String>> headers) throws Exception {
|
||||||
|
ArrayList<Serializable> accountParams = new ArrayList<>();
|
||||||
|
accountParams.add(this.accountName);
|
||||||
|
accountParams.add(this.maxAccounts);
|
||||||
|
ApiCall getAccountByName = new ApiCall(0, RPC.LOOKUP_ACCOUNTS, accountParams, RPC.VERSION, 1);
|
||||||
|
websocket.sendText(getAccountByName.toJsonString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onTextFrame(WebSocket websocket, WebSocketFrame frame) throws Exception {
|
||||||
|
System.out.println("<<< "+frame.getPayloadText());
|
||||||
|
String response = frame.getPayloadText();
|
||||||
|
|
||||||
|
Type LookupAccountsResponse = new TypeToken<WitnessResponse<List<UserAccount>>>(){}.getType();
|
||||||
|
GsonBuilder builder = new GsonBuilder();
|
||||||
|
builder.registerTypeAdapter(UserAccount.class, new UserAccount.UserAccountDeserializer());
|
||||||
|
WitnessResponse<List<UserAccount>> witnessResponse = builder.create().fromJson(response, LookupAccountsResponse);
|
||||||
|
if(witnessResponse.error != null){
|
||||||
|
this.mListener.onError(witnessResponse.error);
|
||||||
|
}else{
|
||||||
|
this.mListener.onSuccess(witnessResponse);
|
||||||
|
}
|
||||||
|
|
||||||
|
websocket.disconnect();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onFrameSent(WebSocket websocket, WebSocketFrame frame) throws Exception {
|
||||||
|
if(frame.isTextFrame())
|
||||||
|
System.out.println(">>> "+frame.getPayloadText());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onError(WebSocket websocket, WebSocketException cause) throws Exception {
|
||||||
|
mListener.onError(new BaseResponse.Error(cause.getMessage()));
|
||||||
|
websocket.disconnect();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void handleCallbackError(WebSocket websocket, Throwable cause) throws Exception {
|
||||||
|
mListener.onError(new BaseResponse.Error(cause.getMessage()));
|
||||||
|
websocket.disconnect();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue