Guest User

Untitled

a guest
Sep 26th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. const express = require('express');
  2. const bodyParser = require('body-parser');
  3. const Request = require("request-promise");
  4. const map = require('crocks/pointfree/map')
  5.  
  6. const app = express();
  7.  
  8. app.use(bodyParser.json());
  9.  
  10. const range = integer => [...new Array(integer).keys()]
  11. const replicate = (i, x) => range(i).fill(x)
  12.  
  13. const handleError = error => {
  14. logger.debug('[' + pid + '] ' + error.reponse.error)
  15. return error;
  16. }
  17.  
  18. const getMaterialURIs = response =>
  19. replicate(response.body.floor.length, `http://localhost:8080/material/q=${response.body.material}`)
  20.  
  21. const processMaterialResponse = response => {
  22. doSomethingWithMaterial(response.body)
  23. return response;
  24. }
  25.  
  26. const processMaterialResponses =
  27.  
  28. const getSiteValueURI = response =>
  29. `http://localhost:8080/value/q=${response.body.value}`;
  30.  
  31. const processSiteValueResponse = response =>
  32. doSomethingWithSiteValue(response.body)
  33. return response;
  34. }
  35.  
  36. app.post('/building/', function (req, res) {
  37. const uri = 'http://localhost:8080/site/';
  38. const body = JSON.stringify(req.body);
  39. const method = 'POST';
  40. const headers = { 'Content-Type': 'application/json' };
  41.  
  42. // First External API Call
  43. Request({uri, body, method, headers})
  44. // 1: fetch to derive the material URI
  45. .then(getMaterialURIs)
  46. // 2: fetch from that material URI
  47. .then(map(Request.get))
  48. .then(Promise.all)
  49. // 3: process the material response
  50. .then(map(processMaterialResponse))
  51. // 4: derive the siteValueURI
  52. .then(map(getSiteValueURI))
  53. // 5: fetch the siteValueURI
  54. .then(map(Request.get))
  55. .then(Promise.all)
  56. // 6: process the site value response
  57. .then(map(processSiteValueResponse))
  58. .catch(handleError)
  59. .then(response => {
  60. res.writeHead(200, { 'Content-Type': 'application/json' });
  61. res.end('{"material":"materialquerystring","value":"valuequerystring"}');
  62. })
  63. })
  64. });
Add Comment
Please, Sign In to add comment