-
Notifications
You must be signed in to change notification settings - Fork 83
/
AlpacaBrokerAPI.java
338 lines (309 loc) · 10.7 KB
/
AlpacaBrokerAPI.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
package net.jacobpeterson.alpaca.rest.broker;
import com.google.gson.JsonElement;
import net.jacobpeterson.alpaca.model.util.apitype.BrokerAPIEndpointType;
import net.jacobpeterson.alpaca.openapi.broker.ApiClient;
import net.jacobpeterson.alpaca.openapi.broker.api.AccountsApi;
import net.jacobpeterson.alpaca.openapi.broker.api.AssetsApi;
import net.jacobpeterson.alpaca.openapi.broker.api.CalendarApi;
import net.jacobpeterson.alpaca.openapi.broker.api.ClockApi;
import net.jacobpeterson.alpaca.openapi.broker.api.CorporateActionsApi;
import net.jacobpeterson.alpaca.openapi.broker.api.CountryInfoApi;
import net.jacobpeterson.alpaca.openapi.broker.api.DocumentsApi;
import net.jacobpeterson.alpaca.openapi.broker.api.FundingApi;
import net.jacobpeterson.alpaca.openapi.broker.api.FundingWalletsApi;
import net.jacobpeterson.alpaca.openapi.broker.api.JournalsApi;
import net.jacobpeterson.alpaca.openapi.broker.api.KycApi;
import net.jacobpeterson.alpaca.openapi.broker.api.LogosApi;
import net.jacobpeterson.alpaca.openapi.broker.api.OAuthApi;
import net.jacobpeterson.alpaca.openapi.broker.api.RebalancingApi;
import net.jacobpeterson.alpaca.openapi.broker.api.ReportingApi;
import net.jacobpeterson.alpaca.openapi.broker.api.TradingApi;
import net.jacobpeterson.alpaca.openapi.broker.api.WatchlistApi;
import net.jacobpeterson.alpaca.openapi.broker.model.AdminActionLegacyNote;
import net.jacobpeterson.alpaca.openapi.broker.model.AdminActionLiquidation;
import net.jacobpeterson.alpaca.openapi.broker.model.AdminActionTransactionCancel;
import net.jacobpeterson.alpaca.openapi.broker.model.JNLC;
import net.jacobpeterson.alpaca.openapi.broker.model.JNLS;
import net.jacobpeterson.alpaca.openapi.broker.model.NonTradeActivity;
import net.jacobpeterson.alpaca.openapi.broker.model.TradeActivity;
import net.jacobpeterson.alpaca.rest.broker.events.EventsApiSSE;
import okhttp3.OkHttpClient;
import java.util.function.BiFunction;
import static com.google.common.base.Preconditions.checkNotNull;
import static net.jacobpeterson.alpaca.util.apikey.APIKeyUtil.createBrokerAPIAuthKey;
/**
* {@link AlpacaBrokerAPI} is the class used to interface with the Alpaca Broker API endpoints. This class is
* thread-safe.
*/
public class AlpacaBrokerAPI {
// Set validation predicates for 'anyOf' and 'oneOf' models
static {
// 'AccountsApi' models
TradeActivity.isValid = jsonElement -> jsonElement.getAsJsonObject().has("type");
NonTradeActivity.isValid = jsonElement -> !jsonElement.getAsJsonObject().has("type");
// 'JournalsApi' models
final BiFunction<Boolean, JsonElement, Boolean> transferPredicate = (jnlc, jsonElement) -> {
final String entryType = jsonElement.getAsJsonObject().get("entry_type").getAsString();
return jnlc ? entryType.equals("JNLC") : entryType.equals("JNLS");
};
JNLS.isValid = jsonElement -> transferPredicate.apply(true, jsonElement);
JNLC.isValid = jsonElement -> transferPredicate.apply(false, jsonElement);
// 'EventsApi' models
AdminActionLegacyNote.isValid = jsonElement -> jsonElement.getAsJsonObject()
.get("type").getAsString().contains("note_admin_event");
AdminActionLiquidation.isValid = jsonElement -> jsonElement.getAsJsonObject()
.get("type").getAsString().equals("liquidation_admin_event");
AdminActionTransactionCancel.isValid = jsonElement -> jsonElement.getAsJsonObject()
.get("type").getAsString().equals("transaction_cancel_admin_event");
}
private final ApiClient apiClient;
private AccountsApi accounts;
private AssetsApi assets;
private CalendarApi calendar;
private ClockApi clock;
private CorporateActionsApi corporateActions;
private CountryInfoApi countryInfo;
private DocumentsApi documents;
private EventsApiSSE events;
private FundingApi funding;
private FundingWalletsApi fundingWallets;
private JournalsApi journals;
private KycApi kyc;
private LogosApi logos;
private OAuthApi oAuth;
private RebalancingApi rebalancing;
private ReportingApi reporting;
private TradingApi trading;
private WatchlistApi watchlist;
/**
* Instantiates a new {@link AlpacaBrokerAPI}.
*
* @param brokerAPIKey the Broker API key
* @param brokerAPISecret the Broker API secret
* @param brokerAPIEndpointType the {@link BrokerAPIEndpointType}
* @param okHttpClient an existing {@link OkHttpClient} or <code>null</code> to create a new default
* instance
*/
@SuppressWarnings("UnnecessaryDefault")
public AlpacaBrokerAPI(String brokerAPIKey, String brokerAPISecret, BrokerAPIEndpointType brokerAPIEndpointType,
OkHttpClient okHttpClient) {
checkNotNull(brokerAPIKey);
checkNotNull(brokerAPISecret);
checkNotNull(brokerAPIEndpointType);
checkNotNull(okHttpClient);
apiClient = new ApiClient(okHttpClient);
apiClient.setServerIndex(switch (brokerAPIEndpointType) {
case SANDBOX -> 0;
case PRODUCTION -> 1;
default -> throw new UnsupportedOperationException();
});
apiClient.addDefaultHeader("Authorization", "Basic " +
createBrokerAPIAuthKey(brokerAPIKey, brokerAPISecret));
}
/**
* Gets the internal {@link ApiClient}.
*
* @return the {@link ApiClient}
*/
public ApiClient getInternalAPIClient() {
return apiClient;
}
/**
* Gets the {@link AccountsApi}. Lazily instantiated.
*
* @return the {@link AccountsApi}
*/
public synchronized AccountsApi accounts() {
if (accounts == null) {
accounts = new AccountsApi(apiClient);
}
return accounts;
}
/**
* Gets the {@link AssetsApi}. Lazily instantiated.
*
* @return the {@link AssetsApi}
*/
public synchronized AssetsApi assets() {
if (assets == null) {
assets = new AssetsApi(apiClient);
}
return assets;
}
/**
* Gets the {@link CalendarApi}. Lazily instantiated.
*
* @return the {@link CalendarApi}
*/
public synchronized CalendarApi calendar() {
if (calendar == null) {
calendar = new CalendarApi(apiClient);
}
return calendar;
}
/**
* Gets the {@link ClockApi}. Lazily instantiated.
*
* @return the {@link ClockApi}
*/
public synchronized ClockApi clock() {
if (clock == null) {
clock = new ClockApi(apiClient);
}
return clock;
}
/**
* Gets the {@link CorporateActionsApi}. Lazily instantiated.
*
* @return the {@link CorporateActionsApi}
*/
public synchronized CorporateActionsApi corporateActions() {
if (corporateActions == null) {
corporateActions = new CorporateActionsApi(apiClient);
}
return corporateActions;
}
/**
* Gets the {@link CountryInfoApi}. Lazily instantiated.
*
* @return the {@link CountryInfoApi}
*/
public synchronized CountryInfoApi countryInfo() {
if (countryInfo == null) {
countryInfo = new CountryInfoApi(apiClient);
}
return countryInfo;
}
/**
* Gets the {@link DocumentsApi}. Lazily instantiated.
*
* @return the {@link DocumentsApi}
*/
public synchronized DocumentsApi documents() {
if (documents == null) {
documents = new DocumentsApi(apiClient);
}
return documents;
}
/**
* Gets the {@link EventsApiSSE}. Lazily instantiated.
*
* @return the {@link EventsApiSSE}
*/
public synchronized EventsApiSSE events() {
if (events == null) {
events = new EventsApiSSE(apiClient);
}
return events;
}
/**
* Gets the {@link FundingApi}. Lazily instantiated.
*
* @return the {@link FundingApi}
*/
public synchronized FundingApi funding() {
if (funding == null) {
funding = new FundingApi(apiClient);
}
return funding;
}
/**
* Gets the {@link FundingWalletsApi}. Lazily instantiated.
*
* @return the {@link FundingWalletsApi}
*/
public synchronized FundingWalletsApi fundingWallets() {
if (fundingWallets == null) {
fundingWallets = new FundingWalletsApi(apiClient);
}
return fundingWallets;
}
/**
* Gets the {@link JournalsApi}. Lazily instantiated.
*
* @return the {@link JournalsApi}
*/
public synchronized JournalsApi journals() {
if (journals == null) {
journals = new JournalsApi(apiClient);
}
return journals;
}
/**
* Gets the {@link KycApi}. Lazily instantiated.
*
* @return the {@link KycApi}
*/
public synchronized KycApi kyc() {
if (kyc == null) {
kyc = new KycApi(apiClient);
}
return kyc;
}
/**
* Gets the {@link LogosApi}. Lazily instantiated.
*
* @return the {@link LogosApi}
*/
public synchronized LogosApi logos() {
if (logos == null) {
logos = new LogosApi(apiClient);
}
return logos;
}
/**
* Gets the {@link OAuthApi}. Lazily instantiated.
*
* @return the {@link OAuthApi}
*/
public synchronized OAuthApi oAuth() {
if (oAuth == null) {
oAuth = new OAuthApi(apiClient);
}
return oAuth;
}
/**
* Gets the {@link RebalancingApi}. Lazily instantiated.
*
* @return the {@link RebalancingApi}
*/
public synchronized RebalancingApi rebalancing() {
if (rebalancing == null) {
rebalancing = new RebalancingApi(apiClient);
}
return rebalancing;
}
/**
* Gets the {@link ReportingApi}. Lazily instantiated.
*
* @return the {@link ReportingApi}
*/
public synchronized ReportingApi reporting() {
if (reporting == null) {
reporting = new ReportingApi(apiClient);
}
return reporting;
}
/**
* Gets the {@link TradingApi}. Lazily instantiated.
*
* @return the {@link TradingApi}
*/
public synchronized TradingApi trading() {
if (trading == null) {
trading = new TradingApi(apiClient);
}
return trading;
}
/**
* Gets the {@link WatchlistApi}. Lazily instantiated.
*
* @return the {@link WatchlistApi}
*/
public synchronized WatchlistApi watchlist() {
if (watchlist == null) {
watchlist = new WatchlistApi(apiClient);
}
return watchlist;
}
}