Adding the 'retry' method to both the GetMarketHistory and GetRelativeAccountHistory API call wrappers
This commit is contained in:
parent
9addf3b77f
commit
fb82ce660e
2 changed files with 124 additions and 13 deletions
|
@ -39,9 +39,10 @@ public class GetMarketHistory extends BaseGrapheneHandler {
|
|||
private Date start;
|
||||
private Date end;
|
||||
private WitnessResponseListener mListener;
|
||||
|
||||
private WebSocket mWebsocket;
|
||||
private int currentId = 1;
|
||||
private int apiId = -1;
|
||||
private int counter = 0;
|
||||
|
||||
public GetMarketHistory(Asset base, Asset quote, long bucket, Date start, Date end, WitnessResponseListener listener){
|
||||
super(listener);
|
||||
|
@ -53,8 +54,68 @@ public class GetMarketHistory extends BaseGrapheneHandler {
|
|||
this.mListener = listener;
|
||||
}
|
||||
|
||||
public Asset getBase() {
|
||||
return base;
|
||||
}
|
||||
|
||||
public void setBase(Asset base) {
|
||||
this.base = base;
|
||||
}
|
||||
|
||||
public Asset getQuote() {
|
||||
return quote;
|
||||
}
|
||||
|
||||
public void setQuote(Asset quote) {
|
||||
this.quote = quote;
|
||||
}
|
||||
|
||||
public long getBucket() {
|
||||
return bucket;
|
||||
}
|
||||
|
||||
public void setBucket(long bucket) {
|
||||
this.bucket = bucket;
|
||||
}
|
||||
|
||||
public Date getStart() {
|
||||
return start;
|
||||
}
|
||||
|
||||
public void setStart(Date start) {
|
||||
this.start = start;
|
||||
}
|
||||
|
||||
public Date getEnd() {
|
||||
return end;
|
||||
}
|
||||
|
||||
public void setEnd(Date end) {
|
||||
this.end = end;
|
||||
}
|
||||
|
||||
public int getCount(){
|
||||
return this.counter;
|
||||
}
|
||||
|
||||
public void disconnect(){
|
||||
if(mWebsocket != null && mWebsocket.isOpen()){
|
||||
mWebsocket.disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retries the 'get_market_history' API call.
|
||||
* Hopefully with different 'start' and 'stop' parameters.
|
||||
*/
|
||||
public void retry(){
|
||||
sendHistoricalMarketDataRequest();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onConnected(WebSocket websocket, Map<String, List<String>> headers) throws Exception {
|
||||
mWebsocket = websocket;
|
||||
ArrayList<Serializable> loginParams = new ArrayList<>();
|
||||
loginParams.add(null);
|
||||
loginParams.add(null);
|
||||
|
@ -93,17 +154,37 @@ public class GetMarketHistory extends BaseGrapheneHandler {
|
|||
|
||||
ApiCall getRelativeAccountHistoryCall = new ApiCall(apiId, RPC.CALL_GET_MARKET_HISTORY, params, RPC.VERSION, currentId);
|
||||
websocket.sendText(getRelativeAccountHistoryCall.toJsonString());
|
||||
}else if(baseResponse.id == GET_HISTORY_DATA){
|
||||
}else if(baseResponse.id >= GET_HISTORY_DATA){
|
||||
GsonBuilder builder = new GsonBuilder();
|
||||
Type MarketHistoryResponse = new TypeToken<WitnessResponse<List<BucketObject>>>(){}.getType();
|
||||
builder.registerTypeAdapter(BucketObject.class, new BucketObject.BucketDeserializer());
|
||||
WitnessResponse<List<BucketObject>> marketHistoryResponse = builder.create().fromJson(response, MarketHistoryResponse);
|
||||
mListener.onSuccess(marketHistoryResponse);
|
||||
websocket.disconnect();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Actually sends the 'get_market_history' API call request. This method might be called multiple
|
||||
* times during the life-cycle of this instance because we might not have gotten anything
|
||||
* in the first requested interval.
|
||||
*/
|
||||
private void sendHistoricalMarketDataRequest(){
|
||||
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd'T'HHmmss");
|
||||
|
||||
ArrayList<Serializable> params = new ArrayList<>();
|
||||
params.add(this.base.getObjectId());
|
||||
params.add(this.quote.getObjectId());
|
||||
params.add(this.bucket);
|
||||
params.add(dateFormat.format(this.start));
|
||||
params.add(dateFormat.format(this.end));
|
||||
|
||||
ApiCall getRelativeAccountHistoryCall = new ApiCall(apiId, RPC.CALL_GET_MARKET_HISTORY, params, RPC.VERSION, currentId);
|
||||
mWebsocket.sendText(getRelativeAccountHistoryCall.toJsonString());
|
||||
|
||||
counter++;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFrameSent(WebSocket websocket, WebSocketFrame frame) throws Exception {
|
||||
if(frame.isTextFrame())
|
||||
|
|
|
@ -43,6 +43,7 @@ public class GetRelativeAccountHistory extends BaseGrapheneHandler {
|
|||
private int limit;
|
||||
private int start;
|
||||
private WitnessResponseListener mListener;
|
||||
private WebSocket mWebsocket;
|
||||
|
||||
private int currentId = 1;
|
||||
private int apiId = -1;
|
||||
|
@ -81,6 +82,7 @@ public class GetRelativeAccountHistory extends BaseGrapheneHandler {
|
|||
|
||||
@Override
|
||||
public void onConnected(WebSocket websocket, Map<String, List<String>> headers) throws Exception {
|
||||
mWebsocket = websocket;
|
||||
ArrayList<Serializable> loginParams = new ArrayList<>();
|
||||
loginParams.add(null);
|
||||
loginParams.add(null);
|
||||
|
@ -108,26 +110,54 @@ public class GetRelativeAccountHistory extends BaseGrapheneHandler {
|
|||
WitnessResponse<Integer> witnessResponse = gson.fromJson(response, ApiIdResponse);
|
||||
apiId = witnessResponse.result.intValue();
|
||||
|
||||
ArrayList<Serializable> params = new ArrayList<>();
|
||||
params.add(mUserAccount.toJsonString());
|
||||
params.add(this.stop);
|
||||
params.add(this.limit);
|
||||
params.add(this.start);
|
||||
|
||||
ApiCall getRelativeAccountHistoryCall = new ApiCall(apiId, RPC.CALL_GET_RELATIVE_ACCOUNT_HISTORY, params, RPC.VERSION, currentId);
|
||||
websocket.sendText(getRelativeAccountHistoryCall.toJsonString());
|
||||
}else if(baseResponse.id == GET_HISTORY_DATA){
|
||||
sendRelativeAccountHistoryRequest();
|
||||
}else if(baseResponse.id >= GET_HISTORY_DATA){
|
||||
Type RelativeAccountHistoryResponse = new TypeToken<WitnessResponse<List<HistoricalTransfer>>>(){}.getType();
|
||||
GsonBuilder gsonBuilder = new GsonBuilder();
|
||||
gsonBuilder.registerTypeAdapter(TransferOperation.class, new TransferOperation.TransferDeserializer());
|
||||
gsonBuilder.registerTypeAdapter(AssetAmount.class, new AssetAmount.AssetAmountDeserializer());
|
||||
WitnessResponse<List<HistoricalTransfer>> transfersResponse = gsonBuilder.create().fromJson(response, RelativeAccountHistoryResponse);
|
||||
mListener.onSuccess(transfersResponse);
|
||||
websocket.disconnect();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends the actual get_relative_account_history request.
|
||||
*/
|
||||
private void sendRelativeAccountHistoryRequest(){
|
||||
ArrayList<Serializable> params = new ArrayList<>();
|
||||
params.add(mUserAccount.toJsonString());
|
||||
params.add(this.stop);
|
||||
params.add(this.limit);
|
||||
params.add(this.start);
|
||||
|
||||
ApiCall getRelativeAccountHistoryCall = new ApiCall(apiId, RPC.CALL_GET_RELATIVE_ACCOUNT_HISTORY, params, RPC.VERSION, currentId);
|
||||
mWebsocket.sendText(getRelativeAccountHistoryCall.toJsonString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the arguments and makes a new call to the get_relative_account_history API
|
||||
* @param stop Sequence number of earliest operation
|
||||
* @param limit Maximum number of operations to retrieve (must not exceed 100)
|
||||
* @param start Sequence number of the most recent operation to retrieve
|
||||
*/
|
||||
public void retry(int stop, int limit, int start){
|
||||
this.stop = stop;
|
||||
this.limit = limit;
|
||||
this.start = start;
|
||||
sendRelativeAccountHistoryRequest();
|
||||
}
|
||||
|
||||
/**
|
||||
* Disconnects the websocket
|
||||
*/
|
||||
public void disconnect(){
|
||||
if(mWebsocket != null && mWebsocket.isOpen()){
|
||||
mWebsocket.disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFrameSent(WebSocket websocket, WebSocketFrame frame) throws Exception {
|
||||
if(frame.isTextFrame())
|
||||
|
|
Loading…
Reference in a new issue