From 140cab48cdb84f98641990144b823d946f8f00fd Mon Sep 17 00:00:00 2001 From: "Nelson R. Perez" Date: Wed, 25 Jul 2018 13:08:09 -0500 Subject: [PATCH] Added support for the list_assets API call wrapper in the single connection mode --- .../api/android/DeserializationMap.java | 8 ++++ .../graphenej/api/android/NetworkService.java | 5 +++ .../graphenej/api/calls/ListAssets.java | 44 +++++++++++++++++++ .../cy/agorise/labs/sample/CallsActivity.java | 3 +- .../labs/sample/PerformCallActivity.java | 31 +++++++++++++ sample/src/main/res/values/strings.xml | 5 +++ 6 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 graphenej/src/main/java/cy/agorise/graphenej/api/calls/ListAssets.java diff --git a/graphenej/src/main/java/cy/agorise/graphenej/api/android/DeserializationMap.java b/graphenej/src/main/java/cy/agorise/graphenej/api/android/DeserializationMap.java index e5fbf02..4b1973e 100644 --- a/graphenej/src/main/java/cy/agorise/graphenej/api/android/DeserializationMap.java +++ b/graphenej/src/main/java/cy/agorise/graphenej/api/android/DeserializationMap.java @@ -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){ diff --git a/graphenej/src/main/java/cy/agorise/graphenej/api/android/NetworkService.java b/graphenej/src/main/java/cy/agorise/graphenej/api/android/NetworkService.java index f4ef94d..ca7992a 100644 --- a/graphenej/src/main/java/cy/agorise/graphenej/api/android/NetworkService.java +++ b/graphenej/src/main/java/cy/agorise/graphenej/api/android/NetworkService.java @@ -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>>(){}.getType(); + parsedResponse = gson.fromJson(text, LisAssetsResponse); }else{ Log.w(TAG,"Unknown request class"); } diff --git a/graphenej/src/main/java/cy/agorise/graphenej/api/calls/ListAssets.java b/graphenej/src/main/java/cy/agorise/graphenej/api/calls/ListAssets.java new file mode 100644 index 0000000..570fb60 --- /dev/null +++ b/graphenej/src/main/java/cy/agorise/graphenej/api/calls/ListAssets.java @@ -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 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); + } +} diff --git a/sample/src/main/java/cy/agorise/labs/sample/CallsActivity.java b/sample/src/main/java/cy/agorise/labs/sample/CallsActivity.java index 40fc28b..9375949 100644 --- a/sample/src/main/java/cy/agorise/labs/sample/CallsActivity.java +++ b/sample/src/main/java/cy/agorise/labs/sample/CallsActivity.java @@ -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 diff --git a/sample/src/main/java/cy/agorise/labs/sample/PerformCallActivity.java b/sample/src/main/java/cy/agorise/labs/sample/PerformCallActivity.java index db17e2e..bb8a7b2 100644 --- a/sample/src/main/java/cy/agorise/labs/sample/PerformCallActivity.java +++ b/sample/src/main/java/cy/agorise/labs/sample/PerformCallActivity.java @@ -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()); diff --git a/sample/src/main/res/values/strings.xml b/sample/src/main/res/values/strings.xml index 4d830be..d5f9db1 100644 --- a/sample/src/main/res/values/strings.xml +++ b/sample/src/main/res/values/strings.xml @@ -2,6 +2,7 @@ Sample The entered value doesn\'t seem to be an object id + Illegal number format Send @@ -32,4 +33,8 @@ Asset 2 id Asset 3 id Asset 4 id + + + Lower bound of symbol names to retrieve + Maximum number of assets to fetch (must not exceed 100)