Added support for the list_assets API call wrapper in the single connection mode

This commit is contained in:
Nelson R. Perez 2018-07-25 13:08:09 -05:00
parent e0457c2ad8
commit 140cab48cd
6 changed files with 95 additions and 1 deletions

View file

@ -21,6 +21,7 @@ import cy.agorise.graphenej.api.calls.GetMarketHistory;
import cy.agorise.graphenej.api.calls.GetObjects;
import cy.agorise.graphenej.api.calls.GetRelativeAccountHistory;
import cy.agorise.graphenej.api.calls.GetRequiredFees;
import cy.agorise.graphenej.api.calls.ListAssets;
import cy.agorise.graphenej.api.calls.LookupAssetSymbols;
import cy.agorise.graphenej.models.Block;
import cy.agorise.graphenej.models.BlockHeader;
@ -109,6 +110,13 @@ public class DeserializationMap {
.registerTypeAdapter(Asset.class, new Asset.AssetDeserializer())
.create();
mGsonMap.put(GetObjects.class, getObjectsGson);
// ListAssets
mClassMap.put(ListAssets.class, List.class);
Gson listAssetsGson = new GsonBuilder()
.registerTypeAdapter(Asset.class, new Asset.AssetDeserializer())
.create();
mGsonMap.put(ListAssets.class, listAssetsGson);
}
public Class getReceivedClass(Class _class){

View file

@ -17,6 +17,7 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import cy.agorise.graphenej.Asset;
import cy.agorise.graphenej.AssetAmount;
import cy.agorise.graphenej.RPC;
import cy.agorise.graphenej.api.ApiAccess;
@ -28,6 +29,7 @@ import cy.agorise.graphenej.api.calls.GetMarketHistory;
import cy.agorise.graphenej.api.calls.GetObjects;
import cy.agorise.graphenej.api.calls.GetRelativeAccountHistory;
import cy.agorise.graphenej.api.calls.GetRequiredFees;
import cy.agorise.graphenej.api.calls.ListAssets;
import cy.agorise.graphenej.models.AccountProperties;
import cy.agorise.graphenej.models.ApiCall;
import cy.agorise.graphenej.models.Block;
@ -317,6 +319,9 @@ public class NetworkService extends Service {
parsedResponse = gson.fromJson(text, GetMarketHistoryResponse);
}else if(requestClass == GetObjects.class){
parsedResponse = handleGetObject(text);
}else if(requestClass == ListAssets.class){
Type LisAssetsResponse = new TypeToken<JsonRpcResponse<List<Asset>>>(){}.getType();
parsedResponse = gson.fromJson(text, LisAssetsResponse);
}else{
Log.w(TAG,"Unknown request class");
}

View file

@ -0,0 +1,44 @@
package cy.agorise.graphenej.api.calls;
import java.io.Serializable;
import java.util.ArrayList;
import cy.agorise.graphenej.RPC;
import cy.agorise.graphenej.api.ApiAccess;
import cy.agorise.graphenej.models.ApiCall;
public class ListAssets implements ApiCallable {
public static final int REQUIRED_API = ApiAccess.API_DATABASE;
/**
* Constant that must be used as argument to the constructor of this class to indicate
* that the user wants to get all existing assets.
*/
public static final int LIST_ALL = -1;
/**
* Internal constant used to represent the maximum limit of assets retrieved in one call.
*/
private final int MAX_BATCH_SIZE = 100;
private String lowerBound;
private int limit;
public ListAssets(String lowerBoundSymbol, int limit){
this.lowerBound = lowerBoundSymbol;
this.limit = limit;
}
@Override
public ApiCall toApiCall(int apiId, long sequenceId) {
ArrayList<Serializable> params = new ArrayList<>();
params.add(this.lowerBound);
if(limit > MAX_BATCH_SIZE || limit == LIST_ALL){
params.add(MAX_BATCH_SIZE);
}else{
params.add(this.limit);
}
return new ApiCall(apiId, RPC.CALL_LIST_ASSETS, params, RPC.VERSION, sequenceId);
}
}

View file

@ -42,7 +42,8 @@ public class CallsActivity extends AppCompatActivity {
RPC.CALL_GET_MARKET_HISTORY,
RPC.CALL_GET_RELATIVE_ACCOUNT_HISTORY,
RPC.CALL_GET_REQUIRED_FEES,
RPC.CALL_LOOKUP_ASSET_SYMBOLS
RPC.CALL_LOOKUP_ASSET_SYMBOLS,
RPC.CALL_LIST_ASSETS
};
@NonNull

View file

@ -6,10 +6,12 @@ import android.os.Bundle;
import android.os.IBinder;
import android.support.design.widget.TextInputEditText;
import android.support.design.widget.TextInputLayout;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
@ -28,6 +30,7 @@ import cy.agorise.graphenej.api.android.RxBus;
import cy.agorise.graphenej.api.calls.GetAccounts;
import cy.agorise.graphenej.api.calls.GetBlock;
import cy.agorise.graphenej.api.calls.GetObjects;
import cy.agorise.graphenej.api.calls.ListAssets;
import cy.agorise.graphenej.models.JsonRpcResponse;
import cy.agorise.graphenej.objects.Memo;
import cy.agorise.graphenej.operations.TransferOperation;
@ -114,6 +117,8 @@ public class PerformCallActivity extends ConnectedActivity {
case RPC.CALL_LOOKUP_ASSET_SYMBOLS:
setupLookupAssetSymbols();
break;
case RPC.CALL_LIST_ASSETS:
setupListAssets();
default:
Log.d(TAG,"Default called");
}
@ -182,6 +187,14 @@ public class PerformCallActivity extends ConnectedActivity {
mParam4View.setHint(resources.getString(R.string.lookup_asset_symbols_arg4));
}
private void setupListAssets(){
requiredInput(2);
Resources resources = getResources();
mParam1View.setHint(resources.getString(R.string.list_assets_arg1));
mParam2View.setHint(resources.getString(R.string.list_assets_arg2));
param2.setInputType(InputType.TYPE_CLASS_NUMBER);
}
private void requiredInput(int inputCount){
if(inputCount == 1){
mParam1View.setVisibility(View.VISIBLE);
@ -227,6 +240,8 @@ public class PerformCallActivity extends ConnectedActivity {
break;
case RPC.CALL_LOOKUP_ASSET_SYMBOLS:
break;
case RPC.CALL_LIST_ASSETS:
sendListAssets();
default:
Log.d(TAG,"Default called");
}
@ -256,6 +271,19 @@ public class PerformCallActivity extends ConnectedActivity {
}
}
private void sendListAssets(){
try{
String lowerBound = param1.getText().toString();
int limit = Integer.parseInt(param2.getText().toString());
ListAssets listAssets = new ListAssets(lowerBound, limit);
long id = mNetworkService.sendMessage(listAssets, ListAssets.REQUIRED_API);
responseMap.put(id, mRPC);
}catch(NumberFormatException e){
Toast.makeText(this, getString(R.string.error_number_format), Toast.LENGTH_SHORT).show();
Log.e(TAG,"NumberFormatException while reading limit value. Msg: "+e.getMessage());
}
}
/**
* Internal method that will decide what to do with each JSON-RPC response
*
@ -290,6 +318,9 @@ public class PerformCallActivity extends ConnectedActivity {
case RPC.CALL_LOOKUP_ASSET_SYMBOLS:
mResponseView.setText(mResponseView.getText() + gson.toJson(response, JsonRpcResponse.class) + "\n");
break;
case RPC.CALL_LIST_ASSETS:
mResponseView.setText(mResponseView.getText() + gson.toJson(response, JsonRpcResponse.class) + "\n");
break;
default:
Log.w(TAG,"Case not handled");
mResponseView.setText(mResponseView.getText() + response.result.toString());

View file

@ -2,6 +2,7 @@
<string name="app_name">Sample</string>
<string name="error_input_id">The entered value doesn\'t seem to be an object id</string>
<string name="error_number_format">Illegal number format</string>
<!-- Actions, buttons, etc -->
<string name="action_send">Send</string>
@ -32,4 +33,8 @@
<string name="lookup_asset_symbols_arg2">Asset 2 id</string>
<string name="lookup_asset_symbols_arg3">Asset 3 id</string>
<string name="lookup_asset_symbols_arg4">Asset 4 id</string>
<!-- List assets input fields -->
<string name="list_assets_arg1">Lower bound of symbol names to retrieve</string>
<string name="list_assets_arg2">Maximum number of assets to fetch (must not exceed 100)</string>
</resources>