-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathdemo.js
209 lines (199 loc) · 8.29 KB
/
demo.js
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
/*jslint this: true, browser: true, long: true, unordered: true */
/*global window console demonstrationHelper */
(function () {
// Create a helper function to remove some boilerplate code from the example itself.
const demo = demonstrationHelper({
"responseElm": document.getElementById("idResponse"),
"javaScriptElm": document.getElementById("idJavaScript"),
"accessTokenElm": document.getElementById("idBearerToken"),
"retrieveTokenHref": document.getElementById("idHrefRetrieveToken"),
"tokenValidateButton": document.getElementById("idBtnValidate"),
"accountsList": document.getElementById("idCbxAccount"),
"footerElm": document.getElementById("idFooter")
});
/**
* Authenticate using an old app, created before November 1st, 2021.
* @return {void}
*/
function getTokenOnLegacyAssetTypes() {
const stateObject = {
"redirect": window.location.pathname, // https://auth0.com/docs/protocols/state-parameters#redirect-users
"csrfToken": window.localStorage.getItem("csrfToken"), // https://auth0.com/docs/protocols/state-parameters#csrf-attacks
"env": "sim"
};
const redirectUrl = window.location.protocol + "//" + window.location.host + "/openapi-samples-js/assets/html/redirect.html";
const href = "https://sim.logonvalidation.net/authorize?response_type=token&state=" + window.btoa(JSON.stringify(stateObject)) + "&redirect_uri=" + encodeURIComponent(redirectUrl) + "&client_id=7194692c30db42efb2c675c6c0fb2a67";
// Navigate to authentication of legacy app.
console.log("Redirectiong to " + href);
window.location.href = href;
}
/**
* Authenticate using an app on Extended AssetTypes.
* @return {void}
*/
function getTokenOnExtendedAssetTypes() {
const href = document.getElementById("idHrefRetrieveToken").href; // Just copy the regular redirect
// Navigate to authentication of modern app.
console.log("Redirectiong to " + href);
window.location.href = href;
}
/**
* Example of getting the allowed AssetTypes for an account, with or without using Extended AssetTypes.
* @return {void}
*/
function getLegalAssetTypes() {
const headers = {
"Authorization": "Bearer " + document.getElementById("idBearerToken").value
};
if (document.getElementById("idChkExtAsset").checked) {
headers.Pragma = "oapi-x-extasset";
}
fetch(
demo.apiUrl + "/port/v1/accounts/" + encodeURIComponent(demo.user.accountKey),
{
"method": "GET",
"headers": headers
}
).then(function (response) {
if (response.ok) {
response.json().then(function (responseJson) {
responseJson.LegalAssetTypes.sort();
console.log(JSON.stringify(responseJson.LegalAssetTypes, null, 4));
});
} else {
demo.processError(response);
}
}).catch(function (error) {
console.error(error);
});
}
/**
* Example of a search for an instrument with the name iShares, in general an ETF.
* @return {void}
*/
function findIshares() {
const headers = {
"Authorization": "Bearer " + document.getElementById("idBearerToken").value
};
if (document.getElementById("idChkExtAsset").checked) {
headers.Pragma = "oapi-x-extasset";
}
fetch(
demo.apiUrl + "/ref/v1/instruments?AccountKey=" + encodeURIComponent(demo.user.accountKey) + "&Keywords=ishares",
{
"method": "GET",
"headers": headers
}
).then(function (response) {
if (response.ok) {
response.json().then(function (responseJson) {
console.log(JSON.stringify(responseJson, null, 4));
});
} else {
demo.processError(response);
}
}).catch(function (error) {
console.error(error);
});
}
/**
* Example of a search for an ETF, with or without using Extended AssetTypes.
* @return {void}
*/
function findEtf() {
const headers = {
"Authorization": "Bearer " + document.getElementById("idBearerToken").value
};
if (document.getElementById("idChkExtAsset").checked) {
headers.Pragma = "oapi-x-extasset";
}
fetch(
demo.apiUrl + "/ref/v1/instruments?AccountKey=" + encodeURIComponent(demo.user.accountKey) + "&AssetTypes=Etf",
{
"method": "GET",
"headers": headers
}
).then(function (response) {
if (response.ok) {
response.json().then(function (responseJson) {
if (responseJson.Data.length > 0) {
document.getElementById("idUic").value = responseJson.Data[0].Identifier;
}
console.log(JSON.stringify(responseJson, null, 4));
});
} else {
demo.processError(response);
}
}).catch(function (error) {
console.error(error);
});
}
/**
* Example of getting prices.
* @param {string} assetType The AssetType of the Uic.
* @return {void}
*/
function getPrices(assetType) {
const headers = {
"Authorization": "Bearer " + document.getElementById("idBearerToken").value,
"Content-Type": "application/json; charset=utf-8"
};
if (document.getElementById("idChkExtAsset").checked) {
headers.Pragma = "oapi-x-extasset";
}
const data = {
"ContextId": "MyContextId",
"ReferenceId": "MyReferenceId" + Date.now(),
"Arguments": {
"AccountKey": demo.user.accountKey,
"Uic": document.getElementById("idUic").value,
"AssetType": assetType,
// DisplayAndFormat gives you the name of the instrument in the snapshot in the response.
// MarketDepth gives the order book, when available.
"FieldGroups": ["Quote", /*"MarketDepth",*/ "DisplayAndFormat", "PriceInfoDetails"]
}
};
fetch(
demo.apiUrl + "/trade/v1/prices/subscriptions",
{
"method": "POST",
"headers": headers,
"body": JSON.stringify(data)
}
).then(function (response) {
if (response.ok) {
response.json().then(function (responseJson) {
console.log(JSON.stringify(responseJson, null, 4));
});
} else {
demo.processError(response);
}
}).catch(function (error) {
console.error(error);
});
}
/**
* Request prices and use the AssetType Etf.
* @return {void}
*/
function getPricesAsEtf() {
getPrices("Etf");
}
/**
* Request prices and use the AssetType Stock. For a limited time that will be possible, to make the migration easier.
* @return {void}
*/
function getPricesAsStock() {
getPrices("Stock");
}
demo.setupEvents([
{"evt": "click", "elmId": "idBtnGetTokenOnLegacyAssetTypes", "func": getTokenOnLegacyAssetTypes, "funcsToDisplay": [getTokenOnLegacyAssetTypes]},
{"evt": "click", "elmId": "idBtnGetTokenOnExtendedAssetTypes", "func": getTokenOnExtendedAssetTypes, "funcsToDisplay": [getTokenOnExtendedAssetTypes]},
{"evt": "click", "elmId": "idBtnGetLegalAssetTypes", "func": getLegalAssetTypes, "funcsToDisplay": [getLegalAssetTypes]},
{"evt": "click", "elmId": "idBtnSearchIshares", "func": findIshares, "funcsToDisplay": [findIshares]},
{"evt": "click", "elmId": "idBtnFindEtf", "func": findEtf, "funcsToDisplay": [findEtf]},
{"evt": "click", "elmId": "idBtnGetPrices", "func": getPricesAsEtf, "funcsToDisplay": [getPricesAsEtf, getPrices]},
{"evt": "click", "elmId": "idBtnGetPricesAsStock", "func": getPricesAsStock, "funcsToDisplay": [getPricesAsStock, getPrices]}
]);
demo.displayVersion("ref");
}());