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);









share|improve this question




























    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);









    share|improve this question


























      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);









      share|improve this question















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 17 hours ago









      Jamal

      30.2k11115226




      30.2k11115226










      asked yesterday









      chris loughnane

      178229




      178229



























          active

          oldest

          votes











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "196"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          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






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          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.




          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          Quarter-circle Tiles

          build a pushdown automaton that recognizes the reverse language of a given pushdown automaton?

          Mont Emei