odsl-javascript-sdk 1.0.4 → 1.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.node-xmlhttprequest-sync-116040 +0 -0
- package/.node-xmlhttprequest-sync-148376 +0 -0
- package/package.json +3 -2
- package/src/odsl.js +122 -0
- package/src/process.js +26 -22
- package/src/sdk.js +1 -1
|
File without changes
|
|
File without changes
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "odsl-javascript-sdk",
|
|
3
|
-
"type": "
|
|
4
|
-
"version": "1.0.
|
|
3
|
+
"type": "commonjs",
|
|
4
|
+
"version": "1.0.5",
|
|
5
5
|
"description": "Javascript SDK for OpenDataDSL",
|
|
6
6
|
"main": "src/sdk.js",
|
|
7
7
|
"scripts": {
|
|
@@ -29,6 +29,7 @@
|
|
|
29
29
|
"@azure/identity": "^4.13.0",
|
|
30
30
|
"@azure/msal-node": "^2.6.6",
|
|
31
31
|
"@azure/service-bus": "^7.9.5",
|
|
32
|
+
"node-fetch": "^3.3.2",
|
|
32
33
|
"xmlhttprequest": "^1.8.0"
|
|
33
34
|
},
|
|
34
35
|
"devDependencies": {
|
package/src/odsl.js
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const { ConfidentialClientApplication } = require("@azure/msal-node");
|
|
3
|
+
const { PublicClientApplication } = require("@azure/msal-browser");
|
|
4
|
+
const { XMLHttpRequest } = require("xmlhttprequest");
|
|
5
|
+
|
|
6
|
+
const msalConfig = {
|
|
7
|
+
"auth": {
|
|
8
|
+
"clientId": "d3742f5f-3d4d-4565-a80a-ebdefaab8d08",
|
|
9
|
+
"authority": "https://login.microsoft.com/common"
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
module.exports = class ODSL {
|
|
14
|
+
token = null;
|
|
15
|
+
// host = "https://odsl-dev.azurewebsites.net/api/";
|
|
16
|
+
host = "http://localhost:7071/api/";
|
|
17
|
+
constructor(loginResponse) {
|
|
18
|
+
this.token = loginResponse.accessToken;
|
|
19
|
+
}
|
|
20
|
+
static async login() {
|
|
21
|
+
try {
|
|
22
|
+
let msalInstance = new pcbrowser(msalConfig);
|
|
23
|
+
console.log("Initialising");
|
|
24
|
+
await msalInstance.initialize();
|
|
25
|
+
console.log("Logging In");
|
|
26
|
+
const loginResponse = await msalInstance.loginPopup(loginRequest);
|
|
27
|
+
return new ODSL(loginResponse);
|
|
28
|
+
} catch (err) {
|
|
29
|
+
console.log("Token Acquisition Failed: " + err);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
static async loginWithSecret(config) {
|
|
33
|
+
try {
|
|
34
|
+
let loginRequest = {
|
|
35
|
+
scopes: ["api://opendatadsl/.default"]
|
|
36
|
+
}
|
|
37
|
+
let msalInstance = new ConfidentialClientApplication(config);
|
|
38
|
+
console.log("Logging In");
|
|
39
|
+
const loginResponse = await msalInstance.acquireTokenByClientCredential(loginRequest);
|
|
40
|
+
return new ODSL(loginResponse);
|
|
41
|
+
} catch (err) {
|
|
42
|
+
console.log("Token Acquisition Failed: " + err);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
static loginWithToken(token) {
|
|
46
|
+
return new ODSL({accessToken:token});
|
|
47
|
+
}
|
|
48
|
+
get(service, source, id) {
|
|
49
|
+
try {
|
|
50
|
+
var xhttp = new XMLHttpRequest();
|
|
51
|
+
id = encodeURIComponent(id);
|
|
52
|
+
var url = new URL(this.host + service + "/v1/" + source + "/" + id);
|
|
53
|
+
xhttp.open("GET", url, false);
|
|
54
|
+
xhttp.setRequestHeader("Authorization", "Bearer " + this.token);
|
|
55
|
+
xhttp.responseType = "json";
|
|
56
|
+
xhttp.send();
|
|
57
|
+
if (xhttp.status < 200 || xhttp.status >= 300) {
|
|
58
|
+
throw xhttp.getResponseHeader("x-odsl-error");
|
|
59
|
+
}
|
|
60
|
+
return JSON.parse(xhttp.responseText);
|
|
61
|
+
} catch (err) {
|
|
62
|
+
console.log("GET request Failed: " + err);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
getAsync(service, source, id, callback) {
|
|
66
|
+
try {
|
|
67
|
+
var xhttp = new XMLHttpRequest();
|
|
68
|
+
id = encodeURIComponent(id);
|
|
69
|
+
var url = new URL(this.host + service + "/v1/" + source + "/" + id);
|
|
70
|
+
xhttp.open("GET", url);
|
|
71
|
+
xhttp.setRequestHeader("Authorization", "Bearer " + this.token);
|
|
72
|
+
xhttp.responseType = "json";
|
|
73
|
+
xhttp.send();
|
|
74
|
+
xhttp.onload = function() {
|
|
75
|
+
if (xhttp.status < 200 || xhttp.status >= 300) {
|
|
76
|
+
throw xhttp.getResponseHeader("x-odsl-error");
|
|
77
|
+
} else {
|
|
78
|
+
callback(JSON.parse(xhttp.responseText));
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
} catch (err) {
|
|
82
|
+
console.log("GET request Failed: " + err);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
list(service, source, params) {
|
|
86
|
+
try {
|
|
87
|
+
var xhttp = new XMLHttpRequest();
|
|
88
|
+
var url = new URL(this.host + service + "/v1/" + source);
|
|
89
|
+
if (params != undefined) {
|
|
90
|
+
for (let key of Object.keys(params)) {
|
|
91
|
+
url.searchParams.set(key, params[key]);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
xhttp.open("GET", url, false);
|
|
95
|
+
xhttp.setRequestHeader("Authorization", "Bearer " + this.token);
|
|
96
|
+
xhttp.responseType = "json";
|
|
97
|
+
xhttp.send();
|
|
98
|
+
if (xhttp.status < 200 || xhttp.status >= 300) {
|
|
99
|
+
throw xhttp.getResponseHeader("x-odsl-error");
|
|
100
|
+
}
|
|
101
|
+
return JSON.parse(xhttp.responseText);
|
|
102
|
+
} catch (err) {
|
|
103
|
+
console.log("GET request Failed: " + err);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
update(service, source, body) {
|
|
107
|
+
try {
|
|
108
|
+
var xhttp = new XMLHttpRequest();
|
|
109
|
+
var url = new URL(this.host + service + "/v1/" + source);
|
|
110
|
+
xhttp.open("POST", url, false);
|
|
111
|
+
xhttp.setRequestHeader("Authorization", "Bearer " + this.token);
|
|
112
|
+
xhttp.responseType = "json";
|
|
113
|
+
xhttp.send(JSON.stringify(body));
|
|
114
|
+
if (xhttp.status < 200 || xhttp.status >= 300) {
|
|
115
|
+
throw xhttp.getResponseHeader("x-odsl-error");
|
|
116
|
+
}
|
|
117
|
+
return JSON.parse(xhttp.responseText);
|
|
118
|
+
} catch (err) {
|
|
119
|
+
console.log("GET request Failed: " + err);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
package/src/process.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
const { ServiceBusClient, ServiceBusMessage } = require("@azure/service-bus");
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
module.exports = class PROCESS {
|
|
4
4
|
constructor(p, task) {
|
|
5
5
|
this.process = p;
|
|
6
6
|
this.task = task;
|
|
7
|
-
var cstr = process.env.
|
|
7
|
+
var cstr = process.env.ODSL_SB_CPM;
|
|
8
8
|
this.serviceBusClient = new ServiceBusClient(cstr);
|
|
9
9
|
this.started = false;
|
|
10
10
|
this.queue_name = process.env.ODSL_STAGE + "/process-execution";
|
|
@@ -24,40 +24,44 @@ export default class PROCESS {
|
|
|
24
24
|
var message = new ProcessMessage(this.process, this.task);
|
|
25
25
|
message.status = status;
|
|
26
26
|
message.message = mess;
|
|
27
|
+
console.log(mess);
|
|
27
28
|
await this.updateProcess(message);
|
|
28
29
|
}
|
|
29
30
|
|
|
30
31
|
async startPhase(name) {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
await this.updateProcess(
|
|
32
|
+
this.phase = new ProcessMessage(this.process, this.task);
|
|
33
|
+
this.phase.status = "running";
|
|
34
|
+
this.phase.message = "Initialising Phase: " + name;
|
|
35
|
+
this.phase.setPhase(name);
|
|
36
|
+
await this.updateProcess(this.phase);
|
|
36
37
|
}
|
|
37
38
|
|
|
38
39
|
async endPhase(status, mess) {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
40
|
+
this.phase.status = status;
|
|
41
|
+
this.phase.message = mess;
|
|
42
|
+
this.phase.timestamp = new Date().toISOString().substring(0,19);
|
|
42
43
|
let level = status == "failed" ? "fatal" : "info"
|
|
43
44
|
let lm = new Date().toISOString() + " " + level + " " + mess;
|
|
44
|
-
|
|
45
|
-
await this.updateProcess(
|
|
45
|
+
this.phase.log.push(lm);
|
|
46
|
+
await this.updateProcess(this.phase);
|
|
46
47
|
}
|
|
47
48
|
|
|
48
49
|
async logMessage(level, mess) {
|
|
49
50
|
let lm = new Date().toISOString() + " " + level + " " + mess;
|
|
50
|
-
|
|
51
|
-
|
|
51
|
+
console.log(lm);
|
|
52
|
+
this.phase.log.push(lm);
|
|
53
|
+
await this.updateProcess(this.phase);
|
|
52
54
|
}
|
|
53
55
|
|
|
54
56
|
async updateProcess(message) {
|
|
55
|
-
var sbm =
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
57
|
+
var sbm = {
|
|
58
|
+
body: message,
|
|
59
|
+
applicationProperties:{
|
|
60
|
+
tenantid: this.task.tid,
|
|
61
|
+
environment: this.task.environment
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
var sender = this.serviceBusClient.createSender(this.queue_name);
|
|
61
65
|
await sender.sendMessages(sbm);
|
|
62
66
|
}
|
|
63
67
|
}
|
|
@@ -65,7 +69,7 @@ export default class PROCESS {
|
|
|
65
69
|
class ProcessMessage {
|
|
66
70
|
constructor(p, t) {
|
|
67
71
|
this.log = [];
|
|
68
|
-
this.timestamp = new Date().toISOString();
|
|
72
|
+
this.timestamp = new Date().toISOString().substring(0,19);
|
|
69
73
|
this.type = "ProcessMessage";
|
|
70
74
|
this.id = t._id;
|
|
71
75
|
this.tenantid = t.tid;
|
|
@@ -83,4 +87,4 @@ class ProcessMessage {
|
|
|
83
87
|
setPhase(name) {
|
|
84
88
|
this.phase = name;
|
|
85
89
|
}
|
|
86
|
-
}
|
|
90
|
+
}
|
package/src/sdk.js
CHANGED