Introducing support for the GetMarketHistory api call wrapper using the single connection mode

This commit is contained in:
Nelson R. Perez 2018-06-12 23:27:06 -05:00
parent e056c16aa9
commit 0df3fad6c0
2 changed files with 94 additions and 0 deletions

View file

@ -16,10 +16,12 @@ import cy.agorise.graphenej.Transaction;
import cy.agorise.graphenej.api.calls.GetAccounts;
import cy.agorise.graphenej.api.calls.GetBlock;
import cy.agorise.graphenej.api.calls.GetBlockHeader;
import cy.agorise.graphenej.api.calls.GetMarketHistory;
import cy.agorise.graphenej.api.calls.GetRelativeAccountHistory;
import cy.agorise.graphenej.api.calls.GetRequiredFees;
import cy.agorise.graphenej.models.Block;
import cy.agorise.graphenej.models.BlockHeader;
import cy.agorise.graphenej.models.BucketObject;
import cy.agorise.graphenej.models.OperationHistory;
import cy.agorise.graphenej.objects.Memo;
import cy.agorise.graphenej.operations.CustomOperation;
@ -84,6 +86,12 @@ public class DeserializationMap {
mClassMap.put(GetBlockHeader.class, BlockHeader.class);
mGsonMap.put(GetBlockHeader.class, genericGson);
// GetMarketHistory
mClassMap.put(GetMarketHistory.class, List.class);
Gson getMarketHistoryGson = new GsonBuilder()
.registerTypeAdapter(BucketObject.class, new BucketObject.BucketDeserializer())
.create();
mGsonMap.put(GetMarketHistory.class, getMarketHistoryGson);
}
public Class getReceivedClass(Class _class){

View file

@ -0,0 +1,86 @@
package cy.agorise.graphenej.api.calls;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import cy.agorise.graphenej.Asset;
import cy.agorise.graphenej.RPC;
import cy.agorise.graphenej.api.ApiAccess;
import cy.agorise.graphenej.models.ApiCall;
public class GetMarketHistory implements ApiCallable {
public static final int REQUIRED_API = ApiAccess.API_HISTORY;
public static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyyMMdd'T'HHmmss");
// API call parameters
private Asset base;
private Asset quote;
private long bucket;
private Date start;
private Date end;
/**
* Constructor that receives the start and end time as UNIX timestamp in milliseconds.
*
* @param base Desired asset history
* @param quote Asset to which the base price will be compared to
* @param bucket The time interval (in seconds) for each point should be (analog to
* candles on a candle stick graph).
* @param start Timestamp (POSIX) of of the most recent operation to retrieve
* (Note: The name can be counter intuitive, but it follow the original
* API parameter name)
* @param end Timestamp (POSIX) of the the earliest operation to retrieve
*/
public GetMarketHistory(Asset base, Asset quote, long bucket, long start, long end){
this(base, quote, bucket, fromTimestamp(start), fromTimestamp(end));
}
/**
* Constructor that receives the start and end time as Date instance objects.
*
* @param base Desired asset history
* @param quote Asset to which the base price will be compared to
* @param bucket The time interval (in seconds) for each point should be (analog to
* candles on a candle stick graph).
* @param start Date and time of of the most recent operation to retrieve
* (Note: The name can be counter intuitive, but it follow the original
* API parameter name)
* @param end Date and time of the the earliest operation to retrieve
*/
public GetMarketHistory(Asset base, Asset quote, long bucket, Date start, Date end){
this.base = base;
this.quote = quote;
this.bucket = bucket;
this.start = start;
this.end = end;
}
/**
* Internal method used to convert a timestamp to a Date.
*
* @param timestamp POSIX timestamp expressed in milliseconds since 1/1/1970
* @return Date instance
*/
private static Date fromTimestamp(long timestamp){
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(timestamp);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
return calendar.getTime();
}
@Override
public ApiCall toApiCall(int apiId, long sequenceId) {
ArrayList<Serializable> params = new ArrayList<>();
params.add(this.base.getObjectId());
params.add(this.quote.getObjectId());
params.add(this.bucket);
params.add(DATE_FORMAT.format(this.start));
params.add(DATE_FORMAT.format(this.end));
return new ApiCall(apiId, RPC.CALL_GET_MARKET_HISTORY, params, RPC.VERSION, sequenceId);
}
}