Skip to content

Commit

Permalink
Offers for Account
Browse files Browse the repository at this point in the history
  • Loading branch information
bartekn committed Mar 14, 2016
1 parent 6cc57b3 commit d5225bc
Show file tree
Hide file tree
Showing 6 changed files with 278 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/main/java/org/stellar/sdk/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
75 changes: 75 additions & 0 deletions src/main/java/org/stellar/sdk/requests/OffersRequestBuilder.java
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;
}
}
2 changes: 2 additions & 0 deletions src/main/java/org/stellar/sdk/responses/GsonSingleton.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public static Gson getInstance() {
TypeToken accountPageType = new TypeToken<Page<AccountResponse>>() {};
TypeToken effectPageType = new TypeToken<Page<EffectResponse>>() {};
TypeToken ledgerPageType = new TypeToken<Page<LedgerResponse>>() {};
TypeToken offerPageType = new TypeToken<Page<OfferResponse>>() {};
TypeToken operationPageType = new TypeToken<Page<OperationResponse>>() {};
TypeToken pathPageType = new TypeToken<Page<PathResponse>>() {};
TypeToken transactionPageType = new TypeToken<Page<TransactionResponse>>() {};
Expand All @@ -32,6 +33,7 @@ public static Gson getInstance() {
.registerTypeAdapter(accountPageType.getType(), new PageDeserializer<AccountResponse>(accountPageType))
.registerTypeAdapter(effectPageType.getType(), new PageDeserializer<AccountResponse>(effectPageType))
.registerTypeAdapter(ledgerPageType.getType(), new PageDeserializer<LedgerResponse>(ledgerPageType))
.registerTypeAdapter(offerPageType.getType(), new PageDeserializer<OfferResponse>(offerPageType))
.registerTypeAdapter(operationPageType.getType(), new PageDeserializer<OperationResponse>(operationPageType))
.registerTypeAdapter(pathPageType.getType(), new PageDeserializer<PathResponse>(pathPageType))
.registerTypeAdapter(transactionPageType.getType(), new PageDeserializer<TransactionResponse>(transactionPageType))
Expand Down
97 changes: 97 additions & 0 deletions src/main/java/org/stellar/sdk/responses/OfferResponse.java
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;
}
}
}
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());
}
}
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" +
"}";
}

0 comments on commit d5225bc

Please sign in to comment.