Skip to content

Commit

Permalink
feat: extend offer call builder to return a single offer.
Browse files Browse the repository at this point in the history
  • Loading branch information
overcat committed Feb 19, 2020
1 parent 1a200dd commit 4682ea8
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
26 changes: 26 additions & 0 deletions src/main/java/org/stellar/sdk/requests/OffersRequestBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,32 @@ public OffersRequestBuilder(OkHttpClient httpClient, HttpUrl serverURI) {
super(httpClient, serverURI, "offers");
}

/**
* Requests specific <code>uri</code> and returns {@link OfferResponse}.
* This method is helpful for getting the links.
* @throws IOException
*/
public OfferResponse offer(HttpUrl uri) throws IOException {
TypeToken type = new TypeToken<OfferResponse>() {};
ResponseHandler<OfferResponse> responseHandler = new ResponseHandler<OfferResponse>(type);

Request request = new Request.Builder().get().url(uri).build();
Response response = httpClient.newCall(request).execute();

return responseHandler.handleResponse(response);
}

/**
* The offer details endpoint provides information on a single offer.
* @param offerId specifies which offer to load.
* @return The offer details.
* @throws IOException
*/
public OfferResponse offer(long offerId) throws IOException {
this.setSegments("offers", String.valueOf(offerId));
return this.offer(this.buildUri());
}

/**
* @param account Account for which to get offers
* @see <a href="https://www.stellar.org/developers/horizon/reference/offers-for-account.html">Offers for Account</a>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,49 @@
package org.stellar.sdk.requests;

import okhttp3.HttpUrl;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import okhttp3.mockwebserver.RecordedRequest;
import org.junit.Test;
import org.stellar.sdk.*;
import org.stellar.sdk.Asset;
import org.stellar.sdk.AssetTypeCreditAlphaNum4;
import org.stellar.sdk.Server;

import java.io.IOException;

import static org.junit.Assert.assertEquals;

public class OffersRequestBuilderTest {
private static final String offerResponse = "{\n" +
"\"_links\": {\n" +
"\"self\": {\n" +
"\"href\": \"https://horizon.stellar.org/offers/784340\"\n" +
"},\n" +
"\"offer_maker\": {\n" +
"\"href\": \"https://horizon.stellar.org/accounts/GD66B4ONNNI2WWLILFY4ZPHY4RGJTBDPKG4JID6KF75VIVIJSYXV4CAT\"\n" +
"}\n" +
"},\n" +
"\"id\": 784340,\n" +
"\"paging_token\": \"784340\",\n" +
"\"seller\": \"GD66B4ONNNI2WWLILFY4ZPHY4RGJTBDPKG4JID6KF75VIVIJSYXV4CAT\",\n" +
"\"selling\": {\n" +
"\"asset_type\": \"native\"\n" +
"},\n" +
"\"buying\": {\n" +
"\"asset_type\": \"credit_alphanum4\",\n" +
"\"asset_code\": \"XCN\",\n" +
"\"asset_issuer\": \"GCNY5OXYSY4FKHOPT2SPOQZAOEIGXB5LBYW3HVU3OWSTQITS65M5RCNY\"\n" +
"},\n" +
"\"amount\": \"2000.0000000\",\n" +
"\"price_r\": {\n" +
"\"n\": 5,\n" +
"\"d\": 1\n" +
"},\n" +
"\"price\": \"5.0000000\",\n" +
"\"last_modified_ledger\": 19967410,\n" +
"\"last_modified_time\": \"2018-09-13T16:00:06Z\"\n" +
"}";

@Test
public void testForAccount() {
Server server = new Server("https://horizon-testnet.stellar.org");
Expand Down Expand Up @@ -47,4 +84,16 @@ public void testForBuyingAsset() {
HttpUrl uri = server.offers().forBuyingAsset(buying).buildUri();
assertEquals("https://horizon-testnet.stellar.org/offers?buying_asset_type=credit_alphanum4&buying_asset_code=XCN&buying_asset_issuer=GCNY5OXYSY4FKHOPT2SPOQZAOEIGXB5LBYW3HVU3OWSTQITS65M5RCNY", uri.toString());
}

@Test
public void testOffer() throws IOException, InterruptedException {
MockWebServer mockWebServer = new MockWebServer();
mockWebServer.enqueue(new MockResponse().setResponseCode(200).setBody(offerResponse));
String mockUrl = String.format("http://%s:%d", mockWebServer.getHostName(), mockWebServer.getPort());
Server server = new Server(mockUrl);
long offerId = 784340L;
server.offers().offer(offerId);
RecordedRequest recordedRequest = mockWebServer.takeRequest();
assertEquals(String.format("/offers/%s", offerId), recordedRequest.getPath());
}
}

0 comments on commit 4682ea8

Please sign in to comment.