-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
278 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
src/main/java/org/stellar/sdk/requests/OffersRequestBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <code>GET /accounts/{account}/offers</code> | ||
* @see <a href="https://www.stellar.org/developers/horizon/reference/offers-for-account.html">Offers for Account</a> | ||
* @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 <code>uri</code> 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<OfferResponse> execute(URI uri) throws IOException, TooManyRequestsException { | ||
TypeToken type = new TypeToken<Page<TransactionResponse>>() {}; | ||
ResponseHandler<Page<OfferResponse>> responseHandler = new ResponseHandler<Page<OfferResponse>>(type); | ||
return (Page<OfferResponse>) 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<OfferResponse> 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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
src/main/java/org/stellar/sdk/responses/OfferResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <a href="https://www.stellar.org/developers/horizon/reference/resources/offer.html" target="_blank">Offer documentation</a> | ||
* @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; | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/test/java/org/stellar/sdk/requests/OffersRequestBuilderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
src/test/java/org/stellar/sdk/responses/OfferPageDeserializerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<OfferResponse> transactionsPage = GsonSingleton.getInstance().fromJson(json, new TypeToken<Page<OfferResponse>>() {}.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" + | ||
"}"; | ||
} |