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

78 lines
2.9 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;
2016-11-27 00:02:16 +00:00
import com.luminiasoft.bitshares.Address;
2016-11-25 02:30:04 +00:00
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-27 00:02:16 +00:00
private Address address;
2016-11-25 02:30:04 +00:00
private WitnessResponseListener mListener;
2016-11-27 00:02:16 +00:00
public GetAccountsByAddress(Address address, WitnessResponseListener listener) {
this.address = address;
2016-11-25 02:30:04 +00:00
this.mListener = listener;
}
@Override
public void onConnected(WebSocket websocket, Map<String, List<String>> headers) throws Exception {
ArrayList<Serializable> accountParams = new ArrayList();
2016-11-27 00:02:16 +00:00
ArrayList<Serializable> paramAddress = new ArrayList();
paramAddress.add(address.toString());
2016-11-26 22:39:38 +00:00
accountParams.add(paramAddress);
2016-11-27 00:02:16 +00:00
ApiCall getAccountByAddress = new ApiCall(0, RPC.CALL_GET_KEY_REFERENCES, accountParams, RPC.VERSION, 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-27 00:02:16 +00:00
System.out.println("<<< "+frame.getPayloadText());
2016-11-25 02:30:04 +00:00
String response = frame.getPayloadText();
Gson gson = new Gson();
2016-11-27 00:02:16 +00:00
Type GetAccountByAddressResponse = new TypeToken<WitnessResponse<List<String>>>(){}.getType();
WitnessResponse<WitnessResponse<List<String>>> 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();
}
}