-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
137 lines (121 loc) · 4.4 KB
/
server.js
File metadata and controls
137 lines (121 loc) · 4.4 KB
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
const express = require('express');
let path = require('path');
var bodyParser = require('body-parser');
var rp = require('request-promise');
const web_path = path.join(__dirname, './');
const app = express();
const port = 8080;
// EDP constant variables
const auth_hostname = 'https://api.refinitiv.com';
const EDP_version = '/v1';
const auth_category_URL = '/auth/oauth2';
const auth_endpoint_URL = '/token';
const client_secret = '';
const streaming_category_URL = '/streaming/pricing';
const streaming_category_version = '/v1/';
var hostname_service_endpoint = auth_hostname + streaming_category_URL + streaming_category_version;
var edp_gateway_token_url = auth_hostname + auth_category_URL + EDP_version + auth_endpoint_URL;
app.use(express.static(web_path));
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({
extended: true
})); // for parsing application/x-www-form-urlencoded
app.get("/quoteObject.html", function (req, res) {
res.sendFile(path.join(web_path, "quoteObject.html"));
});
app.get("/quoteObjectERT.html", function (req, res) {
res.sendFile(path.join(web_path, "quoteObjectERT.html"));
});
//[Modify By Wasin W.]
//
// "/requesttoken" HTTP Post
// handle "/requesttoken" HTTP Post from ERTRESTController.js. The function redirects posted data to EDP Authentication server via get_Access_Token() function
//
//
app.post('/token', function (req, res) {
console.log(`receive /token HTTP post from ERTRESTController.js: request parameter is ${JSON.stringify(req.body)}`);
get_Access_Token(req.body, res);
})
//[Modify By Wasin W.]
//
// "/ERT_discovery" HTTP Get
// handle "/ERT_discovery" HTTP Get from ERTRESTController.js. The function redirects getted data to EDP Service Discovery via get_service_discovery() function
//
//
app.post('/streaming/pricing', function (req, res) {
get_service_discovery(req.body, res);
})
//[Modify By Wasin W.]
//
// get_Access_Token(data, response)
// Send HTTP Post request to EDP Authentication gateway, pass HTTP response data back to ERTRESTController.js.
//
//
function get_Access_Token(data, res) {
let authen_options = {
method: 'POST',
uri: edp_gateway_token_url,
form: data,
headers: {
'Accept': 'application/json',
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Basic'
},
json: true,
resolveWithFullResponse: true,
auth: {
username: data.username,
password: ''
},
simple: true,
transform2xxOnly: true
};
return rp(authen_options)
.then(function (response) {
//console.log('response.statusCode =' + response.statusCode);
//console.log(`response = ${JSON.stringify(response)}`);
if (response.statusCode == 200) {
console.log('EDP-GW Authentication succeeded. RECEIVED:')
res.send(response.body);
}
})
.catch(function (error) {
console.log(`EDP-GW authentication result failure: ${error} statusCode =${error.statusCode}`);
//res.send(error);
res.status(error.statusCode).send(error);
});
}
//[Modify By Wasin W.]
//
// get_service_discovery(data, response)
// Send HTTP Post request to EDP Service Discovery gateway, pass HTTP response data back to ERTRESTController.js.
//
//
function get_service_discovery(payload, res) {
let ERT_service_options = {
method: 'GET',
uri: hostname_service_endpoint,
qs: {
transport: payload.transport,
dataformat: payload.dataformat
},
headers: {
'Accept': 'application/json',
'Authorization': 'Bearer ' + payload.access_token
},
json: true,
resolveWithFullResponse: true
};
return rp(ERT_service_options)
.then(function (response) {
if (response.statusCode == 200) {
console.log('ERT in Cloud RealTime Service Discovery succeeded. RECEIVED:')
res.send(response.body);
}
}).catch(function (error) {
console.error(`ERT in Cloud RealTime Service Discovery result failure: ${error} state = ${error.readyState}`);
//res.send(error);
res.status(error.statusCode).send(error);
});
};
app.listen(port, () => console.log(`Application is running at port ${port}`));