-
Notifications
You must be signed in to change notification settings - Fork 22
/
demo.js
175 lines (165 loc) · 7.14 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
/*jslint this: true, browser: true, for: 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")
});
/**
* Example of handling a 404 error.
* @return {void}
*/
function trigger404NotFound() {
fetch(
demo.apiUrl + "/port/invalid",
{
"method": "GET",
"headers": {
"Authorization": "Bearer " + document.getElementById("idBearerToken").value
}
}
).catch(function (error) {
// This will end in a CORS issue on the OPTIONS preflight:
console.error(error);
});
}
/**
* Example of handling a 401 error.
* @return {void}
*/
function trigger401Unauthorized() {
const invalidBearerToken = Math.random(); // This token is intentionally invalid.
fetch(
demo.apiUrl + "/port/v1/orders",
{
"method": "GET",
"headers": {
"Authorization": "Bearer " + invalidBearerToken
}
}
).then(function (response) {
let textToDisplay;
if (!response.ok) {
textToDisplay = "Error with status " + response.status + " " + response.statusText;
// The 401 error has an empty response, so no need to parse the response as JSON message.
// Always log the correlation header, so Saxo can trace this id in the logging.
textToDisplay += "\n\nX-Correlation header (for troubleshooting with Saxo): " + response.headers.get("X-Correlation");
} else {
// We don't end up here..
textToDisplay = "Successful request.";
}
console.error(textToDisplay);
}).catch(function (error) {
console.error(error);
});
}
/**
* Example of handling a 400 error.
* @return {void}
*/
function trigger400BadRequest() {
fetch(
demo.apiUrl + "/ref/v1/instruments?SectorId=Vastgoed&IncludeNonTradable=Ja&CanParticipateInMultiLegOrder=Mag+wel&Uics=N.V.T.&AssetTypes=Aandelen&Tags=Vastgoed&AccountKey=IBAN",
{
"method": "GET",
"headers": {
"Authorization": "Bearer " + document.getElementById("idBearerToken").value
}
}
).then(function (response) {
if (!response.ok) {
demo.processError(response);
} else {
// We don't end up here..
console.log("Successful request.");
}
}).catch(function (error) {
console.error(error);
});
}
/**
* Example of handling a 429 error.
* @return {void}
*/
function trigger429TooManyRequests() {
let i = 0;
// The remaining "RefLimit" is returned in the response headers
// X-RateLimit-AppDay-* & X-RateLimit-RefDataInstrumentsMinute-*.
// JavaScript has no access, but you can find them using F12 developer tools.
function doRequest() {
fetch(
demo.apiUrl + "/ref/v1/instruments?$top=1&$skip=0&Keywords=aex",
{
"method": "GET",
"headers": {
"Authorization": "Bearer " + document.getElementById("idBearerToken").value
}
}
).then(function (response) {
if (response.ok) {
console.log("Successful request #" + i);
i += 1;
if (i < 250) {
doRequest(); // Repeat this request until we are blocked with an HTTP 429.
}
} else {
demo.processError(response);
}
}).catch(function (error) {
console.error(error);
});
}
doRequest();
}
/**
* Example of handling an error in a batch request.
* @return {void}
*/
function triggerErrorInBatchRequest() {
// Info on batch requests: https://saxobank.github.io/openapi-samples-js/batch-request/
const host = "gateway.saxobank.com";
const request = "--+\r\nContent-Type:application/http; msgtype=request\r\n\r\nGET /sim/openapi/ref/v1/instruments?SectorId=Vastgoed&IncludeNonTradable=Ja&CanParticipateInMultiLegOrder=Mag+wel&Uics=N.V.T.&AssetTypes=Aandelen&Tags=Vastgoed&AccountKey=IBAN HTTP/1.1\r\nX-Request-Id:1\r\nAccept-Language:en\r\nHost:" + host + "\r\n\r\n\r\n--+--\r\n";
fetch(
demo.apiUrl + "/ref/batch",
{
"headers": {
"Content-Type": "multipart/mixed; boundary=\"+\"",
"Accept": "*/*",
"Accept-Language": "en, *;q=0.5",
"Authorization": "Bearer " + document.getElementById("idBearerToken").value,
"Cache-Control": "no-cache"
},
"body": request,
"method": "POST"
}
).then(function (response) {
if (response.ok) {
// The request goes probably perfectly well, so we end up here.
response.text().then(function (responseText) {
if (!demo.hasBatchResponseErrors(responseText.split("\n"), response.headers.get("X-Correlation"))) {
// This part is not reached..
console.log("No errors in response!\n\n" + responseText);
}
});
} else {
demo.processError(response);
}
}).catch(function (error) {
console.error(error);
});
}
demo.setupEvents([
{"evt": "click", "elmId": "idBtnGet404NotFound", "func": trigger404NotFound, "funcsToDisplay": [trigger404NotFound]},
{"evt": "click", "elmId": "idBtnGet401Unauthorized", "func": trigger401Unauthorized, "funcsToDisplay": [trigger401Unauthorized]},
{"evt": "click", "elmId": "idBtnGet400BadRequest", "func": trigger400BadRequest, "funcsToDisplay": [trigger400BadRequest, demo.processError]},
{"evt": "click", "elmId": "idBtnGet429TooManyRequests", "func": trigger429TooManyRequests, "funcsToDisplay": [trigger429TooManyRequests, demo.processError]},
{"evt": "click", "elmId": "idBtnGetErrorInBatchRequest", "func": triggerErrorInBatchRequest, "funcsToDisplay": [triggerErrorInBatchRequest, demo.hasBatchResponseErrors]}
]);
demo.displayVersion("ref");
}());