-
Notifications
You must be signed in to change notification settings - Fork 83
/
UpdatesWebsocket.java
169 lines (149 loc) · 6.6 KB
/
UpdatesWebsocket.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package net.jacobpeterson.alpaca.websocket.updates;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import net.jacobpeterson.alpaca.model.util.apitype.TraderAPIEndpointType;
import net.jacobpeterson.alpaca.model.websocket.updates.model.UpdatesMessageType;
import net.jacobpeterson.alpaca.model.websocket.updates.model.authorization.AuthorizationMessage;
import net.jacobpeterson.alpaca.model.websocket.updates.model.tradeupdate.TradeUpdateMessage;
import net.jacobpeterson.alpaca.websocket.AlpacaWebsocket;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.WebSocket;
import okio.ByteString;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static com.google.gson.JsonParser.parseString;
import static java.util.concurrent.TimeUnit.SECONDS;
import static net.jacobpeterson.alpaca.model.websocket.updates.model.UpdatesMessageType.TRADE_UPDATES;
import static net.jacobpeterson.alpaca.openapi.trader.JSON.getGson;
/**
* {@link UpdatesWebsocket} is an {@link AlpacaWebsocket} implementation and provides the
* {@link UpdatesWebsocketInterface} interface for
* <a href="https://docs.alpaca.markets/docs/websocket-streaming">Updates Streaming</a>.
*/
public class UpdatesWebsocket extends AlpacaWebsocket implements UpdatesWebsocketInterface {
private static final Logger LOGGER = LoggerFactory.getLogger(UpdatesWebsocket.class);
@SuppressWarnings("UnnecessaryDefault")
private static HttpUrl createWebsocketURL(TraderAPIEndpointType traderAPIEndpointType) {
return new HttpUrl.Builder()
.scheme("https")
.host((switch (traderAPIEndpointType) {
case LIVE -> "api";
case PAPER -> "paper-api";
default -> throw new UnsupportedOperationException();
}) + ".alpaca.markets")
.addPathSegment("stream")
.build();
}
protected final String keyID;
protected final String secretKey;
protected final String oAuthToken;
protected UpdatesListener listener;
protected boolean listenToTradeUpdates;
/**
* Instantiates a new {@link UpdatesWebsocket}.
*
* @param okHttpClient the {@link OkHttpClient}
* @param traderAPIEndpointType the {@link TraderAPIEndpointType}
* @param keyID the key ID
* @param secretKey the secret key
* @param oAuthToken the OAuth token
*/
public UpdatesWebsocket(OkHttpClient okHttpClient, TraderAPIEndpointType traderAPIEndpointType,
String keyID, String secretKey, String oAuthToken) {
super(okHttpClient, createWebsocketURL(traderAPIEndpointType), "Trades Stream");
this.keyID = keyID;
this.secretKey = secretKey;
this.oAuthToken = oAuthToken;
}
@Override
protected void cleanupState() {
super.cleanupState();
}
@Override
protected void onConnection() {
sendAuthenticationMessage();
}
@Override
protected void onReconnection() {
sendAuthenticationMessage();
if (waitForAuthorization(5, SECONDS) && listenToTradeUpdates) {
sendTradeUpdatesListenMessage();
}
}
@Override
protected void sendAuthenticationMessage() {
// Ensure that the authorization Future exists
getAuthorizationFuture();
final JsonObject authObject = new JsonObject();
authObject.addProperty("action", "authenticate");
final JsonObject authData = new JsonObject();
if (oAuthToken != null) {
authData.addProperty("oauth_token", oAuthToken);
} else {
authData.addProperty("key_id", keyID);
authData.addProperty("secret_key", secretKey);
}
authObject.add("data", authData);
LOGGER.info("{} websocket sending authentication message...", websocketName);
sendWebsocketMessage(authObject.toString());
}
@Override
public void onMessage(@NotNull WebSocket webSocket, @NotNull ByteString byteString) { // Binary framing
final String messageString = byteString.utf8();
LOGGER.trace("Websocket message received: message={}", messageString);
// Parse JSON string and identify 'messageType'
final JsonObject messageObject = parseString(messageString).getAsJsonObject();
final UpdatesMessageType messageType =
getGson().fromJson(messageObject.get("stream"), UpdatesMessageType.class);
// Deserialize message based on 'messageType' and call listener
switch (messageType) {
case AUTHORIZATION:
final AuthorizationMessage authorizationMessage =
getGson().fromJson(messageObject, AuthorizationMessage.class);
authenticated = authorizationMessage.getData().getAction().equalsIgnoreCase("authenticate") &&
authorizationMessage.getData().getStatus().equalsIgnoreCase("authorized");
if (authenticationMessageFuture != null) {
authenticationMessageFuture.complete(authenticated);
}
if (!authenticated) {
throw new RuntimeException(websocketName + " websocket authentication failed!");
} else {
LOGGER.info("{} websocket authenticated.", websocketName);
}
break;
case LISTENING:
break;
case TRADE_UPDATES:
if (listener != null) {
listener.onTradeUpdate(getGson().fromJson(messageObject, TradeUpdateMessage.class));
}
break;
default:
throw new UnsupportedOperationException();
}
}
@Override
public void setListener(UpdatesListener listener) {
this.listener = listener;
}
@Override
public void subscribeToTradeUpdates(boolean subscribe) {
listenToTradeUpdates = subscribe;
sendTradeUpdatesListenMessage();
}
private void sendTradeUpdatesListenMessage() {
final JsonObject requestObject = new JsonObject();
requestObject.addProperty("action", "listen");
final JsonArray streamsArray = new JsonArray();
if (listenToTradeUpdates) {
streamsArray.add(TRADE_UPDATES.toString());
}
final JsonObject dataObject = new JsonObject();
dataObject.add("streams", streamsArray);
requestObject.add("data", dataObject);
sendWebsocketMessage(requestObject.toString());
LOGGER.info("Requested streams: streams={}.", streamsArray);
}
}