Wrapper to translate SOAP requests in to RESTFul API calls
up vote
0
down vote
favorite
I recently had a build a wrapper to translate SOAP requests in to RESTFul API calls to our server and return the results to the requester.
Request
SOAP -> nodejs wrapper / convert to RESTFul -> Spring Boot Server
Response
Spring Boot Server -> nodejs wrapper / convert to SOAP -> SOAP
My code works perfectly, it handles the SOAP request, takes the passed credentials and logs into the server and then carries out the request. What I don't like is it is all in one single file. I have pruned back the code drastically here just to show what I am doing and I'd like to know the best practices to build such an app.
For example, myService
is one object and would it be better to load this in from its own script but I am unsure how to do this in NodeJS. Full disclosure: I'm trying to avoid reading hours of tutorials but if you know of a good one to handle basics like this please let me know in a comment (not an answer).
"use strict";
var port = 2345;
var targetServer = 'https://aserver.com';
var soap = require('soap');
var http = require('http');
var request = require("request");
var myService = {
Pfs: {
PfsSoap: {
abc: function(args, callback, header, req) {
var options = { method: 'POST',
url: targetServer + '/api/v2/login',
headers:
{ 'cache-control': 'no-cache',
'Content-Type': 'application/json' },
body: { username: 'username', password: 'password' },
json: true };
request(options, function (error, response, body) {
if (error) throw new Error(error);
callback({
"Result": {
"tns:MsgHeader": {
"tns:MessageType": "Response",
"tns:ContentVersion": "1.0"
},
"tns:ResponseId": args.requestId//,
// "ENVELOPE-RECEIVED": args
}
});
});
},
xyz: function(args, callback, header, req) {
/****
* The outer function calls the server with login credentials
**/
var options = { method: 'POST',
url: targetServer + '/api/v2/login',
headers:
{ 'cache-control': 'no-cache',
'Content-Type': 'application/json' },
body: {
username: header.AuthenticationHeader.SenderId,
password: header.AuthenticationHeader.Password
},
json: true };
request(options, function (error, response, outerFunctionReturn) {
/****
* Handle authorisation failure here. Error is not for bad credentials.
**/
if (error) {
throw new Error(error);
}
if(!outerFunctionReturn.authToken){ // if this doesn't exist then the credentails failed
callback({
"Result": {
"tns:MsgHeader": {
"tns:MessageType": "Response",
"tns:ContentVersion": "1.0"
},
"tns:ResponseStatus": {
"tns:ResponseResult": "Failure",
"tns:ResponseCode": "401",
"tns:ResponseMessage": ""
},
"tns:ResponseId": args.requestId
}
});
} else {
/****
* The inner function calls the server with the bearer token to test order
**/
var options = { method: 'PUT',
url: targetServer + '/api//v2/orders',
headers:
{
'cache-control': 'no-cache',
Authorization: outerFunctionReturn.authToken,
'Content-Type': 'application/json' },
body:
{ customerEmail: 'someone@gmail.com' },
json: true };
request(options, function (error, response, innerFunctionReturn) {
if (error) {
throw new Error(error);
}
/****
* At this point the login is successful and we have successfully fetched the needed data.
* This callback builds the return SOAP envelope.
**/
callback({
"LoadCardResult": {
"tns:MsgHeader": {
"tns:MessageType": "LoadCardResponse",
"tns:ContentVersion": "1.0"
},
"tns:ResponseStatus": {
"tns:ResponseResult": "Success",
"tns:ResponseCode": innerFunctionReturn.barcode,
"tns:ResponseMessage": ""
},
"tns:CardValue": {
"tns:Amount": "1000",
"tns:CurrencyCode": "EUR",
"tns:EndBalance": "0"
},
"tns:ResponseId": outerFunctionReturn.vendorId/*args.requestId*/
}
});
});
}//END innerFunctionReturn
});
}
}
}
};
var xml = require('fs').readFileSync('Service.wsdl','utf8');
var server = http.createServer(function(request,response) {
console.log('Request ' + request.url);
response.end("404: Not Found: " + request.url);
});
console.log('Started server on port ' + port);
server.listen(port);
soap.listen(server,'/wsdl',myService, xml);
javascript node.js rest soap
add a comment |
up vote
0
down vote
favorite
I recently had a build a wrapper to translate SOAP requests in to RESTFul API calls to our server and return the results to the requester.
Request
SOAP -> nodejs wrapper / convert to RESTFul -> Spring Boot Server
Response
Spring Boot Server -> nodejs wrapper / convert to SOAP -> SOAP
My code works perfectly, it handles the SOAP request, takes the passed credentials and logs into the server and then carries out the request. What I don't like is it is all in one single file. I have pruned back the code drastically here just to show what I am doing and I'd like to know the best practices to build such an app.
For example, myService
is one object and would it be better to load this in from its own script but I am unsure how to do this in NodeJS. Full disclosure: I'm trying to avoid reading hours of tutorials but if you know of a good one to handle basics like this please let me know in a comment (not an answer).
"use strict";
var port = 2345;
var targetServer = 'https://aserver.com';
var soap = require('soap');
var http = require('http');
var request = require("request");
var myService = {
Pfs: {
PfsSoap: {
abc: function(args, callback, header, req) {
var options = { method: 'POST',
url: targetServer + '/api/v2/login',
headers:
{ 'cache-control': 'no-cache',
'Content-Type': 'application/json' },
body: { username: 'username', password: 'password' },
json: true };
request(options, function (error, response, body) {
if (error) throw new Error(error);
callback({
"Result": {
"tns:MsgHeader": {
"tns:MessageType": "Response",
"tns:ContentVersion": "1.0"
},
"tns:ResponseId": args.requestId//,
// "ENVELOPE-RECEIVED": args
}
});
});
},
xyz: function(args, callback, header, req) {
/****
* The outer function calls the server with login credentials
**/
var options = { method: 'POST',
url: targetServer + '/api/v2/login',
headers:
{ 'cache-control': 'no-cache',
'Content-Type': 'application/json' },
body: {
username: header.AuthenticationHeader.SenderId,
password: header.AuthenticationHeader.Password
},
json: true };
request(options, function (error, response, outerFunctionReturn) {
/****
* Handle authorisation failure here. Error is not for bad credentials.
**/
if (error) {
throw new Error(error);
}
if(!outerFunctionReturn.authToken){ // if this doesn't exist then the credentails failed
callback({
"Result": {
"tns:MsgHeader": {
"tns:MessageType": "Response",
"tns:ContentVersion": "1.0"
},
"tns:ResponseStatus": {
"tns:ResponseResult": "Failure",
"tns:ResponseCode": "401",
"tns:ResponseMessage": ""
},
"tns:ResponseId": args.requestId
}
});
} else {
/****
* The inner function calls the server with the bearer token to test order
**/
var options = { method: 'PUT',
url: targetServer + '/api//v2/orders',
headers:
{
'cache-control': 'no-cache',
Authorization: outerFunctionReturn.authToken,
'Content-Type': 'application/json' },
body:
{ customerEmail: 'someone@gmail.com' },
json: true };
request(options, function (error, response, innerFunctionReturn) {
if (error) {
throw new Error(error);
}
/****
* At this point the login is successful and we have successfully fetched the needed data.
* This callback builds the return SOAP envelope.
**/
callback({
"LoadCardResult": {
"tns:MsgHeader": {
"tns:MessageType": "LoadCardResponse",
"tns:ContentVersion": "1.0"
},
"tns:ResponseStatus": {
"tns:ResponseResult": "Success",
"tns:ResponseCode": innerFunctionReturn.barcode,
"tns:ResponseMessage": ""
},
"tns:CardValue": {
"tns:Amount": "1000",
"tns:CurrencyCode": "EUR",
"tns:EndBalance": "0"
},
"tns:ResponseId": outerFunctionReturn.vendorId/*args.requestId*/
}
});
});
}//END innerFunctionReturn
});
}
}
}
};
var xml = require('fs').readFileSync('Service.wsdl','utf8');
var server = http.createServer(function(request,response) {
console.log('Request ' + request.url);
response.end("404: Not Found: " + request.url);
});
console.log('Started server on port ' + port);
server.listen(port);
soap.listen(server,'/wsdl',myService, xml);
javascript node.js rest soap
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I recently had a build a wrapper to translate SOAP requests in to RESTFul API calls to our server and return the results to the requester.
Request
SOAP -> nodejs wrapper / convert to RESTFul -> Spring Boot Server
Response
Spring Boot Server -> nodejs wrapper / convert to SOAP -> SOAP
My code works perfectly, it handles the SOAP request, takes the passed credentials and logs into the server and then carries out the request. What I don't like is it is all in one single file. I have pruned back the code drastically here just to show what I am doing and I'd like to know the best practices to build such an app.
For example, myService
is one object and would it be better to load this in from its own script but I am unsure how to do this in NodeJS. Full disclosure: I'm trying to avoid reading hours of tutorials but if you know of a good one to handle basics like this please let me know in a comment (not an answer).
"use strict";
var port = 2345;
var targetServer = 'https://aserver.com';
var soap = require('soap');
var http = require('http');
var request = require("request");
var myService = {
Pfs: {
PfsSoap: {
abc: function(args, callback, header, req) {
var options = { method: 'POST',
url: targetServer + '/api/v2/login',
headers:
{ 'cache-control': 'no-cache',
'Content-Type': 'application/json' },
body: { username: 'username', password: 'password' },
json: true };
request(options, function (error, response, body) {
if (error) throw new Error(error);
callback({
"Result": {
"tns:MsgHeader": {
"tns:MessageType": "Response",
"tns:ContentVersion": "1.0"
},
"tns:ResponseId": args.requestId//,
// "ENVELOPE-RECEIVED": args
}
});
});
},
xyz: function(args, callback, header, req) {
/****
* The outer function calls the server with login credentials
**/
var options = { method: 'POST',
url: targetServer + '/api/v2/login',
headers:
{ 'cache-control': 'no-cache',
'Content-Type': 'application/json' },
body: {
username: header.AuthenticationHeader.SenderId,
password: header.AuthenticationHeader.Password
},
json: true };
request(options, function (error, response, outerFunctionReturn) {
/****
* Handle authorisation failure here. Error is not for bad credentials.
**/
if (error) {
throw new Error(error);
}
if(!outerFunctionReturn.authToken){ // if this doesn't exist then the credentails failed
callback({
"Result": {
"tns:MsgHeader": {
"tns:MessageType": "Response",
"tns:ContentVersion": "1.0"
},
"tns:ResponseStatus": {
"tns:ResponseResult": "Failure",
"tns:ResponseCode": "401",
"tns:ResponseMessage": ""
},
"tns:ResponseId": args.requestId
}
});
} else {
/****
* The inner function calls the server with the bearer token to test order
**/
var options = { method: 'PUT',
url: targetServer + '/api//v2/orders',
headers:
{
'cache-control': 'no-cache',
Authorization: outerFunctionReturn.authToken,
'Content-Type': 'application/json' },
body:
{ customerEmail: 'someone@gmail.com' },
json: true };
request(options, function (error, response, innerFunctionReturn) {
if (error) {
throw new Error(error);
}
/****
* At this point the login is successful and we have successfully fetched the needed data.
* This callback builds the return SOAP envelope.
**/
callback({
"LoadCardResult": {
"tns:MsgHeader": {
"tns:MessageType": "LoadCardResponse",
"tns:ContentVersion": "1.0"
},
"tns:ResponseStatus": {
"tns:ResponseResult": "Success",
"tns:ResponseCode": innerFunctionReturn.barcode,
"tns:ResponseMessage": ""
},
"tns:CardValue": {
"tns:Amount": "1000",
"tns:CurrencyCode": "EUR",
"tns:EndBalance": "0"
},
"tns:ResponseId": outerFunctionReturn.vendorId/*args.requestId*/
}
});
});
}//END innerFunctionReturn
});
}
}
}
};
var xml = require('fs').readFileSync('Service.wsdl','utf8');
var server = http.createServer(function(request,response) {
console.log('Request ' + request.url);
response.end("404: Not Found: " + request.url);
});
console.log('Started server on port ' + port);
server.listen(port);
soap.listen(server,'/wsdl',myService, xml);
javascript node.js rest soap
I recently had a build a wrapper to translate SOAP requests in to RESTFul API calls to our server and return the results to the requester.
Request
SOAP -> nodejs wrapper / convert to RESTFul -> Spring Boot Server
Response
Spring Boot Server -> nodejs wrapper / convert to SOAP -> SOAP
My code works perfectly, it handles the SOAP request, takes the passed credentials and logs into the server and then carries out the request. What I don't like is it is all in one single file. I have pruned back the code drastically here just to show what I am doing and I'd like to know the best practices to build such an app.
For example, myService
is one object and would it be better to load this in from its own script but I am unsure how to do this in NodeJS. Full disclosure: I'm trying to avoid reading hours of tutorials but if you know of a good one to handle basics like this please let me know in a comment (not an answer).
"use strict";
var port = 2345;
var targetServer = 'https://aserver.com';
var soap = require('soap');
var http = require('http');
var request = require("request");
var myService = {
Pfs: {
PfsSoap: {
abc: function(args, callback, header, req) {
var options = { method: 'POST',
url: targetServer + '/api/v2/login',
headers:
{ 'cache-control': 'no-cache',
'Content-Type': 'application/json' },
body: { username: 'username', password: 'password' },
json: true };
request(options, function (error, response, body) {
if (error) throw new Error(error);
callback({
"Result": {
"tns:MsgHeader": {
"tns:MessageType": "Response",
"tns:ContentVersion": "1.0"
},
"tns:ResponseId": args.requestId//,
// "ENVELOPE-RECEIVED": args
}
});
});
},
xyz: function(args, callback, header, req) {
/****
* The outer function calls the server with login credentials
**/
var options = { method: 'POST',
url: targetServer + '/api/v2/login',
headers:
{ 'cache-control': 'no-cache',
'Content-Type': 'application/json' },
body: {
username: header.AuthenticationHeader.SenderId,
password: header.AuthenticationHeader.Password
},
json: true };
request(options, function (error, response, outerFunctionReturn) {
/****
* Handle authorisation failure here. Error is not for bad credentials.
**/
if (error) {
throw new Error(error);
}
if(!outerFunctionReturn.authToken){ // if this doesn't exist then the credentails failed
callback({
"Result": {
"tns:MsgHeader": {
"tns:MessageType": "Response",
"tns:ContentVersion": "1.0"
},
"tns:ResponseStatus": {
"tns:ResponseResult": "Failure",
"tns:ResponseCode": "401",
"tns:ResponseMessage": ""
},
"tns:ResponseId": args.requestId
}
});
} else {
/****
* The inner function calls the server with the bearer token to test order
**/
var options = { method: 'PUT',
url: targetServer + '/api//v2/orders',
headers:
{
'cache-control': 'no-cache',
Authorization: outerFunctionReturn.authToken,
'Content-Type': 'application/json' },
body:
{ customerEmail: 'someone@gmail.com' },
json: true };
request(options, function (error, response, innerFunctionReturn) {
if (error) {
throw new Error(error);
}
/****
* At this point the login is successful and we have successfully fetched the needed data.
* This callback builds the return SOAP envelope.
**/
callback({
"LoadCardResult": {
"tns:MsgHeader": {
"tns:MessageType": "LoadCardResponse",
"tns:ContentVersion": "1.0"
},
"tns:ResponseStatus": {
"tns:ResponseResult": "Success",
"tns:ResponseCode": innerFunctionReturn.barcode,
"tns:ResponseMessage": ""
},
"tns:CardValue": {
"tns:Amount": "1000",
"tns:CurrencyCode": "EUR",
"tns:EndBalance": "0"
},
"tns:ResponseId": outerFunctionReturn.vendorId/*args.requestId*/
}
});
});
}//END innerFunctionReturn
});
}
}
}
};
var xml = require('fs').readFileSync('Service.wsdl','utf8');
var server = http.createServer(function(request,response) {
console.log('Request ' + request.url);
response.end("404: Not Found: " + request.url);
});
console.log('Started server on port ' + port);
server.listen(port);
soap.listen(server,'/wsdl',myService, xml);
javascript node.js rest soap
javascript node.js rest soap
edited 17 hours ago
Jamal♦
30.2k11115226
30.2k11115226
asked yesterday
chris loughnane
178229
178229
add a comment |
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f209276%2fwrapper-to-translate-soap-requests-in-to-restful-api-calls%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown