-
Notifications
You must be signed in to change notification settings - Fork 0
/
hd-soap-service.ts
97 lines (74 loc) · 2.47 KB
/
hd-soap-service.ts
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
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class HdSoapService {
public host = '';
public port = 80;
public pathToAsmx = '';
public namespace = '';
constructor() {
}
async send(command: string, jsonObject: Object, isListOf: string): Promise<string> {
const xmlhttp = new XMLHttpRequest();
const url = 'http://' + this.host + ':' + this.port + this.pathToAsmx;
xmlhttp.open('POST', url, true);
const parameters = this.buildParameters(jsonObject);
// The following variable contains the xml SOAP request.
const sr = `<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<` + command + ` xmlns="` + this.namespace + `">
` + parameters + `
</` + command + `>
</soap12:Body>
</soap12:Envelope>`;
// console.log('SENDING', sr);
const p = new Promise<string>((resolve) => {
xmlhttp.onreadystatechange = () => {
if (xmlhttp.readyState === 4) {
if (xmlhttp.status === 200) {
const xml = xmlhttp.responseXML;
let responseString: string;
if (isListOf)
{
responseString = this.getResponseStringList(xml
.getElementsByTagName(command + 'Result')[0].outerHTML,
isListOf);
} else {
responseString = xml
.getElementsByTagName(command + 'Result')[0]
.innerHTML;
}
resolve(responseString);
}
}
};
});
// Send the POST request.
xmlhttp.setRequestHeader('Content-Type', 'application/soap+xml; charset=utf-8');
xmlhttp.responseType = 'document';
xmlhttp.send(sr);
return p;
}
getResponseStringList(xml: string, isListOf: string): string {
const arr = [];
// Use JQuery to help me dig through the crap.
let e = $(xml).find(isListOf);
for (var x=0; x<e.length; x++) {
arr.push(e[x].innerText);
}
return JSON.stringify(arr);
}
buildParameters(j: Object): string {
let ret = '';
const keys = Object.keys(j);
keys.forEach((k) => {
ret += '<' + k + '>' + j[k] + '</' + k + '>\r\n';
});
return ret;
}
}