Skip to content

Commit

Permalink
feat: add support for /accounts end-point with ?signer and ?asset f…
Browse files Browse the repository at this point in the history
…ilters.
  • Loading branch information
overcat committed Feb 5, 2020
1 parent a2cec0f commit 1a200dd
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 10 deletions.
34 changes: 34 additions & 0 deletions src/main/java/org/stellar/sdk/requests/AccountsRequestBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.stellar.sdk.Asset;
import org.stellar.sdk.responses.AccountResponse;
import org.stellar.sdk.responses.Page;

Expand All @@ -14,6 +15,9 @@
* Builds requests connected to accounts.
*/
public class AccountsRequestBuilder extends RequestBuilder {
private static final String ASSET_PARAMETER_NAME = "asset";
private static final String SIGNER_PARAMETER_NAME = "signer";

public AccountsRequestBuilder(OkHttpClient httpClient, HttpUrl serverURI) {
super(httpClient, serverURI, "accounts");
}
Expand Down Expand Up @@ -44,6 +48,36 @@ public AccountResponse account(String account) throws IOException {
return this.account(this.buildUri());
}

/**
* Returns all accounts that contain a specific signer.
*
* @param signer Account ID
* @return current {@link AccountsRequestBuilder} instance
* @see <a href="https://www.stellar.org/developers/horizon/reference/endpoints/accounts.html">Accounts</a>
*/
public AccountsRequestBuilder forSigner(String signer) {
if (uriBuilder.build().queryParameter(ASSET_PARAMETER_NAME) != null) {
throw new RuntimeException("cannot set both signer and asset");
}
uriBuilder.setQueryParameter(SIGNER_PARAMETER_NAME, signer);
return this;
}

/**
* Returns all accounts who are trustees to a specific asset.
*
* @param asset An issued asset
* @return current {@link AccountsRequestBuilder} instance
* @see <a href="https://www.stellar.org/developers/horizon/reference/endpoints/accounts.html">Accounts</a>
*/
public AccountsRequestBuilder forAsset(Asset asset) {
if (uriBuilder.build().queryParameter(SIGNER_PARAMETER_NAME) != null) {
throw new RuntimeException("cannot set both signer and asset");
}
setAssetParameter(ASSET_PARAMETER_NAME, asset);
return this;
}

/**
* Requests specific <code>uri</code> and returns {@link Page} of {@link AccountResponse}.
* This method is helpful for getting the next set of results.
Expand Down
34 changes: 25 additions & 9 deletions src/main/java/org/stellar/sdk/requests/RequestBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,25 +79,41 @@ public RequestBuilder order(Order direction) {

/**
* Sets a parameter consisting of a comma separated list of assets on the request.
* @param name the name of the query parameter
*
* @param name the name of the query parameter
* @param assets the list of assets to be serialized into the query parameter value
*/
public RequestBuilder setAssetsParameter(String name, List<Asset> assets) {
List<String> assetStrings = Lists.newArrayList();
for (Asset asset : assets) {
if (asset instanceof AssetTypeNative) {
assetStrings.add("native");
} else if (asset instanceof AssetTypeCreditAlphaNum) {
AssetTypeCreditAlphaNum creditAsset = (AssetTypeCreditAlphaNum) asset;
assetStrings.add(creditAsset.getCode()+":"+creditAsset.getIssuer());
} else {
throw new RuntimeException("unsupported asset "+ asset.getType());
}
assetStrings.add(buildAssetParameter(asset));
}
uriBuilder.setQueryParameter(name, Joiner.on(",").join(assetStrings));
return this;
}

/**
* Sets a parameter consisting of an asset represented as "assetCode:assetIssue" on the request.
*
* @param name the name of the query parameter
* @param asset the asset to be serialized into the query parameter value
*/
public RequestBuilder setAssetParameter(String name, Asset asset) {
uriBuilder.setQueryParameter(name, buildAssetParameter(asset));
return this;
}

private String buildAssetParameter(Asset asset) {
if (asset instanceof AssetTypeNative) {
return "native";
} else if (asset instanceof AssetTypeCreditAlphaNum) {
AssetTypeCreditAlphaNum creditAsset = (AssetTypeCreditAlphaNum) asset;
return creditAsset.getCode() + ":" + creditAsset.getIssuer();
} else {
throw new RuntimeException("unsupported asset " + asset.getType());
}
}

HttpUrl buildUri() {
if (segments.size() > 0) {
for (String segment : segments) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import okhttp3.HttpUrl;
import org.junit.Test;
import org.stellar.sdk.Network;
import org.stellar.sdk.Asset;
import org.stellar.sdk.AssetTypeCreditAlphaNum4;
import org.stellar.sdk.Server;

import static org.junit.Assert.assertEquals;
Expand All @@ -18,4 +19,23 @@ public void testAccounts() {
.buildUri();
assertEquals("https://horizon-testnet.stellar.org/accounts?cursor=13537736921089&limit=200&order=asc", uri.toString());
}

@Test
public void testForSigner() {
Server server = new Server("https://horizon-testnet.stellar.org");
HttpUrl uri = server.accounts()
.forSigner("GDSBCQO34HWPGUGQSP3QBFEXVTSR2PW46UIGTHVWGWJGQKH3AFNHXHXN")
.buildUri();
assertEquals("https://horizon-testnet.stellar.org/accounts?signer=GDSBCQO34HWPGUGQSP3QBFEXVTSR2PW46UIGTHVWGWJGQKH3AFNHXHXN", uri.toString());
}

@Test
public void testForAsset() {
Server server = new Server("https://horizon-testnet.stellar.org");
Asset asset = new AssetTypeCreditAlphaNum4("USD", "GDVDKQFP665JAO7A2LSHNLQIUNYNAAIGJ6FYJVMG4DT3YJQQJSRBLQDG");
HttpUrl uri = server.accounts()
.forAsset(asset)
.buildUri();
assertEquals("https://horizon-testnet.stellar.org/accounts?asset=USD%3AGDVDKQFP665JAO7A2LSHNLQIUNYNAAIGJ6FYJVMG4DT3YJQQJSRBLQDG", uri.toString());
}
}

0 comments on commit 1a200dd

Please sign in to comment.