diff --git a/src/main/java/org/stellar/sdk/requests/AccountsRequestBuilder.java b/src/main/java/org/stellar/sdk/requests/AccountsRequestBuilder.java index bca3e9507..c99c0845d 100644 --- a/src/main/java/org/stellar/sdk/requests/AccountsRequestBuilder.java +++ b/src/main/java/org/stellar/sdk/requests/AccountsRequestBuilder.java @@ -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; @@ -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"); } @@ -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 Accounts + */ + 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 Accounts + */ + 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 uri and returns {@link Page} of {@link AccountResponse}. * This method is helpful for getting the next set of results. diff --git a/src/main/java/org/stellar/sdk/requests/RequestBuilder.java b/src/main/java/org/stellar/sdk/requests/RequestBuilder.java index 2c17a34b8..653c29e8b 100644 --- a/src/main/java/org/stellar/sdk/requests/RequestBuilder.java +++ b/src/main/java/org/stellar/sdk/requests/RequestBuilder.java @@ -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 assets) { List 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) { diff --git a/src/test/java/org/stellar/sdk/requests/AccountsRequestBuilderTest.java b/src/test/java/org/stellar/sdk/requests/AccountsRequestBuilderTest.java index 627327086..d4b446222 100644 --- a/src/test/java/org/stellar/sdk/requests/AccountsRequestBuilderTest.java +++ b/src/test/java/org/stellar/sdk/requests/AccountsRequestBuilderTest.java @@ -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; @@ -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()); + } }