From d5225bc4f8613bf1f1fd2a45893f0b5ca4933e76 Mon Sep 17 00:00:00 2001 From: Bartek Nowotarski Date: Mon, 14 Mar 2016 14:45:35 +0100 Subject: [PATCH] Offers for Account --- src/main/java/org/stellar/sdk/Server.java | 7 ++ .../sdk/requests/OffersRequestBuilder.java | 75 ++++++++++++++ .../stellar/sdk/responses/GsonSingleton.java | 2 + .../stellar/sdk/responses/OfferResponse.java | 97 +++++++++++++++++++ .../requests/OffersRequestBuilderTest.java | 22 +++++ .../responses/OfferPageDeserializerTest.java | 75 ++++++++++++++ 6 files changed, 278 insertions(+) create mode 100644 src/main/java/org/stellar/sdk/requests/OffersRequestBuilder.java create mode 100644 src/main/java/org/stellar/sdk/responses/OfferResponse.java create mode 100644 src/test/java/org/stellar/sdk/requests/OffersRequestBuilderTest.java create mode 100644 src/test/java/org/stellar/sdk/responses/OfferPageDeserializerTest.java diff --git a/src/main/java/org/stellar/sdk/Server.java b/src/main/java/org/stellar/sdk/Server.java index 9b0f9cb07..dcf3c791f 100644 --- a/src/main/java/org/stellar/sdk/Server.java +++ b/src/main/java/org/stellar/sdk/Server.java @@ -59,6 +59,13 @@ public LedgersRequestBuilder ledgers() { return new LedgersRequestBuilder(serverURI); } + /** + * Returns {@link OffersRequestBuilder} instance. + */ + public OffersRequestBuilder offers() { + return new OffersRequestBuilder(serverURI); + } + /** * Returns {@link OperationsRequestBuilder} instance. */ diff --git a/src/main/java/org/stellar/sdk/requests/OffersRequestBuilder.java b/src/main/java/org/stellar/sdk/requests/OffersRequestBuilder.java new file mode 100644 index 000000000..133780aa9 --- /dev/null +++ b/src/main/java/org/stellar/sdk/requests/OffersRequestBuilder.java @@ -0,0 +1,75 @@ +package org.stellar.sdk.requests; + +import com.google.gson.reflect.TypeToken; + +import org.apache.http.client.fluent.Request; +import org.stellar.sdk.KeyPair; +import org.stellar.sdk.responses.OfferResponse; +import org.stellar.sdk.responses.Page; +import org.stellar.sdk.responses.TransactionResponse; + +import java.io.IOException; +import java.net.URI; + +import static com.google.common.base.Preconditions.checkNotNull; + +/** + * Builds requests connected to offers. + */ +public class OffersRequestBuilder extends RequestBuilder { + public OffersRequestBuilder(URI serverURI) { + super(serverURI, "offers"); + } + + /** + * Builds request to GET /accounts/{account}/offers + * @see Offers for Account + * @param account Account for which to get offers + */ + public OffersRequestBuilder forAccount(KeyPair account) { + account = checkNotNull(account, "account cannot be null"); + this.setSegments("accounts", account.getAccountId(), "offers"); + return this; + } + + /** + * Requests specific uri and returns {@link Page} of {@link OfferResponse}. + * This method is helpful for getting the next set of results. + * @return {@link Page} of {@link OfferResponse} + * @throws TooManyRequestsException when too many requests were sent to the Horizon server. + * @throws IOException + */ + public static Page execute(URI uri) throws IOException, TooManyRequestsException { + TypeToken type = new TypeToken>() {}; + ResponseHandler> responseHandler = new ResponseHandler>(type); + return (Page) Request.Get(uri).execute().handleResponse(responseHandler); + } + + /** + * Build and execute request. + * @return {@link Page} of {@link TransactionResponse} + * @throws TooManyRequestsException when too many requests were sent to the Horizon server. + * @throws IOException + */ + public Page execute() throws IOException, TooManyRequestsException { + return this.execute(this.buildUri()); + } + + @Override + public OffersRequestBuilder cursor(String token) { + super.cursor(token); + return this; + } + + @Override + public OffersRequestBuilder limit(int number) { + super.limit(number); + return this; + } + + @Override + public OffersRequestBuilder order(Order direction) { + super.order(direction); + return this; + } +} diff --git a/src/main/java/org/stellar/sdk/responses/GsonSingleton.java b/src/main/java/org/stellar/sdk/responses/GsonSingleton.java index 6f3c0a3e8..88582af26 100644 --- a/src/main/java/org/stellar/sdk/responses/GsonSingleton.java +++ b/src/main/java/org/stellar/sdk/responses/GsonSingleton.java @@ -19,6 +19,7 @@ public static Gson getInstance() { TypeToken accountPageType = new TypeToken>() {}; TypeToken effectPageType = new TypeToken>() {}; TypeToken ledgerPageType = new TypeToken>() {}; + TypeToken offerPageType = new TypeToken>() {}; TypeToken operationPageType = new TypeToken>() {}; TypeToken pathPageType = new TypeToken>() {}; TypeToken transactionPageType = new TypeToken>() {}; @@ -32,6 +33,7 @@ public static Gson getInstance() { .registerTypeAdapter(accountPageType.getType(), new PageDeserializer(accountPageType)) .registerTypeAdapter(effectPageType.getType(), new PageDeserializer(effectPageType)) .registerTypeAdapter(ledgerPageType.getType(), new PageDeserializer(ledgerPageType)) + .registerTypeAdapter(offerPageType.getType(), new PageDeserializer(offerPageType)) .registerTypeAdapter(operationPageType.getType(), new PageDeserializer(operationPageType)) .registerTypeAdapter(pathPageType.getType(), new PageDeserializer(pathPageType)) .registerTypeAdapter(transactionPageType.getType(), new PageDeserializer(transactionPageType)) diff --git a/src/main/java/org/stellar/sdk/responses/OfferResponse.java b/src/main/java/org/stellar/sdk/responses/OfferResponse.java new file mode 100644 index 000000000..e36994439 --- /dev/null +++ b/src/main/java/org/stellar/sdk/responses/OfferResponse.java @@ -0,0 +1,97 @@ +package org.stellar.sdk.responses; + +import com.google.gson.annotations.SerializedName; + +import org.stellar.sdk.Asset; +import org.stellar.sdk.KeyPair; + +/** + * Represents offer response. + * @see Offer documentation + * @see org.stellar.sdk.requests.OffersRequestBuilder + * @see org.stellar.sdk.Server#offers() + */ +public class OfferResponse extends Response { + @SerializedName("id") + private final Long id; + @SerializedName("paging_token") + private final String pagingToken; + @SerializedName("seller") + private final KeyPair seller; + @SerializedName("selling") + private final Asset selling; + @SerializedName("buying") + private final Asset buying; + @SerializedName("amount") + private final String amount; + @SerializedName("price") + private final String price; + @SerializedName("_links") + private final Links links; + + OfferResponse(Long id, String pagingToken, KeyPair seller, Asset selling, Asset buying, String amount, String price, Links links) { + this.id = id; + this.pagingToken = pagingToken; + this.seller = seller; + this.selling = selling; + this.buying = buying; + this.amount = amount; + this.price = price; + this.links = links; + } + + public Long getId() { + return id; + } + + public String getPagingToken() { + return pagingToken; + } + + public KeyPair getSeller() { + return seller; + } + + public Asset getSelling() { + return selling; + } + + public Asset getBuying() { + return buying; + } + + public String getAmount() { + return amount; + } + + public String getPrice() { + return price; + } + + public Links getLinks() { + return links; + } + + /** + * Links connected to ledger. + */ + public static class Links { + @SerializedName("self") + private final Link self; + @SerializedName("offer_maker") + private final Link offerMager; + + public Links(Link self, Link offerMager) { + this.self = self; + this.offerMager = offerMager; + } + + public Link getSelf() { + return self; + } + + public Link getOfferMager() { + return offerMager; + } + } +} diff --git a/src/test/java/org/stellar/sdk/requests/OffersRequestBuilderTest.java b/src/test/java/org/stellar/sdk/requests/OffersRequestBuilderTest.java new file mode 100644 index 000000000..70f661eb3 --- /dev/null +++ b/src/test/java/org/stellar/sdk/requests/OffersRequestBuilderTest.java @@ -0,0 +1,22 @@ +package org.stellar.sdk.requests; + +import org.junit.Test; +import org.stellar.sdk.KeyPair; +import org.stellar.sdk.Server; + +import java.net.URI; + +import static org.junit.Assert.assertEquals; + +public class OffersRequestBuilderTest { + @Test + public void testForAccount() { + Server server = new Server("https://horizon-testnet.stellar.org"); + URI uri = server.offers() + .forAccount(KeyPair.fromAccountId("GBRPYHIL2CI3FNQ4BXLFMNDLFJUNPU2HY3ZMFSHONUCEOASW7QC7OX2H")) + .limit(200) + .order(RequestBuilder.Order.DESC) + .buildUri(); + assertEquals("https://horizon-testnet.stellar.org/accounts/GBRPYHIL2CI3FNQ4BXLFMNDLFJUNPU2HY3ZMFSHONUCEOASW7QC7OX2H/offers?limit=200&order=desc", uri.toString()); + } +} diff --git a/src/test/java/org/stellar/sdk/responses/OfferPageDeserializerTest.java b/src/test/java/org/stellar/sdk/responses/OfferPageDeserializerTest.java new file mode 100644 index 000000000..d06ad4f57 --- /dev/null +++ b/src/test/java/org/stellar/sdk/responses/OfferPageDeserializerTest.java @@ -0,0 +1,75 @@ +package org.stellar.sdk.responses; + +import com.google.gson.reflect.TypeToken; + +import junit.framework.TestCase; + +import org.junit.Test; +import org.stellar.sdk.Asset; +import org.stellar.sdk.KeyPair; + +public class OfferPageDeserializerTest extends TestCase { + @Test + public void testDeserialize() { + Page transactionsPage = GsonSingleton.getInstance().fromJson(json, new TypeToken>() {}.getType()); + + assertEquals(transactionsPage.getRecords().get(0).getId(), new Long(241)); + assertEquals(transactionsPage.getRecords().get(0).getSeller().getAccountId(), "GA2IYMIZSAMDD6QQTTSIEL73H2BKDJQTA7ENDEEAHJ3LMVF7OYIZPXQD"); + assertEquals(transactionsPage.getRecords().get(0).getPagingToken(), "241"); + assertEquals(transactionsPage.getRecords().get(0).getSelling(), Asset.createNonNativeAsset("INR", KeyPair.fromAccountId("GA2IYMIZSAMDD6QQTTSIEL73H2BKDJQTA7ENDEEAHJ3LMVF7OYIZPXQD"))); + assertEquals(transactionsPage.getRecords().get(0).getBuying(), Asset.createNonNativeAsset("USD", KeyPair.fromAccountId("GA2IYMIZSAMDD6QQTTSIEL73H2BKDJQTA7ENDEEAHJ3LMVF7OYIZPXQD"))); + assertEquals(transactionsPage.getRecords().get(0).getAmount(), "10.0000000"); + assertEquals(transactionsPage.getRecords().get(0).getPrice(), "11.0000000"); + + assertEquals(transactionsPage.getLinks().getNext().getHref(), "https://horizon-testnet.stellar.org/accounts/GA2IYMIZSAMDD6QQTTSIEL73H2BKDJQTA7ENDEEAHJ3LMVF7OYIZPXQD/offers?order=asc&limit=10&cursor=241"); + assertEquals(transactionsPage.getLinks().getPrev().getHref(), "https://horizon-testnet.stellar.org/accounts/GA2IYMIZSAMDD6QQTTSIEL73H2BKDJQTA7ENDEEAHJ3LMVF7OYIZPXQD/offers?order=desc&limit=10&cursor=241"); + assertEquals(transactionsPage.getLinks().getSelf().getHref(), "https://horizon-testnet.stellar.org/accounts/GA2IYMIZSAMDD6QQTTSIEL73H2BKDJQTA7ENDEEAHJ3LMVF7OYIZPXQD/offers?order=asc&limit=10&cursor="); + } + + String json = "{\n" + + " \"_links\": {\n" + + " \"self\": {\n" + + " \"href\": \"https://horizon-testnet.stellar.org/accounts/GA2IYMIZSAMDD6QQTTSIEL73H2BKDJQTA7ENDEEAHJ3LMVF7OYIZPXQD/offers?order=asc\\u0026limit=10\\u0026cursor=\"\n" + + " },\n" + + " \"next\": {\n" + + " \"href\": \"https://horizon-testnet.stellar.org/accounts/GA2IYMIZSAMDD6QQTTSIEL73H2BKDJQTA7ENDEEAHJ3LMVF7OYIZPXQD/offers?order=asc\\u0026limit=10\\u0026cursor=241\"\n" + + " },\n" + + " \"prev\": {\n" + + " \"href\": \"https://horizon-testnet.stellar.org/accounts/GA2IYMIZSAMDD6QQTTSIEL73H2BKDJQTA7ENDEEAHJ3LMVF7OYIZPXQD/offers?order=desc\\u0026limit=10\\u0026cursor=241\"\n" + + " }\n" + + " },\n" + + " \"_embedded\": {\n" + + " \"records\": [\n" + + " {\n" + + " \"_links\": {\n" + + " \"self\": {\n" + + " \"href\": \"https://horizon-testnet.stellar.org/offers/241\"\n" + + " },\n" + + " \"offer_maker\": {\n" + + " \"href\": \"https://horizon-testnet.stellar.org/accounts/GA2IYMIZSAMDD6QQTTSIEL73H2BKDJQTA7ENDEEAHJ3LMVF7OYIZPXQD\"\n" + + " }\n" + + " },\n" + + " \"id\": 241,\n" + + " \"paging_token\": \"241\",\n" + + " \"seller\": \"GA2IYMIZSAMDD6QQTTSIEL73H2BKDJQTA7ENDEEAHJ3LMVF7OYIZPXQD\",\n" + + " \"selling\": {\n" + + " \"asset_type\": \"credit_alphanum4\",\n" + + " \"asset_code\": \"INR\",\n" + + " \"asset_issuer\": \"GA2IYMIZSAMDD6QQTTSIEL73H2BKDJQTA7ENDEEAHJ3LMVF7OYIZPXQD\"\n" + + " },\n" + + " \"buying\": {\n" + + " \"asset_type\": \"credit_alphanum4\",\n" + + " \"asset_code\": \"USD\",\n" + + " \"asset_issuer\": \"GA2IYMIZSAMDD6QQTTSIEL73H2BKDJQTA7ENDEEAHJ3LMVF7OYIZPXQD\"\n" + + " },\n" + + " \"amount\": \"10.0000000\",\n" + + " \"price_r\": {\n" + + " \"n\": 10,\n" + + " \"d\": 1\n" + + " },\n" + + " \"price\": \"11.0000000\"\n" + + " }\n" + + " ]\n" + + " }\n" + + "}"; +}