const doPostAsync = async (uri, contentType, headers, data) => {
return utils.PostAsync(uri, contentType, JSON.stringify(headers), data)
.ToPromise()
.then(success => {
let parsedSuccess = JSON.parse(success);
if (parsedSuccess.Data.IsSuccess === false) {
Console.WriteLine("ERROR" + parsedSuccess.Data.ErrorMessage);
}
return JSON.parse(utils.XmlToJson(parsedSuccess.Data.Response));
});
};
var auth = {
username: 'USERNAME',
password: 'PASSWORD'
}
const checkErrors = response => {
if (response &&
response['soap:Envelope'] &&
response['soap:Envelope']['soap:Body'] &&
response['soap:Envelope']['soap:Body']['soap:Fault'] &&
response['soap:Envelope']['soap:Body']['soap:Fault']['faultstring'].length > 0){
throw response['soap:Envelope']['soap:Body']['soap:Fault']['faultstring'];
}
}
const doRequest = async (serviceAddress, soapAction, body) =>{
var response = await doPostAsync(serviceAddress, "application/xml", {
'Content-Length': body.length,
'Soapaction': soapAction,
'Host': 'localhost'
}, body)
checkErrors(response);
return response;
}
const getAuthToken = async (username, password) => {
let body = `
${username}
${password}
`;
var response = await doRequest(
'http://services.discretewireless.com/v2009_1/AuthenticationManagerV1.asmx',
'http://services.discretewireless.com/v2009_1/:getAuthenticationToken',
body);
const token = response['soap:Envelope']['soap:Body']['getAuthenticationTokenResponse']['AuthToken'];
const expirationDate = response['soap:Envelope']['soap:Body']['getAuthenticationTokenResponse']['ExpirationDate']
return token;
}
try {
const authToken = await getAuthToken(auth.username, auth.password);
// GetLocationGroupDetails
let response = await doRequest(
'http://services.discretewireless.com/v2009_2/LocationsManagerV2.asmx',
'http://services.nextraq.com/v2009_2/LocationManagerV2:getLocationDetails', `
${authToken}
10
0
false
`)
jsonStringResult.registerJsonResponse(JSON.stringify(response));
} catch(e){
logger.registerErrorMessage(e.toString());
}