-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
API is now fully wrapped!
- Loading branch information
jorn
committed
May 26, 2018
1 parent
7094c8f
commit 435657b
Showing
14 changed files
with
358 additions
and
7 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
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
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,47 @@ | ||
package io.yogh.bl3p.api.v1; | ||
|
||
import java.net.URI; | ||
import java.util.function.Consumer; | ||
|
||
import javax.websocket.ContainerProvider; | ||
import javax.websocket.MessageHandler; | ||
import javax.websocket.Session; | ||
import javax.websocket.WebSocketContainer; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import io.yogh.bl3p.api.v1.request.domain.Market; | ||
|
||
public class WebSocketClient implements MessageHandler { | ||
private static final Logger LOG = LoggerFactory.getLogger(Bl3pClientRequestUtil.class); | ||
|
||
private static final String HOST = "wss://api.bl3p.eu/1/"; | ||
|
||
private static Object waitLock = new Object(); | ||
|
||
public WebSocketClient(final Market market, final String channel, final Consumer<String> parser) { | ||
final WebSocketContainer container = ContainerProvider.getWebSocketContainer(); | ||
|
||
final String url = HOST + market.name() + "/" + channel; | ||
|
||
try (final Session session = container.connectToServer(new WebSocketEndPoint(parser), URI.create(url))) { | ||
waitForTerminate(); | ||
} catch (final Exception e) { | ||
LOG.error("Exception during websocket session.", e); | ||
// Squelch error. | ||
} | ||
} | ||
|
||
private static void waitForTerminate() { | ||
synchronized (waitLock) { | ||
try { | ||
waitLock.wait(); | ||
} catch (final InterruptedException e) {} | ||
} | ||
} | ||
|
||
public void terminate() { | ||
waitLock.notifyAll(); | ||
} | ||
} |
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,40 @@ | ||
package io.yogh.bl3p.api.v1; | ||
|
||
import java.util.function.Consumer; | ||
|
||
import javax.websocket.ClientEndpoint; | ||
import javax.websocket.EndpointConfig; | ||
import javax.websocket.OnClose; | ||
import javax.websocket.OnMessage; | ||
import javax.websocket.OnOpen; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
@ClientEndpoint | ||
public class WebSocketEndPoint { | ||
private static final Logger LOG = LoggerFactory.getLogger(WebSocketEndPoint.class); | ||
|
||
private final Consumer<String> consumer; | ||
|
||
public WebSocketEndPoint(final Consumer<String> consumer) { | ||
this.consumer = consumer; | ||
|
||
LOG.debug("Created WebSocketEndPoint: {}", this); | ||
} | ||
|
||
@OnOpen | ||
public void onOpen(final EndpointConfig conf) { | ||
LOG.debug("Opened WebSocketEndPoint: {}", conf); | ||
} | ||
|
||
@OnMessage | ||
public void onMessage(final String message) { | ||
consumer.accept(message); | ||
} | ||
|
||
@OnClose | ||
public void onClose(final EndpointConfig conf) { | ||
LOG.debug("Closed WebSocketEndPoint: {}", conf); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
src/main/java/io/yogh/bl3p/api/v1/response/domain/TradeFeedInfo.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,55 @@ | ||
package io.yogh.bl3p.api.v1.response.domain; | ||
|
||
public class TradeFeedInfo { | ||
private long timestamp; | ||
private String marketplace; | ||
private int price; | ||
private String type; | ||
private int amount; | ||
|
||
public long getTimestamp() { | ||
return timestamp; | ||
} | ||
|
||
public void setTimestamp(final long timestamp) { | ||
this.timestamp = timestamp; | ||
} | ||
|
||
public String getMarketplace() { | ||
return marketplace; | ||
} | ||
|
||
public void setMarketplace(final String marketplace) { | ||
this.marketplace = marketplace; | ||
} | ||
|
||
public int getPrice() { | ||
return price; | ||
} | ||
|
||
public void setPrice(final int price) { | ||
this.price = price; | ||
} | ||
|
||
public String getType() { | ||
return type; | ||
} | ||
|
||
public void setType(final String type) { | ||
this.type = type; | ||
} | ||
|
||
public int getAmount() { | ||
return amount; | ||
} | ||
|
||
public void setAmount(final int amount) { | ||
this.amount = amount; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "TradeFeedInfo [timestamp=" + timestamp + ", marketplace=" + marketplace + ", price=" + price + ", type=" + type + ", amount=" + amount | ||
+ "]"; | ||
} | ||
} |
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
19 changes: 19 additions & 0 deletions
19
src/main/java/io/yogh/bl3p/api/v1/response/websocket/FeedForwarder.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,19 @@ | ||
package io.yogh.bl3p.api.v1.response.websocket; | ||
|
||
import java.util.Set; | ||
import java.util.function.Consumer; | ||
import java.util.function.Function; | ||
|
||
public class FeedForwarder<T> extends SubscriptionForwarder<T> implements Consumer<String> { | ||
private final Function<String, T> parser; | ||
|
||
public FeedForwarder(final Set<Consumer<T>> subscribers, final Function<String, T> parser) { | ||
super(subscribers); | ||
this.parser = parser; | ||
} | ||
|
||
@Override | ||
public void accept(final String text) { | ||
forward(parser.apply(text)); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/io/yogh/bl3p/api/v1/response/websocket/OrderBookResponseCallback.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,17 @@ | ||
package io.yogh.bl3p.api.v1.response.websocket; | ||
|
||
import java.util.Set; | ||
import java.util.function.Consumer; | ||
import java.util.function.Function; | ||
|
||
import io.yogh.bl3p.api.v1.response.domain.OrderBook; | ||
|
||
public class OrderBookResponseCallback extends FeedForwarder<OrderBook> implements Consumer<String> { | ||
public OrderBookResponseCallback(final Set<Consumer<OrderBook>> subscribers, final Function<String, OrderBook> parser) { | ||
super(subscribers, parser); | ||
} | ||
|
||
public static OrderBookResponseCallback create(final Set<Consumer<OrderBook>> subscribers) { | ||
return new OrderBookResponseCallback(subscribers, OrderBookResponseParser.create()); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/main/java/io/yogh/bl3p/api/v1/response/websocket/OrderBookResponseParser.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,42 @@ | ||
package io.yogh.bl3p.api.v1.response.websocket; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.json.JSONArray; | ||
import org.json.JSONObject; | ||
|
||
import io.yogh.bl3p.api.v1.response.domain.OrderBook; | ||
import io.yogh.bl3p.api.v1.response.domain.SimpleOrder; | ||
import io.yogh.bl3p.api.v1.response.parser.CommonParser; | ||
|
||
public class OrderBookResponseParser implements StringJsonParser<OrderBook> { | ||
@Override | ||
public OrderBook apply(final String text) { | ||
final JSONObject data = new JSONObject(text); | ||
|
||
final OrderBook orderBook = new OrderBook(); | ||
|
||
final List<SimpleOrder> bids = new ArrayList<>(); | ||
final JSONArray bidsJson = data.getJSONArray("bids"); | ||
for (int i = 0; i < bidsJson.length(); i++) { | ||
bids.add(CommonParser.parseSimpleOrder(bidsJson.getJSONObject(i))); | ||
} | ||
|
||
final List<SimpleOrder> asks = new ArrayList<>(); | ||
final JSONArray asksJson = data.getJSONArray("asks"); | ||
for (int i = 0; i < asksJson.length(); i++) { | ||
asksJson.get(i); | ||
asks.add(CommonParser.parseSimpleOrder(asksJson.getJSONObject(i))); | ||
} | ||
|
||
orderBook.setBids(bids); | ||
orderBook.setAsks(asks); | ||
|
||
return orderBook; | ||
} | ||
|
||
public static OrderBookResponseParser create() { | ||
return new OrderBookResponseParser(); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
src/main/java/io/yogh/bl3p/api/v1/response/websocket/StringJsonParser.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,5 @@ | ||
package io.yogh.bl3p.api.v1.response.websocket; | ||
|
||
import java.util.function.Function; | ||
|
||
public interface StringJsonParser<T> extends Function<String, T> {} |
16 changes: 16 additions & 0 deletions
16
src/main/java/io/yogh/bl3p/api/v1/response/websocket/SubscriptionForwarder.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,16 @@ | ||
package io.yogh.bl3p.api.v1.response.websocket; | ||
|
||
import java.util.Set; | ||
import java.util.function.Consumer; | ||
|
||
public class SubscriptionForwarder<T> { | ||
private final Set<Consumer<T>> subscribers; | ||
|
||
public SubscriptionForwarder(final Set<Consumer<T>> subscribers) { | ||
this.subscribers = subscribers; | ||
} | ||
|
||
protected void forward(final T item) { | ||
subscribers.forEach(v -> v.accept(item)); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/io/yogh/bl3p/api/v1/response/websocket/TradeFeedResponseCallback.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,17 @@ | ||
package io.yogh.bl3p.api.v1.response.websocket; | ||
|
||
import java.util.Set; | ||
import java.util.function.Consumer; | ||
import java.util.function.Function; | ||
|
||
import io.yogh.bl3p.api.v1.response.domain.TradeFeedInfo; | ||
|
||
public class TradeFeedResponseCallback extends FeedForwarder<TradeFeedInfo> implements Consumer<String> { | ||
public TradeFeedResponseCallback(final Set<Consumer<TradeFeedInfo>> subscribers, final Function<String, TradeFeedInfo> parser) { | ||
super(subscribers, parser); | ||
} | ||
|
||
public static TradeFeedResponseCallback create(final Set<Consumer<TradeFeedInfo>> subscribers) { | ||
return new TradeFeedResponseCallback(subscribers, TradeFeedResponseParser.create()); | ||
} | ||
} |
Oops, something went wrong.