forked from Petersoj/alpaca-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AssetsEndpoint.java
78 lines (67 loc) · 2.66 KB
/
AssetsEndpoint.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
package net.jacobpeterson.alpaca.rest.endpoint;
import com.google.gson.reflect.TypeToken;
import net.jacobpeterson.alpaca.model.endpoint.asset.Asset;
import net.jacobpeterson.alpaca.model.endpoint.asset.enums.AssetStatus;
import net.jacobpeterson.alpaca.rest.AlpacaClient;
import net.jacobpeterson.alpaca.rest.AlpacaClientException;
import okhttp3.HttpUrl;
import okhttp3.Request;
import java.util.ArrayList;
import java.util.List;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* {@link AlpacaEndpoint} for <a href="https://docs.alpaca.markets/api-documentation/api-v2/assets/">Assets</a>.
*/
public class AssetsEndpoint extends AlpacaEndpoint {
/**
* Instantiates a new {@link AssetsEndpoint}.
*
* @param alpacaClient the {@link AlpacaClient}
*/
public AssetsEndpoint(AlpacaClient alpacaClient) {
super(alpacaClient, "assets");
}
/**
* Get a list of {@link Asset}s.
*
* @param assetStatus the {@link AssetStatus}. By default, all {@link AssetStatus}es are included.
* @param assetClass the asset class. Defaults to "us_equity".
*
* @return a {@link List} of {@link Asset}s
*
* @throws AlpacaClientException thrown for {@link AlpacaClientException}s
*/
public List<Asset> get(AssetStatus assetStatus, String assetClass) throws AlpacaClientException {
HttpUrl.Builder urlBuilder = alpacaClient.urlBuilder()
.addPathSegment(endpointPathSegment);
if (assetStatus != null) {
urlBuilder.addQueryParameter("status", assetStatus.toString());
}
if (assetClass != null) {
urlBuilder.addQueryParameter("asset_class", assetClass);
}
Request request = alpacaClient.requestBuilder(urlBuilder.build())
.get()
.build();
return alpacaClient.requestObject(request, new TypeToken<ArrayList<Asset>>() {}.getType());
}
/**
* Gets an {@link Asset} by a symbol or {@link Asset#getId()}.
*
* @param symbolOrAssetID the symbol or {@link Asset#getId()}
*
* @return the {@link Asset}
*
* @throws AlpacaClientException thrown for {@link AlpacaClientException}s
*/
public Asset getBySymbol(String symbolOrAssetID) throws AlpacaClientException {
checkNotNull(symbolOrAssetID);
HttpUrl.Builder urlBuilder = alpacaClient.urlBuilder()
.addPathSegment(endpointPathSegment)
.addPathSegment(symbolOrAssetID);
Request request = alpacaClient.requestBuilder(urlBuilder.build())
.get()
.build();
return alpacaClient.requestObject(request, Asset.class);
}
}