graphenej/src/main/java/com/luminiasoft/bitshares/ws/GetAccountsByAddress.java

90 lines
3.1 KiB
Java
Raw Normal View History

2016-11-25 02:30:04 +00:00
package com.luminiasoft.bitshares.ws;
import com.google.gson.Gson;
2016-11-26 22:39:38 +00:00
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
2016-11-25 02:30:04 +00:00
import com.google.gson.reflect.TypeToken;
import com.luminiasoft.bitshares.RPC;
2016-11-26 22:39:38 +00:00
import com.luminiasoft.bitshares.interfaces.JsonSerializable;
2016-11-25 02:30:04 +00:00
import com.luminiasoft.bitshares.interfaces.WitnessResponseListener;
import com.luminiasoft.bitshares.models.AccountProperties;
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 nelson on 11/15/16.
*/
public class GetAccountsByAddress extends WebSocketAdapter {
2016-11-26 22:39:38 +00:00
private String publicKey;
2016-11-25 02:30:04 +00:00
private WitnessResponseListener mListener;
2016-11-26 22:39:38 +00:00
public GetAccountsByAddress(String publicKey, WitnessResponseListener listener) {
2016-11-25 02:30:04 +00:00
this.publicKey = publicKey;
this.mListener = listener;
}
@Override
public void onConnected(WebSocket websocket, Map<String, List<String>> headers) throws Exception {
ArrayList<Serializable> accountParams = new ArrayList();
2016-11-26 22:39:38 +00:00
ArrayList<JsonSerializable> paramAddress = new ArrayList();
paramAddress.add(new JsonSerializable() {
@Override
public String toJsonString() {
return publicKey;
}
@Override
public JsonElement toJsonObject() {
return new JsonParser().parse(publicKey);
}
});
accountParams.add(paramAddress);
ApiCall getAccountByAddress = new ApiCall(0, RPC.CALL_GET_KEY_REFERENCES, accountParams, "2.0", 1);
2016-11-25 02:30:04 +00:00
websocket.sendText(getAccountByAddress.toJsonString());
}
@Override
public void onTextFrame(WebSocket websocket, WebSocketFrame frame) throws Exception {
2016-11-26 22:39:38 +00:00
System.out.println(frame.toString());
2016-11-25 02:30:04 +00:00
String response = frame.getPayloadText();
Gson gson = new Gson();
2016-11-26 22:39:38 +00:00
Type GetAccountByAddressResponse = new TypeToken<WitnessResponse<List<Object>>>() {
2016-11-25 02:30:04 +00:00
}.getType();
2016-11-26 22:39:38 +00:00
WitnessResponse<WitnessResponse<List<Object>>> witnessResponse = gson.fromJson(response, GetAccountByAddressResponse);
2016-11-25 02:30:04 +00:00
if (witnessResponse.error != null) {
this.mListener.onError(witnessResponse.error);
} else {
this.mListener.onSuccess(witnessResponse);
}
websocket.disconnect();
}
@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();
}
}